Code: Select all
cp de,bc
jp c,bcIsBigger
If there is no efficient way to do this with these registers, what would be the fastest/smallest way to do it with hl? sbc hl,de?
Moderator: MaxCoderz Staff
Code: Select all
cp de,bc
jp c,bcIsBigger
Code: Select all
ld h,d
ld l,e
or a
sbc hl,bc
Code: Select all
push hl
ld h,0
ld l,h
add hl,de
add hl,bc
ld d,h
ld e,l
pop hl
Code: Select all
ld a,d
cp b
jp c,bcIsBigger
jp nz,deIsBigger
ld a,e
cp c
jp c,bcIsBigger
jp nz,deIsBigger
<they are equal here>
Another way of doing it byte by byte:CoBB wrote:What about doing it byte by byte?
Code: Select all
ld a,d cp b jp c,bcIsBigger jp nz,deIsBigger ld a,e cp c jp c,bcIsBigger jp nz,deIsBigger <they are equal here>
Code: Select all
ld a, e
sub c
ld a, d
sbc a, b
Indeed, I knew I was overlooking something trivial. That’s what happens when you stop dealing with low-level stuff...qarnos wrote:Another way of doing it byte by byte:
Carry flag set if BC > DE.Code: Select all
ld a, e sub c ld a, d sbc a, b
Zero flag set if BC == DE.
Code: Select all
ld a,e
add a,c
ld e,a
ld a,d
adc a,b
ld d,a
Yep, that would work.Timendus wrote:That looks real nice, thanks qarnos!
And doing a smaller add de,bc, would that be something like this?Just thinking out loud here, no idea if it'll workCode: Select all
ld a,e add a,c ld e,a ld a,d adc a,b ld d,a
Code: Select all
ex de, hl
add hl, bc
ex de, hl
Pretty sure that doesn't work.qarnos wrote:Another way of doing it byte by byte:CoBB wrote:What about doing it byte by byte?
Code: Select all
ld a,d cp b jp c,bcIsBigger jp nz,deIsBigger ld a,e cp c jp c,bcIsBigger jp nz,deIsBigger <they are equal here>
Carry flag set if BC > DE.Code: Select all
ld a, e sub c ld a, d sbc a, b
Zero flag set if BC == DE.
Code: Select all
ex de,hl
or a
sbc hl,bc
add hl,bc
ex de,hl
Interesting - I hadn't though of that.Jim e wrote:Pretty sure that doesn't work.qarnos wrote:Carry flag set if BC > DE.
Zero flag set if BC == DE.
BC = $00FF
DE = $0100
e - c = 00 - FF = carry set
d - b = 01 - 00 - carry = zero and no carry.
This was my first idea, but I forgot ADD HL doesn't affect the zero flag. That'll learn me.For a non-destructive comparison of de - bc this would work.
Yes all the flags do match CP despite that add hl,bc being there. It doesn't use the stack nor is it very large so you can macro it.Code: Select all
ex de,hl or a sbc hl,bc add hl,bc ex de,hl
That wouldn’t be enough, because B can be so much greater than D that the difference goes below -128. You’d have to check the overflow flag too, and it would become rather complicated.qarnos wrote:Perhaps we could check the sign flag. It should be M if BC >= DE and P otherwise, right? I haven't thought that one through at all - it's just a guess, so I may be wrong.
I thought there'd be a gotcha in there somewhere. Oh well.CoBB wrote:That wouldn’t be enough, because B can be so much greater than D that the difference goes below -128. You’d have to check the overflow flag too, and it would become rather complicated.qarnos wrote:Perhaps we could check the sign flag. It should be M if BC >= DE and P otherwise, right? I haven't thought that one through at all - it's just a guess, so I may be wrong.
Yes, that's what I thought. but I never like to say "this will work" without actually running the codeqarnos wrote:You can think of the carry flag from the ADD instruction as being the 9th bit of the result. When you do the ADC, you are carrying-over that bit into the high byte.
Also, it's much easier for other people to read, because it makes more sense. So I should definately use this one, thanks!Edit: Though a smaller (3 bytes instead of 6) and faster (19 T-states versus 24) way to add DE and BC would be this:
Code: Select all
ex de, hl add hl, bc ex de, hl
So, the subtraction sbc hl,bc sets all the flags right and add hl,bc restores de without screwing up the flags? That sounds pretty good. In fact, I can probably just throw one value away after the comparison, and just doJim e wrote:Yes all the flags do match CP despite that add hl,bc being there. It doesn't use the stack nor is it very large so you can macro it.Code: Select all
ex de,hl or a sbc hl,bc add hl,bc ex de,hl
Code: Select all
ex de,hl
or a
sbc hl,bc
ex de,hl