How to compare 16bits registers (Z80)???

بواسطة CrazyBoss

Master (209)

صورة CrazyBoss

25-10-2011, 07:45

Hi there

Made this working:
LA002: ;if hl=$B2C0
ld a,h
cp $b2
ret nz
ld a,l
cp $c0
ret nz
ld hl,nextlv
ret

but why does this not work???

LA002:
ld de,$B2C0
ld a,h
cp d
ret nz
ld a,l
cp e
ret nz
ld hl,nextlv
ret
//CB

Login أوregister لوضع تعليقاتك

بواسطة pitpan

Prophet (3140)

صورة pitpan

25-10-2011, 08:01

DCOMPR BIOS routine will do that: compare HL with DE updating all necessary flags. You can CALL it or even RST it. If you plan to disable the BIOS, just copy the code used within.

بواسطة Prodatron

Paragon (1789)

صورة Prodatron

09-11-2016, 15:10

I wonder if this is the fastest way to compare two 16bit signed integers?

;### CMPGTE -> test if A>=B
;### Input      HL=A, DE=B
;### Output     HL=1, CF=0 -> true
;###            HL=0, CF=1 -> false
cmpgte  ld a,h
        xor d
        jp m,cmpgte2
        sbc hl,de
        jr nc,cmpgte3
cmpgte1 scf             ;false
        ld hl,0
        ret
cmpgte2 bit 7,d
        jr z,cmpgte1
cmpgte3 or a            ;true
        ld hl,1
        ret

(the variations for <,> and <= would look similiar)

بواسطة hit9918

Prophet (2904)

صورة hit9918

10-11-2016, 13:50

it could be faster when removing ld hl,1 and ld hl,0 Smile
if it is about one special problem that needs speed, then one would need to look at the whole code.
but when it is about a general function, not trashing the registers can save quite some speed and code size on the callers side.

about 8bit signed cp is said: If A < N, then S and P/V are different. A >= N, then S and P/V are the same.
maybe it goes the same with 16bit SBC.

and a : sbc hl,de : add hl,de
this makes 16bit cp hl,de. the 16bit add only sets the carry flag. and does it always same as sbc did, so flags dont get changed at all. the nice thing is that HL keeps unchanged.
and then some branching for the signed version.