Reading — step 1 of 5
Read
Memory Instructions
I (index register) holds 12-bit memory addresses. Most memory ops use I.
ANNN: I = NNN.
FX1E: I += VX. (Some implementations set VF on overflow; not in original spec.)
FX55: store V0..VX into memory[I, I+1, ..., I+X].
FX65: load V0..VX from memory[I, I+1, ..., I+X].
Used for save/restore of registers, table lookups, copying data.
FX33: BCD (Binary Coded Decimal). Store the three decimal digits of VX into memory[I, I+1, I+2].
E.g., VX = 156 → memory[I]=1, memory[I+1]=5, memory[I+2]=6.
Used for displaying scores: combined with FX29 (load font for digit) and DXYN (draw sprite).
Example: display VX as 3 digits at position (V1, V2):
LD I, score_buffer ; AXXX
LD F, VX ; FX29 — actually loads digit; do BCD first
LD B, VX ; FX33 — BCD into score_buffer
LD V0, score_buffer ; ?
... draw three digits
This is convoluted; full sequence in real games is:
LD V0, 100
LD I, 0x300
LD B, V0 ; FX33
LD V2, [I] ; F265 -> V0=1, V1=0, V2=0
LD F, V0 ; F029 — I = font of digit 1
DRW VC, VD, 5 ; draw at (VC, VD) for 5 rows
ADD VC, 5 ; advance x
LD F, V1
DRW VC, VD, 5
...
FX29: I = address of font digit VX (in interpreter font area).
- Each digit is 5 bytes.
- I = 0x50 + (VX & 0xF) * 5 (font lives at 0x50).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…