Addition of multi-byte numbers in Z-80


Now, in this section we will see how we can use the Zilog Z-80 Microprocessor to add Multi-byte numbers.

In this example, we are using 4-byte numbers (56 2F 7A 89)16 and (21 FB A9 AF)16

In the memory, at first, we are storing the byte counts, and then the numbers (from least significant bytes to Most significant bytes) in different segments. So after storing the data, the memory structure will be look like this

AddressValue
5000H04H

.
.
.
5050H89H
5051H7AH
5052H2FH
5053H56H

.
.
.
5070HAFH
5071HA9H
5072HFBH
5073H21H

.
.
.

Now, we are writing a program at location 8000H to add these two 4-byte number and store the result at location 5090H onwards.

Program

AddressHex CodesLabelsMnemonicsComments
800037
SCFSet the carry flag
80013F
CCFComplement the carry flag
800221 00 50
LD HL, 5000HLoad 5000h into HL pair
8005DD 21 50 50
LD IX, 5050HSet 5050 into index register IX
8009DD 7E 00LOOPLD A, (IX+0H)Load accumulator with IX + 00H
800CDD 8E 20
ADC A, (IX+20H)Add IX + 20H with Acc and Carry
800FDD 77 40
LD (IX+40H), AStore accumulator data at IX + 40H
8012DD 23
INC IXIncrease IX register
801435
DEC (HL)Decrease the memory content, pointer by HL pair
8015C2 09 80
JP NZ, LOOPJump to Loop, until it is 0
8018D2 20 80
JP NC, DONEIf Carry flag is off, go to Done
801BDD 36 40 01
LD (IX+40H), 01Store 01H at location IX + 40H
801F76
HALTHalt the program
8020DD 36 40 00DONELD (IX+40H), 00Store 00H at location IX + 40H
802476
HALTHalt the program

Output

AddressValue
5000H04H

.
.
.
5050H89H
5051H7AH
5052H2FH
5053H56H

.
.
.
5070HAFH
5071HA9H
5072HFBH
5073H21H

.
.
.
5090H38H
5091H24H
5092H2BH
5093H78H
5094H00H

Updated on: 30-Jul-2019

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements