Assembly Sample Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Q1: Addition of two numbers

stacksg segment 'stack'


dw 32 dup(0)
stacksg ends

datasg segment 'data'

fno dW 250
sno dW 125
res dW ?

datasg ends

codesg segment 'code'

assumE ss: stacksg, ds: datasg, cs: codesg


MOV AX, OFFSET DATASG
MOV DS, AX

MOV AX, OFFSET FNO


ADD AX, SNO
MOV RES, AX

MOV AX,4C00H
INT 21H

CODESG ENDS
END

Q2:

;PROGRAM TO print welcome messages

data segment
MSG1 db 'WELCOME TO ASSEMBLY LANGUAGE PROGRAMMING',13,10,'$'
MSG2 DB 'AT INSDOC DELHI',13,10,'$'
data ends

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX
LEA DX, MSG1 ;PRINT MESSAGE1
MOV AX,0900H
INT 21H

LEA DX, MSG2 ;PRINT MESSAGE1


MOV AX,0900H
INT 21H

MOV AX,4C00H ;EXIT TO DOS


INT 21H

CODE ENDS
END

Q3: Program to find out whether given number is odd or even

data segment
num dw 24h
lf equ 10
cr equ 13
;crlf db cr,lf,'$'
evenmsg db 'Number is even..',cr,lf,'$'
oddmsg db 'Number is odd..',cr,lf,'$'
data ends
code segment
assume ds:data, cs:code

start: mov ax,data


mov ds,ax
mov ax,num
mov bh,02h
div bh
cmp ah,00h
jne oddnum
mov dx, offset evenmsg
mov ah,09h
int 21h
mov ax,4c00h
int 21h

oddnum:
mov dx, offset oddmsg
mov ah,09h
int 21h
mov ax,4c00h
int 21h
code ends

end start
question

; This program compares two strings and


; print message if two strings are not equal

data segment
password db 'failsafe'
deststr db 'feelsafe'
message db 'strings are equal'
data ends
code segment
assume cs:code,ds:data,es:data

mov ax, data


mov ds,ax
mov es,ax
lea si,password
lea di,deststr
mov cx,08 ;load counter with string lenght
cld ;clear direction flag
repe cmpsb ;compare two strings byte by byte
jne notequal
mov ah,09
mov dx, offset message
int 21h
notequal: mov ax,4c00h
int 21h

code ends
end
Program : This program compares two strings and
; print message if two strings are not equal

data segment
password db 'failsafe'
deststr db 'feelsafe'
message db 'strings are equal'
data ends
code segment
assume cs:code,ds:data,es:data

mov ax, data


mov ds,ax
mov es,ax
lea si,password
lea di,deststr
mov cx,08 ;load counter with string lenght
cld ;clear direction flag
repe cmpsb ;compare two strings byte by byte
jne notequal
mov ah,09
mov dx, offset message
int 21h
notequal:mov ax,4c00h
int 21h

code ends
end

Program: largest of five numbers

data segment
numbers db 30h,35h,32h,31h,33h
largest db 00h,13,10,00h
data ends

code segment
assume ds:data,cs:code
mov ax,data
mov ds,ax
mov cx,05
mov si,offset numbers
lea di,largest
mov al,[si]
mov byte ptr [di],al

c20:
mov al,[si]
cmp al,largest
jg replaceval
inc si
loop c20
jmp printit
replaceval:
mov byte ptr [di],al
inc si
jmp c20

printit:
mov ah,09h
mov dx, offset largest
int 21h

exit_to_dos:

mov ax,4c00h
int 21h

code ends
end

;PROGRAM TO REVERSE THE STRING


data segment
str1 db 'INSDOC DELHI',13,10,'$'
str2 db 12 dup (' '),13,10,'$'
MSG1 DB 'YOU HAVE ENTERED : ','$'
MSG2 DB '...AND ITS REVERSE IS: ','$'
data ends

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX
MAIN PROC
CALL REVERSEDATA
CALL PRINTIT
MOV AX,4C00H
INT 21H
MAIN ENDP

REVERSEDATA PROC
CLD
LEA SI,STR1
LEA DI,STR2+11 ;LOAD ADDRESS OF THE LAST CHARACTER
MOV CX,12

B20:
LODSB ;AL <- FIRST CHARACTR OF THE STRING
MOV [DI],AL ;MOVES FIRST CHARACTER OF STR1
;AS A LAST CHARACTER OF STR2
DEC DI
LOOP B20 ;IF COUNTER IS NOT ZERO THEN LOOP
;ELSE
RET ;RETURN
REVERSEDATA ENDP

PRINTIT PROC

;********** CODE FOR CLEAR WHOLE SCREEN ******

MOV AX,0600H ;SCROLL FULL SCREEN FUNCTION


MOV BH,07 ;BACKGROUND WHITE ON BLACK
MOV CX,0000 ;UPPER LEFT (ROW:00 AND COLUMN:00) CORNER
MOV DX,184FH ;TO LOWER LEFT CORNER (ROW:24, COLUMN:79)
INT 10H ; CALL INTERRUTP OF BIOS

;********** NOW SET CURSOR POSITION **********

MOV AH,02H ; FUNCTION 02 OF BIOS INTERRUPT IS FOR


; SETTING THE CURSOR
MOV BH,00 ;PAGE NO:0 (DEFAULT VALUE)
MOV DX,0000H ;ROW : 00, COLUMN:00
INT 10H ; BIOS INTERRUPT

;*********** HERE LETS PRINT THE MESSAGES******


LEA DX, MSG1 ;PRINT MESSAGE1
MOV AX,0900H
INT 21H

LEA DX, STR1 ;PRINT REVERSE


MOV AX,0900H
INT 21H

LEA DX, MSG2 ;PRINT MESSAGE2


MOV AX,0900H
INT 21H
LEA DX, STR2 ;PRINT REVERSE STRING
MOV AX,0900H
INT 21H
RET
PRINTIT ENDP

CODE ENDS
END
Que: Takes two multi digit number as input from keyboard and add it
STACK SEGMENT
DW 32 DUP(0)
STACK ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX

;;****** INPUT FIRST NUMBER*******


MOV AH,09H
MOV DX,OFFSET PROMPT1
INT 21H

MOV AH,0AH
MOV DX, OFFSET BUFFER
INT 21H
;;***** STORE THE ACCEPTED VALUE TO THE VARIABLE ASC1

LEA SI, BUFFER+2


LEA DI, ASC1
MOV CX,0400H
L1: MOVSB
INC SI
INC DI

LOOP L1

;;****** INPUT SECOND NUMBER*******


MOV AH,09H
MOV DX,OFFSET PROMPT2
INT 21H

MOV AH,0AH
MOV DX, OFFSET BUFFER
INT 21H
;;***** STORE THE ACCEPTED VALUE TO THE VARIABLE ASC2
LEA SI, BUFFER+2
LEA DI, ASC2
MOV CX, 0500H ;;IN SECOND BYTE OF THE BUFFER ACTUAL LENGH
IS STORED
REP MOVSB

;;******** PERFORM ASCII ADDITION******

CLC ;CLEAR CARRY


LEA SI,ASC1+4 ;or MOV SI,OFFSET ASC1+4 SINCE OFFSET
STARTS WITH 0
LEA DI,ASC2+4 ;or MOV DI,OFFSET ASC2+4
LEA BX,ASCSUM+5 ;LOAD ADDRESS OF LAST DIGIT OF ASCSUM IN
BX
MOV CX,05 ; SET COUNTER TO 5 SINCE THERE ARE 5
DIGITS

A20:
MOV AH,00 ; HIGHER BITS ARE LOADED WITH ZEROS AND
MOV AL,[SI] ; LOWER BITS WITH DIGIT OF FIRST NUMBER
ADC AL,[DI] ; ADD ONE DIGIT OF FISRT NUMBER
; WITH ONE DIGIT OF SECOND NUMBER ALONG WITH
CARRY
AAA ;ADJUST ASCII NUMBER ie. SUM

MOV [BX],AL ;NOW STORE THIS DIGIT TO THE LOCATION INDICATED


BY BX
DEC SI ; DECREMENT SI SO THAT NEXT DIGIT ADDITION WILL
BE PERFORMED
DEC DI
DEC BX
LOOP A20 ; PERFORM LOOP 5 TIMES

MOV [BX],AH ;FINALLY STORE LAST CARRY GENERATED, IN THE


RESULT
LEA BX,ASCSUM+5 ;START FORM LAST DIGIT AGAIN
MOV CX,06 ;SINCE THERE ARE TOTLA SIX DIGITS IN THE RESULT

A30: ; THIS WILL INSERT "3" SO AS TO MAKE IT ASCII


OR BYTE PTR[BX],30H
DEC BX
LOOP A30

;********** CODE FOR CLEAR WHOLE SCREEN ******

MOV AX,0600H ;SCROLL FULL SCREEN FUNCTION


MOV BH,07 ;BACKGROUND WHITE ON BLACK
MOV CX,0000 ;UPPER LEFT (ROW:00 AND COLUMN:00) CORNER
MOV DX,184FH ;TO LOWER LEFT CORNER (ROW:24, COLUMN:79)
INT 10H ; CALL INTERRUTP OF BIOS

;********** NOW SET CURSOR POSITION **********

MOV AH,02H ; FUNCTION 02 OF BIOS INTERRUPT IS FOR


; SETTING THE CURSOR
MOV BH,00 ;PAGE NO:0 (DEFAULT VALUE)
MOV DX,0000H ;ROW : 00, COLUMN:00
INT 10H ; BIOS INTERRUPT

MOV AX,0900H
LEA DX,ASCSUM
INT 21H

MOV AX,4C00H
INT 21H
CODE ENDS

DATA SEGMENT

prompt1 db 13,10,'Enter the first Number: ','$'


prompt2 db 13,10,'Enter the second Number: ','$'

ASC1 DB '00000'
ASC2 DB '00000'
ASCSUM DB '0000000',13,10,'$'
buffer db 6
db 00
db 5 dup(0),'$'

DATA ENDS

END

que:;convert ascii to bcd and add


;; program uses procedure

data segment
asc1 db '057836'
asc2 db '069427'
bcd1 db '000'
bcd2 db '000'
bcdsum db '00000000',13,10,'$'

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX
main proc near
LEA SI, ASC1+4 ;initialize for ascii
LEA DI, BCD1+2
CALL ASC2BCD
LEA SI,ASC2+4
LEA DI, BCD2+2
CALL ASC2BCD
CALL ADDBCD
MOV AX,0900H
MOV DX, OFFSET BCDSUM
INT 21H
MOV AX,4C00H
INT 21H
MAIN ENDP

ASC2BCD PROC
MOV CL,04 ;SHIFT FACTOR
MOV DX,3 ;NO OF WORDS TO CONVERT

B20: MOV AX,[SI]


XCHG AH,AL
SHL AL,CL
SHL AX,CL
MOV [DI],AH
DEC SI
DEC DI
DEC DX
JNZ B20
RET
ASC2BCD ENDP

ADDBCD PROC
XOR AH,AH ;CLEAR AH
LEA SI,BCD1+2 ;INITIALIZE
LEA DI,BCD2+2 ; BCD ADDRESSES
LEA BX,BCDSUM+3
MOV CX,03 ;3-BYTE FIELDS
CLC
ADD20:
MOV AL,[SI] ;GET BCD1 (or LOADSB)
ADC AL,[DI] ;ADD BCD2
DAA ;DECIMAL ADJUST
MOV [BX],AL ; STORE IN BCDSUM
DEC SI
DEC DI
DEC BX
LOOP ADD20 ; LOOP 3 TIMES (AS THERE ARE 3 IN CX)
RET
ADDBCD ENDP

CODE ENDS ; CODE SEGMENT ENDS HERE


END ;PROGRAM ENDS

You might also like