Hot to test 16-bit numbers in assembler?

Page 1/3
| 2 | 3

By DarkSchneider

Paladin (1020)

DarkSchneider's picture

17-01-2016, 13:32

I want to test 16-bit numbers. But how? For <, ==, >, <= and >=.

Edit: SBC works well, the problem were I was loading a DB (byte) into DE, so the MSB was wrong.

Login or register to post comments

By Prodatron

Paragon (1843)

Prodatron's picture

17-01-2016, 14:05

Are you sure? This example works quite well:

org #4000
ld hl,4000
ld de,3000
or a
sbc hl,de
jp z,equal
jp p,positive
jp m,negative
ret

equal ld a,0
ret

positive ld a,1
ret

negative ld a,-1
ret

By DarkSchneider

Paladin (1020)

DarkSchneider's picture

17-01-2016, 14:10

Why the "or a", SBC does not "clean" the flags by its own?

By Grauw

Ascended (10768)

Grauw's picture

17-01-2016, 14:18

Better to test carry in stead of p/m, otherwise if de is large you will not get the result expected:

ld hl,4000
ld de,3000
or a                  ; this resets carry so sbc doesn’t deduct one extra
sbc hl,de
jr z,hl_equals_de
jr c,hl_lessthan_de
jr nc,hl_greaterthan_de

To test >=, simply remove the jr z and use the jr nc (as nc includes zero).
To test <=, you can replace the or a by scf (deducts 1 more) and then jr c.

By Grauw

Ascended (10768)

Grauw's picture

17-01-2016, 14:21

DarkSchneider wrote:

Why the "or a", SBC does not "clean" the flags by its own?

It is used to reset the carry flag. Also and a is often used for the same purpose (I usually do). Please refer to the Z80 user manual on SBC for the reason to reset the carry flag :).

By kanima

Master (194)

kanima's picture

17-01-2016, 14:26

DarkSchneider wrote:

Why the "or a", SBC does not "clean" the flags by its own?

SBC means "subtract with carry". So, SBC HL,DE -> HL = HL - DE - CY. Unfortunately there's no SUB HL,DE instruction, so if you just want to get HL=HL-DE then you have to use SBC HL,DE with a non-set (i.e. 0) carry. Using the OR A instruction before clears the carry flag.

By Prodatron

Paragon (1843)

Prodatron's picture

17-01-2016, 14:25

Btw what is the fastest way to do such a test for signed 16bit values?
E.g. HL=-100, DE=50

By Grauw

Ascended (10768)

Grauw's picture

17-01-2016, 16:19

One way is to add 8000H to both values before doing the test.

(Note that instead of actually adding 8000H, you can add 80H to h and d, or just toggle bit 7 of h and d.)

By sd_snatcher

Prophet (3659)

sd_snatcher's picture

17-01-2016, 16:15

Quote:

I want to test 16-bit numbers. But how? For <, ==, >, <= and >=.

On MSX, the BIOS can do a non-destructive test for you. Just call the routine DCOMPR, or use a RST 20h for short.

By DarkSchneider

Paladin (1020)

DarkSchneider's picture

17-01-2016, 16:24

sd_snatcher wrote:
Quote:

I want to test 16-bit numbers. But how? For <, ==, >, <= and >=.

On MSX, the BIOS can do a non-destructive test for you. Just call the routine DCOMPR, or use a RST 20h for short.

Nice! I like to use BIOS when possible.

By Grauw

Ascended (10768)

Grauw's picture

17-01-2016, 16:42

sd_snatcher wrote:
Quote:

I want to test 16-bit numbers. But how? For <, ==, >, <= and >=.

On MSX, the BIOS can do a non-destructive test for you. Just call the routine DCOMPR, or use a RST 20h for short.

For reference, the BIOS does:

ld a,h
sub d
ret nz
ld a,l
sub e
ret
Page 1/3
| 2 | 3