Look-up tables (LUTs)
To make a look-up table (LUT), you must set the bounds. Look-up tables are often used in calculations which are not easy to find with arithmetic, such as the sine function. Lets model the sine function with a look-up table.
LD H, 0
LD L, A
LD DE, sine_table
ADD HL, DE
LD A, (HL)
INC HL
LD H, (HL)
LD L, A
RET
sine_table:
; The lookup table
.DW 0000,ドル 0004,ドル 0009,ドル 000ドルD, 0012,ドル 0016,ドル 001ドルB, 001ドルF, 0024ドル
.DW 0028,ドル 002ドルC, 0031,ドル 0035,ドル 003ドルA, 003ドルE, 0042,ドル 0047,ドル 004ドルB
.DW 004ドルF, 0053,ドル 0058,ドル 005ドルC, 0060,ドル 0064,ドル 0068,ドル 006ドルC, 0070ドル
.DW 0074,ドル 0078,ドル 007ドルC, 0080,ドル 0084,ドル 0088,ドル 008ドルB, 008ドルF, 0093ドル
.DW 0096,ドル 009ドルA, 009ドルE, 00ドルA1, 00ドルA5, 00ドルA8, 00ドルAB, 00ドルAF, 00ドルB2
.DW 00ドルB5, 00ドルB8, 00ドルBB, 00ドルBE, 00ドルC1, 00ドルC4, 00ドルC7, 00ドルCA, 00ドルCC
.DW 00ドルCF, 00ドルD2, 00ドルD4, 00ドルD7, 00ドルD9, 00ドルDB, 00ドルDE, 00ドルE0, 00ドルE2
.DW 00ドルE4, 00ドルE6, 00ドルE8, 00ドルEA, 00ドルEC, 00ドルED, 00ドルEF, 00ドルF1, 00ドルF2
.DW 00ドルF3, 00ドルF5, 00ドルF6, 00ドルF7, 00ドルF8, 00ドルF9, 00ドルFA, 00ドルFB, 00ドルFC
.DW 00ドルFD, 00ドルFE, 00ドルFE, 00ドルFF, 00ドルFF, 00ドルFF, 0100,ドル 0100,ドル 0100ドル
The main disadvantage is the size, but we can't do much about it.
Jump Tables
These are just like LUTs, except that rather than values being returned, they return an area in memory.
A vector table holds only the addresses.
Here is an example:
VectTbl:
.DW ClearScreen
.DW PutSprite
.DW DrawLine
.DW EndPrgm
.DW BlackScreen
LD H, 0
LD L, A
ADD HL, HL
LD DE, VectTbl
ADD HL, DE;Just like a look-up table
LD A, (HL)
INC HL
LD H, (HL)
LD L, A
JP (HL)
A Jump table is essentially a vector table, but rather than holding the address, it holds the entire jump instruction.
JumpTbl:
JP ClearScreen
JP PutSprite
JP DrawLine
JP EndPrgm
JP BlackScreen
To call or jump to a routine in the jump table, you use an address of
JumpTbl + 3 * n
where n is the number of the routine you want. Supposing you wanted to run DrawLine, then you would use
CALL JumpTbl + 3 * 2
If you notice, the default B_CALLs have offsets of 3. For example, B_CALL(_ClrLCDFull) is EF4045 in machine code, and B_CALL(_ClrLCD) is EF4345 in machine code, and B_CALL(_ClrScreenFull) is EF4645. EF is BCALL(**). But why is the first byte of the address increasing? This is because the calculators use Little-Endian; the least-significant-byte and most-significant-byte are essentially swapped.