05 Arithmetic & Logical Instruction
05 Arithmetic & Logical Instruction
Programming PART - 2
8-bit Multiplication
• Let we have to perform 4*3 and initially
sum=0
– 1.> 0+4=4 Decrement Multiplier=2
– 2.> 4+4=8 Decrement Multiplier=1
– 3.> 8+4=12 Decrement Multiplier=0
• Since multiplier is decremented to 0, stop
the process.
• So Result = 12
8-bit Multiplication (8-bit Result)
START: MVI A, 00 ; Initial Product
MVI D, 03 ; Initialize Multiplicand
MVI C, 04 ; Initialize Multiplier
AGAIN: ADD D ; Perform Repeated Addition
DCR C ; Decrement Multiplier by 1
JNZ AGAIN ; Multiplication Done?
; ‘NO’ Go back to ADD again
STA 2503H ; ‘YES’ Store the Result
HLT ; STOP
8-bit Multiplication (16-bit Result)
START: MVI A, 00 ; Initial Product
MVI D, 03 ; Initialize Multiplicand
MVI C, 04 ; Initialize Multiplier
MVI B, 00 ; Initialize Carry Counter
AGAIN: ADD D ; Perform Repeated Addition
JNC DN1 ; Carry Generated?
INR B ; ‘YES’, Increment the carry count
DN1: DCR C ; Decrement Multiplier by 1
JNZ AGAIN ; Multiplication Done?
; ‘NO’ Go back to ADD again
STA 2503H ; ‘YES’ Store Low byte of Product.
MOV A,B ; Copy High byte of Product in A
STA 2504H ; Store the high byte of Product.
HLT ; STOP
DAD – Double Addition
• DAD RP HL ← HL + RP
– Only instruction that performs 16-bit
addition.
– Only CY Flag is affected.
– Takes 3 machine cycles for execution.
E.g.
• DAD B HL ← HL + BC
• DAD D HL ← HL + DE
• DAD H HL ← HL + HL
• DAD SP HL ← HL + SP
8-bit Multiplication (Result 16-bits)
START: MVI E, 05 ; Multiplicand in E
MVI D, 00 ; D=00, so DE = 0005
MVI C, 06 ; Multiplier in C
LXI H,0000 ; Initialize partial product
AGAIN: DAD D ; Add Multiplicand in HL
DCR C ; Decrement multiplier by 1
JNZ AGAIN ; Multiplication Done?
;‘NO’ Go back to ADD again
SHLD 2500 ; ‘YES’ Store the product.
HLT ; STOP
8-bit Division
• Let we have to perform 5/2 and initially
quotient Q=0
– 1.> 5-2=3 Increment Q; Q=1
– 2.> 3-2=1 Increment Q; Q=2
– 3.> 1-2=-1 Increment Q; Q=3
• Since result is negative so it should be
adjusted
– 4.> -1+2=1 Decrement Q; Q=2
• So Remainder = 1 and Quotient = 2
8-bit Division (Contd.)
START: MVI C, 00 ; Initialize Quotient
MVI A, 05 ; Initialize Dividend
MVI D, 02 ; Initialize Divisor
AGAIN: SUB D ; Perform Repeated Subtraction
INR C ; Increment Quotient by 1
JNC AGAIN ;Result of subtraction is Negative?
; ‘NO’ go to Subtract Again
ADD D ; ‘YES’ Adjust the Result
DCR C ; Adjust the Quotient
STA 2501H ; Store the Remainder at 2501.
MOV A,C ; Copy Quotient in A
STA 2500H ; Store the Quotient at 2500.
HLT ; STOP
Using Look-Up Table
• Write an 8085 ALP to ADDRESS DATA Comments
2600 00H Square of 0
find square of an
2601 01H Square of 1
hexadecimal integer : : :
using a Look Up 2605 19H Square of 5
Table shown next. : : :
2609 51H Square of 9
260A 64H Square of A
• Adding the integer to
260B 79H Square of B
base address would 260C :
give the address of 260D :
memory where 260E :
square is stored. 260F E1H Square of F
Square of a Number
START: MVI A, 05 ; Integer in A
LXI H,2600H ; Base Address of Table
ADD L ; Get address of square
MOV L, A
MOV A, M ; Get Square in A.
STA 2610H ; Store square.
HLT ; STOP
……alternate contd.
Square of a Number (Alternate)
START: MVI E, 05 ; Get Integer in DE
MVI D, 00 ; So DE = 0005
LXI H,2600H ; Base Address of Table
DAD D ; Get address of square
MOV A, M ; Get Square in A.
STA 2610H ; Store square.
HLT ; STOP
DAA – Decimal Adjust Accumulator
• ORA M A ← A OR ((HL))
• CY=0, AC=0, Rest of the flags are
affected by the result.
LOGICAL XOR
• XRA R A ← A XOR R
– E.g. XRA D A ← A XOR D
b7 b6 b5 b4 b3 b2 b1 b0 CY
01 01 10 1 10 10 10 10 01
Rotating Accumulator (Contd.)
• RAR Rotate Acc Right through Carry
CY A7, A7 A6, ….. A1 A0 , A0 CY
CY A7 A6 A5 A4 A3 A2 A1 A0
CY A7 A6 A5 A4 A3 A2 A1 A0
Rotating Accumulator (Contd.)
• RALRotate Acc Left through Carry
CY ← A7, A7 ← A6, ….. A1 ← A0 , A0 ← CY
CY A7 A6 A5 A4 A3 A2 A1 A0
CY A7 A6 A5 A4 A3 A2 A1 A0
A Simple Program
Write an 8085 ALP to count ‘Negative’ and
‘Positive’ numbers in a data table stored at
memory location 2500H. Assume 10 data in
the table and store the counts in memory at
locations marked as “NEG” and “POS”.
Is A < Mem Y
Data?
N
A ← Memory Data (Smaller)
All Data Y
N Checked?
Smallest Number in a Table
(Contd.)
START: LXI H, 2500H ; Initialize Memory Pointer
MVI C, 09 ; Initialize Data Counter (N-1)
MOV A, M ; Get First Data in A.
AGAIN: INX H ; Point to Next Data
CMP M ; Compare A with Memory Data.
JC DN1 ; Is A<M ?
MOV A, M ; ‘NO’, Get Smaller Number in A
DN1: DCR C ; ‘YES’, Decrement Count by 1
JNZ AGAIN ; All Data scanned?
; ‘NO’ Go back to compare Again
STA SMALLEST ; ‘YES’ Store Smallest Number
HLT ; STOP
All bytes Y
N Added?
Multibyte Addition Program
START: LXI D, 2600H ; Initialize Memory Pointers
LXI H, 2604H
LXI B, 2608H
MVI A, 04 ; Setup Byte Counter at 260D.
STA 260DH
STC; Set initial CY=0
CMC
; Since all the GPRs i.e. B, C, D, E, H, L are used to form
memory pointers, we are out of GPR to setup the byte counter.
; A is required by ALU from time to time for addition so it can
not be used as byte counter.
; Solution is to use location 260DH as byte counter.
; When A is not used for addition, count from 260DH can be -
read into A, decremented and stored back.
Multibyte Addition Program (Contd.)
NEXT: LDAX D ; Get byte of DATA1 in Acc.
ADC M ; Add the bytes of DATA1 & DATA2 with CY.
STAX B ; Store the result byte.
INX H ; Increment memory pointers for next byte.
INX D
INX B
LDA 260D ; Decrement Byte Count by 1
DCR A
STA 260D
JNZ NEXT ; All Bytes Added?
; ‘NO’ Go back to Add next byte.
MVI A, 00 ; ‘YES’ Store the final Carry
JNC CARRY
INR A
CARRY: STAX B
STOP: HLT
Table Addition
Write an 8085 ALP to ADD two data tables
and store the result in another data table.
Addr Data Addr Data Addr Data
2400 91H 2500 21H 2600 B2H
2401 28H 2501 57H 2601 7FH
2402 86H 2502 15H 2602 9BH
2403 45H 2503 A2H 2603 :
2404 97H + 2504 01H = 2604 :
2405 53H 2505 35H 2605 :
2406 98H 2506 33H 2606 :
2407 53H 2507 47H 2607 :
2408 53H 2508 28H 2608 :
2409 44H 2509 31H 2609 :
Table Addition (Contd.)
A ← A+Table2 Data Y
STOP
Increment Pointer & Store
Table Addition (Contd.)
START: LXI H, 2400H ; Store Table 1 Pointer
SHLD 2300H
LXI H, 2500H ; Store Table 2 Pointer
SHLD 2302H
LXI H, 2600H ; Store Table 3 Pointer
SHLD 2304H
MVI C, 09 ; Setup data Counter
AGAIN: LHLD 2300 ; Get Table 1 Pointer in HL
MOV A, M ; Get Table 1 Data in A
INX H ; Increment the pointer
SHLD 2300H ; Store the pointer
…………contd.
Table Addition (Contd.)
LHLD 2302H ; Get Table 2 Pointer in HL
ADD M ; A ← A+Table2 Data
INX H ; Increment the pointer
SHLD 2302H ; Store the pointer
LHLD 2304H ; Get Result Pointer in HL
MOV M, A ; Store result
INX H ; Increment the pointer
SHLD 2304H ; Store the pointer
DCR C ; Decrement Data Count
JNZ AGAIN ; Go back if Count ≠ 0.
HLT ; Stop