Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z80: How to add 16 and 8 bit registers?

Tags:

assembly

z80

How do you add a 16 and a 8 bit register with carry (for example, HL, and A)?

like image 528
xkdkxdxc Avatar asked Feb 11 '10 21:02

xkdkxdxc


2 Answers

I would like to point out that the checked response (by Carl Norum) is correct, but not the best answer. The following shows the speed of the two strategies with clock cycles. Using the right solution saves time, and won't destroy a second 16 bit register pair.

  4   ld c,a            4   add a,l
  7   ld b,0            4   ld l,a
  11  add hl,bc         4   adc a,h
                        4   sub l
                        4   ld h,a

However, the solution on the right does take an extra byte of code.

like image 161
James Avatar answered Nov 19 '22 18:11

James


You can't do it directly. You need to copy A into a 16-bit register pair and then do the add:

LD  B, 0
LD  C, A
ADC HL, BC
like image 39
Carl Norum Avatar answered Nov 19 '22 18:11

Carl Norum