Sherlock: annotated disassembly

Routines

Prev: D9C3 Up: Map Next: DA32
D9C5: Tokenizer: read and classify the next word from input
Abbreviations; but EXAMINE LA finds the LABEL and EXAMINE L, TA LAMP and T LAMP give 'WHAT ?', while words with letters added (EXAMINATION, EXAMINEXYZ) are not recognised. So a typed word is matched as the start of a dictionary word (DA7D counts the letters that agree, keeping the best in $DC14; the typed length is DC13), the first word under its letter winning a tie.
How it works:
1. Skip spaces and remember where the word starts ($A029).
2. A carriage return gives an end-of-line token ($C0).
3. Characters that are not letters (the pound sign, digits, full stop, comma, quote) are read as their own tokens (DAC6).
4. Otherwise look up the dictionary words under the first letter (DA34), spell each one out (A94D) and compare it with the typed word (DA7D), keeping the best match.
5. A word that matches returns its class and word reference; no match gives a $D0 token.
Input
HL The typed line
Output
A, D The token class ($C0 end of line, $D0 not understood, $F0 number, and so on)
BC The token's value
HL Past the word
$A029 Where the word started
D9C5 PUSH DE Save DE on the stack
D9C6 LD A,(HL) Read the byte HL points at into A - Skip spaces
D9C7 INC HL Step HL on to the next byte
D9C8 CP $20 Compare A with $20
D9CA JR Z,$D9C6 If A equals $20, go back to D9C6
D9CC DEC HL Step HL back one byte
D9CD LD ($A029),HL Remember where this word starts ($A029)
D9D0 CP $0D Compare A with $0D - End of the line: a $C0 token
D9D2 JR Z,$DA06 If A equals $0D, jump on to DA06
D9D4 CALL $DAC6 Call the routine "Read a token that is not a word: money, a number or..." (DAC6) - A special character (DAC6)? Its own token
D9D7 JR Z,$DA2F If the zero flag is set, jump on to DA2F
D9D9 CALL $DA34 Call the routine "Look up this word's first letter in the dictionary..." (DA34) - Find the dictionary words under its first letter (DA34); none: a $D0 token (unknown word)
D9DC JR Z,$DA0A If the zero flag is set, jump on to DA0A
D9DE PUSH IX Save IX on the stack
D9E0 XOR A Clear A to zero (and the carry flag)
D9E1 LD ($DC14),A Best match so far: none
D9E4 LD ($D9C3),DE Store DE in "The tokenizer's current dictionary candidate" (D9C3) - Next candidate: spell it out (A94D)
D9E8 LD DE,($DA32) Read "The tokenizer's word pointer" (DA32) into DE
D9EC AND A Test A for zero (this leaves A as it was and clears carry)
D9ED PUSH HL Save HL on the stack
D9EE SBC HL,DE Subtract DE and the carry from HL
D9F0 POP HL Restore HL from the stack
D9F1 JR Z,$DA12 If the zero flag is set, skip ahead to DA12
D9F3 PUSH HL Save HL on the stack
D9F4 POP DE Restore DE from the stack
D9F5 CALL $A94D Call the routine "Spell dictionary record HL into the buffer at $5C42" (A94D)
D9F8 LD A,(DE) Read the byte DE points at into A - Skip records of class $D0
D9F9 AND $F0 Keep only bits 4, 5, 6, 7 of A (mask $F0)
D9FB CP $D0 Compare A with $D0
D9FD JR Z,$D9E8 If A equals $D0, go back to D9E8
D9FF CALL $DA7D Call the routine "Compare the typed word against one dictionary..." (DA7D) - Compare it with the typed word (DA7D); Z at the end of this letter's words
DA02 JR Z,$DA12 If the zero flag is set, skip ahead to DA12
DA04 JR $D9E4 Go back to D9E4
DA06 LD A,$C0 Load A with $C0 - $C0: end of the line
DA08 JR $DA0C Skip ahead to DA0C
DA0A LD A,$D0 Load A with $D0 - $D0: not a word the game knows
DA0C LD BC,$0000 Load BC with $0000
DA0F POP DE Restore DE from the stack
DA10 LD D,A Copy A into D
DA11 RET Return to the caller
DA12 POP HL Restore HL from the stack - Fewer than four letters agree: they must be all the letters typed
DA13 LD A,($DC14) Read $DC14 in "The number reader's working bytes" into A
DA16 CP $04 Compare A with $04
DA18 JR NC,$DA21 If A is $04 or more, skip ahead to DA21
DA1A LD B,A Copy A into B
DA1B LD A,($DC13) Read "The number reader's working bytes" (DC13) into A
DA1E CP B Compare A with B
DA1F JR NZ,$DA0A If A differs from B, go back to DA0A
DA21 PUSH HL Save HL on the stack - BC = the chosen word's offset from the dictionary base $5DBF, A = its class
DA22 LD HL,($D9C3) Read "The tokenizer's current dictionary candidate" (D9C3) into HL
DA25 LD A,(HL) Read the byte HL points at into A
DA26 AND $F0 Keep only bits 4, 5, 6, 7 of A (mask $F0)
DA28 LD BC,$5DBF Point BC at $5DBF
DA2B SBC HL,BC Subtract BC and the carry from HL
DA2D EX (SP),HL Swap HL with the word on top of the stack
DA2E POP BC Restore BC from the stack
DA2F POP DE Restore DE from the stack
DA30 LD D,A Copy A into D
DA31 RET Return to the caller
Prev: D9C3 Up: Map Next: DA32