I've been having some trouble with a Z80 assembler code and maybe you could help me out. The code is as follows:
aseg
org 3000h
start: ld A, (tops)
ld B, A
cycle: add A, B
djnz cycle
rst 38h
tops: db 3
end start
The code is supposed to add up the first n integer numbers and the number n would be taken from the memory zone of tops. Thanks for the help
Take into account that if the byte at tops
is 0, the loop will actually run 256 times, and the result will overflow. In fact, the result will overflow with any value equal or greater than 23.
This program takes into consideration both issues. Result is in 16 bit HL register.
ld a,(tops)
ld hl,0
ld d,0
or a
jr z,EndSum
ld e,a
Loop: add hl,de
dec e
jr nz,Loop
EndSum: