You are on page 1of 49

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 ADDRESS DATA Comments

to find square of an 2600 00H Square of 0

hexadecimal integer 2601 01H Square of 1

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

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

• DAA A ← BCD (A) after ADD


– Used after ADD instruction.
– Checks both the Nibbles of Accumulator
• Lower Nibble > 9 OR Flag AC=1 then
Lower Nibble ← Lower Nibble + 6
• Higher Nibble > 9 OR Flag CY=1 then
Higher Nibble ← Higher Nibble + 6
FLAGS: All the flags are affected.
BCD Addition

START: MVI A, 55 ; BCD1 into A


MVI B, 39 ; BCD2 into B
ADD B ; Binary Addition
DAA ; Adjust result of Binary
Addition for BCD.
STA 2500 ; Store the result.
HLT ; STOP

Note: DAA adjusts for BCD, only & only when


addition (ADD) was done on Valid BCD No.
BCD Subtraction
Logic:
BCD1 – BCD2 = BCD1+10’s Comp of BCD2
START: MVI B, 39H ; BCD2 into B
MVI A, 99 H ; 99 into A for 9’s Comp
SUB B ; 9’s comp of BCD2 in A
ADI 01 ; 10’s comp of BCD2 in A
DAA ; Adjust for BCD
MOV B,A ; Get 10’s comp of BCD2 in B from A
MVI A,55 ; Get BCD1 in A
ADD B ; A = BCD1 + 10’s comp of BCD2
DAA ; Adjust result for BCD.
STA 2501 ; Store the result of BCD Subtraction.
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 2601 31H ASCII of 1
to base address would : : :
give the address of 2605 35H ASCII of 5
memory where the : : :
corresponding ASCII 2609 39H ASCII of 9
code is stored.
260A 41H ASCII of A
• Subtracting the base 260B 42H ASCII of B
address of table from
260C 43H ASCII of C
the memory location of
code 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

• Use the Look-Up table for ASCII codes


shown previously and
– Write an 8085 ALP to find the ASCII code of a
Hex Integer stored at 2500H. Store the result at
location 2501H.

– Write an 8085 ALP to find the Hex Integer


corresponding to an ASCII code stored at
2502H. Store the result at location 2503H.
LOGICAL AND
• ANA R A ← A AND R
– E.g. ANA D A ← A AND D

• ANI DATA8 A ← A AND DATA8


– E.g. ANI 80H A ← A AND 80H

• ANA M A ← A AND ((HL))


• CY=0, AC=1, Rest of the flags are
affected by the result.
LOGICAL AND (Contd.)
e.g. ANI F0H A ← A AND F0H
ANI F0H, would MASK the Lower
nibble of A.
Let A = 45H = 0100 0101
F0H = 1111 0000
After ANDing A = 0100 0000 = 40H
Lower Nibble (05) is Masked.
FLAGS: SF = 0, ZF=0, PF=0.
LOGICAL OR
• ORA R A ← A OR R
– E.g. ORA D A ← A OR D

• ORI DATA8 A ← A OR DATA8


– E.g. ORI 75H A ← A OR 75H

• 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

• XRI DATA8 A ← A XOR DATA8


– E.g. XRI 95H A ← A XOR 95H

• XRA M A ← A XOR ((HL))


• CY=0, AC=0, Rest of the flags are
affected by the result.
LOGICAL XOR (Contd.)
e.g. XRI FFH A ← A XOR FFH
XRI FFH, would Complement the
content of A.
Let A = 45H = 0100 0101
F0H = 1111 1111
After XORing A = 1011 1010 = BAH
Accumulator is Complemented.
FLAGS: SF = 1, ZF=0, PF=1.
A Simple Program

Write an 8085 ALP to count ‘Even’ and ‘Odd’


numbers in a data table stored at memory
location 2500H. Assume 20 data in the
table and store the counts in memory at
locations marked as “EVEN” and “ODD”.

Hint: LS bit = 1 means Odd number

LS bit = 0 means Even number


Rotating Accumulator (RAR)

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

• RRC Rotate Acc Right without Carry


A7  A6, ….. A1  A0 , A0  A7 , A0  CY

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

• RLC Rotate Acc Left without Carry


CY ← A7, A7 ← A6, ….. A1 ← A0 , A0 ← A7

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”.

Hint: MS bit = 1 means Negative number

MS bit = 0 means Positive number


Negative & Positive Count
START: LXIH 2500H ; Set memory pointer
MVI C, 0AH ; Set data counter
MVI D, 00 ; Set positive counter
MVI E, 00 ; Set negative counter
AGAIN: MOV A, M ; Get table data in A
RAL ; Get MS bit in carry
JC DN1 ; CY=1? ‘YES’ Go for Negative CTR
INR D ; ‘NO’ Increment Positive counter
JMP DN2 ; Go to test next data.
DN1: INR E ; Increment Negative Counter
DN2: INX H ; Point to next data
DCR C ; Decrement data counter
JNZ AGAIN
MOV A, D
STA POS ; Store positive count
MOV A, E
STA NEG ; Store Negative count
HLT ; STOP
CMP – Compare with Accumulator

• 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

• CPI DATA8 FLAGS ← A - DATA8


– E.g. CPI 80H FLAGS ← A - 80H

• CMP M FLAGS ← A - ((HL))


A Simple Program

Write an 8085 ALP to find Smallest Number


in a data table stored at memory location
2500H. Assume 10 data in the table and
store the smallest number in memory at
locations marked as “SMALLEST,” a symbol
for location 2600H.
A Data Table
• Assume a Table of 10 Address Data Comments
(N=10) Data is stored 2500 91H DATA1
in memory, as shown.
2501 28H DATA2
• Take DATA1 and
compare it with 2502 86H DATA3
remaining 9 (N-1) Data. 2503 45H DATA4
• So, a total of 9 (N-1)
2504 97H DATA5
Data comparisons are
required. 2505 53H DATA6
• So, our program should 2506 98H DATA7
hold a loop counter for
9 (N-1) comparisons. 2507 53H DATA8

• If there are N Data, 2508 87H DATA9


count is N-1 for 2509 44H DATA10
comparisons.
Smallest Number in a Table
HL ← Base Address of Table
C ← Count (N-1)
A ← FIRST DATA of table
A has the smallest Data
Go to next data location Store A in Memory
i.e. HL ← HL + 1
STOP

Is A < Mem Y
Data?
N
A ← Memory Data (Smaller)

Decrement the data counter

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.

• CMC CY ← 1’s Comp (CY)


– Only CY Flags is affected.

May turn out to be useful in multibyte


addition & subtraction.
Multibyte Addition

• Add byte by byte and transfer the carry to


next higher byte for addition.
Record CY Output
from the addition
0 1 0 1 0 Ensure CY input to
of last byte.
addition of the first
byte is ‘0’.
Let Data1 = 45 86 28 91
Let Data2 = 53 98 53 97
Ans 99 1E 7C 28

• Use ADC for such additions.


Multibyte Addition (Contd.)
• Let Data are stored in Addr Data Comments
Memory as shown. 2600 91H DATA1-Lowest Byte
2601 28H :
• Note that Lowest byte
2602 86H :
of data is stored first,
2603 45H DATA1-Highest Byte
this is called “Little-
2604 97H DATA2-Lowest Byte
Endian” format, used by
2605 53H :
all the Intel Processors.
2606 98H :
• Location 260C holds CY
2607 53H DATA2-Highest Byte
arising due to the
2608 53H RESULT-Lowest Byte
addition of Highest
2609 44H :
bytes.
260A 45H :
• Location 260D will be 260B 46H RESULT-Highest Byte
used as a variable to 260C 00 CY out of RESULT
set up byte counter in 260D XX Byte Count (04) Loaded
program. & used by Program
Multibyte Addition (Contd.)
DE ← Base Address of DATA1
HL ← Base Address of DATA2
BC ← Base Address of RESULT
(260D) ← Byte Count (4)
CY ← 0

A ← DATA1 Byte + DATA2 Byte


Store CY out
+ CY
i.e. ((BC)) ← CY out

Store Result Byte


STOP
i.e. ((BC)) ← Result Byte

Increment Pointers (HL, DE and


BC) for next bytes.

Decrement the data counter

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.)

• These memory locations Addr Data Comments


will be used by the 2300 00H Table 1 Pointer
(Initially 2400)
program. 2301 24H
2302 00H Table 2 Pointers
• Program will Initialize (Initially 2500)
2303 25H
these locations and use
2304 00H Result Table Pointer
during execution. (Initially 2600)
2305 26H
Table Addition (Contd.)
Store base addresses of
tables in memory. HL ← Result Pointer
C ← Element Count
Store Result; ((HL)) ← A
HL ← Table1 Pointer
Increment Pointer & Store
A ← Table1 Data

Decrement the data counter


Increment Pointer & Store

HL ← Table2 Pointer All data


N Added?

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

You might also like