How can read 16 bit address from db table?

Door smx

Resident (42)

afbeelding van smx

01-03-2018, 15:32

I everybody,
My question is to read 16 bit address from db table.
This is my listing, but t'is wrong.
My desire is also to assign "alias" in db list like showed in the same example (ALIAS should be another 16 bit address).

Thanks.

ld b,$2
ld IX,Sequence
CicloMain:
ld B,(IX+$1)
ld C,(IX+$0)
INC IX
INC IX
DJNZ CicloMain
ret
Sequence:
db $1122,$3344,$8866,ALIAS

Aangemeld of registreer om reacties te plaatsen

Van Grauw

Ascended (9334)

afbeelding van Grauw

01-03-2018, 15:40

You must use "dw" to define 16-bit values, not "db".

Van Grauw

Ascended (9334)

afbeelding van Grauw

01-03-2018, 17:28

Also, as your list gets longer the number of iterations gets quite costly. Better to multiply the index by two (left-shift or add with self), and then add it to the base address:

    ld bc,2
    ld ix,Sequence
    add ix,bc         ; + index * 2
    add ix,bc
    ld c,(ix + 0)
    ld b,(ix + 1)
    ret

Sequence:
    dw 1122H, 3344H, 5566H, 7788H

Van TomH

Champion (327)

afbeelding van TomH

01-03-2018, 16:03

Or if the table is less than 128 entries, I guess pretend you're a 6502:

    ld bc,2
    ld ix,Sequence
    add ix,bc         ; + index
    ld c,(ix + 0)
    ld b,(ix + SequenceHigh - SequenceLow) ; assuming SequenceHigh - SequenceLow is less than 128
    ret

SequenceLow:
    db 22H, 44H, 66H, 88H
SequenceHigh:
    db 11H, 33H, 55H, 77H

Somebody soon will point out that using a different combination of registers — HL in particular — is faster, I'm sure, but that'd have wider effect. I've already made one assumption that isn't necessarily valid and which Grauw declined to.

Van Grauw

Ascended (9334)

afbeelding van Grauw

01-03-2018, 16:40

There are too many ways to micro-optimise (and obfuscate) it further that I decided to just mention the algorithmic optimisation Smile.

Van smx

Resident (42)

afbeelding van smx

01-03-2018, 16:50

Thanks! Thanks! Thanks!

Van TomH

Champion (327)

afbeelding van TomH

01-03-2018, 16:52

Right — nobody's going to beat turning O(n) into O(1) with a seven-line direct drop-in replacement. You've out computer scienced us all!