Hello,
I'm searching for a (very) fast way to do a 16-bit signed comparison between 'de' and 'hl'.
More specifically, I have two signed numbers stored in 'de' and 'hl' and I want to be able to output according to that matrix :
hl<=0 hl>0 de<=0 0 hl de>0 de max(hl,de)
I've coded this, but it's messy ('hl' contains the output):
ld a,h ; 5 cp d ; 5 jp z,.same ; 11 | same sign jp c,.hl_max ; 11 | de < 0 and hl >= 0, therefore 'hl' is greater jp .hl_max-1 ; 11 .same ld a,l ; 5 cp e ; 5 jr nc,.hl_max+1 ; 8/13 | l>e therefore 'hl' is greater ex de,hl ; 5 .hl_max xor a ; 5 cp h ; 5 | is hl positive ? jp z,.next ; 11 ld hl,0 ; 11 | hl is negative jp .dothings ; 11 .next ld a,l ; 5 and a ; 5 | is hl = 0 ? jp nz,.dothings ; 11 ld hl,0 ; 11 | hl =0 .dothings ...
Is there a faster and better way ? (I'm sure there is)
I thought I found a way using 'sbc hl,de' (see the the snippet of code below). However, unfortunately, it does not work correctly when 'hl' and 'de' have opposite signs ...
; de = max(hl,de) (-29/+41) and a ; 5 sbc hl,de ; 16 jr c,$+4 ; 8/13 add hl,de ; 12 | hl>=de ex de,hl ; 5 ...
Login sesión o register para postear comentarios