8085 micro processor- notes
DESCRIPTION
Useful for Undergraduate electronics & Physics studentsTRANSCRIPT
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 X (say :8000).Step2. : Clear accumulator.Step3. : Add contents of memory location M to accumulator.Step4. : Increment memory pointer (i.e. X=X+1).Step5. : Add the contents of memory indicated by memory pointer to
accumulator .Step6. : Store the contents of accumulator in 8902.Step7. : Halt
Flow Chart :
Start
Initialise H-L Pair with XX00
Clear Accumulator(00) A
Add contents of M to Accumulator
# Increment memory pointer(XX01)
# Add contents of M to A
Stop
Copy the contents of Reg. A to memory location
XX02
PROGRAM:
Address of the memor
y locatio
n
Hex code
Label Mnemonics CommentsOp-code
Operand
8000 21 LXI H, 9000 Initialise memory pointer to point the first data location 9000.8001 00
8002 908003 3E MVI A,00 Clear accumulator8004 008005 86 ADD A,M The first number is added to
accumulator [A] [A]+M8006 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 accumulator8008 32 STA 9002 The contents of accumulator are
stored in memory location 8902.8009 02800A 90800B 76 HLT Stop the execution
Ex: Input : Ex:(i) 9000 – 29 H Ex:(ii) 9000 –49 H 9001 – 16 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 CarryStep 7 : Store the sum in 8502.Step8 : Store the Carry in 8503 locationStep 9 : Halt
Flow Chart :
Yes
No
PROGRAM:
Initialise H-L Pair with XX00
Clear Accumulator(00) A
Add contents of M to
Accumulator
# Increment memory pointer(XX01) &
# Add contents of M to A
Stop
Store the sum in the location xx02
Start
Is Carry exists ?
Store carry in the XX03 location
Store Zero in the XX03 location
Address of the memor
y locatio
n
Hex code
Label Mnemonics CommentsOp-code
Operand
8000 21 LXI H, 8500 H Initialise memory pointer to point the first data location 9000.8001 00
8002 858003 3E MVI A,00 Clear accumulator8004 008005 86 ADD A, M The first number is added to
accumulator [A] [A]+M8006 0E MVI C,00 Initial value of Carry is 08007 008008 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 accumulator800A 32 JNC FWD Is Carry exists ? No,go to the label
FWD800B 0E800C 80800D 0C INR C Make carry =1800E 32 FWD STA 8502 H The contents of accumulator are
stored in memory location 8502.800F 028010 858011 79 MOV A,C8012 32 STA 8503 H8013 038014 858015 76 HLT Stop the execution
Ex:
Input : Ex : 8500 – 97 H RESULT: 8502 – 32 H 8501 – 98H 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 X (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)Step 7 : Check for CarryStep 8 : Store the sum in 8502.Step8 : Store the Carry in 8503 locationStep 9 : Halt
Flow Chart :
Yes
No
PROGRAM
Initialise H-L Pair with XX00
Clear Carry register(00) C
Add contents of M to
Accumulator
# Increment memory pointer(XX01) &
# Add contents of M to A
Stop
Store the sum in the location XX02
Start
Is Carry exists ?
Store carry in the XX03 location
Store Zero in the XX03 location
Apply DAA instruction to convert Hex data to decimal
form
Address of the memor
y locatio
n
Hex code
Label Mnemonics CommentsOp-code
Operand
8000 21 LXI H, 8500 H Initialise memory pointer to point the first data location 9000.8001 00
8002 858003 0E MVI C,00 Clear accumulator8004 008005 7E MOV A, M The first number is added to
accumulator [A] [A]+M8006 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 accumulator8008 27 DAA8009 D2 JNC FWD Is Carry exists ? No,go to the label
FWD0D80
800C 0C INR C Make carry =1800D 32 FWD STA 8502 H The contents of accumulator are
stored in memory location 8502.800E 02800F 858010 79 MOV A,C Carry is moved to accumulator8011 32 STA 8503 H aCarry is stored in the location 85038012 038013 858014 76 HLT Stop the execution
Ex: Input : Ex : 8500 – 67 D RESULT: 8502 – 52 D 8501 – 85 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. Step7 : Halt
Flow Chart :
Yes
No
No
Start
Initialise H-L Pair with XXXX
Load the first 16-bit number in to H-
L Pair A
Exchange this number in to D-E
pair
Load the second 16-bit number in to H-L pair
Stop
Add the LSB of Ist number to the LSB of the second
number & MSB of the second number to MSB of the second number using
DAD instruction
Is Carry Set ?
Store “0” in the location 8506
Store ”1” in the 8506 location
Store the LSBs of the sum in 8504 & 8505 locations
PROGRAM :
ADDR-ESS
HEX –CODE
LABEL
MNEMONIC COMMENTSOPCOD
EOPERAN
D8000 2A LHLD 8500 H First 16-bit number in H-L pair8001 008002 858003 EB XCHG Exchange first number to D-E Pair8004 2A LHLD 8502 H8005 028006 858007 0E MVI 00 MSB of the sum is initially 00 8008 008009 19 DAD D Add two 16 –bit numbers800A D2 JNC FWD Is Carry? If yes go to the next
line .Else go to the 800E LOCATION800B 0E800C 80800D OC INR C Increment carry800E 22 FWD SHLD 8504 H Store the LSB of the Sum in 8504 &
MSB in 8505 locations800F 048010 858011 79 MOV A,C MSBs of the sum is in Accumulator8012 32 STA 8506 H Store the MSB (Carry) of the result in
8506 location8013 068014 858015 76 HLT Stop execution
Ex: INPUT : 8500- 12 H LSB of the Ist Number RESULT : 8504 -25H LSB of the Sum 8501- 13 H MSB of the Ist Number 8505 – 25H MSB of the Sum 8502 -13 H LSB of the IInd Number 8506- 00 Carry . 8503 -12H MSB of the IInd number
Subtraction Programs :
Program 5: Subtraction of two 8-bit numbers without borrow.
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 accumulatorStep3. : Increment H-L pair Step4. : Subtract the subtrahend in memory location M from the minuend.
Step5. : Store the result in 8502.Step6. : Stop the execution
Flow Chart :
PROGRAM: ADDR-ESS
HEX CODE
LABEL
MNEMONIC COMMENTSOPCOD
EOPERAN
D8000 21 LXI H,8500 Initialise H-L pair and get the First
number in to 8500 location 8001 008002 858003 7E MOV A,M [A] [M] 8004 23 INX H [M+1] [M]8005 96 SUB M A [A] – [M]8006 23 INX H Next memory location8007 77 MOV M,A Store the result in the location
85028008 76 HLT Stop the execution INPUT: Ex : 8500- 59H
Start
Initialise H-L Pair with XX00
Move the contents of M to Accumulator
Increment memory pointer(XX01)
Subtract the subtrahend in memory location M from the minuend in A
Stop
Store the result in the location XX02
8501-30H Result: 8502 – 29H
Example 5: 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 complementStep3. :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
Flow Chart :
Start
Initialise H-L Pair with XX01 and get the second
number
Find it’s 10’s complement
Add first number to the 10’s complement of the second number
Apply the DAA instruction to get decimal result
Stop
Store the result in the location XX02
ADDR-ESS
HEX CODE
LABEL
MNEMONIC COMMENTSOPCOD
EOPERAN
D8000 21 LXI H,8500 Initialise H-L pair and get
theSecond number in to 8501 location
8001 008002 858003 3E MVI A,99 [A] 99 8004 998005 96 SUB M 9’s complement of second number8006 3C INR A 10’s complement of second
number8007 2B DCX H Address of the first number8008 86 ADD M Add first number to 10’s
complement of second number8009 27 DAA800A 32 STA 8502 Store the result in the location
8502800B 02800C 85800D 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 numers.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 aare 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 8506Step 7: Stop the execution
Flow Chart :
Start
Yes
No
No
PROGRAM MNEMONIC COMMENTS
Initialise H-L Pair with XX00
Load the first 16-bit number in to H-
L Pair A
Exchange this number in to D-E
pair
Load the second 16-bit number in to H-L pair
Stop
Subtract LSB of 2 nd number from the LSB of the Ist number & MSB of the 2
nd number from MSB of the first number
Is borrow Set ?
Store “0” in the location XX06
Store ”1” in the XX06 location
Store the result in XX04 & XX05 locations
ADDR-ESS
HEX –CODE
LABEL
OPCODE
OPERAND
8000 2A LHLD 8500 H First 16-bit number in H-L pair8001 008002 858003 EB XCHG Exchange first number to D-E Pair8004 2A LHLD 8502 H Get the second 16-bit number in H-L
pair8005 028006 858007 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 number8009 6F MOV L,A Store the result in L- register800A MOV A,D Get higher byte of the first number800A 9C SBB H Subtract higher byte of second
number with borrow800B 67 MOV H,A800C 22 SHLD 8504 Store the result in memory locations
with LSB in 8504 & MSB in 8505 800D 04
80OE 8580OF 76 HLT Stop execution
Ex: INPUT : 8500- FF H LSB of the Ist Number RESULT : 8504 - 11H LSB 8501 - FF H MSB of the Ist Number 8505 – 11 H MSB 8502 -EE H LSB of the IInd Number 8503 –EE H MSB of the IInd number
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; Goto step 4Step 8 : Store the product i.e result in memory location.Step 9 : Stop the execution
Flow Chart :
Start
No
Yes
No
Yes
Load the multiplier in to Accumulator & multiplicand in D-E pair
Initial value of product=00.Count=08
Shift product left by one bit &Shift multiplier left by one bit
Product = Product + Multiplicand
Count = count -1
Is carry exists from
multiplier ?
Store result
Stop
Is count = 0 ?
PROGRAM :ADDR-ESS
HEX –CODE
LABEL
MNEMONIC COMMENTSOPCODE OPERAN
D8000 2A LHLD H, 8500 Load the multiplicand in to H-L pair8001 008002 858003 EB XCHG Exchange the multiplicand in to D-E
pair8004 3A LDA 8502 Multiplier in Accumulator8005 028006 858007 21 LXI H.0000 Initial value in H-L pair is 008008 008009 00800A 0E MVI C,08 Count =08800B 08800C 29 LOOP DAD H Shift the partial product left by one bit.800D 17 RAL Rotate multiplier left by one bit800E D2 JNC FWD Is Multiplier bit =1? No go to label FWD800F 128010 808011 19 DAD D Product =Product +Multiplicand8012 0D FWD DCR C COUNT=COUNT-18013 C2 JNZ LOOP8014 0C8015 808016 22 SHLD 8503 Store the result in the locations 8503
& 85048017 038018 858019 76 HLT Stop the execution
Ex: INPUT :Address Data
8500 8A H – LSB of Multiplicand
8501 00 H – MSB of Multiplicand
8502 52 H - Multiplier
Result 8503 34 H – LSB of Product8504 2C H – MSB of Product
Example 7 : Division of two 8-bit numbers.
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 arised due to shifting
is occupied by the quotient .In the present example the dividend is a 16-bit number
and the divisor is a 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.BSTEP3. : Make count C=08STEP4. : Shift dividend and divisor left by one bit
STEP 5: Subtract divisor from dividend.STEP6. : If carry = 1 : goto step 8 else step7.STEP7. : Increment quotient register.STEP8. : Decrement count in CSTEP9. : If count not equal to zero go to step 4STEP10 : Store the quotient in 8503STEP11. : Store the remainder in 8504
STEP12. : Stop execution.
Flowchart :
No
Yes
No Yes
START
Initialise H-L pair with address of dividend
Copy the divisor in to Reg. A
# Shift dividend left by one bit
# Shift Quotient left by one
8 MSBs of dividend =8 MSBs of dividend -divisor
IsCount
=0?
Quotient = Quotient +1
Count = Count-1
store th quotient in 8503 &remainder in
8504
Move the divisor in to Reg. B
Move count =08 in to C register
Is MSBs of
Dividend > Divisor
?
Stop
PROGRAM :
ADDR-ESS
HEX –CODE
LABEL MNEMONIC COMMENTSOPCODE OPERAND
8000 21 LHLD H, 8500 Initialize the H-L pair for dividend8001 00
8002 858003 3A LDA 8502 H Load the divisor from location
8502 to accumulator8004 028005 858006 47 MOV B,A Move Divisor to Reg.B from A8007 0E MVI C,08 Count =088008 088009 29 BACK DAD H Shift dividend and quotient left
by one bit800A 7C MOV A,H MSB of dividend in to
accumulator800B 90 SUB B Subtract divisor from MSB bits
of divisor800C DA JC FWD Is MSB part of dividend >
divisor ? No,goto label FWD800D 11800E 80800F 67 MOV H,A MSB of the dividend in Reg.H8010 2C INR L Increment quotient8011 0D FWD DCR C Decrement count8012 C2 JNZ BACK If count is not zero jump
to8009 location8013 098014 808015 22 SHLD 8503H Store quotient in 8503 and
remainder in 8504 locations8016 038017 858018 76 HLT Stop execution
Ex: INPUT : Address Data
8500 64 LSB of Dividend 8501 00 MSB of Dividend8502 07 Divisor 8503 0E Quotient8504 02 Remainder
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 accumulatorStep3: Compare this with the second number in Memory location.Step4: The larger in the two is placed in AccumulatorStep5: 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
Flow Chart :
Yes
No
No
No
Yes
Yes
START
Initialise H-L pair
Get the count in Register C
Get Larger number inAccumulator
IsNumber in
Accumulator> Next
number ?
Get first number in Accumulator
Decrement Count
store the result in the location 85XX
STOP
Is count = 0 ?
PROGRAM
ADDR-ESS
HEX –CODE
LABEL MNEMONIC COMMENTSOPCODE OPERAND
8000 21 LXI H, 8500 INITIALISE H-L PAIR8001 008002 858003 7E MOV C,M Count in the C register8004 23 INX H First number in H-L pair8005 4E MOV A,M Move first number in to
Accumulator8006 0D DCR C Decrement the count8007 91 LOOP1 INX H Get the next number8008 BE CMP M Compare the next number
with previous number 8009 D2 JNC LOOP2 Is next number >previous
maximum?No,go to the loop2800A 0D800B 80800C 7E MOV A,M If,yes move the large number
in to Accumulator 800D 0D LOOP2 DCR C Decrement the count800E C2 JNZ LOOP1 If count not equal to
zero,repeat 800F 078011 808012 788013 32 STA 85XX Store the largest number in
the location 85XX8014 XX8015 858016 76 HLT Stop the execution
Ex : Input : 8500- N(Say N=7 ) Result : 8508 - 7F 8501-05 8502-0A 8503-08 8504-14 8505 -7F 8506-25 8507-2D
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 I st number of the data array in to accumulatorStep3: Compare this with the second number in Memory location.Step4: The smaller in the two is placed in AccumulatorStep5: 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
Flow Chart :
Yes
No
No
No
Yes
START
Initialise H-L pair
Get the count in Register C
Get first number in to Accumulator
Get smaller number inAccumulator
IsNumber in
Accumulator< Next
number ?
Decrement Count
store the result
STOP
Is count = 0 ?
PROGRAM
ADDR-ESS
HEX –CODE
LABEL MNEMONIC COMMENTSOPCODE OPERAND
8000 21 LXI H, 8500 Initialise the H-L pair.8001 008002 858003 7E MOV C,M Count in the C register8004 23 INX H First number in H-L pair8005 4E MOV A,M Move first number in to
Accumulator8006 0D DCR C Decrement the count8007 91 LOOP1 INX H Get the next number8008 BE CMP M Compare the next number
with previous number 8009 D2 JC LOOP2 Is next number <previous
smallest ?If yes go to the loop2
800A 0D800B 80800C 7E MOV A,M No,move the smaller number
in to Accumulator 800D 0D LOOP2 DCR C Decrement the count800E C2 JNZ LOOP1 If count not equal to
zero,repeat 800F 078011 808012 788013 32 STA 85XX Store the smallest number in
the location 85XX8014 XX8015 858016 76 HLT Stop the execution
Ex: Input : 8500 - N((Say N=7) Result : 8508 – 04 8501-09 8502-0A 8503-08 8504-14 8505 -7F 8506-04 8507-2D