ASSEMBLY LANGUAGE PROGRAMMING EXAMPLES

ASSEMBLY LANGUAGE PROGRAMMING EXAMPLES
Addition Programs
Example 1: Addition of two 8-bit numbers whose sum is 8-bits.
Explanation: This assembly language program adds two 8-bit numbers stored in two memory locations .The sum of the two numbers is 8-bits only. The necessary algorithm and flow charts are given below.

ALGORITHM:
Step1. : Initialize H-L pair with memory address XX00 (say: 9000).
Step2. : Clear accumulator.
Step3. : Add contents of memory location M to accumulator.
Step4. : Increment memory pointer (i.e. XX01).
Step5. : Add the contents of memory indicated by memory pointer to accumulator.
Step6. : Store the contents of accumulator in 9002.
Step7. : Halt

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,90

LXI
H, 9000
Initialise memory pointer to point the first data location 9000.
8003
3E

MVI
A, 00
Clear accumulator
8004
00




8005
86

ADD
A, M
The first number is added to accumulator [A] = [A] + M
8006
23

INX
H
Increment the memory pointer to next location of the Data.
8007
86

ADD
A, M
The 2nd number is added to contents of accumulator
8008
32,02,90

STA
9002
The contents of accumulator are stored in memory 8009 02 location 9002.
800B
76

HLT

Stop the execution

Ex: Input: Ex: (i) 9000 – 29 H & 9001 – 16 H Ex :(ii) 9000 –49 H   & 9001 –32 H
Result: Ex: (i) 9002 – 3F H Ex :( ii) 9002 – 7B

Example 2: Addition of two 8-bit numbers whose sum is 16 bits.
Explanation: The first 8-bit number is stored in one memory location (say 8500) and the second 8-bit number is stored in the next location (8501).Add these two numbers and check for carry. Store the LSB of the sum in one memory location (8502) and the MSB (carry) in the other location (8503).

ALGORITHM:
Step1. : Initialize H-L pair with memory address X (say: 8500).
Step2. : Clear accumulator.
Step3. : Add contents of memory location M to accumulator.
Step4. : Increment memory pointer (i.e. 8501).
Step5. : Add the contents of memory indicated by memory pointer to accumulator.
Step6. : Check for Carry
Step 7: Store the sum in 8502.
Step 8: Store the Carry in 8503 location
Step 9: Halt

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise memory pointer to point the first data location 8500.
8003
3E

MVI
A, 00
Clear accumulator
8004
00




8005
86

ADD
A, M
The first number is added to accumulator [A] = [A] + M
8006
0E

MVI
C, 00
Initial value of Carry is 0
8007
00




8008
23

INX
H
Increment the memory pointer to next location of the Data.
8009
86

ADD
A, M
The 2nd number is added to contents of accumulator
800A
32,0E,80

JNC
FWD
Is Carry exists? No, go to the label FWD
800D
0C

INR
C
Make carry =1
800E
32,02,85
FWD
STA
8502H
The sum is stored in memory location 8502.
8011
79

MOV
A, C
A=C
8012
32,03,85

STA
8503 H
Store the carry at 8503 location
8015
76

HLT

Stop the execution

Ex: Input: 8500 – 97 H & 8501 – 98H                      
RESULT: 8502 – 32 H & 8503 - 01 H

Example 3: Decimal addition of two 8-bit numbers whose sum is 16 bits.
Explanation: Decimal addition of two 8-bit numbers is same as that of two 8-bit numbers program. Except that the use of DAA instruction. The first 8-bit number is stored in one memory location (say 8500) and the second 8-bit number is stored in the next location(8501).Add these two numbers and use the DAA instruction to get the result in decimal. Also check for carry. Store the LSB of the sum in one memory location (8502) and the MSB (carry) in the other location (8503).

ALGORITHM:
Step1. : Initialize H-L pair with memory address XXXX (say: 8500).
Step2. : Clear Carry register C.
Step3. : Move contents of memory location M to accumulator.
Step4. : Increment memory pointer (i.e. 8501).
Step5. : Add the contents of memory indicated by memory pointer to accumulator.
Step6. : Apply the instruction DAA (Decimal adjust after addition)
Step7: Check for Carry
Step8: Store the sum in XX02.
Step9: Store the Carry in XX03 location
Step10: Halt

PROGRAM

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise memory pointer to point the first data location 8500.
8003
3E

MVI
C, 00
Clear Carry register C.
8004
00




8005
7E

MOV
A, M
Move contents of memory location M to accumulator.
8006
23

INX
H
Increment memory pointer (i.e. 8501).
8007
86

ADD
A, M
The first number is added to accumulator [A] = [A]+M
8008
27

DAA

Apply the instruction DAA(Decimal adjust after addition)
8009
D2,OD,80

JNC
FWD
Is Carry exists? No, go to the label FWD
800C
OC

INR
C
Make carry =1
800D
32,02,85
FWD
STA
8502 H
The contents of accumulator are stored in memory location 8502.
8010
79

MOV
A,C
Carry is moved to accumulator
8011
32,03,85

STA
8503 H
A Carry is stored in the location
8503
8014
76

HLT

Stop the execution

Ex: Input: 8500 – 67 D & 8501 – 85 D                        
RESULT: 8502 – 52 D & 8503 – 01 (Carry)

Example 4: Addition of two 16-bit numbers whose sum is 16 bits or more
Explanation: First 16-bit number is stored in two consecutive locations (Ex 8500 &8501) because in each location we can store only one 8-bit number. Store the second 16-bit number in the next two consecutive locations (For Ex: 8502 &8503).Add the LSB of the first number to the LSB of the second number and the MSB of the first number to the MSB of the second number using the DAD instruction. Store the sum in the next two locations and the carry (if any) in the third location

ALGORITHM:

Step1: First 16 bit number is in locations 8500 & 8501 respectively
Step2: Second 16-bit number is in locations 8502 & 8503
Step3: Add the two 16-bit numbers using DAD Instruction.
Step4: Sum is stored in locations 8504 & 8505.
Step5: Carry (if any) is stored in the location 8506.
Step6: Halt

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
2A,00,85

LHLD
8500 H
First 16-bit number in H-L pair
8003
EB

XCHB

Exchange first number to D-E Pair
8004
2A,02,85

LHLD
8502

8007
0E,00

MVI
00
MSB of the sum is initially 00
8009
19

DAD
D
Add two 16 –bit numbers
800A
D2,0E,80

JNC
FWD
FWD Is Carry? If yes go to the next line .Else go to the 800E Location
800D
OC

INR
C
Increment carry
800E
22,04,85
FWD
SHLD
8504 H
Store the LSB of the Sum in 8504 & MSB in 8505 locations
8011
79

MOV
A,C
MSBs of the sum is in Accumulator
8012
32,06,85

STA
8506 H
Store the MSB (Carry) of the result in 8506 location
8015
76

HLT

Stop the execution

Ex: INPUT:
·         8500- 12 H LSB of the 1st Number
·         8501- 13 H MSB of the 1st Number
·         8502 -13 H LSB of the 2nd Number
·         8503 -12H MSB of the 2nd number
RESULT: 8504 - 25H LSB of the Sum8505 – 25H MSB of the Sum 8506 -- 00 Carry.

Subtraction Programs:
Example 5: Subtraction of two 8-bit numbers without borrows.
Explanation: It’s a simple program similar to addition of two 8- bit numbers, except that we use the instruction SUB instead of ADD. The first 8-bit number is stored in XX00 memory location and the second 8-bit number is stored in the XX01 location .Use the SUB instruction and store the result in the XX02 location.

ALGORITHM:

Step1. : Initialise H-L pair with the address of minuend.
Step2. : Move the minuend into accumulator
Step3. : Increment H-L pair
Step4. : Subtract the subtrahend in memory location M from the minuend.
Step5. : Store the result in XX02.
Step6. : Stop the execution

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise H-L pair and get the First number in to 8500 location
8003
7E

MOV
A,M
[A] = [M]
8004
23

INX
H
Increment the memory pointer to next location of the Data.
8005
96

SUB
M
The first number is Subtract  to accumulator [A] = [A] - M
8006
23

INX
H
Increment the memory pointer to next location of the Data.
8007
77

MOV
M,A
Store the result in the location 8502
8008
76

HLT

Stop the execution

INPUT: Ex: 8500- 59H & 8501- 30H
Result: 8502 – 29H

Example 6: Subtraction of two 8-bit Decimal numbers.
Explanation: In this program we can’t use the DAA instruction after SUB or SBB instruction because it is decimal adjust after addition only. So, for decimal subtraction the number which is to be subtracted is converted to 10’s complement and then DAA is applied.

ALGORITHM:

Step1. : Initialise H-L pair with the address of second number (XX01).
Step2. : Find its ten’s complement
Step3. : Decrement the H-L pair for the first number (XX00)
Step4. : Add the first number to the 10’s complement of second number.
Step5. : Store the result in XX02.
Step6. : Stop the execution

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise H-L pair and get the Second number in to 8500 location
8003
3E

MVI
A, 99
[A] = 99
8005
96

SUB
M
9’s complement of second number
8006
3C

INR
A
10’s complement of second number
8007
2B

DCX
H
Increment the memory pointer to next location of the Data.
8008
86

ADD
M
Add first number to 10’s complement of second number
8009
27

DAA


800A
32,02,85

STA
8502
Store the result in the location 8502
800D
76

HLT

Stop the execution

Ex: Input: 8500 -76 D & 8501- 35 D
Result: 8502 - 41 D

Example 6: Subtraction of two 16 –bit numbers.
Explanation: It is very similar to the addition of two 16-bit numbers. Here we use SUB &SBB instructions to get the result .The first 16-bit number is stored in two consecutive locations and the second 16-bit number is stored in the next two consecutive locations. The lsbs are subtracted using SUB instruction and the MSBs a are subtracted using SBB instruction. The result is stored in different locations.

ALGORITHM:

Step1. : Store the first number in the locations 8500 & 8501.
Step2. : Store the second number in the locations 8502 &8503.
Step4. : Subtract the second number from the first number with borrow.
Step5. : Store the result in locations 8504 & 8505.
Step6. : Store the borrow in location 8506
Step 7: Stop the execution

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
2A,00,85

LHL
D,8500H
First 16-bit number in H-L pair
8003
EB

XCHG

Exchange first number to D-E Pair
8004
2A,02,85

LHLD
8502 H
Get the second 16-bit number in H-L pair
8007
7B

MOV
A,E
Get the lower byte of the First number in to Accumulator
8008
95

SUB
L
Subtract the lower byte of the second number
8009
6F

MOV
L,A
Store the result in L- register
800A


MOV
A,D
Get higher byte of the first number
800A
9C

SBB
H
Subtract higher byte of second number with borrow
800B
67

MOV
H,A

800C
22,04,85

SHLD
8504
Store the result in memory locations with LSB in 8504 & MSB in 8505
800F
76

HLT

Stop the execution

Ex: INPUT:
·         8500- FF H LSB of the 1st  Number
·         8501 - FF H MSB of the 1st  Number
·         8502 -EE H LSB of the 2nd  Number
·         8503 –EE H MSB of the 2nd  number
RESULT: 8504 - 11H LSB & 8505 – 11 H MSB

Multiplication Programs
Example 7: Multiplication of two 8-bit numbers. Product is 16-bits.
Explanation: The multiplication of two binary numbers is done by successive addition. When multiplicand is multiplied by 1 the product is equal to the multiplicand, but when it is multiplied by zero, the product is zero. So, each bit of the multiplier is taken one by one and checked whether it is 1 or 0 .If the bit of the multiplier is 1 the multiplicand is added to the product and the product is shifted to left by one bit. If the bit of the multiplier is 0, the product is simply shifted left by one bit. This process is done for all the 8-bits of the multiplier.

ALGORITHM:

Step 1: Initialise H-L pair with the address of multiplicand.(say 8500)
Step 2: Exchange the H-L pair by D-E pair. so that multiplicand is in D-E pair.
Step 3: Load the multiplier in Accumulator.
Step 4: Shift the multiplier left by one bit.
Step 5: If there is carry add multiplicand to product.
Step 6: Decrement the count.
Step 7: If count ¹ 0; Go to step 4
Step 8: Store the product i.e. result in memory location.
Step 9: Stop the execution

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
2A,00,85

LHLD
H,8500
Load the multiplicand in to H-L pair
8003
EB

XCHG

Exchange the multiplicand in to D-E pair
8004
3A,02,85

LDA
8502
Multiplier in Accumulator
8007
21,00,00

LXI
H,0000
Initial value in H-L pair is 00
800A
0E,08

MVI
C,08
Count =08.
800C
29
LOOP
DAD
H
Shift the partial product left by one bit
800D
17

RAL

Rotate multiplier left by one bit
800E
D2,12,80

JNC
FWD
Is Multiplier bit =1? No go to label FWD
8011
19

DAD
D
Product =Product +Multiplicand
8012
0D
FWD
DCR
C
COUNT=COUNT-1
8013
C2

JNZ
LOOP

8016
22,03,85

SHLD
8503
Store the result in the locations 8503 & 8504
8019
76

HLT

Stop the execution

INPUT:
·         8500 8AH – LSB of Multiplicand
·         8501 00 H – MSB of Multiplicand
·         8502 52 H - Multiplier
Result: 8503 34 H – LSB of Product & 8504 2C H – MSB of Product

Division Programs
Example 7: Division of a 16- bit number by a 8-bit number.
Explanation: The division of a 16/8-bit number by a 8-bit number follows the successive subtraction method. The divisor is subtracted from the MSBs of the dividend .If a borrow occurs, the bit of the quotient is set to 1 else 0.For correct subtraction process the dividend is shifted left by one bit before each subtraction. The dividend and quotient are in a pair of register H-L. The vacancy raised due to shifting is occupied by the quotient .In the present example the dividend is a 16-bit number and the divisor is an 8-bit number. The dividend is in locations 8500 &8501.Similarly the divisor is in the location 8502.The quotient is stored at 8503 and the remainder is stored at 8504 locations.

ALGORTHM:

STEP1. : Initialise H-L pair with address of dividend.
STEP2. : Get the divisor from 8502 to register A & then to Reg. B
STEP3. : Make count C=08
STEP4. : Shift dividend and divisor left by one bit
STEP 5: Subtract divisor from dividend.
STEP6. : If carry = 1: go to step 8 else step7.
STEP7. : Increment quotient register.
STEP8. : Decrement count in C
STEP9. : If count not equal to zero go to step 4
STEP10: Store the quotient in 8503
STEP11: Store the remainder in 8504
STEP12: Stop execution.

PROGRAM:

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LHLD
H, 8500
Initialize the H-L pair for dividend
8003
3A,02,85

LDA
8502 H
Load the divisor from location 8502 to accumulator
8006
47

MOV
B,A
Move Divisor to Reg. B from A
8007
0E,08

MVI
C,08
Count =08
8009
29
BACK
DAD
H
Shift dividend and quotient eft by one bit Ex: Input & Result
800A
7C

MOV
A,H
MSB of dividend in to accumulator
800B
90

SUB
B
Subtract divisor from MSB bits of divisor
800C
DA,11,80

JC
FWD
Is MSB part of dividend > divisor? No, go to label FWD
800F
67

MOV
H,A
MSB of the dividend in Reg. H
8010
2C

INR
L
Increment quotient
8011
0D
FWD
DCR
C
Decrement count
8012
C2,09,80

JNZ
BACK
If count is not zero jump to8009 location
8015
22,03,85

SHLD
8503 H
Store quotient in 8503 and remainder in 8504 locations
8018
76

HLT

Stop the execution

Ex: Input & Result
·         8500 64 _ LSB of Dividend
·         8501 00 _ MSB of Dividend
·         8502 07 _ Divisor
·         8503 0E _ Quotient
·         8504 02 _ Remainder

Largest & Smallest numbers in an Array
Example 8: To find the largest number in a data array
Explanation: To find the largest number in a data array of N numbers (say)first the count is placed in memory location (8500H) and the data are stored in consecutive locations.(8501….onwards).The first number is copied to Accumulator and it is compared with the second number in the memory location. The larger of the two is stored in Accumulator. Now the third number in the memory location is again compared with the accumulator. And the largest number is kept in the accumulator. Using the count, this process is completed, until all the numbers are compared .Finally the accumulator stores the smallest number and this number is stored in the memory location.85XX.

ALGORTHM:

Step1: Store the count in the Memory location pointed by H-L register.
Step2: Move the I st number of the data array in to accumulator
Step3: Compare this with the second number in Memory location.
Step4: The larger in the two is placed in Accumulator
Step5: The number in Accumulator is compared with the next number in memory .
Step 6: The larger number is stored in Accumulator.
Step 7; The process is repeated until the count is zero.
Step 8: Final result is stored in memory location.
Step 9: Stop the execution

PROGRAM

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise H-L PAIR
8003
7E

MOV
C,M
Count in the C register
8004
23

INX
H
First number in H-L pair
8005
4E

MOV
A, M
Move first number in to Accumulator
8006
0D

DCR
C
Decrement the count
8007
91
LOOP1
INX
H
Get the next number
8008
BE

CMP
M
Compare the next number with previous number
8009
D2,0D,80

JNC
LOOP2
Is next number > previous maximum? No, go to the loop2
800C
7E

MOV
A,M
If, yes move the large number in to Accumulator
800D
OD
LOOP2
DCR
C
Decrement the count
800E
C2,07,80

JNZ
LOOP1
If count not equal to zero, repeat
8012
78




8013
32,XX,85

STA
85XX
Store the largest number in the location 85XX
8016
76

HLT

Stop the execution

Ex: Input:
·         8500- N(Say N=7 )
·         8501-05
·         8502-0A
·         8503-08
·         8504-14
·         8505 -7F
·         8506-25
·         8507-2D
Result: 8508 - 7F

Example 9: To find the smallest number in a data array.
Explanation: To find the smallest number in a data array of N numbers (say)first the count is placed in memory location (8500H) and the data are stored in consecutive locations.(8501….onwards).The first number is copied to Accumulator and it is compared with the second number in the memory location. The smaller of the two is stored in Accumulator. Now the third number in the memory location is again compared with the accumulator. and the smallest number is kept in the accumulator. Using the count, this process is completed until all the numbers are compared .Finally the accumulator stores the smallest number and this number is stored in the memory location.85XX.

ALGORTHM:

Step1: Store the count in the Memory location pointed by H-L register.
Step2: Move the 1st number of the data array in to accumulator
Step3: Compare this with the second number in Memory location.
Step4: The smaller in the two is placed in Accumulator
Step5: The number in Accumulator is compared with the next number in memory .
Step 6: The smaller number is stored in Accumulator.
Step 7; The process is repeated until the count is zero.
Step 8: Final result is stored in memory location.
Step 9: Stop the execution

PROGRAM

Address of the memory location
Hex code
Label
Mnemonics
Comments
Op-code
Operand
8000
21,00,85

LXI
H, 8500
Initialise the H-L pair.
8003
7E

MOV
C,M
Count in the C register
8004
23

INX
H
First number in H-L pair
8005
4E

MOV
A, M
Move first number in to Accumulator
8006
0D

DCR
C
Decrement the count
8007
91
LOOP1
INX
H
Get the next number
8008
BE

CMP
M
Compare the next number with previous number
8009
D2,0D,80

JC
LOOP2
Is next number <previous smallest ?If yes go to the loop2
800C
7E

MOV
A,M
No, move the smaller number in to Accumulator
800D
0D
LOOP2
DCR
C
Decrement the count
800E
C2,07,80

JNZ
LOOP1
If count not equal to zero, repeat
8012
78




8013
32,XX,85

STA
85XX
Store the smallest number in the location 85XX
8016
76

HLT

Stop the execution

Ex: Input:
·         8500 - N((Say N=7)
·         8501-09
·         8502-0A
·         8503-08
·         8504-14
·         8505 -7F
·         8506-04
·         8507-2D
Result: 8508 – 04


REFERENCES

1.      R. S. Gaonkar, Microprocessor Architecture, Programming, and Applications with the 8085, Fifth Edition, Penram International Publishing (India) Private Limited.
2.      S Ghoshal, Microprocessor Based System Design, Macmillan India Limited, 1996
3.      M. Mano, Digital Logic and Computer Design, Prentice – Hall India
4.      B. Ram - Fundamentals of Microprocessor and Microcontrollers
5.      “Microprocessors: Principles and Applications” by A Pal
6.      “Microprocessors and Microcontrollers : Architecture, Programming and Interfacing Using 8085, 8086 and 8051” by Soumitra Kumar Mandal
7.      “Introduction to Microprocessors and Microcontrollers” by Crisp John Crisp
8.      “Microprocessors And Microcontrollers” by A Nagoor Kani
9.      “Microprocessors And Microcontrollers : Architecture, Programming and System Design 8085, 8086, 8051, 8096” by KRISHNA KANT
10.  “8 - Bit Microprocessor” by Vibhute

Comments