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

Unit-4 Notes

The document discusses several 8086 assembly language programs including programs to find the factorial of a number, divide a 16-bit number by an 8-bit number, add two 16-bit numbers with or without carry, subtract two 16-bit numbers with or without borrow, count the number of 1s in the contents of the accumulator, and transfer a block of 4 bytes using string instructions.

Uploaded by

AjitKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit-4 Notes

The document discusses several 8086 assembly language programs including programs to find the factorial of a number, divide a 16-bit number by an 8-bit number, add two 16-bit numbers with or without carry, subtract two 16-bit numbers with or without borrow, count the number of 1s in the contents of the accumulator, and transfer a block of 4 bytes using string instructions.

Uploaded by

AjitKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Prepared by Dr. Ajit Kumar (Asst. Professor, Dept.

of ECE, Presidency University, Bengaluru)

Unit-4 8086 Assembly language program

Syllabus:

Content Page no.


1. Program to find the factorial of a number 2
2. Program to divide a 16 bit number by an 8 bit number 3
3. Program to add two 16-bit numbers with or without carry 4
4. Program to subtract two 16-bit numbers with or without borrow 5
5. Program to count the number of 1s in the contents of Accumulator 6
6. Program to transfer a block of 4-bytes by using string instruction 7
7. program to find 1’s and 2’s complement of 8-bit number 8
8. Program to find sum of ‘n’ numbers 9
9. Program to find average of ‘n’ numbers 10
10. Program to sort numbers in ascending order 11
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

1. Program to find the factorial of a number

Solution:
The factorial of a number ‘n’ is n × ( n−1 ) × ( n−2 ) × ( n−3 ) …
i.e. factorial of 4 is 4*3*2*1 = 24
Assumptions –
Starting address of program: 0400
Input memory location: 0500
Output memory location: 0600 and 0601

MNEMONICS COMMENTS
MOV CX, [0500] Move the content of memory address 0500 in register CX
MOV AX, 0001 Move 1 in the register AX
MOV DX, 0000 Move 0 in the register DX
MUL CX Multiply the contents of CX & DX and store product in
DX:AX
LOOP 040A Go to address [040A] till CX->00
MOV [0600], AX Move the content of AX in memory address 0600
MOV [0601], DX Move the content of DX in memory address 0601
HLT Stop Execution

Note –
The result of multiplication is stored in AX for byte data type.
For the word data type, the result of multiplication is stored in AX-DX. i.e.
90C3×12DA=AA8FC0E;
After multiplication AX=FC0E & DX=AA8

2
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

2. Program to divide a 16 bit number by an 8 bit number

Algorithm –
1. Assign value 500 in SI and 600 in DI
2. Move the contents of [SI] in BL and increment SI by 1
3. Move the contents of [SI] and [SI + 1] in AX
4. Use DIV instruction to divide AX by BL
5. Move the contents of AX in [DI].
6. Halt the program.

MNEMONICS COMMENT

MOV SI, 500 Assign value 500 in SI

MOV DI, 600 Assign value 600 in DI

MOV BL, [SI] Move the content of SI in Register BL

INC SI Increment SI by 1 (500+1=501)

MOV AX, [SI] Increment the content of memory


address specified by SI in register AX
DIV BL Divide the content of AX by content of
BL and store the result in AX
MOV [DI], AX Assign the value of AX to the memory
address pointed by DI
HLT End of program

Quotient Remainder

3
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

3. Program to add two 16-bit numbers with or without carry

Problem – Write a program to add two 16-bit numbers where starting address is 2000H and the
numbers are at 3000H and 3002H memory address and store result into 3004H and 3006H
memory address

Mnemonics Comment

MOV CX, 0000 Assign 0 into the reg. CX

MOV AX, [3000] Move the data of address [3000] to reg. AX

MOV BX, [3002] Move the data of address [3002] to reg. BX

ADD AX, BX AX  AX + BX
Add the contents of AX & BX and store the result in
AX
JNC L2 Jump to address 2010 if there is no carry

INC CX CX CX+ 1(0+1=1CX)

L2: MOV [3004], AX [3004] AX

MOV [3006], CX [3006]  CX

HLT Stop

Algorithm –
1. Load 0000H into CX register (for carry)
2. Load the data into AX(accumulator) from memory 3000
3. Load the data into BX register from memory 3002
4. Add BX with Accumulator AX
5. Jump if no carry
6. Increment CX by 1
7. Move data from AX(accumulator) to memory 3004
8. Move data from CX register to memory 3006
9. Stop

4
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

4. Program to subtract two 16-bit numbers with or without borrow

Problem – Write a program to subtract two 16-bit numbers where starting address is 2000 and
the numbers are at 3000 and 3002 memory address and store result into 3004 and 3006 memory
address

Mnemonics Comment
MOV CX, 0000 [CX]  0000
Assign 0 into the reg. CX
MOV AX, [3000] [AX]  [3000]
Move the data of address [3000] to reg. AX
MOV BX, [3002] [BX]  [3002]
Move the data of address [3002] to reg. BX
SUB AX, BX AX  AX - BX
JNC 2010 Jump to memory address 2010 if there is
no borrow
INC CX CX <- CX + 1
MOV [3004], AX [3004] AX
MOV [3006], CX [3006]CX
HLT Stop

Algorithm –
1. Load 0000H into CX register (for borrow)
2. Load the data into AX(accumulator) from memory 3000
3. Load the data into BX register from memory 3002
4. Subtract BX with Accumulator AX
5. Jump if no borrow
6. Increment CX by 1
7. Move data from AX(accumulator) to memory 3004
8. Move data from CX register to memory 3006
9. Stop

5
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

5. Program to count the number of 1s in the contents of Accumulator

Solution: Let AL=50H=0101 0000 has two 1s.


We have written program in which the count in register BL will give the numbers of 1s:
MOV AL, 50H Load the number in Accumulator

MVI BL, 00H Initialize register B as a counter with initial value


00H; BL0
MVI CL, 08H Initialize register C as a counter for looping with
initial value of 08H; CL8

L1: SAR AL, 1 Rotate the content of the accumulator right


through carry
JNC NEXT If Carry bit is not 1, go to NEXT

INC BL If the bit is 1, increment BL

NEXT: DEC CL Decrement counter CL(CL-1=6)


JNZ L1 If the counter CL is not zero, jump to loop
HLT Terminate program execution

After Five number iterations of CL, the content of BL=1. This process will continue until BL is
not equal to 2

6
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

6. Program to transfer a block of 4-bytes by using string instruction

Problem – Write a program to transfer a block of 4 bytes, starting address is 0500 and transfer
the block at address 0600 by using string instructions

Input data 04 18 AD 05 Output data 04 18 AD 05


Memory address Memory address
0500 0501 0502 0503 0600 0601 0602 0603

Mnemonics Comments
MOV SI, 500 SI500 Move the offset address 500 of Data segment to SI
MOV DI, 600 DI600 Move offset address 600 of Extra segment to DI
MOV AX, 0000 AX0000
MOV DS, AX Move 0000H to DS register so that base address of data segment is
0000H
MOV ES, AX Move 0000H to ES register so that base address of Extra segment is
0000H
MOV CX, 0004 CX0004 since four data have to be moved
CLD Clear directional Flag
REP reduce CX and repeat till CX≠ 0
MOVSB Move the string byte from DS:[SI] to ES:[DI]
HLT End of the program

7. Program to find 1’s and 2’s complement of 8-bit number

Problem – Write a program to convert a string of data to its two’s compliment form

7
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

Mnemonics Comment
MOV SI, 3000H SI  [3000]
MOV DI, 4000H DI  [4000]
MOV CX, 000AH CX  000A
LODSB Load the string byte at DS:[SI] into AL
NEG AL Negative of AL
STOSB Load the string byte from AL into ES:[DI]
LOOP NZ Loop through a sequence of instructions till CX>0
HLT Stop

8. Program to find sum of ‘n’ numbers


Problem – Write a program to find sum of ‘n’ numbers. Assume four numbers stored in memory
address 500, 501, 502, 503. Store the sum in address 600

Output data 0A
Input data 04 03 02 01
8 0600
Memory address 0500 0501 0502 0503 Memory address
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

Mnemonics Comments

MOV AX, 0000H Initialize AX equal to 0

MOV SI, 500 SI500 Move the offset address 500 to SI

MOV DI, 600 DI500 Move the offset address 600 to DI

MOV CX, 0004 Assume 4 data whose sum has to be found

L1: ADD AX, [SI] AXAX+[SI]


INC SI Increment SI; 500+1=501(SI)……… so on

LOOP L1 Decrement CX. Go to L1 if CX≠ 0

MOV [DI], AX Assign the value of AX to the memory address pointed by DI

HLT Stop

9. Program to find average of ‘n’ numbers


Problem – Write a program to find sum of ‘n’ numbers. Assume four numbers stored in memory
address 500, 501, 502, 503. Store the sum in address 600

Output data 2.5


Input data 04 03 02 01
0600
Memory address 0500 0501 0502 0503 Memory address

Mnemonics Comments

MOV AX, 0000H Initialize AX equal to 0

9
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

MOV SI, 500 SI500 Move the offset address 500 to SI

MOV DI, 600 DI500 Move the offset address 600 to DI

MOV CX, 0004 Assume 4 data whose sum has to be found

L1: ADD AX, [SI] AXAX+[SI]


INC SI Increment SI; 500+1=501(SI)……… so on

LOOP L1 Decrement CX. Go to L1 if CX≠ 0

DIV CX AX=AX/CX (Average of four numbers)

MOV [DI], AX Assign the value of AX to the memory address pointed by DI

HLT Stop

10. Program for Sorting an array in ascending order

• The array can be sorted in ascending order by bubble sorting.


• In bubble sorting, n-1 comparisons are performed by taking two data at a time.
• After each comparison, the two data can be rearranged in the ascending order in the same
memory location, i.e., smaller first and larger next.
• When the above n-1 comparisons are performed n-1 times, the array will be sorted in
ascending order in the same locations.
In general
For array of size ‘n’ There will be (n-1) comparison & (n-1) iterations
i.e., array of size 5  4 no. of comparisons & 4 no. of iterations

10
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

Procedure to do sorting is explained below

Flow chart to do sorting is shown below

11
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

Program for sorting is provided in next page

12
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)

Label MNEMONICS COMMENT


MOV SI, 500
MOV CL, [SI]
DEC CL
L1: MOV SI, 500
MOV CH, [SI]
DEC CH
INC SI
M1: MOV AL, [SI]
INC SI
CMP AL, [SI]
JC L2 JUMP to label L2 If CY=1
XCHG AL, [SI] SWAP AL and [SI]
L2: DEC CH CH<-CH-1
JNZ M1 JUMP to label M1 If ZF=0
DEC CL CL<-CL-1
JNZ L1 JUMP to label L1 If ZF=0
HLT END

13

You might also like