0% found this document useful (0 votes)
30 views

UNIT-IV Assembly Language Programming

Uploaded by

gareprathmesh0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

UNIT-IV Assembly Language Programming

Uploaded by

gareprathmesh0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

UNIT-IV Assembly Language Programming

UNIT-IV Assembly Language Programming

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-IV Assembly Language Programming

Syllabus: Total Marks-20

3.1 Machine Language Instruction Format.

3.2 Addressing modes.

3.3 Instruction set, group of instructions: Arithmetic instructions, logical instructions, data transfer
instructions, bit manipulation instructions, string operation instructions, program control transfer or
branching instructions, process control instructions.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-IV Assembly Language Programming

Model of Assembly Language Programming:

###Model-1###
1) Using segment, assume, ends directives

mydata SEGMENT
-
-
Program data declaration (Data Segment of the program)
-
-
mydata ENDS

mycode SEGMENT
ASSUME CS:mycode, DS:mydata
MOV AX,mydata ;Data segment initialization
MOV DS,AX
-
-
Program Codes (Code Segment of the program)
-
-
mycode ENDS
END

###Model-2###
1) Using .DATA and. CODE directives

.MODEL SMALL
.STACK 100
.DATA
-
-
Program data declaration (Data Segment of the program)
-
-
.CODE
MOV AX,@DATA ;Data segment initialization
MOV DS,AX
-
-
Program Codes (Code Segment of the program)
-
-
ENDS
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-IV Assembly Language Programming

Use of Assembler:

How to execute Assembly Program:

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-IV Assembly Language Programming

Programming using Assembler:


 Write an Assembly language program to display “Hello World” message.

.MODEL SMALL
.DATA
MSG DB 'Hello World$'
.CODE
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG
MOV AH,9H
INT 21h
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform addition of two 8-bit numbers.

.MODEL SMALL
.DATA
num1 DB 80H
num2 DB 08H
sum DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
ADD AL,BL
MOV sum,AL
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-IV Assembly Language Programming

 Write an Assembly language program to perform addition of two 16-bit numbers.

.MODEL SMALL
.DATA
num1 DW 8A64H
num2 DW 5F98H
sum DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
ADD AX,BX
MOV sum,AX
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform addition of two 16-bit numbers where
result may be more than 16 bits.

.MODEL SMALL
.DATA
num1 DW FFFFH
num2 DW FFFFH
res_lsb DW 0000H
res_msb DW 0000H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
ADD AX,BX
JNC EXIT ;if result < 16 bit jump to exit
INC res_msb
EXIT:
MOV res_lsb,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-IV Assembly Language Programming

 Write an Assembly language program to perform subtraction of two 8-bit numbers.

.MODEL SMALL
.DATA
num1 DB 80H
num2 DB 08H
res DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
SUB AL,BL
MOV res,AL
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform subtraction of two 16-bit numbers.

.MODEL SMALL
.DATA
num1 DW 8A64H
num2 DW 5F98H
res DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
SUB AX,BX
MOV res,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-IV Assembly Language Programming

 Write an ALP to find sum of 8-bit array elements.

.MODEL SMALL
.DATA
array DB 03H,07H,06H,04H,01H
res_lsb DB ?
res_msb DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,05H
MOV SI, OFFSET array
UP:MOV AL,[SI]
ADD res_lsb,AL
JNC NEXT
INC res_msb
NEXT:
INC SI
LOOP UP;

MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-IV Assembly Language Programming

 Write an ALP to find sum of 16-bit array elements.

.MODEL SMALL
.DATA
array DW 0003H,0070H,0060H,004H,001H
res_lsb DW ?
res_msb DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,05H
MOV SI, OFFSET array
UP:MOV AX,[SI]
ADD res_lsb,AX
JNC NEXT
INC res_msb
NEXT:
ADD SI,2
LOOP UP;

MOV AH,4CH
INT 21h
END

 Write an ALP for multiplication of two 8-bit numbers

.MODEL SMALL
.DATA
num1 DB 10H
num2 DB 07H
res DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
MUL BL
MOV res,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-IV Assembly Language Programming

 Write an ALP for multiplication of two 16-bit numbers

.MODEL SMALL
.DATA
num1 DW 1234H
num2 DW 0132H
res_lsb DW 00H
res_msb DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
MUL BX
MOV res_lsb,AX
MOV res_msb,DX
MOV AH,4CH
INT 21h
END

 Write an ALP to divide 16 bit by 8 bit numbers

.MODEL SMALL
.DATA
num1 DW 1254H
num2 DB 12H
Quotient DB 00H
Remainder DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BL,num2
DIV BL
MOV Quotient,AL
MOV Remainder,AH
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-IV Assembly Language Programming

 Write an ALP to divide 8 bit by 8 bit numbers

.MODEL SMALL
.DATA
num1 DB 54H
num2 DB 12H
Quotient DB 00H
Remainder DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
DIV BL
MOV Quotient,AL
MOV Remainder,AH
MOV AH,4CH
INT 21h
END

 Write an ALP to divide 16 bit by 16 bit numbers

.MODEL SMALL
.DATA
num1 DW 1254H
num2 DW 0012H
Quotient DW 0000H
Remainder DW 0000H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
DIV BX
MOV Quotient,AX
MOV Remainder,DX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-IV Assembly Language Programming

Arithmetic Operations on BCD Numbers

 Write an ALP for addition of two 8 bit BCD numbers. Consider Carry

.MODEL SMALL
.DATA
num1 DB 84H
num2 DB 28H
res_lsb DB 00H
res_msb DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
ADD AL,BL
DAA ; Adjust result to BCD
JNC EXIT
INC res_msb
EXIT:
MOV res_lsb,AL
MOV AH,4CH
INT 21h
END

The program is similar to addition of 8 bit hex numbers. Only DAA instruction needs to be used after
ADD to adjust result to BCD.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-IV Assembly Language Programming

 Write ALP for addition of two 16 bit BCD numbers. Consider Carry

.MODEL SMALL
.DATA
NUM1 DW 9999H
NUM2 DW 9999H
RES_LSB DW 0000H
RES_MSB DW 0000H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
MOV BX, NUM2
ADD AL, BL ; Lower byte addition and result in AL
DAA ; Adjust result to BCD
MOV CL, AL
MOV AL, AH
ADC AL, BH ;Higher byte addition and result in AL because
DAA works on AL only
DAA ; Adjust result to BCD
MOV CH, AL
JNC EXIT
INC RES_MSB
EXIT:
MOV RES_LSB, CX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-IV Assembly Language Programming

 Write an ALP for subtraction of two 8 bit BCD numbers.

.MODEL SMALL
.DATA
num1 DB 85H
num2 DB 57H
res_bcd DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
SUB AL,BL
DAS ; Adjust result to BCD
MOV res_bcd,AL
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-IV Assembly Language Programming

 Write ALP for subtraction of two 16 bit BCD numbers.

.MODEL SMALL
.DATA
NUM1 DW 9000H
NUM2 DW 0999H
RES_BCD DW 0000H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
MOV BX, NUM2
SUB AL, BL ; Lower byte subtraction and result in AL
DAS ; Adjust result to BCD
MOV CL, AL
MOV AL, AH
SBB AL, BH ;Higher byte subtraction and result in AL
DAS ; Adjust result to BCD
MOV CH, AL
MOV RES_BCD, CX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-IV Assembly Language Programming

FIND SMALLEST AND LARGEST NUMBER IN ARRAY PROGRAMS

 Write ALP to find smallest number (8 bit) from array

.MODEL SMALL
.DATA
ARRAY DB 12H,16H,11H,08H,23H
SMALLEST DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AL,[SI] ;AL=08H SI=23H CX=00H
UP: INC SI
CMP AL,[SI] ;IF 1ST NO < 2ND NO THEN GO TO NEXT
JC NEXT
MOV AL,[SI]
NEXT:
LOOP UP
MOV SMALLEST,AL
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-IV Assembly Language Programming

 Write ALP to find smallest number(16 bit) from array

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
SMALLEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI] ;AL=0008H SI=0023H CX=0000H
UP: INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO < 2ND NO THEN GO TO
NEXT
JC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV SMALLEST,AX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-IV Assembly Language Programming

 Write ALP to find largest number (8 bit) from array

.MODEL SMALL
.DATA
ARRAY DB 12H,16H,11H,08H,23H
LARGEST DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AL,[SI] ;AL=23H SI=23H CX=00H
UP: INC SI
CMP AL,[SI] ;IF 1ST NO > 2ND NO THEN GO TO NEXT
JNC NEXT
MOV AL,[SI]
NEXT:
LOOP UP
MOV LARGEST,AL
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-IV Assembly Language Programming

 Write ALP to find largest number (16 bit) from array

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
LARGEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI] ;AX=0023H SI=0023H CX=00H
UP: INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO > 2ND NO THEN GO TO NEXT
JNC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV LARGEST,AX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-IV Assembly Language Programming

ODD AND EVEN PROGRAMS

 Write ALP to check number is Even or Odd

.MODEL SMALL
.DATA
NUMBER DB 11H
ODD_NO DB 00H
EVEN_NO DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,NUMBER

ROR AL,1 ;ROTATE AT RIGHT


JNC DOWN
ROL AL,1 ;RESTORE NUMBER
MOV ODD_NO,AL
JMP EXIT
DOWN:
ROL AL,1 ;RESTORE NUMBER
MOV EVEN_NO,AL
EXIT:
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-IV Assembly Language Programming

 Write ALP to count odd numbers in the array

.MODEL SMALL
.DATA
ARRAY DW 0011H,0013H,0012H,0010H,0024H
ODD_COUNT DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
NEXT:MOV AX,[SI]
ROR AX,1 ;AX=0024H , ODD_COUNT=2 , CX=0
JNC DOWN
INC ODD_COUNT
DOWN:
ADD SI,2
LOOP NEXT
MOV AH,4CH
INT 21H
END

NOTE: Here we have done the program for 16 bit numbers array. For 8 bit numbers, only change the
directives from DW to DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-IV Assembly Language Programming

 Write ALP to count EVEN numbers in the array

.MODEL SMALL
.DATA
ARRAY DW 0011H,0013H,0012H,0010H,0024H
EVEN_COUNT DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
NEXT:MOV AX,[SI]
ROR AX,1
JC DOWN
INC EVEN_COUNT
DOWN:
ADD SI,2
LOOP NEXT
MOV AH,4CH
INT 21H
END

NOTE: Here we have done the program for 16 bit numbers array. For 8 bit numbers, only change the
directives from DW to DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-IV Assembly Language Programming

 Write ALP program to find count of even and odd numbers in given array

.MODEL SMALL
.DATA
ARRAY DB 11H,12H,13H,14H,15H
ODD_COUNT DB 00H
EVEN_COUNT DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
UP:MOV AL,[SI]
ROR AL,1
JNC DOWN
INC ODD_COUNT
JMP NEXT
DOWN:
INC EVEN_COUNT
NEXT:INC SI
LOOP UP
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-IV Assembly Language Programming

POSITIVE AND NEGATIVE NUMBER PROGRAMS

 Write ALP to check whether number is Positive or Negative

.MODEL SMALL
.DATA
NUMBER DB 11H
POSITIVE_NO DB 00H
NEGATIVE_NO DB 00H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AL, NUMBER

ROL AL,1 ;ROTATE AT LEFT


JNC DOWN ;CHECK POSITIVE OR NEGATIVE
ROR AL,1 ;RESTORE NUMBER
MOV NEGATIVE_NO,AL
JMP EXIT
DOWN:
ROR AL,1 ;RESTORE NUMBER
MOV POSITIVE_NO,AL
EXIT:
MOV AH,4CH
INT 21H
END

NOTE: The MSB bit indicates sign of number i.e. D7 or D15. If it is 1, the number is Negative else the
number is positive.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count positive numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
POSITIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JC DOWN ;If number is positive
INC POSITIVE_COUNT ;Increment positive counter
DOWN:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: 16 bit numbers in array. For 8 bit numbers, only change the directives from DW to DB. Use AL
instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count negative numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
NEGATIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JNC DOWN ;If number is Negative
INC NEGATIVE_COUNT ;Increment negative counter
DOWN:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: 16 bit numbers in array. For 8 bit numbers, only change the directives from DW to DB. Use AL
instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count positive and negative numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
POSITIVE_COUNT DW 00H
NEGATIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JNC DOWN
INC NEGATIVE_COUNT ;Increment negative counter
JMP DOWN1
DOWN:
INC POSITIVE_COUNT ;Increment positive counter
DOWN1:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: This program for 16-bit numbers array. For 8-bit numbers, only change the directives from DW to
DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
UNIT-IV Assembly Language Programming

COUNT NUMNER OF ZEROS AND ONES PROGRAMS


- The number of zeroes and ones can be count by rotating the number towards right or left 8
times for 8-bit number and 16 times for 16-bit number.
- Any of instruction ROR, ROL, RCR, RCL can be used.
- When we rotate the number left or right, corresponding bit i.e. D0 or D7 initially goes into carry
flag. Then we can check carry flag by JC or JNC to count number of ones or zeroes.

 Write an ALP to count number of ONES in 16 bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ONE_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JNC DOWN ;if bit not equal to 1 then go to down
INC ONE_COUNT ;else increment counter ones by one
DOWN:
LOOP NEXT ;decrement counter by 1 & if its not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
UNIT-IV Assembly Language Programming

 Write an ALP to count number of ZEROES in 16 bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ZERO_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JC DOWN ;if bit not equal to 1 then go to down
INC ZERO_COUNT ;else increment counter ones by one
DOWN:
LOOP NEXT ;decrement counter by 1 and if not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
UNIT-IV Assembly Language Programming

 Write an ALP to count number of ZEROES & ONES in 16-bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ONE_COUNT DB 00H
ZERO_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JC DOWN ;if bit not equal to 1 then go to down
INC ZERO_COUNT ;increment ZERO counter ones by one
JMP DOWN1
DOWN:
INC ONE_COUNT ;increment ONE counter ones by one
DOWN1:
LOOP NEXT ;decrement counter by 1 & if its not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
UNIT-IV Assembly Language Programming

BLOCK TRANSFER PROGRAM

 Write ALP to perform block transfer operation of 5 NOS. Assume block of FIVE 16 bit NOS

.MODEL SMALL
.DATA
ARRAY1 DW 1011H,0403H,0505H,2007H,0069H
ARRAY2 DW 5 DUP(0) ; Empty Array
.CODE
MOV AX, @DATA
MOV DS, AX
MOV CX, 05H ; Initialize counter
MOV SI, OFFSET ARRAY1 ; Initialize Memory ptr for Source Block
MOV DI, OFFSET ARRAY2 ;Initialize Memory ptr for destination Block
UP: MOV AX, [SI] ; Read number from source array
MOV [DI], AX ; Write number to destination array
ADD SI, 2
ADD DI, 2
LOOP UP
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
UNIT-IV Assembly Language Programming

ASCENDING AND DESCENDING ORDER

Write an ALP to arrange numbers(8-bit) in array in descending order.

.MODEL SMALL
.DATA
ARRAY DB 56H,45H,12H,08H,05H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET
MOV CX,04H ;compare counter
UP:MOV AL,[SI]
CMP AL,[SI+1] ;If first no>second no then go to label Down
JNC DOWN
XCHG AL,[SI+1] ;INTERCHANGE NUMBERS
XCHG AL,[SI]
DOWN:
INC SI
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (16-bit) in array in descending order.

.MODEL SMALL
.DATA
ARRAY DW 0005H,0045H,0012H,0008H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AX,[SI]
CMP AX,[SI+2] ;If first no>second no then go to label Down
JNC DOWN
XCHG AX,[SI+2] ;INTERCHANGE NUMBERS
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (8-bit) in array in ascending order.

.MODEL SMALL
.DATA
ARRAY DB 05H,08H,12H,45H,56H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AL,[SI]
CMP AL,[SI+1] ;If first no<second no then go to label Down
JC DOWN
XCHG AL,[SI+1] ;INTERCHANGE NUMBERS
XCHG AL,[SI]
DOWN:
INC SI
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (16-bit) in array in ascending order.

.MODEL SMALL
.DATA
ARRAY DW 0005H,0008H,0012H,0045H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AX,[SI]
CMP AX,[SI+2] ;If first no<second no then go to label Down
JC DOWN
XCHG AX,[SI+2] ;INTERCHANGE NUMBERS
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35
UNIT-IV Assembly Language Programming

STRING OPERATIONS
Write an ALP to find length of string

.MODEL SMALL
.DATA
MSG DB 'COMPUTER$'
COUNT DB 0H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET MSG
NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
INC COUNT
INC SI
JMP NEXT
EXIT:
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 36
UNIT-IV Assembly Language Programming

Write an ALP to display reverse of given string

.MODEL SMALL
.DATA
MSG1 DB 'VJTECH$'
MSG2 DB 20 DUP('$')
STR2 DB 'Reverse String:$'
COUNT DB 0H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET MSG1
NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
INC SI
INC COUNT
JMP NEXT
EXIT:
MOV DI,OFFSET MSG2
UP: DEC SI
MOV AL,[SI]
MOV [DI],AL
INC DI
DEC COUNT
JNZ UP

MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,09H
LEA DX,MSG2
INT 21H

MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 37

You might also like