Hot to test 16-bit numbers in assembler?

Страница 1/3
| 2 | 3

By DarkSchneider

Paladin (861)

Аватар пользователя DarkSchneider

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

By Prodatron

Paragon (1788)

Аватар пользователя Prodatron

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 (861)

Аватар пользователя DarkSchneider

17-01-2016, 14:10

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

By Grauw

Ascended (8382)

Аватар пользователя Grauw

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 (8382)

Аватар пользователя Grauw

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 (192)

Аватар пользователя kanima

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 (1788)

Аватар пользователя Prodatron

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 (8382)

Аватар пользователя Grauw

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 (3047)

Аватар пользователя sd_snatcher

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 (861)

Аватар пользователя DarkSchneider

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 (8382)

Аватар пользователя Grauw

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
Страница 1/3
| 2 | 3