ASSEMBLY LANGUAGE PROGRAMS
INDEX
No EXERCISE Page.No
1 Addition of two 8 bit numbers 3
2 Subtract two 16-bit numbers 5
3 Multiply two 8-bit numbers 9
4 Fibonacci numbers 11
5 Sum and average 14
6 Mov and xchg functions 17
7 Average of two values 19
8 Largest and smallest array values 21
9 Compare a pair of characters 24
10 Perform an operation 26
COURSE CODE: BCSL-022
EXERCISE:-1
Addition of two 8 bit numbers
ALGORITHM
Step 1 : Start the program by loading the first data into the accumulator
Step 2 : Move the data into the register (B register)
Step 3 : Get the second data and load it into the accumulator
Step 4 : Add the two register contents
Step 5 : Check for carry
Step 6 : Store the value of sum and carry into the memory location
Step 7 : Terminate the program
PROGRAM
DATA SEGMENT
A db 10 ; Variable
B db 20
C db ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START : MOV AX,DATA
MOV DS,AX
MOV AL, A ; move value of A into al register
ADD AL, B ; all al with value of B
MOV C, AL ; store result in C
INT 21H
CODE ENDS
END START
3
COURSE CODE: BCSL-022
OUTPUT:
4
COURSE CODE: BCSL-022
EXERCISE : 2
Subtract two 16-bit numbers stored in memory
ALGORITHM
Step 1 : Start the program
Step 2 : Get lower byte of the first number
Step 3 : Get lower byte of the second number
Step 4 : Subtract the lower byte of second number from lower byte of first number
Step 5 : Get higher byte of the first number
Step 6 : Get higher byte of the second number
Step 7 : Subtract the higher byte of second number and borrow from the previous
subtraction
Step 8 : Store the result
Step 9 : Terminate the program
PROGRAM
DATA SEGMENT
A DW 21145 ; Variable
B DW 11162
C DW ?
DATA ENDS
CODE SEGMENT ; Code
ASSUME CS: CODE,DS: DATA
START : MOV AX,A ; offset/ Base address of variable A
MOV SI, AX
MOV AX,B ; offset of variable B
MOV DI, AX
5
COURSE CODE: BCSL-022
MOV AX,C ; offset of variable C
MOV BX, AX
MOV AX, 0
MOV AL, [SI]
SUB AL, [DI] ; subtraction of lower byte
MOV [BX], AL
INC SI ; increment index variables
INC DI
INC BX
MOV AL, [SI]
SBB AL, [DI] ; subtraction of higher byte
MOV [BX], AL
INT 21H
CODE ENDS
END START
6
COURSE CODE: BCSL-022
OUTPUT:
7
COURSE CODE: BCSL-022
8
COURSE CODE: BCSL-022
EXERCISE :- 3
Multiply two 8-bit numbers using repetive addition
ALGORITHM
Step 1 : Start the program by loading the first data into the accumulator
Step 2 : Get the first number
Step 3 : Initialize second number
Step 4 : Result=0
Step 5 : Result= Result + first number
Step 6 : Decrement counter
Step 7 : If count = 0 then go to step 4 else go to step 8
Step 8 : Terminate the program
PROGRAM
DATA SEGMENT
A DB 125 ; Variable
B DB 5
C DW ?
DATA ENDS
CODE SEGMENT ; Code
ASSUME CS: CODE,DS: DATA
Start : MOV AL, B ; load multiplier in CX register
MOV CL, AL
MOV BL, A
MOV BH, 0
MOV AX, 0
ADC AX, BX ; add multiplicand, multiplier number of time
MOV C, AX
INT 21H
CODE ENDS
END START
9
COURSE CODE: BCSL-022
OUTPUT
10
COURSE CODE: BCSL-022
EXERCISE : -4
Generate first n fibonacci numbers
ALGORITHM:
Step 1 : Start the program
Step 2 : Enter two terms to the register and set the count as N
Step 3 : Allocate memory for result and move to two terms to the corresponding memory area
Step 4 : Add the content in the register with the result; store it to the memory and exchange
the terms
Step 5 : Increment memory address and decrement the count
Step 6 : Repeat step 4 and 5 until count = 0
Step 7 : Stop
PROGRAM:
ORG 100H
.MODEL SMALL
.DATA
NUM_1 DB?
NUM_2 DB?
NUM_3 DB?
V1 DB?
V2 DB?
NL DB ‘ ‘, 0DH, 0AH,’$’
.CODE
MAIN PROC
MOV AX,@DATA
MOV DX, AX
MOV CX, 10
MOV CH, 0
MOV NUM_1, 0
MOV NUM_2, 1
MOV DL, NUM_1
11
COURSE CODE: BCSL-022
OR DL, 30H
MOV A H, 02H
INT 21H
MOV DL, NUM_2
OR DL, 30H
MOV AH, 02H
INT 21H
L1:
MOV AL, NUM_1
ADD AL, NUM_2
MOV AH, 0
MOV BL, AL
MOV DL, 10
DIV DL
ADD AX, 3030H
MOV V1, AL
MOV V2, AH
MOV DL, V1
MOV AH, 02H
INT 21H
MOV DL, V2
MOV AH, 02H
INT 21H
SHIFT:
MOV AL, NUM_2
MOV NUM_1, AL
MOV NUM_2, BL
LOOP L1
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
RET
12
COURSE CODE: BCSL-022
OUTPUT
13
COURSE CODE: BCSL-022
EXERCISE : 5
Program to sum and average an array of 8bit numbers
ALGORITHM
Step 1 : Start
Step 2 : Store the input data into register AX
Step 3 : Move the content of AX into DS
Step 4 : Load the content of A into BX
Step 5 : Move the value 10 to CL
Step 6 : Move the value 00 to AX
Step 7 : Start the loop 11
Step 8 : Add the content of AL and point to the AX
Step 9 : Increment BX
Step 10 : Decrement CL
Step 11 : Compare the value of 00 to CL
Step 12 : Jump to the loop 11 until non zero
Step 13 : Move the content of AL to sum
Step 14 : Move the value 10 to BH
Step 15 : Move the value 4CH into the register AH
Step 16 : Stop
14
COURSE CODE: BCSL-022
PROGRAM
DATA SEGMENT
A DB 5, 4, 5, 2, 1, 7 ; Variable
Sum DB 0
Avg DB ?
DATA ENDS
CODE SEGMENT ; Code
ASSUME CS:CODE,DS:DATA
START : MOV CX, 6 ; number of elements:
MOV AL, 0 ; AL will store the sum:
MOV BX, 0 ; BX is an index:
ADD AL, A[BX] ; add multiplicand, multiplier number of time
INC BX ; next byte:
; loop until CX=0:
MOV Sum, AL ; store result in sum:
DIV BX ; divide sum with number of elements
MOV Avg, AL ; store average in Avg
INT 21H
CODE ENDS
END START
15
COURSE CODE: BCSL-022
OUTPUT
16
COURSE CODE: BCSL-022
EXERCISE : 6
Program to identify the difference in mov and xchg functions
PROGRAM
DATA SEGMENT
VAL DW 5678h
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE,DS:DATA
MAINP : MOV AX,1234h ;AH=12 & AL=34
XCHG AH,AL ;AH=34 & AL=12
MOV AX,1234H ;AH=12 & AL=34
MOV BX,VAL ;BH=56 & BL=78
XCHG AX,BX ;AX=5678 & BX=1234
XCHG AH,BL ;AH=34,AL=78 ,BH=12 ,BL=56
MOV AX,4C00H ; Halt using INT 21h
INT 21H
CODE ENDS
END MAINP
17
COURSE CODE: BCSL-022
OUTPUT
18
COURSE CODE: BCSL-022
EXERCISE : 7
Program to find the average of two values
PROGRAM
DATA SEGMENT
FIRST DB 90H
SECOND DB 78H
AVGE DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START : MOV AX,DATA
MOV DS,AX
MOV AL,FIRST
ADD AL,SECOND
MOV AH,00H
ADC AH,00H
MOV BL,02H
DIV AL
MOV AVGE,AL
MOV AH,4CH
INT 21H
CODE ENDS
END START
RET
19
COURSE CODE: BCSL-022
OUTPUT
20
COURSE CODE: BCSL-022
EXERCISE : 8
Find the largest and smallest array values
PROGRAM
DATA SEGMENT
ARRAY DW -1,2000,-4000,32767,500,0
LARGE1 DW ?
SMALL1 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START : MOV AX,DATA
MOV DS,AX ;initialize DS
MOV DI,OFFSET ARRAY DIi points to the array
MOV AX,[DI] ;AX contains the first element
MOV DX,AX ;initialize large in DX register
MOV BX,AX ;initialize large in BX register
MOV CX,6 ;initialize loop counter
MOV AX,[DI] ;get next array value
A1 : CMP AX,BX ;Is the new value smaller?
JGE A2 ;if greater the jump to A2,to check larger
MOV BX,AX ;if not it is smaller ,move to smallest
JMP A3 ;as it is small, thus need to compare it with the large
A2 : CMP AX,DX ;then jump to A3 to continue or terminate loop
JLE A3 ;[DI]=large, if less tha it implies not large go to A3
MOV DX,AX ;otherwise it is large value, so move to DX
A3 : ADD DI,2 ;repeat the loop until CX=0
LOOP A1
MOV LARGE1,DX
MOV SMALL1,BX ;move the large small in the memory locations
21
COURSE CODE: BCSL-022
MOV AX,4C00H ; halt , return to DOS
INT 21H
CODE ENDS
END START
OUTPUT
22
COURSE CODE: BCSL-022
23
COURSE CODE: BCSL-022
EXERCISE : 9
Program to compare a pair of characters
PROGRAM
DATA SEGMENT
XX DB ?
YY DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MAINP : MOV AX,DATA ;initialize data
MOV DS,AX ;segment using ax
MOV CX,03H ;set counter to 3
NEXTP : MOV AH,01H ;waiting for user to enter a character
INT 21H
MOV XX,AL ;store the first character in xx
MOV AH,01H ;waiting for user to enter second character
INT 21H
MOV YY,AL ;store the first character in yy
MOV BH,XX ;load first character in bh
MOV BL,YY ;load first character in bl
CMP BH,BL ;compare the characters
JNE NOT_EQUAL
EQUAL : MOV AH,02H ;if characters are equal then control
MOV DL,'Y' ; will execute this block and
INT 21H ; display ‘y’
JMP CONTINUE ; jump to continue loop
NOT_EQUAL : MOV AH,02H ;if characters are not equal then control
MOV DL,"N" ; will execute this block and display ‘n’
CONTINUE : LOOP NEXTP ;Get the next character
MOV AH,4CH
INT 21H
24
COURSE CODE: BCSL-022
CODE ENDS
END MAINP
OUTPUT
25
COURSE CODE: BCSL-022
EXERCISE : 10
To perform the operation z=((a-b)/10*c)**2)
PROGRAM
DATA SEGMENT
A DB 95H ;variables
B DB 64H
C DB 32H
D DW ?
DATA ENDS
CODE SEGMENT
START : ASSUME CS:CODE,DS:DATA
MOV AX,DATA
MOV DS,AX
MOV AH,00
MOV AL,A ;al=95
SUB AL,B ;al=al-b;ie 95-64=31
MOV BL,10H ; bl=10
DIV BL ; ax=ax/bl,ie 31/10
MOV AH,00 ; ah=remainder is erased, al=quotient
MUL C ; ax=ax*c
MUL AX ; ax=ax*ax=ax^2
MOV AH,4CH ; to return to DOS
INT 21H ;NOP
CODE ENDS
END START
26
COURSE CODE: BCSL-022
OUTPUT
27