How can read 16 bit address from db table?

By smx

Rookie (28)

smx's picture

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

Login or register to post comments

By Grauw

Enlighted (7840)

Grauw's picture

01-03-2018, 15:40

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

By Grauw

Enlighted (7840)

Grauw's picture

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

By TomH

Champion (310)

TomH's picture

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.

By Grauw

Enlighted (7840)

Grauw's picture

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.

By smx

Rookie (28)

smx's picture

01-03-2018, 16:50

Thanks! Thanks! Thanks!

By TomH

Champion (310)

TomH's picture

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!

My MSX profile