Beruflich Dokumente
Kultur Dokumente
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)
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
using a Look Up : : :
2605 19H Square of 5
Table shown next.
: : :
2609 51H Square of 9
• Adding the integer 260A 64H Square of A
to base address 260B 79H Square of B
would give the 260C :
address of memory 260D :
where square is 260E :
stored. 260F E1H Square of F
Square of a Number
……alternate contd.
Square of a Number (Alternate)
• 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 0
1 1
0 1 10 10 10 0
1 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.)
• RAL Rotate 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
Note: Assuming table has a total of 10 (N) Data items.
A Different Organization of Data Table
• First Location tells how Address Data Comments
many data are there in 2500 0AH DATA COUNT
the given table.
2501 91H DATA1
• Program should use this
2502 28H DATA2
fact as the data starts
from next location. 2503 86H DATA3
• Write program to find 2504 45H DATA4
smallest from this 2505 97H DATA5
table.
2506 53H DATA6
2507 98H DATA7
2508 53H DATA8
2509 87H DATA9
250A 44H DATA10
Manipulating Carry
• STC CY ← 1
– Only CY Flags is affected.
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