0 ratings0% found this document useful (0 votes) 384 views37 pagesCode Examples PDF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Programming of 8085
In this chapter assembly language programming of different programs will be
discussed. It will help readers to go into the details of 8085 programming operations and
their applications. For this, one is supposed to be well acquainted with 8085 instruction
set discussed in the preceding chapters. Though some programming examples have also
been discussed in these chapters, yet this chapter will exclusively deal with some more
programming examples. The assembly language program written by the programmer in
mnemonic form is also called as source program. The instructions, operand and data may
be converted to binary (machine language) either by hand assembler or machine
assembler. In case of hand assembler the hexadecimal data (op code / operand and data)
are fed to the microprocessor kit through the hexadecimal key board. But in machine
assembler, instructions (in mnemonic form) with data or operand are directly fed to the
microprocessor kit through Alpha-numeric key board (computer key board). Here we are
concemed with the assembly language program (source program).
In computers high level language like C, C+, Pascal, BASIC ete are used, which
are casier than the assembly language. The computer is used for the conversion of high
level language to assembly language. The advantage of using assembly language is that it
can directly go into the details of the microprocessor’s register and manipulate as the
requirement.
6.1 SIMPLE PROGAMS
The simple programs based on the instruction set of 8085 microprocessor are given
in the following examples.
Example 6.1. Write an assembly language program of 8085 to find I's complement of
the data stored in memory location 2500 I and the result is to be stored in memory
location 2501 H.
Solution,
Program:
Label Mnemonics Operand Comments
LDA 2500H ; Load the data from 2500 H
memory location to accumulator.
MA ; Complement the contents of
accumulator (Converts in 1’s
complement).
STA 2501H ; Store the result in memory location
2501 IL.
HLT ; Stop processing.Example 6.2. Write an assembly language program of 8085 to find 2's complement of
the data stored in memory location 2100 IT and the result is to be stored in memory
location 2101 H.
Solution,
Program:
Label Mnemonics Operand Comments
LDA 2100 H ; Load the data from 2100 H
memory location to accumulator.
cMA : Complement the contents of,
accumulator (Converts in 1's
complement).
ADI o1H O1 is added to the accumulator
contents to get the 2s complement,
STA 2101H ; Store the result in memory location
2101 H.
HLT Stop processing.
Example 6.3. Write an assembly language program of 8085 to find 1's complement of n
(decimal number) bytes of data stored in memory location starting at 2501 H. The
number n (decimal number) is stored in memory location 2500 H. Store the result in
memory locations starting at 2601 H.
Solution.
Program:
Label Mnemonics Operand Comments
LXID, 2601 H : Get first address of the destination
in D-E register pair.
LXI H, 2500 H ; Get HeL pair with 2500 H.
MOV C, M ; Data in 2500 H is loaded to C=
register which will be used as
counter.
INXH ; Increment H-L register pair.
Loop MOV A, M ; Accumulator is loaded with the
data.
CMA. ; Complement the contents of
accumulator (Converts in 1's
complement)
STAX D : Store the result in memory location
addressed by D-E register pair.
INXD ; Increment D-E register pair.
INX H : Increment H-L register pair.
DCRC ; Decrement the C-register data.
IN: LOOP : If data in C-reg, is not zero jump to
LOOP for next conversion.
HLT ; Stop processing.
Example 6.4. Write an assembly language program of 8085 to find 9's complement of n
bytes (in BCD) of data stored in memory location starting from 2501 H. The number n
159(decimal number) is stored in memory location 2500 H. Store the result in memory
locations starting from 2601 I.
Solution,
Program:
Label Mnemonics
LXID,
LXI.H,
MOV C,
INX H
LOOP MVIA,
SUBM
STAXD
INXD
INX H
DCRC
INZ.
HLT
It is clear from this example that for 9”
from 99,
Operand
2601 H
2500
M
99H
Loop
Comments
5 Get first address of the destination
in D-E register pair.
+ Get H-L pair with 2500 H.
: Data in 2500 H is loaded to C-
register which will be used as
counter.
; Increment H-L register pair.
Get 99 H in accumulator.
} Subtract each byte by 99 H to get
9°s complement.
Store the result in memory location
addressed by D-E register pair.
3 Inctement D-E register pair.
3 Increment H-L register pair.
: Decrement the C-register data.
Ifdata in C-reg. is not zero jump to
loop for next conversion.
Stop processing.
complement each BCD byte is subtracted
Example 6.5. Write an assembly language program of 8085 to find 2's complement of a
16 bit number stored in memory locations 2101 H and 2102 H. The least significant byte
is in 2101 H. The result isto be stored in memory locations 2103 H and 2104 H.
Solution.
Program:
Label Mnemonics
LXI H,
MVIB,
MOV A,
CMA
ADI
STA
INC
INRB
Operand
2101
00H
M
O1H
2103 H
NXT
160
Comments
3 Get H-L pair with 2101 H.
Store 00 to register B which will be
used to store carry.
Accumulator is loaded with the
data.
; Complement the contents of
accumulator (Converts in I’s
complement)
Add 01 H to the accumulator
content to get 2°s complement.
Store the result in memory location
2103 H.
; If there is no carry jump to NXT.
Increment | to B-register as carry.NXT INXH ; Increment H-L register pair for the
second byte.
MOV A, M ; Move the second byte to
aceumulator.
CMA ; Complement the contents of
accumulator.
ADDB ; Add carry stored in register B.
STA 2504 H ; Store in 2504 H.
HLT : Stop processing.
From this example it is clear that | is added to the 1’s complement of LS byte and
to the 1’s complement of MS byte | is added if there is a carry from the previous byte.
Example 6.6. Write an assembly language program of 8085 to find 10's complement of
@ 16 bit number (BCD) stored in memory locations 2101 If and 2102 IL. The least
significant byte is in 2101 H. The result is to be stored in memory locations 2103 H and
2104 H.
Solution.
Program:
Label Mnemonics Operand Comments
LXIH, 2101 H : Get H-L pair with 2101 H.
MVIB, 00H 3 Store 00 to register B which will be
used to store carry.
MVIA, 99H Store 99 in accumulator
SUBM. ; Subtract each byte by 99 H to get
9°s complement.
ADI O1H ; Add 01 H to the accumulator
content to get 10°s complement,
STA 2103 H Store the result in memory location
2103 HL.
INC NXT ; If there is no carry jump to NXT.
INRB ; Increment 1 to B-register as carry.
NXT INXH ; Increment H-L register pair for the
second byte.
MVIA, 99 : Store 99 in accumulator
SUB M Subtract each byte by 99 H to get
9°s complement.
ADDB ; Add carry stored in register B.
STA 2104 Store in 2104
HLT + Stop processing.
From this example it is clear that 1 is added to the 9°s complement of LS byte and
to the 9°s complement of MS byte | is added if there is a carry from the previous byte.
Example 6.7. Write an assembly language program of 8085 to find 2's complement of N
bytes (N22). The number of bytes N in hexadecimal is stored in 2100 H. The bytes are
stored in memory locations starting at 2101 H. The least significant byte is in 2101 H.
The result is to be stored in memory locations starting at 2201 H.
161Solution.
Program:
Label Mnemonics
LXIH,
LXID,
MOV,
INX H
MOV A,
CMA
ADI
STAXD
INC
MVIB,
JMP
NXT MVIB,
NXT1 DCRC
LOOP INX H
INX D
MOV A,
CMA
ADDB
STAXD
INC
MVIB,
IMP
NXT2 MVIB,
NXT3 DCRC
INZ
HLT
Operand
2100H
2201H
M
O1H
NXT
O1H
NXT1
00H
NXT2
Ol
NXT3
00H
LOOP
Comments
: Get H-L pair with 2100 H.
} Get D-E pair with 2201 H.
; Get the number N in c-register to be
used as counter.
; Increment H-L pair.
; Accumulator is loaded with first
data.
; Complement the contents of
accumulator (Converts in 1's
complement).
Add 01 H to the accumulator
content to get 2°s complement.
Store the result in memory location
addressed by D-E pair.
; If there is no carry jump to NXT.
3 Store 01 to B-register as carry.
; Jump to NXTI.
3 Store 00 to B-register as carry.
; Decrement C-reg,
Increment H-L pair.
; Increment D-E pair.
; Move the next byte to accumulator.
Complement the contents of
accumulator.
3 Add carry stored in register B.
Store in the memory location
addressed by D-E pair.
; If there is no carry jump to NXT2.
3 Store 01 to B-register as carry.
; Jump to NXT3,
3 Store 00 to b-register as
Decrement C-reg
: If not zero jump to LOOP.
+: Stop processing,
Example 6.8. Write an assembly language program of 8085 to find 10's complement of
N bytes (N 22). The number of bytes N in hexadecimal is stored in 2100 H. The bytes
are stored in memory locations starting at 2101 H. The least significant byte is in 2101
H. The result is to be stored in memory locations starting at 2201 H.
Solution.
Program:
Label Mnemonics
LXI H,
LXID,
Operand
2100 H
2201H
162
Comments
: Get H-L pair with 2100 H.
; Get D-E pair with 2201 H.MOV C, M ; Get the number N in c-register to be
used as counter.
INX 1 ; Increment H-L pair.
MVIA, 99H ; Accumulator is loaded with 99 H.
SUB M ; Get the 9’s Complement of the
number stored in location
addressed by H-L pair.
ADI o1H ; Add 01 H to the accumulator
content to get 2°s complement.
STAXD Store the result in memory location
addressed by D-E pair.
INC NXT ; If there is no carry jump to NXT.
MVIB, ol ; Store 01 to B-register as carry.
JMP. NXTI ; Jump to NXTI
NXT MVIB, 00H 5 Store 00 to b-register as carry.
NXTI DCRC 3 Decrement C-reg.
LOOP INX H 3 Increment H-L pair.
INX D ; Increment D-E pair.
MVIA, 99H 3 Get 99 in accumulator.
SUBM. Get 9's Complement of the
contents of accumulator.
ADDB ; Add carry stored in register B to
get 10°s complement.
STAX D ; Store in the memory location
addressed by D-E pair.
INC NXT2 Ifthere is no carry jump to NXT2.
MVIB, o1H : Store 01 to Beregister as carry.
JMP. NXT3 ; Jump to NXT3.
NXT2 MVIB, 00H ; Store 00 to b-register as carry.
NXT3 DCRC ; Decrement C-reg.
INZ Loop 3 If not zero jump to LOOP.
HLT 3 Stop processing.
Example 6.9. Write an assembly language program of 8085 to combine two hex nibbles
stored in 2500 H and 2501 H memory locations, to form a byte. The least significant
nibble is stored in 2500 H and most significant nibble is stored in 2501 H. The byte thus
combined should be stored in 2502 H. (Let 08 H is stored in 2500 H and 09 H is stored in
2501 H, after the program is executed 2502 H should be loaded with the combined byte
89H)
Solution.
Program:
Label Mnemonics Operand Comments
LXI H, 2500 H ; Get H-L pair with 2500 H.
MOV A, M ; Data in 2500 H is loaded to
Accumulator.
RLC ; Rotate left
163RLC ; Rotate left
RLC ; Rotate left
RLC Rotate left (Rotated left four times
so that it is moved to MS nibble),
INXH ; Increment H-L register pair.
ORAM 3 Oring of two nibbles combines
them to form a byte in
accumulator.
INXH ; Increment H-L register pair.
MOV M, A Move the accumulator content
(required byte) to the memory
location addressed by H-L register
pair.
HLT ; Stop processing.
Example 6.10. Write an assembly language program of 8085 to separate a
hexadecimal number into two nibbles. The hexadecimal number is stored in 2501 H
memory location. The least significant nibble of the byte is to be stored in 2502 memory
location and the most significant nibble is to be stored in 2503 H memory location.
(Suppose 3A H is a byte stored in memory location 2501 H and the lower nibble 0A
should be stored in 2502 H and 03 should be stored in 2503 H)
Solution,
Program:
Label Mnemonics
LXI H,
MOV A,
MOVB,
ANI
INXH
MOVM,
MOV A,
ANI
INX H
MOV M,
HLT
Operand
2501
M
A
OF H
FOH
164
Comments
: Get H-L pair with 2501 H.
: Data in 2501 H_ is loaded to
Accumulator.
Accumulator content are also
loaded to B register.
; Mask off the first four digits
(higher nibble).
; Increment H-L register pair
Lower nibble is loaded to the
memory location addressed by H-L
register pair.
;Get the byte again in the
accumulator.
3 Mask off the lower nibble
: Increment H-L register pair.
: Higher nibble is loaded to the
memory location addressed by H-L
register pair.
; Stop processing.Example 6.11. Write an assembly language program of 8085 to check a hexadecimal
number stored in 2500 II memory location for odd or even parity. If the parity is even
store data EE H to memory location 2501H otherwise store 00 H in 2501 H.
Solution.
Program:
Label Mnemonics Operand Comments
LXI H, 2500 H : Get H-L pair with 2500 H.
MOV A, M ; Data in 2500 H is loaded to
Accumulator.
ORAA ; Set the flag.
JPE EVEN : Check for even parity, if parity is
even jump to EVEN.
INXH ; Increment H-L register pair.
MVIM, 00H ; Load 00 H to the memory location
addressed by H-L register pair.
JMP DONE ; Jump to DONE,
EVEN INXH ; Increment H-L register pair.
MVIM, EEH ; Load EE H to the memory lo:
addressed by H-L register pair.
DONE, HLT ; Stop processing.
Example 6.12. » (decimal number) data bytes are stored in the memory locations
starting at 2501 H. Write an assembly language program of 8085 to check if 12 H is
stored in any of the given locations. If any of the locations has 12 II then store 12 H in
that location else load 00 Hin that memory location
Solution.
Program:
Label Mnemonics Operand Comments
LXI H, 2500 H 3 Get H-L pair with 2500 H.
MOV C, M 3 Data in 2500 H is loaded to C reg
which is used as the counter.
Loop INXH ; Increment H-L register pair.
MOV A, M Load the content of Mim to
accumulator.
Pl 12H ; Compare if the accumulator data
are 12H,
JZ. NXT : Ifit is 12 H then jump to NXT.
MVIM, 00H ; Else load 00 H to the memory
location addressed by H-L register
pair.
JMP. DONE ; Jump to DONE.
NXT MVIM, 12H ; Load 12 H to the memory location
addressed by H-L register pair.
165DONE DCRC ; Decrement the content of C-reg,
for next byte.
INZ LOOP ; If the contents of C-reg. are not
zero then jump to LOOP.
HLT ; Stop processing,
Example 6.13. 16 data bytes are stored in memory locations 2001 H to 2010 H. Write
an assembly language program of 8085 to transfer this block of data bytes to memory
locations 2501 to 2510 H.
Solution.
Program:
Label Mnemonics Operand Comments
LX H, 2001H ; Get H-L pair with 2001 H.
LXID, 2501H ; Get the address of the memory
location (destination) in D-E
register pair
MVIC, 10H 3 Get 10 H (16,0) Data in C register
which is used as the counter.
LOOP MOV A, M : Load the content of My to
accumulator.
STAXD ; Load the content of accumulator to
the memory locations addressed by
D-E register pair.
INXH ; Increment H-L register pair.
INXD : Increment D-E register pair.
DCRC : Decrement the content of C-reg
for next byte.
INZ LOOP : If the contents of C-reg. are not
zero then jump to LOOP.
HLT ; Stop processing.
Example 6.14. 16 data bytes are stored in memory locations 2001 H to 2010 H. Write
an assembly language program of 8085 to transfer this block of data bytes to memory
locations 2501 to 2510 H in the reverse order (i.e. the data of 2001 is to transferred to
2510 Hand data of 2002 H to 250F H and so on).
Solution.
Program:
Label Mnemonics Operand Comments
LX! H, 2001 H ; Get H-L pair with 2001 H.
LXID, 2510 : Get the address of the memory
location (destination) in D-E
register pair
MVIC, 10H Get 10 H (16,0) Data in C register
which is used as the counter.
166LOOP M ; Load the content of Mur to
accumulator.
STAXD ; Load the content of accumulator to
the memory locations addressed by
D-E register pair.
INXH ; Increment H-L register pair.
DCXD ; Decrement D-E register pair.
DCRC ; Decrement the content of C-reg
for next byte.
INZ LOOP : If the contents of C-reg. are not
zero then jump to LOOP.
HLT ; Stop processing.
Example 6.15. Write an assembly language program of 8085 to clear the memory
locations starting at 2001 (i.e. each location should have 00 H). The length of data bytes
is given in 2000 H memory location.
Solution.
Program:
Label Mnemonics Operand Comments
LXI H, 2000 H Get H-L pair with 2000 H.
MOV C, M Use C register as counter.
XRA A : Clear the accumulator.
LOOP INXH ; Increment H-L register pair.
MOV M, A ; Load the accumulator content to
the memory location.
DCRC 3 Decrement the content of C-reg.
for next byte.
INZ Loop 3 If the contents of C-reg. are not
zero then jump to LOOP.
HLT 3 Stop processing.
Example 6.16. Write an assembly language program of 8085 to add five bytes of data
stored in memory locations starting at 2000 H. If the sum generates a carry, stop
addition and store 01 H to the memory location 2501 H; else continue addition and store
the sum at 2501 memory location.
Solution.
Program:
Label Mnemonics Operand Comments
LXIH, 2000 H : Get H-L pair with 2000 H.
MVIC, OSH 3 Store the number of data bytes in
C register as counter.
XRA A Clear the accumulator and CY flag.
LOOP ADD M ; Add the byte in accumulator.
167Je NXT If there is a carry jump to NXT.
INXH ; Increment the HeL register pair.
DCRC ; Decrement the content of C-reg
for next byte.
INZ LOOP If the contents of C-reg. are not
zero then jump to LOOP.
STA 2501 H ; Store the sum in memory location
2501.
HLT : Stop processing.
NXT MVIA, ol ; If carry occurs load 01 to
accumulator.
STA 2501 H : Store 01 H to 2501 H.
HLT : Stop processing,
Example 6.17. Write an assembly language program of 8085 to add five bytes of data
stored in memory locations starting at 2000 H. The sum may be more than one byte.
Store the result at two consecutive memory locations2500 H and 2501 H.
Solution.
Program:
Label Mnemonics Operand Comments
LXIH, 2000 H : Get H-L pair with 2000 H.
MVIC, 05H Store the number of data bytes in C
register as counter.
XRA A ; Clear the accumulator and CY flag.
MOVB, A ; Store the accumulator contents to
Beregister also for the carry.
LOOP ADD M 3 Add the byte in accumulator.
INC NXT ; If there is ano carry jump to NXT.
INRB ; Increment B as carry is generated
NXT INXH ; Increment the H-L register pair.
DCRC ; Decrement the content of C-reg
for next byte.
INZ LOOP : If the contents of C-reg. are not
zero then jump to LOOP.
LXIH, 2500 H ; Store HeL pair with 2500 H
(destination address),
MOV M, A
INX H 3 Store the sum to 2500 H.
MOV M, B : Content of B are stored in 2501 H.
HLT : Stop processing.
Example 6.18. Write a program in assembly language of 8085 to test 6" bit (De bit) of
a byte stored at 2500 H memory location. If the bit De is zero then store 00 H at 2501 H
else store the same number at 2501 H.
Solution,
Program:
168Label Mnemonics Operand Comments
LXIH, 2500 H ; Get H-L pair with 2500 H.
MOV A, M : Store the byte in accumulator
ANI 40H + Reset all the bits except Ds
JZ. END 5 If this bit is zero jump to END.
MOV A, M ; Store the byte in accumulator
again,
INXH ; Increment H-L pair.
MOV M, A The byte is stored in 2501 H.
HLT Stop processing.
END XRA A : Clear accumulator.
INXH ; Increment H-L pair.
MOV M, A ; 00 His stored in 2501 H.
HLT ; Stop processing
Example 6.19. Write an ALP (Assembly Language Program) of 8085 to find logical OR
and exclusive OR of two numbers stored at 2501 H and 2502 H memory locations. The
results should be stored at 2503 H (logical OR operation) and 2504 H (XOR operation)
memory locations.
Solution.
Program:
Label Mnemonics Operand Comments
LXIH, 2501 H Get H-L pair with 2501 H.
MOV B, M ; Store the byte in B-register.
MOV A, B Store this byte in accumulator also.
INX H : Increment H-L pair.
MOV C, M 3 Store second byte to C register.
ORAC 5 Logical OR of two bytes.
INX H ; Increment H-L pair.
MOVM, A Store the result (OR) in 2503 H.
MOV A, B ; Load the first number in
accumulator.
XRAC ; Ex-OR the two number.
INX 1 ; Increment H-L pair.
MOV M, A Store the result (KOR)
HLT : Stop processing,
Example 6.20. Write an ALP (Assembly Language Program) for 8085 to shift an 8-bit
number left by one bit. The number is stored in 2101 H memory location. The result is to
be stored in 2102 H memory location.
Solution.
Program:
Label Mnemonics Operand Comments
LDA 2101 H ; Get the number is accumulator.
ADDA : Shift it left by one bit.
STA 2102H Store the result in 2502 H.
HLT ; Stop processing,
169Example 6.21. Write an ALP 8085 to shift a 16-bit number left by one bit. The number
is stored in 2101 If and 2102 II memory locations. The result is to be stored in 2103 IT
and 2104 H memory locations,
Solution.
Program:
Label Mnemonics Operand Comments
LHLD 2101 H : Get the 16 bit number in H-L
register pair.
DAD H : Shift left by one bit.
SHLD 2103 H Store the result in 2103 H and 2104
H memory locations.
HLT ; Stop processing,
6.2 PROGAMS ON CODE CONVERSION
The programs on code conversion will now be discussed,
6.2.1 BCD to Binary Conversion
Suppose we have 0 to 99 decimal numbers (BCD numbers) and we wish to
convert any of these numbers to its equivalent binary number, we proceed in the
following way:
For example 49 (4 x 10 + 9) is the given decimal number, its binary equivalent is
(00011 00012 =31 1).
Step I Unpack the number in MSD and LSD form.
0000 1001 LSD in four least significant nibble in a register.
0000 0100 MSD in four least significant nibble in anther register.
Step II Multiply MSD by decimal number 10.
or MSD is added 10 times with 0000.
So we get (4x 10 = 0010 1000).
Step III ADD LSD to the result of step I.
ie. 00101000 + 00001001 = 00110001
Hence we get the result
Example 6.22. 4 BCD number (0 to 99) is stored in a memory location 2500 H, write
an ALP of 8085 to convert the BCD number into its equivalent binary number. Store the
result in 2600 H memory location
Solution,
Main Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H ; Initialize the stack pointer.
LXLH, 2500H ; Get H-L pair with 2500 H.
LXID, 2600 H } Get D-E pair with 2600 H.
MOV A, M : Load the byte to accumulator.
CALL. CONV ; Call conversion subroutine
program,
STAXD ; Store the result in 2600 H.
HLT
Subroutine Program:
170Label Mnemonics Operand Comments
CONV PUSH D ; Push the contents of D-E register
pair to stack
PUSH H ; Push the contents of HeL register
pair to stack.
ANI OF H 3 Separate LSD
MOV, A ; Store it to C-register.
MOV A, M ; Load the byte to accumulator again,
ANI FOH : Separate MSD.
RRC ; Rotate right four times
RRC ; to shift MSD to four
RRC ; least significant nibble
RRC of accumulator,
MOVD, A ; Store MSD to D register.
XRA A 5 Clear accumulator.
MVLE, OAH 3 Get 0A (decimal number 10) to B=
register.
SUM ADD D ; Add MSD ten times to 00H.
DCRE 3 Decrement C
INZ SUM : If addition not complete then move
to SUM.
ADDC ; Add LSD to 10 X MSD
POPH Pop the contents of H-L pair.
POPD : Pop the contents of D-E pair.
RET : Go back to main program,
6.2.2. Binary to BCD (Unpacked) Conversion
For example binary number is 1111 1111 (FF H). Its decimal equivalent is 25510
having the unpacked BCD numbers as
05 (00000101) BCD 1
05 (00000101) BCD2
02 (00000010) BCD3
‘The procedure for converting the 8 bit binary number (0000 0000 to 1111 1111)
into unpacked BCD number is given in the following steps.
Step I If the number is less than 100 go to step II. If the number is more than
100, divide the number by 100 or subtract 100 repeatedly till the reminder
is less than 100. The quotient is MS BCD (BCD 3).
Step II If the number (or remainder) is less than 10, go to step III. If number is
>10 < 100, then subtract 10 repeatedly till the remainder is less than 10.
The quotient is BCD 2.
Step III The remainder from step II is BCD 1
7Example 6.23. A binary number (between 00 H to FF H) ix stored in a memory location
2500 HI, write an ALP of 8085 to convert this number into BCD number. Store each BCD
as unpacked BCD digit in the memory locations at 2601 H to 2603 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H ; Initialize the stack pointer.
LXIH, 2500 H 3 Get H-L pair with 2500 H.
MOV A, M : Load the byte to accumulator.
CALL CONV : Call conversion subroutine
program
HLT
Subroutine Program 1:
Label Mnemonics Operand Comments
CONV LXIH, 2601 3 Get H-L pair with 2601 H.
MVIB, 64H : Load 100 in B-reg.
CALL CONVI Call another subroutine program for
the division by 100.
MVIB, OAH ; Load 10 in B-reg.
CALL CONV : Call the subroutine again for the
division by 10.
MoV M, A ; Load the accumulator contents to
the memory location,
RET ; Go back to main program
Subroutine Program 2:
Label Mnemonics Operand Comments
CONVI MVIM, FFH 3 Get FF H in the Mins.
NXT INRM. ; Increment [M,,_,]
SUBB ; Subtract the content of B from
accumulator.
INC NXT : Jump to NXT if no carry.
ADDB Add the contents of B to
accumulator.
INXH + Increment H-L register pair.
RET Go back.
Example 6.24, Write ALP of 8085 for the following statement:
A set of three packed BCD numbers (six digits or three bytes) are
memory locations starting at 2500 II. The seven segment codes of the digits 0 to 9 for
stored in
common cathode LED are stored in memory locations starting at 2050 H; and the answer
is to be stored in memory locations starting at 2070 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H Initialize the stack pointer
LXI H, 2500 H Get H-L pair with 2500 H.
172MVI D,
CALL
HLT
Subroutine Program 1:
Label
CONV
NXT
Mnemonics
LXI B,
MOV A,
ANI
RRC
RRC
RRC
RRC
CALL
INX B
MOV A,
ANI
CALL
INX B
INX H.
DCRD
INZ
RET
Subroutine Program 2:
Label
SEGMENT
‘Mnemonics
PUSH H
LXI H,
ADDL
MOV L,
MOV A,
STAX B
03H
CONV
Operand
2070 H
M
FOH
SEGMENT
M
OF H
SEGMENT
NXT
Operand
2050 HH
z>
1733
; Number of bytes to be converted is
placed in D-register.
; Call conversion subroutine
program,
Comments
: Get B-C pair with 2070 H.
; Move the content of [M,,_, ]to
accumulator.
Separate MSD.
Rotate right four times
to shift MSD to four
+ least significant nibble
+ of accumulator.
; Call subroutine program for the
conversion of unpacked BCD to
seven segment,
: Increment B-C register pair.
: Get the byte ag;
: Separate LSD.
: Call subroutine program for the
conversion of unpacked BCD
(LSD) to seven segment.
; Increment B-C register pair.
+ Increment H-L register pair.
Decrement the contents of D
register.
: If the content in D register is not
then jump to NXT for next byte.
; Return to main program.
Comments
; Push the contents of H-L register
pair to stack,
; Get H-L pair with 2050 H.
} Add the content of L register to
accumulator to get right location
for the digit from the look-up table.
3 Store it to L-register.
; Load the corresponding data from
the look-up table to accumulator
: Store the result in the memory
location addressed by B-C register
pair.POPH ; Get the contents of H-L register
pair from the stack.
RET ; Go back
DATA (in the form of look-up table) is stored in the following memory locations:
2050H 3FH (Digit 0)
2051 H 06H (Digit 1)
2052 H SBH (Digit 2)
2053 H 4FH (Digit 3)
2054 H 66H (Digit 4)
2055 H 6DH (Digit 5)
2056 H 7IDH (Digit 6)
2057 H o7H (Digit 7)
2058 H TPH (Digit 8)
2059 H oFH (Digit 9)
The 3F H data is given for the digit 0. Itis ck
D, Dg D5 Dg Dp
SFH= 0 0 ttt
of © d@ © »b a
Commn Cathode
a
£
DIGITO —» lel
‘|_I
Fig. 6.1
6.2.3 Binary to ASCII Conversion
ASCII (American Standard Code for Information Interchange) is a most
commonly used alphanumeric code. It is a seven bit code. The hexadecimal numbers 30
to 39 (0011 0000 to 0011 1001) represent 0 to 9 ASCII decimal numbers. Similarly, 41 TT
to 5A H (0100 0001 to 0101 1010) represent capital letters A to Z in ASCII.
For example, the ASCTI representation of a binary number 8E is 38 H and 45 1
(as 8 is represented by 0011 1000 or 38 H; and F is represented by 0100 0101 or 45 H).
The following steps are carried out for the conversion of Binary to ASCIT
Step I First separate the LSD and MSD of the given binary number (or byte).
Each digit is then converted to ASCIL.
Step IL If the digit is less than 10 (0A H) then add 30 H (0011 0000) to the binary
number else add 37 H (0011 0111).
For binary number 8E H, 30 is added to 08 and we get 38 H (the ASCII Code for
8) and 37 is added to OE I! to get 45 II (the ASCII Code for E).
Example 6.25. An eight bit binary number is stored in 2500 H. Write an ALP for 8085
to convert the binary number to its equivalent ASCII code. The ASCII code for most
174significant binary digit should be stored to 2601 H location; and the code for least
significant binary digit should be stored to 2602 IT location.
Solution.
in Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H ; Initialize the stack pointer.
LXIH, 2500H 3 Get HLL pair with 2500 H.
LXID, 2601 H : Get D-E pair with 2601 H.
MOV A, M ; Move the content of [M,,_, ]to
accumulator.
MOV B, A Move the accumulator content to
B-register.
RRC Rotate right four times
RRC
RRC
RRC
ASCII } Call the subroutine to convert
MSD to ASCIL
; Store the ASCII code of MS digit to
the memory location addressed by
D-E register pair.
INX D ; Increment D-E register pair.
MOV A, B ; Move the binary number again to
accumulator.
CALL ASCII ; Call the subroutine to convert LSD
to ASCII.
STAX D ; Store the ASCII code of LS digit to
the memory location addressed by
D-E register pair.
HLT : Stop processing,
Subroutine Prograt
Label Mnemonics Operand Comments
ASCII ANI OF H Isolate MS digit.
Pl OA Compare with 10 (0A H).
Jc NXT Ifit less than 10, jump to NXT.
ADI 07H : Else add 07 H.
NXT ADI 30H : Add 30 H.
RET Go back to main program,
6.2.4 ASCII to Binary Conversion:
The following steps are carried out to convert the ASCII code to binary (Hex):
Step I ‘Subtract 30 H (0011 0000) from the given binary (Hex) number.
Step II If the difference after subtraction in step I is less than 10, then it is the
required binary (Hex) number
175Step II If the difference after subtraction in step II is more than 10 then subtract
07 also from it, This will then give the required binary (Hex) number.
Example 6.26. An ASCII number is stored in 2500 H memory location. Write an ALP
for 8085 to convert this number into its equivalent binary (Hex) number and store it to
2501 memory location.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H ; Initialize the stack pointer.
LXI H, 2500 H ; Get H-L pair with 2500 H.
MOV A, M ; Move the content of [M,,_, ]to
accumulator.
CALL CONV ; Call the subroutine to convert
ASCII to binary.
INX H ; Increment H-L register pair.
MOV M, A ; Store the binary number to
required memory location.
HLT ; Stop processing.
Subroutine Program:
Label Mnemonics Operand Comments
CONV SUI 30H ; Subtract 30 H from accumulator.
CPI 0A ; Compare with 10 (0A H).
RC ; If it less than 10 go back to main
program,
SUI oH ; Else subtract 07 H.
RET ; Go back to main program.
6.3 PROGAMS ON ADDITION AND SUBTRACTION
Now we shall take up the problems on BCD or decimal addition and subtraction of
‘two bytes or multibyte. These problems are self explanatory and can be understood by
considering proper logic.
Example 6.27. Write an ALP for 8085 to add two 16-bit numbers. The augend's LS byte
is stored in 2101 H and MS byte in 2102 H. The addend’s LS and MS byte are stored in
2103 Hand 2104 H respectively. The result is to be stored in 2105 H to 2107 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LHLD 2101H ; Load the H-L pair direct with the
contents of 2101 H and 2102 H
Locations.
XCHG ; Exchange the contents of H-L and
D-E pair.
LHLD 2103 H ; Load the H-L pair direct with the
contents of 2103 H and 2104 H
Locations.
176MVIC, 00H ; Store 00 H to C register for carry.
DAD D ; Add the contents of H-L and D-E
register pair and result is in Hl
pair.
INC NXT : If there is no carry after the
addition jump to NXT.
INR C ; Increment the carry.
NXT SHLD 2105 H ; Store the contents in the required
locations.
MOV A, c ; Move carry to accumulator.
STA 2107 Store carry in 2107 H.
HLT ; Stop processing,
Example 6.28. Write an ALP for 8085 to add two N byte numbers. The augend’s bytes
are stored in memory locations starting at 2101 H and the addend's bytes are stored in
memory locations starting at 2201 H. The number N is stored in memory location 2100
H. The result is to be stored in the memory locations starting at 2201 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXIH, 2100 : Get H-L pair with 2100 H.
MOV C, M ; Move the number N to C-register
for counter.
INX 1 Increment the H-L pair.
LXI D, 2201 : Get D-E pair with 2201 H.
XRA A ; Clear the accumulator and carry
flag.
LOOP LDAX D Get the addend byte to
accumulator.
ADC M ; Add with carry the two bytes.
MOVM, A 3 Store the result in the location
addressed by H-L pair.
INX D ; Increment the D-E pair.
INX 1 ; Increment the H-L pair.
DCR C Decrement C.
INZ LOOP : If C is not zero, then jump to
LOOP.
HLT ; Stop processing
Example 6.29. Write an ALP for 8085 to add two N byte numbers. The augend's bytes
are stored in memory locations starting at 2101 H and the addend’s bytes are stored in
memory locations starting at 2201 H. The number N is stored in memory location 2100
H. The result should in decimal form and is to be stored in the memory locations starting
at 2201 H.
Solution,
Main Program:
Label Mnemonics Operand Comments
7LXI H, 2100 Get H-L pair with 2100 H.
MOV C, M ; Move the number N to C-register
for counter.
INX H Increment the HeL pair.
LXI D, 2201 H : Get D-E pair with 2201 H.
XRA A 3 Clear the accumulator and cary
flag.
Loop LDAX D Get the addend byte to
accumulator.
ADC M ; Add with carry the two bytes.
DAA ; Decimal adjust the accumulator for
the result in decimal form.
MOVM, = A ; Store the result in the location
addressed by H-L pair.
INX D ; Increment the D-E pair.
INX H Increment the HeL pair.
DCR C ; Decrement C.
INZ Loop : If C is not zero, then jump to
Loop.
HLT Stop processing,
It may be noted from this example that DAA (decimal adjust the accumulator)
instruction is used after ADC M for the conversion of the result in decimal form,
Example 6.30. Write an ALP for 8085 for multibyte (N bytes) subtraction. The number
of bytes (N) is stored in 2100 H location. The minuend bytes are stored in the memory
locations starting at 2101 II and the subtrahend bytes are stored in the memory locations
starting at 2201 H. The answer should be stored in the memory locations starting at
2101 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI H, 2100 H 3 Get H-L pair with 2100 H.
MOV C, M ; Move the number N to C-register
for counter.
INX 1 ; Increment the H-L pair.
LXI D, 2201 H Get D-E pair with 2201 H.
XRA A ; Clear the accumulator and carry
flag,
LOOP LDAX D Get the addend byte to
accumulator.
SBB M 3 Subtract with borrow the contents
of [M,,.,] from the accumulator
contents.
MOVM, A ; Store the result in the location
addressed by H-L pair.
INX D Increment the D-E pair.
INX H Increment the H-L pair.
178DCR C ; Decrement C.
INZ LOOP : If C is not zero, then jump to
LOOP.
HLT ; Stop processing,
Decimal Subtraction
It may be noted from the above example that SBB M (subtract with borrow)
instruction is used for the subtraction. The result will not be obtained in the decimal form
as DAA instruction can not be used after Subtract instructions. For decimal subtractions,
the subtrahend number should be converted to its equivalent 10’s complement which will
then be added to minuend.
Example 6.31. Write an ALP for 8085 to subtract 78 from 96. The answer in decimal
form should be stored in 2100 H memory location.
Solution.
Main Program:
Label Mnemonics — Operand Comments
MVI C, 96H 3 Get 96 in C register.
MVI B, 78H 3 Get 78 in B register.
MVI A, 99H 5 Get 99 in accumulator.
SUB B 3 Subtract the content of B-register
fiom 99 to get the 9°s complement
of 78.
INR A ; Increment accumulator to get 10’s
complement.
ADD C ; Add the content of C to. the
accumulator.
DAA ; Decimal adjust the accumulator, to
get the answer in decimal for.
STA 2100 ; Store the answer in decimal form at
2100 H location.
HLT ; Stop processing,
Example 6.32. Write an ALP for 8085 for multibyte (N bytes) decimal subtraction. The
number of bytes (N) is stored in 2100 H location. The minuend bytes are stored in the
‘memory locations starting at 2101 H and the subtrahend bytes are stored in the memor
locations starting at 2201 H. The answer obtained in decimal form should be stored in
the memory locations starting at 2101 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI SP, XXXX H : Initialize the stack pointer.
LXI H, 2100 H 2 Get H-L pair with 2100 H.
MOV C. M ; Move the number N to C-register
for counter.
INX I ; Increment the H-L pair.
LXI D, 2201 H ; Get D-E pair with 2201 H.
LDAX D Get the subtrahend byte in
accumulator.
179MOV B,
MVI A,
SUB B
INR A
ADD M
DAA
PUSH PSW
MOV M,
Loop INX D
INX H
LDAX D
MOV B,
MVI A,
SUB B
MOV B,
POP PSW
MOV A,
ADC M
DAA
PUH PSW
MOV M,
DCR C
INZ
HLT
A ; Move the accumulator content to B-
register.
99H ; Store 99 to accumulator.
; Get 9's complement of the
subtrahend
; Get 10's complement of the
subtrahend,
; Add 10’s complement of the
subtrahend to minuend.
: Give the answer in decimal form,
; Push the carry to stack.
Store the result in the location
addressed by H-L pair.
; Increment the D-E pair.
Increment the HeL pair.
3 Get the second number to
accumulator.
A ; Store it to B-register.
99H 3 Store 99 to accumulator.
: Get 9°s complement,
A Store it to B-register.
; Get the cary of the previous
addition from the stack.
B Get the Beregister content to
accumulator.
: Add with carry.
: Give the answer in decimal form,
Store the carry to stack for further
addition.
A ; Store the result in the memory
location addressed by H-L register
pair.
; Decrement the content of C-
register.
LOOP If C is not zero, then jump to
Loop.
; Stop processing,
A
6.4 PROGAMS TO FIND LARGEST OR SMALLEST NUMBER
Example 6.33 Write an ALP for 8085 to find the larger of two numbers stored in
memory locations 2101 H and 2102 H. Store the result in memory location 2103 H.
Solution.
Main Program:
Label Mnemonics
LXI H,
MOV A,
Operand Comments
2101 H : Get H-L pair with 2101 H.
M Move the first number in
accumulator.
180INX H
CMP M
INC
MOV A,
NXT INX H
MOV M,
HLT
NXT
Increment the H-L pair.
; Compare the second number with
first number.
Ifthe accumulator content is larger
then jump to NXT
; Else move [4,,_, ] to accumulator.
: Increment the H-L pair.
; Store the result in the required
memory location.
: Stop processing
Note: To get the smaller number from the given two numbers, use the instruction
JC in place of INC.
Example 6.34. Write an ALP for 8085 to find the largest number among three numbers
stored in memory locations
2104.
Solution.
Main Program:
Label Mnemonics
LXIH,
MOV A,
INX H
CMP M
INC
MOV A,
NXT INX H
CMP M
INC
MOV A,
NXTI INX H
MOV M,
HLT
Operand
2101H
M
NXT
NXT1
staring at 2101 H. Store the result in memory location
Comments
3 Get HLL pair with 2101 H.
Move the first number in
accumulator.
; Increment the H-L pair.
; Compare the second number with
first number,
Ifthe accumulator content is larger
then jump to NXT.
; Else move [44,,_, ] to accumulator.
3 Increment the H+L pair.
; Compare the third number with the
larger of first two numbers.
; If the accumulator content is larger
then jump to NXT1
; Else move [M,,_, ] to accumulator.
; Increment the H-L pair.
; Store the result in the required
memory location.
; Stop processing
Example 6.35, Write an ALP for 8085 to find the largest number among a series of N
numbers stored in memory locations staring at 2101 H. The number N is stored in
‘memory location 2100 H. Store the result in memory location 2201 H.
Solution,
Main Program:
Label Mnemonics
Operand
181
CommentsLXIH,
MOV C,
INX H
MOV A,
DCR C
Loop INX H
CMP M
INC
MOV A,
NXT DCR C
INZ
STA
HLT
65 PROGAMS TO ARRANGE A GIVEN
DESCENDING ORDER
2100
M
NXT
M
LOOP
2201
; Get H-L pair with 2100 H.
; Store the number N in C-register
which will be used as the counter.
; Increment the H-L pair.
Move the first number in
accumulator.
; Decrement count.
; Increment the H-L pair.
} Compare the second number with
first number.
; If the accumulator content is larger
then jump to NXT.
; Else move [M,,_, ] to accumulator.
; Decrement count.
; Jump to LOOP if not zero.
; Store the result in 2201 H.
; Stop processing,
SERIES IN ASCENDING OR
Example 6.36. Write an ALP for 8085 to arrange a series of N-numbers in descending
order. The number N is stored in memory location 2100 H. The series is stored in
‘memory location starting at 2101 H. The result is to be stored in memory locations
starting at 2201 H.
Solution.
Main Program:
Label Mnemonics
LXI SP,
LXI D,
LXIH,
MOV B,
START LXIH,
MOV C,
INX H
MOV A,
DCR C
LOOP INX H
CMP M
INC
MOV A,
NXT DCR C
INZ
Operand
XXXX H
2201H
2100
M
2100H
M
NXT
M
LOOP
182
Comments
; Initialize stack pointer.
: Get D-E pair with 2201 H.
3 Get H-L pair with 2100 H.
+ Store the count N in B-register.
3 Get H-L pair with 2100 H.
; Store the number N in C-register
also.
Increment the H-L pair.
Move the first number in
accumulator.
; Decrement count.
Increment the HeL pair.
; Compare the second number with
first number.
Ifthe accumulator content is larger
then jump to NXT
; Else move [M,,_, ] to accumulator,
; Decrement count,
; Jump to LOOP if not zero.STAX D
CALL
INX D
DCR B
INZ
HLT
Subroutine Program:
Label Mnemonics
SUBR LXIH,
MOV C,
AGAIN INX H
CMP M
IZ
DCRC
INZ
MVI A,
MOV M,
RET
; Store the largest number in
memory location addressed by D-E
register pair.
SUBR ; Call the subroutine SUBR to put
00 H to the memory location where
the largest number was found.
; Inctement D-E register pair.
: Decrement B.
START : Jump to START if not zero.
: Stop processing
Operand Comments
2100 ; Get FLL pair with 2100 H.
M ; Store the number N is C-register
which will be used as the counter.
Increment the HeL pair.
; Compare the number with the
largest number obtained in the
main program.
NXT : [fit is largest jump to NXT.
: Decrement count.
AGAIN : If counts not complete jump to
AGAIN.
00H ; Store 00 H to the accumulator.
A } Put 00 H to the location at which
the largest number was found.
: Go back to main program.
Example 6.37. Write an ALP for 8085 to arrange a series of N-numbers in ascending
order. The number N is stored in memory location 2100 H. The series is stored in
memory location starting at 2101 H. The result is to be stored in memory locations
starting at 2201 H.
Solution.
Main Program:
Label Mnemonics
LXI SP,
LXI D,
LXIH,
MOV B,
START LXI H,
MOV C,
INX H
MOV A,
DCR C
LOOP INX H
Operand Comments
XXXX H Initialize stack pointer.
2201 H : Get D-E pair with 2201 H.
2100 H : Get H-L pair with 2100 H.
M } Store the count N in B-register.
2100 H + Get HeL pair with 2100 H.
M : Store the number N in C-register
also.
; Increment the H-L pair.
M ; Move the first number in
accumulator.
‘Decrement count.
Increment the H-L pair.
183CMP M
Jc
MOV A,
NXT DCR C
INI
CALL
INX D
DCR B
IN}
HLT
Subroutine Program:
Label ‘Mnemonics
SUBR LXI H,
MOV C,
AGAIN INX H
CMP M
JZ
DCRC
INZ
MVI A,
MOV M,
RET
6.6 PROGRAMS ON MULTIPLICATION
NXT
LOOP
SUBR
START
Operand
2100 H
M
NXT
AGAIN
FFH
A
; Compare the second number with
first number.
; If the accumulator content is
smaller then jump to NXT.
; Else move [44,_,] to accumulator.
; Decrement count.
: Jump to LOOP if not zero.
; Store the largest mumber in
memory location addressed by D-E
register pair.
Call the subroutine SUBR to put
FF H to the memory location
where the smallest number was
found.
; Increment D4
: Decrement B.
: Jump to START ifnot zero,
: Stop processing
1 register pair.
Comments
: Get H-L pair with 2100 H.
; Store the number N is C-register
which will be used as the counter.
; Increment the H-L pair.
; Compare the number with the
smalest number obtained in the
main program.
[fit is smallest jump to NXT.
; Decrement count,
: If counts not complete jump to
AGAIN.
; Store FF H to the accumulator.
; Put FF H to the location at which
the smallest number was found.
; Go back to main program.
‘There are two methods for multiplication of two numbers.
1. Repetitive Addition Method
2. Shift and Add Method
Repetitive Addition Method
In this method multiplicand is added by the multiplier times.
For example, we
184
to multiply 8 and 4 numbers. So add 08 to 00 for four times.00
+ 08 4 time
os
+ 08 2 times
16
+ 08 3times
24
+ 08 4 times
32 Answer
Shift and Add Method
For example, we wish to multiply two decimal numbers 32 and 12 as:
32. Muttiplicand
x12. Multiplier
34
32. Shiftby one digit
384 Answer
In this example, first of all multiplicand 32 is multiplied by 2 and the result 64 is
copied in the temporary register (or written on the scratch pad). Again 32 is multiplied by
1 and result 32 is shifted left by one digit to make it 320, Now 64 and 320 are added and
wwe get the answer 384
Similar method used in binary multiplication. In binary multiplication, when a
multiplicand is multiplied by 1, we get the product equal to multiplicand. When a
multiplicand is multiplied by 0, we the product zero. For example 08 and 04 are
multiplied as:
00001000
xX 00000100
00000000
00000000
00001000
00000000
90000000
00000000
00000000
00000000
0000000000100000
The answer is 0020 H (3210). It may be noted that the answer is not in BCD.
This method may further be illustrated using the following flow chart,
185START
GET MULTIPLIER AND
‘MULTIPLICANT
INITIAL VALUE OF
PRODUCT = 00, COUNT = 00
SHIFT PRODUCT LEFT BY
SHIFT MULTIPLIER LEFT BY ONE BIT
PRODUCT = PRODUCT +
‘MULTIPLICAND
COUNT = COUNT -1
STORE PRODUCT
Fig. 6.1
186Example 6.38.
Write an ALP for 8085 to multiply any number (stored in memory
location 2101 11) by 2 and store the result in 2102 H memory location.
Solution,
Program:
Label Mnemonics Operand
LX H, 2101H
MOV A, M
sTC
cmc
RAL
INX H
MOV M, A
HLT
Comments
3 Get the H-L pair with 2101 H.
; Move the number in accumulator.
Set the carry flag.
: Complement the carry ( it clear the
camry.
; Rotate accumulator with carry ice
it multiplies the accumulator
content by two.
; Increment the HeL pair.
Store the result in 2102 H memory
location.
; Stop processing,
Example 6.39, Write an ALP for 8085 to multiply two numbers using repetitive addition
method. The two numbers are in memory locations 2101 H and 2102 H. Store the result
in 2103 H and 2104 H.
Solution.
Main Program:
Label Mnemonics Operand
LXIH, 2101 H
MOV B, M
INX H
MOV A, M
CMP B
INC NXT
MVI D, 00H
MOV E, B
JMP NXT
NXT MVI D, 00H
MOV EF, A
MOV A,B
NXTI LXI H, 0000 H
ORA A
JZ. END
DAD D
187
Comments
; Get FLL pair with 2101 H.
+ Get first number in B-register.
Increment H-L pair.
3 Get the second
accumulator.
+: Compare the two numbers.
: IEAB, so B should be used as
counter.
; Store accumulator content in E-
register.
; Store Beregister
accumulator.
+; Put 00 to H and L registers.
: ORing of A with A to find if A =
00H.
; Ifzero jump to End,
: Add the contents of H-L pair with
DE pair and the result is in H-L
pair.
number in
content inDCR A ; Decrement count.
INZ LOOP : If not zero jump to LOOP.
END SHLD 2103 H Store the result.
HLT Stop processing,
Example 6.40, Write an ALP for 8085 to multiply two numbers using shift and add
method. The two numbers are in memory locations 2101 H and 2102 H. Store the result
in 2103 H and 2104 H.
Solution.
‘Main Program:
Label Mnemonics Operand Comments
LXI H, 2101 H : Get H-L pair with 2101 H.
MOV E, M : Get first number in E-register.
MVI D, 00H ; Extend to 16 bits.
INX H ; Increment H-L pair.
MOV A, M 3 Get the multiplier in aecumulator.
LXI H, 0000 H ; Put 00 to H and L registers.
MVI B, 08H 3 Get count = 08,
MULT DAD H ; Multiplicand = 2 x Multiplicand.
RAL ; Rotate accumulator left to find if
most significant bit is 1.
INC NXT : If no carry jump to NXT.
DAD D Get Product. = product
multiplicand.
NXT DCR B ; Decrement B
INZ MULT +: Ifnot zero jump to MULT.
SHLD 2103 H ; Store the result in 2103 H and
2104
HLT Stop processing,
6.7 PROGRAM ON 8-BIT DIVISION
To divide 16 bit dividend with 8 bit divisor, the divisor is subtracted from the 8
most significant bits of the dividend. If there is no borrow, the bit of the quotient is set to
1, otherwise 0. The dividend is then shifted left by one bit before each trial of subtraction.
The dividend and quotient share a register pair. Due to shift of dividend one bit of the
register falls vacant in each subtraction, The quotient is stored in vacant bit positions. It is
illustrated by the flow chart shown in figure 6.2.
If the dividend of the division is an 8-bit number, then it is extended to a 16-bit
number by placing zeros in the MSBs positions.
188GET DIVIDEND. DIVISOR
QUOTIENT =00
COUNT = 08
SHIFT DIVIDEND LEFT BY ONE BIT
SHIFT QUOTIENT LEFT BY ONE BIT
QUOTIENT = QUOTIENT + 1
»
8 MSB'S OF DIVIDEND=
8 MSB' § DIVIDEND - DIVISOR
1
COUNT = COUNT -1
No
Yes
STORE RESULT
189Example 6.41, Write an ALP for 8085 to divide a 16-bit number by an 8-bit number. The
dividend bytes are stored in memory locations 2101 IT and 2102 II. (LS byte in 2101 IT
and MS byte in 2102 H). The divisor is stored in memory location 2103 H. The quotient
is to he stored in the memory location 2104 H and the remainder is to be stored in the
memory location 2105 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LHLD 2101 H : Get 16-bit dividend in H-L pair.
LDA 2103 H : Get divisor in accumulator.
MVI C, 08 1 3 Get count 08 in C-register.
MVI D, 00 H + Quotient = 00.
MOV B, A ; Keep the divisor in B-register also,
LOOP DAD H Shift dividend left by one bit.
MOV A, D ; Keep the quotient in accumulator.
ADD A +} Shift quotient left by one bit.
MOV D, A 3 Store the quotient in D-register.
SUB B Perform trial subtraction.
Je NXT ; If carry then dividend = previous
dividend.
MOV H, A ; Put most significant bits of,
dividend in register H.
INR D Increment quotient by 1
NXT DCR C ; Decrement C
INZ LOOP : Ifnot zero jump to LOOP.
MOV L, D Store the quotient in L-register.
SHLD 2104H ; Store the result in 2104 H and
2105.
HLT : Stop processing,
6.8 MISCELLANEOUS PROGRAMS
Example 6.42. Write an ALP for 8085 to find the square of a given number stored in
‘memory location 2101 H and the result is to be stored in memory location 2102 H. Use
Look-up table starting at 2200 H.
Solution.
Main Program:
Label Mnemonics Operand Comments
LXI D, 2200H ; Get D-E pair with 2200 H (Starting
address of the look-up table).
LDA 2101H Get the number in accumulator.
MOV L, A Load the number to L-register
MVI H, 00H ; Get 00 H to H-register.
DAD D ; Get effective address in H-L
register pair.
MOV A, M Get the result in accumulator.
190Look-up table:
Location
2200
220111
2202 H
2203 H
2204 H
2205 H
2206 H
2207H
2208 H
22094
220A H
220B H
220C H
220D H
220E H
220F H
Example 6.43.
Solution.
‘Main Program:
Label
Loop
END
STA
HLT
Data
0oH
on
04H
oon
10H
19H
24H
31H
40H
SIH
64H
79H
90H
AQH
C4H
EIH
2102
; Store the result in the required
memory location.
; Stop processing,
Write an ALP for 8085 to find the square of a given number stored in
memory location 2101 II and the result is to he stored in memory location 2102 I. Use
the addition of successive odd integers.
Mnemonics
LDA
MVI L,
ORA L
IZ
MOV C,
MOV A,
MVIB,
ADD B
INR B
INR B
DCR C
INZ
STA
Operand
2101 H
OOH
END
A
L
1H
LOOP
21021
191
Comments
: Get the number in accumulator.
Load 00 H to L-register.
; Find if number is zero,
; If number is zero then no need to
find the square and jump to END.
Move the number to C-register.
; Move the content of L-reg to
accumulator so that square is zero.
; Move 01 to Beregister (first odd
number)
5 Add next odd number
; Find next odd number to add
3 02.
; Decrement C-register contents.
Continue if not zero,
Store the result in the memory
location 2102 H.HLT ; Stop processing,
Example 6.44, Write an ALP for 8085 to find the square root of a given number (perfect
positive square) stored in memory location 2101 H and the result is to be stored in
memory location 2102 H. Use the subtraction of successive odd integers
Solution.
Main Program:
Label Mnemonics Operand Comments
LDA 2101 H ; Get the number in accumulator.
ORA A : Find if number is zero,
A END ; If number is zero then no need to
find the square root and jump to
END.
MVIB, ol ; Move 01 to B-register (first odd
number)
MVIC, o1H Initial value of square root as one.
LOOP SUB B 3 Subtract next odd number.
JZ. END ; If number is zero then jump to
END.
INR B ; Find next odd number to add
INR B 3 02.
INR C ; Decrement square root value by 1.
INC LOOP ; Continue if no carry
END MOV A, c Store the result in accumulator.
STA 2102 H ; Store the result in the memory
location 2102 H.
HLT ; Stop processing.
Example 6.45. Write an ALP for 8085 to find the Fibonacci series. Sixteen terms of this
series are to be store in the memory locations starting at 2101 H. Let 00 H and 01 H
data are stored in memory locations 2101 H and 2102 H respectively before the
execution of the program.
Solution. The terms of Fibonacci series are given as:
1, 1, 2, 3, 5, 8 13, 21,
This sequence is defined by assuming first two terms have the same value 1, and
each term afterwards is the sum of the previous two terms, that is,
T¥1=2, 1+2=3, 243=5, 3+5=8, andsoon
‘The required terms of the
Main Program:
Label Mnemonics Operand Comments
MVI C, 10H ; Decimal number 16 (10 H) is stored
in C-register which will be used as
counter.
LXI H, 2100H ; Get H-L pair with 2100 H.
MOV A, M ; Move first data to accumulator.
INX H Point to next data.
LOOP ADD M ; Get next term of the series.
19210,
INX H 5 Point to the next memory location
for storing the next term,
MOVM, A Store the next term.
DCX H +: Get previous term of the Fibonacci
series.
MOV A, M 3 Get this term in accumulator.
INX H ; Point to the next memory location.
DCR C ; Decrement count.
INZ LOOP : If not zero jump to LOOP.
HLT ; Stop processing,
Data stored before the execution of the program.
2100 1 00H
2101 H O1H
PROBLEMS
Write an assembly language program of 8085 to fill the RAM area from 2500 H to
25FF H with a byte 33 H.
Sixteen bytes of data are stored in memory locations 2001 H to 2010 H. Write an
assembly language program of 8085 to transfer this block of data to 2006 H to 2015
H.
(Hint: In this case data will be transferred in the reverse order otherwise some of
the data will coincide.)
Write a program (WAP) in assembly language of 8085 to compare two hexadecimal
numbers, stored in 2001 H and 2002 H memory locations, for equality. If the
numbers are equal, the number itself will be stored in 2003 H memory location, else
00 H will be stored in the memory location 2003 H.
WAP in assembly language of 8085 to test 3° bit (Ds bit) of a byte stored at 2000 H
memory location. If the bit D3 is zero store 00 at 2101 H else store the same
number at 2101 H.
Write an assembly language program (ALP) of 8085 to find the logical AND and
Logical OR of 26 H and 39 H. Store the result in 2500 H and 2501 H.
Write an ALP of 8085 to load EE H to the memory locations starting at 2501 1.
The length of data bytes is given in 2500 H memory jocation.
Write an ALP for 8085 to add two 8-bit numbers stored in memory locations 2101
H and 2102 H. The result should be stored in 2103 H and the carry if any should be
stored in 2104 H.
Write an assembly language program for 8085 to add two 8 bit numbers stored in
memory locations 2101 H and 2102 H. The answer is required in decimal form and
it should be stored in 2103 H and the carry if any should be stored in 2104 H
Write an ALP for 8085 to add 833 and 776 decimal numbers. The result should be
stored in 2101 H to 2103 H memory locations. The memory location 2103 H should
store the carry bit if any.
(Hint: First convert the decimal numbers 833 and 776 to hexadecimal numbers.)
Write an ALP for 8085 to subtract an 8 bit number (stored in 2101 H memory
location) from another 8 bit number (stored in 2102 H memory location). The
19311
12,
16.
7.
answer should be stored in 2103 H and the borrow bit if any should be stored in
2104 H,
Repeat the problem 10 to get the result in decimal form,
Write an ALP for 8085 to find smaller of two numbers stored in 2301 H and 2302
H. Store the smaller number in memory location 2303 H.
Write an ALP for 8085 to find the smallest of the three numbers stored in memory
Jocations starting at 2301 H. Store he smallest number in 2304 H.
Write an ALP for 8085 to find the smallest number among a series of 16 numbers
stored in the memory locations starting at 2301 H. The number 16 (10 H) is stored
in 2300 H. The smallest number should be should be stored in 2401 H.
Write an ALP to find 13 terms of Fibonacci series. The terms of the series are to be
stored in the memory locations starting at 2501 H.
Write an ALP for 8085 to shift an 8-bit number left by two bits. The number is
stored in memory location 2501 H and the result is to be stored in 2502 H memory
location,
(Hint: Keep the number in accumulator and use ADD A instruction twice.)
Write an ALP for 8085 to shift a 16-bit number left by two bits. The number is
stored in memory locations 2501 H and 2502 H. The result is to be stored in 2503 H
and 2504 H memory locations.
194