How do you add a 16 and a 8 bit register with carry (for example, HL, and A)?
+6
A:
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
Carl Norum
2010-02-11 21:44:16
Yea I think that works.. but shouldn't B be 0 and C be A, not the other way around? (wait now it doesn't work anymore since 'ld bc, a' isn't valid)
xkdkxdxc
2010-02-11 21:47:19
Yeah, quite possibly. Sorry don't remember the right order off the top of my head. Edited back.
Carl Norum
2010-02-11 21:50:35
Oh i think it should be add, not adc
xkdkxdxc
2010-02-11 21:53:03
I thought you wanted with Carry?
Carl Norum
2010-02-11 21:53:53
C holds the lower byte, B the higher byte. So, yeah, B should be the zero.
bart
2010-02-12 00:57:00
A:
From http://nemesis.lonestar.org/computers/tandy/software/apps/m4/qd/opcodes.html
Add Byte with Carry-In Instructions
8080 Mnemonic Z80 Mnemonic Machine Code Operation
ADC M ADC A,(HL) 8E A <- A + (HL) + Carry
Roland Bouman
2010-02-11 21:44:28
+1
A:
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 cylcles. 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
James
2010-07-26 22:12:10