05 Arithmetic - Logical Instruction
05 Arithmetic - Logical Instruction
Programming PART - 2
8-bit Multiplication
• 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)
……alternate contd.
Square of a Number (Alternate)
All bytes Y
N Added?
Multibyte Addition Program
START: LXI D, 2601H ; Initialize Memory Pointers
LXI H, 2605H
LXI B, 2609H
MVI A, 04 ; Setup Byte Counter at 260D.
STA 2600H
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 2600H as byte counter.
; When A is not used for addition, count from 2600H 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 2600 ; Decrement Byte Count by 1
DCR A
STA 2600
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, 0A ; 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
LOGICAL INSTRUCTIONS
Carry & Auxiliary carry flags are fixed
and don’t convey useful information.
• NOT => CMA
• Bitwise AND => ANA
• Bitwise OR => ORA
• Bitwise XOR => XRA
NOT - CMA
• CMA A ← NOT (A)
– None of the Flags are affected.
– Only Accumulator is complemented.
– E.g. Let Flags are SF=0, ZF=0, PF=1,
AC=1, CY=0 and A = 59H
A = 59H = 0101 1001
After CMA, A = 1010 0110 = A6H
FLAGS: SF=0, ZF=0, PF=1, AC=1, CY=0
Flags are same as previous i.e. unaffected.
16-bit Subtraction
START: LXID 2050H ; Get Data 2 in DE pair
MOV A, E ; Take 2’s Comp of Data2
CMA
MOV E, A
MOV A, D
CMA
MOV D, A
INX D
LXI H,3050H ; Get Data 1 in HL pair
DAD D ; Perform subtraction
SHLD 2100H; Store result
HLT ; STOP
ASCII Code Look-Up Table
• Base Address of the ADDRESS DATA Comments
Look-Up Table is 2600. 2600 30H ASCII of 0
• Adding the Hex integer to 2601 31H ASCII of 1
base address would give : : :
the address of memory 2605 35H ASCII of 5
where the corresponding : : :
ASCII code is stored. 2609 39H ASCII of 9
• Subtracting the base 260A 41H ASCII of A
address of table from the 260B 42H ASCII of B
memory location of code 260C 43H ASCII of C
gives Hex Integer 260D 44H ASCII of D
corresponding to the 260E 45H ASCII of E
given ASCII code. 260F 46H ASCII of F
A Simple Program
• 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.)
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
• CMP B FLAG ← A - B
– Compare register B with Accumulator
– It performs a subtraction to compare
– Result of the subtraction is not stored i.e.
after CMP, both A & B remains unaffected.
– Result of subtraction is reflected through
the Flags i.e. all the flags are affected by
CMP.
CMP (Contd.)
Let initially A = 45H and B = 30H
Execution of CMP B means
– Perform A–B = 45H-30H= 15H = (0001 0101)B
After Execution of CMP B
– A = 45H and B = 30H
– Flags SF=0, ZF=0, PF=0, AC=0, CY=0.
– Note that both A & B, remains unaffected from
compare but all the flags are affected.
Uses
CY=1 ⇒ A<B, CY=0 ⇒ A≥B, (CY=0 AND ZF=0) ⇒ A>B
CMP (Contd.)
• CMP R FLAGS ← A - R
– E.g. CMP D FLAGS ← A - D
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