문제

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:

다른 팁

When you enter the cycle loop, both A and B contains 3. So you end up with the result 3+3+2+1, which is 9.

If you add an LD A,0 right before you enter the loop you should get 3+2+1 (6), which I assume is the expected result.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top