Prev: 9D97 Up: Map Next: 9DD9
9DBD: Find a record in a keyed table
The game's general-purpose lookup. A table is a run of three-byte records -- a key, then a two-byte value -- ending at a key of $FF, and this walks it in order looking for the key in A. It is not sorted and does not need to be. Eleven routines use it: the picture table at PICTURE_TABLE is one, the action table at ACTION_TABLE another.
Input
A The key to find
IX The table
Output
IX The matching record, or the $FF that ended the table
F NZ if the key was found, Z if the table ran out
FIND_RECORD 9DBD EXX Work in the other register set, so the caller's BC, DE and HL survive
9DBE PUSH IX HL walks the table from IX
9DC0 POP HL
9DC1 LD B,A B = the key being looked for
9DC2 LD E,$03 DE = 3, the size of a record
9DC4 LD D,$00
FIND_RECORD_0 9DC6 LD A,(HL) Found it? The key is tested before the end marker, so a key of $FF can never be found
9DC7 CP B
9DC8 JR Z,FIND_RECORD_1
9DCA CP $FF Run off the end?
9DCC JR Z,FIND_RECORD_1
9DCE ADD HL,DE On to the next record
9DCF JP FIND_RECORD_0
FIND_RECORD_1 9DD2 PUSH HL IX = the record, or the $FF that ended the table
9DD3 POP IX
9DD5 CP $FF NZ if found; A is the key
9DD7 EXX
9DD8 RET
Done in the alternate register set, so the caller's BC, DE and HL survive it.
Prev: 9D97 Up: Map Next: 9DD9