TRS-80 DOS - NEWDOS/80 v2.0 for the Model I - SYS12/SYS Disassembled

Page Customization

Description:

SYS12/SYS - BASIC REF Command Executor

SYS12 implements the BASIC REF direct command, which generates a cross-reference listing of variable usage within a BASIC program. The REF command scans the BASIC program text and produces a report showing where each variable, line number reference, or string literal appears in the program.

Command Syntax:

REF - List all variables
REF @ or REF ` - List all line number references (GOTO, GOSUB targets)
REF "string" or REF USR - Search for specific string literal
REF varname - List references to specific variable
REF varname$ - List references to specific string variable

Key Features:

1. Variable Reference Tracking: Scans BASIC program lines for variable names (A-Z, A0-Z9, A$-Z$, etc.) and records which lines contain each variable.

2. Line Number Cross-Reference: When invoked with @ or ` parameter, tracks all line number references in GOTO, GOSUB, RESTORE, ON...GOTO, and similar statements.

3. String Literal Search: Can search for occurrences of specific quoted strings or USR function references within the program.

4. Output Formatting: Displays results in columns with variable names followed by the line numbers where they appear. Handles both numeric (0-9 prefix) and alphabetic (A-Z prefix) variable name formats.

5. Reference Table: Maintains a table at 4200H-423FH to track line references, with each entry containing the line number where a reference was found.

Program Flow:

Entry at 4D00H initializes by calling the BASIC line advance routine. The command parses its argument to determine the search mode (all variables, line references, string search, or specific variable). The main loop at 4F6BH scans through each BASIC program line, examining tokens and building the reference list. Results are displayed via 4E80H which formats and prints the cross-reference output. The program terminates by returning control to BASIC through the standard exit at 002BH.

Variables:

AddressBytesPurpose
4E0CH1Search mode flag: 0=all variables, 1=specific variable, 2=line refs, 3=string search
4E76H-4E77H2Target line number for line reference search (-1 = search all)
4E7AH-4E7BH2Current line pointer during scan
4EEFH-4EF0H2Reference table write pointer (starts at 4200H)
4EF2H-4EF3H2Reference table read pointer for output
4ED9H1Temporary storage for reference counter during output
4F6DH1Self-modifying: End-of-program flag (CAH=continue, C3H=done)
4F7CH1Self-modifying: Search type flag (C3H=string/USR, 21H=variable)
4FBEH1Self-modifying: Column width for output (30H or 18H)
4FFCH1Self-modifying: JP opcode for conditional branch (DAH or C2H)
5021H1Self-modifying: Variable name comparison offset
5085H-5086H2DEF statement pointer (tracks DEF FN references)
50A9H1Self-modifying: JP opcode for comparison branch
51B1H-51B5H5Variable type suffix characters: $ % ! # (space)
51B6H-51BEH9Message: "!#$%EXT END" + CR
51BFH-51C0H2Search variable name (first 2 chars or line number low)
51C1H-51C4H4Search variable name extension (remaining chars)
51C5H1Search variable name length
51C6H-51CAH5Current variable being processed during scan
51CBH1Current variable first character
51CCH-51D0H5Variable name comparison buffer
51D1H-51DAH10Reference count array (10 entries for output formatting)
51DBH-51EAH16String search buffer (stores search string)
4200H-423FH64Reference table (line number storage, 32 entries × 2 bytes)

Key Routines:

AddressPurpose
4D00HEntry point, parse command argument
4D49HParse variable name argument
4DB6HKeyboard BREAK check and main loop setup
4DFCHEnd of program handler
4E28HNext variable scan setup
4E80HOutput reference results
4F07HInitialize search parameters
4F46HSkip quoted string
4F70HStore line pointer and main token scanner
4F9BHHandle alphabetic character or token
4FBAHHandle numeric character
5073HHandle Z variable special case
5091HParse alphabetic variable name
50D9HRecord reference (increment count)
50EEHTrack new variable for all-vars mode
5101HCheck and record line reference
512FHString literal match check
514BHGet next non-space character
515AHCheck if digit
5161HCheck if alphanumeric
5168HInitialize variable tracking
5171HInitialize reference table

Self-Modifying Code Locations:

AddressSet ByPurpose
4E0CH4DD1HSearch mode flag (in LD A,nn)
4E76H4D1DH, 4E17H, 50E7HTarget line number (in LD HL,nnnn)
4E7AH4F70HCurrent line pointer (in LD DE,nnnn)
4ED9H4E9CHReference counter (in LD DE,nnnn)
4EEFH5182H, 512AHReference table write pointer
4EF2H5185H, 4E6DHReference table read pointer
4F6DH4DF0H, 50DDHEnd-of-program flag (CAH/C3H)
4F7CH4D44H, 4D4DHSearch type flag (C3H/21H)
4FBEH4DEBHColumn width (30H/18H)
4FFCH4DDAHJP opcode for conditional (C2H/DAH)
5021H517CHVariable comparison offset
5085H4FB4H, 4DF6HDEF statement pointer
50A9H4DDDHJP opcode for comparison (C2H/DAH)

Important Memory Locations:

Reference Table:

AddressPurpose
4200H-423FHReference table (32 entries × 2 bytes = 64 bytes)
4EEFH-4EF0HWrite pointer
4EF2H-4EF3HRead pointer

Variable Tracking:

AddressPurpose
51BFH-51C0HSearch variable (first 2 chars)
51C1H-51C4HSearch variable extension
51C5HSearch variable length
51C6H-51CAHCurrent variable being scanned
51CBHCurrent variable first char
51CCH-51D0HVariable comparison buffer
51D1H-51DAHReference count array (10 entries)
51DBH-51EAHString search buffer (16 chars)

Type Suffix Table:

AddressPurpose
51B1H20H (space = no suffix)
51B2H24H ($)
51B3H25H (%)
51B4H21H (!)
51B5H 23H (#)

Messages:

AddressPurpose
51B6H-51BEH"!#$%EXT END" + CR

Disassembly:

4D00H - REF Command Entry Point

Main entry point for the REF command. Parses the command argument to determine which search mode to use: all variables, line number references, string search, or specific variable lookup.

Entry point - HL points to BASIC command line after "REF" token.

4D00
Call BASIC routine to advance past spaces and get next non-space character into A. HL points to that character.
4D03
LD A,(HL) 7E
Load the first character of the REF argument into Register A.
4D04
LD (5D6CH),A 32 6C 5D
Store the character at 5D6CH (BASIC work area) for later reference.
4D07
OR A B7
Test if character is 00H (end of line). Sets Z FLAG if no argument given.
4D08
If no argument (Z FLAG set), JUMP to 4D21H to list all variables in program.
4D0A
CP 40H FE 40
Compare character to @ (40H). This indicates "list all line references" mode.
4D0C
If @ character (Z FLAG set), JUMP to 4D12H to handle line reference mode.
4D0E
CP 60H FE 60
Compare character to ` (60H backtick). Alternate syntax for line reference mode.
4D10
If not ` (NZ FLAG set), JUMP to 4D24H to check for other argument types.

Line number reference mode: REF @ or REF ` - optionally with a specific line number.

4D12
Call ROM routine to parse a decimal number from (HL). Returns number in DE, A=0 if valid number found.
4D15
OR A B7
Test if a valid line number was parsed (A=0 means success).
4D16
If parsing failed (NZ FLAG set), JUMP to 4D7FH for syntax error.
4D18
LD A,D 7A
Load high byte of parsed line number into A.
4D19
OR E B3
OR with low byte. Tests if line number is 0000H.
4D1A
If line number is zero (Z FLAG set), skip the decrement.
4D1C
DEC DE 1B
Decrement line number by 1. This adjusts for the search algorithm which uses "greater than" comparison.
4D1D
LD (4E76H),DE ED 53 76 4E
Store the target line number (or adjusted value) at 4E76H for line reference search.
4D21
JUMP to 4F07H to begin the program scan with current search parameters.

Check for string search mode: REF "string" or REF USR

4D24
CP 22H FE 22
Compare character to " (double quote). Indicates string literal search.
4D26
If quote character (Z FLAG set), JUMP to 4D2CH to copy search string.
4D28
CP D5H FE D5
Compare to D5H (USR token). REF USR searches for USR function calls.
4D2A
If not USR token (NZ FLAG set), JUMP to 4D49H to parse as variable name.

String search mode: Copy the search string (up to 16 characters) into buffer at 51DBH.

4D2C
EX DE,HL EB
Exchange DE and HL. DE now points to the source (command line).
4D2D
LD HL,51DBH 21 DB 51
Load HL with 51DBH, the string search buffer destination.
4D30
LD B,10H 06 10
Load B with 16 (10H), maximum string length to copy.
4D32
INC DE 13
[LOOP START] Advance source pointer past quote or to next character.
4D33
LD A,(DE) 1A
Load next character from source string.
4D34
LD (HL),A 77
Store character in search buffer.
4D35
INC HL 23
Advance destination pointer.
4D36
Decrement B and loop until 16 characters copied or B=0. [LOOP END]
4D38
LD (HL),B 70
Store 00H (B is now 0) as string terminator.
4D39
LD HL,3000H 21 00 30
Load HL with 3000H. This is used as a "match all" marker for variable comparison.
4D3C
LD (51BFH),HL 22 BF 51
Store 3000H at 51BFH (search variable low bytes) - signals string search mode.
4D3F
LD (51C5H),HL 22 C5 51
Store 3000H at 51C5H (variable length) - same marker value.
4D42
LD A,C3H 3E C3
Load A with C3H (JP opcode). This will modify code for string search path.
4D44
LD (4F7CH),A 32 7C 4F
Store C3H at 4F7CH. Self-modifying: Changes instruction to enable string search branch.
4D47
JUMP to 4D9AH to continue initialization with string search mode.

4D49H - Parse Variable Name Argument

Parses a variable name argument for REF command. Handles names like A, AB, A$, A1$, etc. Stores the parsed name for comparison during program scan.

Parse variable name: up to 6 characters (2 base + optional $ or % + digits).

4D49
LD B,06H 06 06
Load B with 6, maximum characters for a variable name.
4D4B
LD A,21H 3E 21
Load A with 21H (opcode for LD HL,nnnn). Used for self-modifying code flag.
4D4D
LD (4F7CH),A 32 7C 4F
Store 21H at 4F7CH. Self-modifying: Sets variable search mode (not string search).
4D50
EX DE,HL EB
Exchange DE and HL. DE now points to command line argument.
4D51
LD HL,51C6H 21 C6 51
Load HL with 51C6H, destination buffer for parsed variable name.
4D54
LD C,00H 0E 00
Clear C. Bit 0 will indicate if string variable (has $ suffix).
4D56
LD A,(DE) 1A
Load first character of variable name.
4D57
CP 24H FE 24
Compare to $ (24H). Check if this is a "$" prefix (string type indicator).
4D59
If $ (Z FLAG set), JUMP to 4D5FH to handle string variable prefix.
4D5B
CP CFH FE CF
Compare to CFH (TO token). This handles special case of parsing.
4D5D
If not TO token (NZ FLAG set), JUMP to 4D68H to parse normal variable.
4D5F
INC DE 13
Advance past the $ or special character.
4D60
LD A,(DE) 1A
Load next character after prefix.
4D61
OR A B7
Test if end of line (00H).
4D62
If end of line (Z FLAG set), JUMP to 4DAAH to handle "REF $" (all string vars).
4D64
INC C 0C
Set C=1 to indicate string variable type.
4D65
LD HL,51C0H 21 C0 51
Load HL with 51C0H, alternate buffer for string variable parsing.

Main variable name parsing loop: validate and copy characters.

4D68
PUSH HL E5
Save destination buffer pointer.
4D69
LD A,(DE) 1A
Load character from command line.
4D6A
SUB 30H D6 30
Subtract 30H ('0'). Tests if character is digit or letter.
4D6C
CP 0AH FE 0A
Compare to 10. If less than 10, character was digit '0'-'9'.
4D6E
If valid digit (C FLAG set), JUMP to 4D76H to accept character.
4D70
SUB 11H D6 11
Subtract 11H more (total 41H = 'A'). Tests for letters A-Z.
4D72
CP 1AH FE 1A
Compare to 26. If less than 26, character was letter 'A'-'Z'.
4D74
If not valid alphanumeric (NC FLAG set), JUMP to 4D7FH for syntax error.
4D76
LD A,(DE) 1A
[LOOP] Reload original character (before subtraction).
4D77
OR A B7
Test if character is 00H (end of string).
4D78
INC DE 13
Advance source pointer.
4D79
If end of string (Z FLAG set), JUMP to 4D82H to finish parsing.
4D7B
LD (HL),A 77
Store character in destination buffer.
4D7C
INC HL 23
Advance destination pointer.
4D7D
Decrement B and loop until all characters parsed or max reached. [LOOP END]

Syntax error handler - invalid variable name character encountered.

4D7F
JUMP to ROM 1997H - ?SN ERROR (Syntax Error) handler.

Variable name parsing complete - calculate length and validate.

4D82
LD A,05H 3E 05
Load A with 5 (max chars - 1, since we started with B=6).
4D84
SUB B 90
Subtract remaining count. A now holds number of characters parsed (1-6).
4D85
EX DE,HL EB
Exchange DE and HL. DE now points past parsed name.
4D86
POP HL E1
Restore original buffer pointer (51C0H or 51C6H).
4D87
BIT 6,(HL) CB 76
Test bit 6 of first byte in buffer. Checks if lowercase letter (60H-7AH range).
4D89
If bit 6 clear (Z FLAG set), JUMP to 4D94H - uppercase variable name.
4D8B
CP 02H FE 02
Compare parsed length to 2.
4D8D
If length >= 2 (NC FLAG set), JUMP to 4D7FH - syntax error (lowercase vars are 1 char).
4D8F
LD A,20H 3E 20
Load A with 20H (space character).
4D91
LD (DE),A 12
Store space as second character (padding single-char variable).
4D92
LD A,01H 3E 01
Load A with 1 (actual length).
4D94
DEC HL 2B
Decrement HL to point to length byte position (one before name).
4D95
LD (HL),A 77
Store variable name length.
4D96
BIT 0,C CB 41
Test bit 0 of C. This indicates if string variable (C=1) or numeric (C=0).
4D98
If string variable (NZ FLAG set), JUMP to 4DA2H for different handling.
4D9A
Call 5171H to initialize search parameters and reference table.
4D9D
LD A,02H 3E 02
Load A with 2 (search mode: specific variable).
4D9F
JUMP to 4E2FH to continue with variable-specific search initialization.

String variable type: adjust the reference counter for $ suffix.

4DA2
LD B,00H 06 00
Clear B for 16-bit offset calculation.
4DA4
LD C,(HL) 4E
Load variable length into C.
4DA5
INC C 0C
Increment C to account for the length byte itself.
4DA6
ADD HL,BC 09
Add BC to HL, pointing past the variable name to the type suffix position.
4DA7
DEC (HL) 35
Decrement the type suffix counter. This marks string variable status.
4DA8
JUMP to 4DB0H to continue initialization.

REF $ (search all string variables): Set marker to match any string var.

4DAA
LD HL,0000H 21 00 00
Load HL with 0000H (match any variable).
4DAD
LD (51BFH),HL 22 BF 51
Store 0000H at 51BFH - signals "match all" mode for string variables.
4DB0
LD BC,FFFFH 01 FF FF
Load BC with FFFFH (match any line number / all lines).
4DB3
Call 5168H to store BC at 51C6H and initialize reference table pointers.

4DB6H - Keyboard BREAK Check and Main Loop Setup

Checks for BREAK key to abort the REF command, then initializes the main scanning loop that processes each BASIC program line.

Check keyboard for BREAK key press to allow user to abort REF command.

4DB6
LD HL,3840H 21 40 38
Load HL with 3840H, the keyboard memory-mapped I/O address for BREAK key row.
4DB9
BIT 3,(HL) CB 5E
Test bit 3 of keyboard byte. This bit indicates BREAK key status.
4DBB
If BREAK pressed (NZ FLAG set), JUMP to ROM 002BH to return to BASIC READY prompt.
4DBE
BIT 2,(HL) CB 56
Test bit 2 of keyboard byte. Secondary break check.
4DC0
If not pressed (Z FLAG set), JUMP to 4DCBH to continue with scan setup.

Wait loop for key release if secondary key detected.

4DC2
BIT 3,(HL) CB 5E
[WAIT LOOP] Test bit 3 again while waiting.
4DC4
If BREAK pressed (NZ FLAG set), JUMP to exit to BASIC.
4DC7
BIT 0,(HL) CB 46
Test bit 0 of keyboard byte.
4DC9
If still pressed (Z FLAG set), JUMP back to keep waiting. [LOOP END]

Initialize for main program scan loop.

4DCB
XOR A AF
Clear A to 00H.
4DCC
LD DE,(40A4H) ED 5B A4 40
Load DE with TXTTAB (40A4H) - start address of BASIC program text.
4DD0
OR A B7
Test A (search mode flag). Z means first pass, NZ means subsequent pass.
4DD1
LD (4E0CH),A 32 0C 4E
Store search mode flag at 4E0CH. 0=all vars, 1=specific, 2=line refs, 3=strings.
4DD4
LD A,DAH 3E DA
Load A with DAH (JP C opcode).
4DD6
If first pass (Z FLAG set), JUMP to 4DDAH to use JP C.
4DD8
LD A,C2H 3E C2
Otherwise load A with C2H (JP NZ opcode) for subsequent passes.
4DDA
LD (4FFCH),A 32 FC 4F
Self-modifying: Store opcode at 4FFCH. Changes conditional jump behavior.
4DDD
LD (50A9H),A 32 A9 50
Self-modifying: Store same opcode at 50A9H for comparison logic.
4DE0
LD A,(51C0H) 3A C0 51
Load first character of search variable from 51C0H.
4DE3
CP 3AH FE 3A
Compare to : (3AH). Determines if numeric or alpha variable prefix.
4DE5
LD A,30H 3E 30
Load A with 30H (48 decimal) for column width.
4DE7
If numeric prefix (C FLAG set, char < ':'), use 48-column width.
4DE9
LD A,18H 3E 18
Otherwise load A with 18H (24 decimal) for narrower column width.
4DEB
LD (4FBEH),A 32 BE 4F
Self-modifying: Store column width at 4FBEH for output formatting.
4DEE
LD A,CAH 3E CA
Load A with CAH (JP Z opcode).
4DF0
LD (4F6DH),A 32 6D 4F
Self-modifying: Store CAH at 4F6DH. This creates "JP Z,4DFCH" instruction.
4DF3
LD HL,0000H 21 00 00
Load HL with 0000H.
4DF6
LD (5085H),HL 22 85 50
Store 0000H at 5085H - clear DEF FN tracking pointer.
4DF9
JUMP to 4F6BH to begin the main program scanning loop.

4DFCH - End of Program Handler

Called when the end of the BASIC program is reached during scanning. Handles output of collected references and determines next action.

End of program reached - check if more processing needed.

4DFC
LD A,(4F6DH) 3A 6D 4F
Load A with the self-modified opcode at 4F6DH.
4DFF
CP C3H FE C3
Compare to C3H (JP opcode). If C3H, we've already done final output.
4E01
If equals C3H (Z FLAG set), JUMP to 4E80H to output remaining references.
4E04
INC DE 13
Advance DE past current position.
4E05
LD A,(DE) 1A
Load next byte.
4E06
OR A B7
Test if it's 00H (end marker).
4E07
DEC DE 1B
Restore DE to previous position.
4E08
If not end (NZ FLAG set), JUMP to 4F70H to continue scanning.

Check search mode to determine what output action to take.

4E0B
LD A,00H 3E 00
Load A with 00H. Self-modifying target: This value changes during execution.
4E0D
OR A B7
Test the mode value.
4E0E
If mode 0 (Z FLAG set), JUMP to 4E28H for "all variables" processing.
4E10
PUSH AF F5
Save mode flag.
4E11
Call BASIC routine to print CR/LF (newline).
4E14
LD HL,FFFFH 21 FF FF
Load HL with FFFFH (search all lines marker).
4E17
LD (4E76H),HL 22 76 4E
Store FFFFH at 4E76H - reset to search all line numbers.
4E1A
POP AF F1
Restore mode flag.
4E1B
DEC A 3D
Decrement mode: 1→0, 2→1, 3→2.
4E1C
If now 0 (was mode 1), JUMP to 4DB0H for another pass.
4E1E
DEC A 3D
Decrement again: 1→0, 2→1.
4E1F
RET Z C8
If now 0 (was mode 2), RETURN - we're done.

Mode 3 (string search): Display final message and return.

4E20
LD B,09H 06 09
Load B with 9, message length.
4E22
LD HL,51B6H 21 B6 51
Load HL with 51B6H, pointer to "!#$%EXT END" message.
4E25
JUMP to BASIC routine to print B characters from (HL) and return.

4E28H - Next Variable Scan Setup

Sets up parameters for scanning the next variable in "all variables" mode. Advances through variable name space systematically.

Check if there are more variables to scan.

4E28
LD A,(51C6H) 3A C6 51
Load current variable first character from 51C6H.
4E2B
INC A 3C
Increment. If was FFH (end marker), A becomes 00H setting Z flag.
4E2C
RET Z C8
If we've processed all variables (Z FLAG set), RETURN to BASIC.
4E2D
LD A,01H 3E 01
Load A with 01H (continue flag).
4E2F
PUSH AF F5
Save mode/continue flag.
4E30
LD HL,51C5H 21 C5 51
Load HL with 51C5H, pointer to variable length byte.
4E33
LD C,(HL) 4E
Load variable name length into C.
4E34
INC C 0C
Increment C to include length byte itself.
4E35
LD A,(4F7CH) 3A 7C 4F
Load search type flag from 4F7CH.
4E38
CP C3H FE C3
Compare to C3H (JP opcode = string search mode).
4E3A
LD A,06H 3E 06
Load A with 6 (spaces to print for string search).
4E3C
If string search (Z FLAG set), JUMP to 4E53H to print spacing.

Variable search mode: Print variable name with proper spacing.

4E3E
PUSH HL E5
Save pointer to length byte.
4E3F
INC HL 23
Advance to variable name first character.
4E40
LD A,05H 3E 05
Load A with 5 (max variable name display width).
4E42
SUB C 91
Subtract actual length. A = spaces needed for padding.
4E43
If padding needed (NZ FLAG set), CALL BASIC routine to print A spaces.
4E46
LD B,C 41
Copy length to B for print loop.
4E47
Call BASIC routine to print B characters from (HL) - the variable name.
4E4A
POP HL E1
Restore pointer to length byte.
4E4B
LD DE,51BFH 11 BF 51
Load DE with 51BFH (search variable storage destination).
4E4E
INC C 0C
Increment C to include any type suffix.
4E4F
LDIR ED B0
Copy C bytes from (HL) to (DE) - copy variable info to search buffer.
4E51
LD A,01H 3E 01
Load A with 1 (one space after variable name).
4E53
Call BASIC routine to print A spaces.

Clear reference count array for new variable.

4E56
LD HL,51D1H 21 D1 51
Load HL with 51D1H, reference count array.
4E59
LD B,0AH 06 0A
Load B with 10 (10 entries to clear).
4E5B
LD (HL),00H 36 00
[LOOP] Store 00H at current position.
4E5D
INC HL 23
Advance to next position.
4E5E
Decrement B and loop until all 10 entries cleared. [LOOP END]
4E60
POP AF F1
Restore mode flag.
4E61
CP 01H FE 01
Compare mode to 1.
4E63
If not mode 1 (NZ FLAG set), JUMP to 4DCCH to reinitialize scan loop.

Mode 1: Get next line from reference table.

4E66
LD HL,4200H 21 00 42
Load HL with 4200H, start of reference table.
4E69
LD E,(HL) 5E
Load low byte of line number into E.
4E6A
INC HL 23
Advance to high byte.
4E6B
LD D,(HL) 56
Load high byte of line number into D.
4E6C
INC HL 23
Advance to next table entry.
4E6D
LD (4EF2H),HL 22 F2 4E
Store updated read pointer at 4EF2H.
4E70
JUMP to 4DD0H to continue with next line to process.

4E73H - Stack Cleanup and BASIC Return

Cleans up the stack and returns control to BASIC by jumping to the standard BASIC return point at ROM 2B33H.

Clean up stack and return to BASIC interpreter.

4E73
POP AF F1
Pop and discard saved AF from stack.
4E74
POP AF F1
Pop and discard another saved value.
4E75
LD HL,0000H 21 00 00
Load HL with 0000H (will push as return values).
4E78
PUSH HL E5
Push 0000H onto stack.
4E79
LD HL,0000H 21 00 00
Load HL with 0000H again.
4E7C
PUSH HL E5
Push another 0000H onto stack.
4E7D
JUMP to ROM 2B33H - BASIC command completion routine that returns to READY.

4E80H - Output Reference Results

Outputs the collected variable references. Displays each variable's reference count and the line numbers where it appears, formatted in columns.

Check if we should exit or continue output processing.

4E80
LD A,(4E0CH) 3A 0C 4E
Load search mode flag from 4E0CH.
4E83
CP 03H FE 03
Compare to 3 (string search mode).
4E85
If string search (Z FLAG set), JUMP to 4E73H to exit (no count output for strings).
4E87
PUSH AF F5
Save mode flag.
4E88
PUSH DE D5
Save current position pointer.
4E89
LD A,(51C6H) 3A C6 51
Load first character of current variable from 51C6H.
4E8C
LD BC,0100H 01 00 01
Load BC with 0100H. B=1 (loop count), C=0 (column counter).
4E8F
CP 41H FE 41
Compare variable char to 'A' (41H).
4E91
If numeric variable (C FLAG set, char < 'A'), skip with B=1.
4E93
LD B,0AH 06 0A
Otherwise load B with 10 for alpha variables (10 type variants: A, A$, A%, etc.).
4E95
LD HL,51D1H 21 D1 51
Load HL with 51D1H, reference count array.

Loop through reference count entries and display non-zero counts.

4E98
LD A,(HL) 7E
[OUTPUT LOOP START] Load reference count from array.
4E99
OR A B7
Test if count is zero.
4E9A
If zero (Z FLAG set), JUMP to 4EE3H to skip this entry.
4E9C
LD (4ED9H),A 32 D9 4E
Store count at 4ED9H for later reference. Self-modifying target.
4E9F
LD (HL),00H 36 00
Clear the count entry (mark as processed).
4EA1
PUSH HL E5
Save array pointer.
4EA2
PUSH BC C5
Save loop counter and column counter.
4EA3
Call 519EH to position cursor and print spacing.
4EA6
LD DE,(4E76H) ED 5B 76 4E
Load target line number from 4E76H.
4EAA
Call BASIC routine to print DE as decimal number.
4EAD
POP BC C1
Restore loop counter and column counter.
4EAE
LD A,C 79
Load column counter into A.
4EAF
OR A B7
Test if column 0.
4EB0
PUSH BC C5
Save BC again.
4EB1
If not column 0 (NZ FLAG set), JUMP to 4EBBH to print separator.

Column 0: Check if we need special formatting.

4EB3
LD A,(4ED9H) 3A D9 4E
Load saved reference count.
4EB6
CP 02H FE 02
Compare count to 2.
4EB8
If count < 2 (C FLAG set), JUMP to 4EE1H (single reference, no column needed).
4EBA
LD A,C 79
Reload column counter (should be 0).

Print separator character based on column position.

4EBB
CP 05H FE 05
Compare column to 5.
4EBD
LD A,2FH 3E 2F
Load A with 2FH (/ slash character).
4EBF
If column < 5 (C FLAG set), use slash separator.
4EC1
LD A,C 79
Load column into A.
4EC2
SUB 05H D6 05
Subtract 5 to get position in second group.
4EC4
LD C,A 4F
Update column counter.
4EC5
LD A,28H 3E 28
Load A with 28H (( open parenthesis).
4EC7
Call BASIC routine to print character in A.
4ECA
LD A,C 79
Load column counter.
4ECB
OR A B7
Test if zero.
4ECC
If zero (Z FLAG set), JUMP to 4ED8H to continue.

Print type suffix character for this variant.

4ECE
LD B,00H 06 00
Clear B for 16-bit offset.
4ED0
LD HL,51B1H 21 B1 51
Load HL with 51B1H, type suffix table ("$%!# ").
4ED3
ADD HL,BC 09
Add column offset to get correct suffix character.
4ED4
LD A,(HL) 7E
Load type suffix character.
4ED5
Call BASIC routine to print the suffix character.
4ED8
LD DE,0000H 11 00 00
Load DE with 0000H. Self-modifying target at 4ED9H holds count.
4EDB
LD A,E 7B
Load low byte (the reference count) into A.
4EDC
CP 02H FE 02
Compare to 2.
4EDE
If count >= 2 (NC FLAG set), CALL to print DE as decimal (the count).
4EE1
POP BC C1
Restore loop counter and column counter.
4EE2
POP HL E1
Restore array pointer.
4EE3
INC HL 23
Advance to next array entry.
4EE4
INC C 0C
Increment column counter.
4EE5
Decrement B and loop for all type variants. [OUTPUT LOOP END]
4EE7
POP DE D1
Restore position pointer.
4EE8
POP AF F1
Restore mode flag.
4EE9
CP 01H FE 01
Compare mode to 1.
4EEB
If not mode 1 (NZ FLAG set), JUMP to 4DD0H to continue scanning.

Mode 1: Check reference table for more entries to process.

4EEE
LD BC,0000H 01 00 00
Load BC with 0000H. Self-modifying target at 4EEFH holds read pointer.
4EF1
LD HL,0000H 21 00 00
Load HL with 0000H. Self-modifying target at 4EF2H holds write pointer.
4EF4
OR A B7
Clear carry flag.
4EF5
SBC HL,BC ED 42
Subtract read from write pointer. Tests if table has more entries.
4EF7
ADD HL,BC 09
Restore HL (add BC back).
4EF8
If more entries (NZ FLAG set), JUMP to 4E69H to process next.
4EFB
LD BC,4240H 01 40 42
Load BC with 4240H (end of reference table area).

4EFEH - Reference Table Overflow Check

Checks if the reference table at 4200H has overflowed and handles table wraparound for continued scanning.

Continue checking reference table state.

4EFE
OR A B7
Clear carry flag for subtraction.
4EFF
SBC HL,BC ED 42
Subtract 4240H from write pointer. Checks if we've reached end of table.
4F01
If exactly at end (Z FLAG set), JUMP to 4DD0H to continue scanning.
4F04
Otherwise JUMP to 4E0BH to process end of program.

4F07H - Initialize Search Parameters

Validates the search variable and sets up parameters before beginning the main program scan loop.

Check search type and validate variable name.

4F07
LD A,(4F7CH) 3A 7C 4F
Load search type flag from 4F7CH (C3H=string, 21H=variable).
4F0A
CP C3H FE C3
Compare to C3H (JP opcode = string search mode).
4F0C
LD A,(51C6H) 3A C6 51
Load first character of search variable.
4F0F
If not string search (NZ FLAG set), JUMP to 4F15H for variable validation.
4F11
LD A,(51DBH) 3A DB 51
Load first character of search string from 51DBH.
4F14
DEC A 3D
Decrement A. Adjusts for string validation.
4F15
INC A 3C
Increment A. If was FFH (no variable), becomes 00H.
4F16
If no valid search term (Z FLAG set), JUMP to ROM 1997H for ?SN ERROR.
4F19
LD BC,(4E76H) ED 4B 76 4E
Load target line number from 4E76H into BC.
4F1D
LD DE,(40A4H) ED 5B A4 40
Load TXTTAB (start of BASIC program) into DE.
4F21
LD A,B 78
Load high byte of target line into A.
4F22
AND C A1
AND with low byte. If both FFH, result is FFH.
4F23
INC A 3C
Increment. If was FFH (-1), becomes 00H.
4F24
If FFFFH (search all lines), JUMP to 4F41H to skip line search.

Search for specific target line number in BASIC program.

4F26
EX DE,HL EB
[LINE SEARCH LOOP] Exchange DE and HL. HL now points to program.
4F27
PUSH HL E5
Save current line pointer.
4F28
LD A,(HL) 7E
Load low byte of next-line pointer.
4F29
INC HL 23
Advance to high byte.
4F2A
OR (HL) B6
OR with high byte. If both 00H, end of program.
4F2B
If end of program (Z FLAG set), JUMP to 4E20H to display message and exit.
4F2E
INC HL 23
Advance to line number low byte.
4F2F
LD E,(HL) 5E
Load line number low byte into E.
4F30
INC HL 23
Advance to line number high byte.
4F31
LD D,(HL) 56
Load line number high byte into D. DE now has line number.
4F32
INC HL 23
Advance to line content.

Skip to end of this line to get next line pointer.

4F33
LD A,(HL) 7E
[SKIP LINE CONTENT] Load byte from line.
4F34
OR A B7
Test if end of line (00H).
4F35
INC HL 23
Advance to next byte.
4F36
If not end (NZ FLAG set), JUMP back to keep scanning. [SKIP LOOP END]
4F38
EX DE,HL EB
Exchange. HL=line number, DE=next line address.
4F39
SBC HL,BC ED 42
Subtract target line number from current. Tests if we've passed it.
4F3B
POP HL E1
Restore saved line pointer.
4F3C
If found exact match (Z FLAG set), JUMP to 4F41H.
4F3E
EX DE,HL EB
Exchange. HL=next line address.
4F3F
If not yet reached target (C FLAG set), JUMP to continue search. [SEARCH LOOP END]
4F41
LD A,03H 3E 03
Load A with 3 (search mode = ready to scan).
4F43
JUMP to 4DD0H to begin main scanning loop with current position.

4F46H - Skip Quoted String

Skips over a quoted string in the BASIC program text, advancing the pointer past the closing quote.

Search for matching quote to skip string content.

4F46
LD BC,4F77H 01 77 4F
Load BC with return address 4F77H.
4F49
PUSH BC C5
Push return address onto stack (simulating CALL).
4F4A
LD B,A 47
Copy quote character (22H) to B as search target.
4F4B
INC DE 13
[QUOTE SEARCH LOOP] Advance past current character.
4F4C
LD A,(DE) 1A
Load next character.
4F4D
CP B B8
Compare to target quote.
4F4E
RET Z C8
If found closing quote (Z FLAG set), RETURN to 4F77H.
4F4F
OR A B7
Test if end of line (00H).
4F50
If not end (NZ FLAG set), JUMP to continue search. [LOOP END]
4F52
POP AF F1
Pop and discard return address (unterminated string).
4F53
JUMP to 4F6AH to advance to next line.

4F55H - Skip to End of Statement

Advances the scan pointer to the end of the current statement (colon or end of line).

Find colon or end of line to skip statement.

4F55
INC DE 13
[STATEMENT SKIP LOOP] Advance to next byte.
4F56
LD A,(DE) 1A
Load current byte.
4F57
OR A B7
Test if end of line.
4F58
If end of line (Z FLAG set), JUMP to 4F6AH to advance to next line.
4F5A
CP 3AH FE 3A
Compare to : (colon = statement separator).
4F5C
If colon (Z FLAG set), JUMP to 4F77H to continue scanning next statement.
4F5E
CP 22H FE 22
Compare to " (quote = start of string).
4F60
If quote (Z FLAG set), CALL 4F4AH to skip quoted string.
4F63
JUMP back to continue scanning. [LOOP END]

4F65H - Skip to End of Line

Advances the scan pointer to the end of the current line (00H terminator).

Find end of line marker.

4F65
INC DE 13
[LINE SKIP LOOP] Advance to next byte.
4F66
LD A,(DE) 1A
Load current byte.
4F67
OR A B7
Test if end of line (00H).
4F68
If not end (NZ FLAG set), JUMP to continue. [LOOP END]
4F6A
INC DE 13
Advance past the 00H terminator to next line header.

Main loop entry: Check for end of program and continue scanning.

4F6B
LD A,(DE) 1A
Load first byte of next line (low byte of next-line pointer).
4F6C
OR A B7
Test if zero (end of program marker).
4F6D
JP Z,4DFCH CA FC 4D
If end of program (Z FLAG set), JUMP to 4DFCH. Self-modifying: opcode at 4F6DH changes.

4F70H - Store Line Pointer and Advance

Stores the current line address for reference tracking and advances through the line header to begin scanning line content.

Save current position and skip line header (4 bytes: next-ptr + line-num).

4F70
LD (4E7AH),DE ED 53 7A 4E
Store current line address at 4E7AH for reference when matches are found.
4F74
INC DE 13
Skip byte 1 of line header (next-line pointer low).
4F75
INC DE 13
Skip byte 2 of line header (next-line pointer high).
4F76
INC DE 13
Skip byte 3 of line header (line number low).
4F77
INC DE 13
Skip byte 4 of line header (line number high). DE now points to line content.

Main character scanning loop - classify each byte.

4F78
LD A,(DE) 1A
[MAIN SCAN LOOP] Load current byte from program.
4F79
OR A B7
Test if end of line (00H).
4F7A
If end of line (Z FLAG set), JUMP to 4F6AH to advance to next line.
4F7C
LD HL,512FH 21 2F 51
Load HL with 512FH (string match routine). Self-modifying: First byte changes to C3H or 21H.
4F7F
CP 41H FE 41
Compare byte to 'A' (41H).
4F81
If >= 'A' (NC FLAG set), JUMP to 4F9BH - could be variable or token.
4F83
CP 30H FE 30
Compare to '0' (30H).
4F85
If >= '0' (NC FLAG set), JUMP to 4FBAH - numeric character (digit or numeric var).
4F87
CP 20H FE 20
Compare to space (20H).
4F89
If space (Z FLAG set), JUMP to 4F77H to skip and continue.
4F8B
CP 22H FE 22
Compare to " (quote = string literal start).
4F8D
If quote (Z FLAG set), JUMP to 4F46H to handle string literal.
4F8F
CP 26H FE 26
Compare to & (ampersand = hex/octal prefix).
4F91
If ampersand (Z FLAG set), JUMP to 504BH to handle &H, &O, &D.
4F94
CP 2EH FE 2E
Compare to . (period = decimal point).
4F96
If period (Z FLAG set), JUMP to 4FE1H to skip decimal number.
4F99
Otherwise skip character and continue scanning.

4F9BH - Handle Alphabetic Character or Token

Processes bytes >= 41H ('A'). Determines if it's a variable name, keyword token, or special token requiring specific handling.

Character is >= 'A' - check if it's a letter, token, or special case.

4F9B
LD C,A 4F
Save character in C for later use.
4F9C
If 'Z'+1 (5BH) exactly (Z FLAG set), JUMP to 5073H. (Note: CP already done, this is redundant check)
4F9F
CP 5BH FE 5B
Compare to '[' (5BH) - first non-letter after 'Z'.
4FA1
If < "[" (C FLAG set), it's a letter A-Z. JUMP to 5091H to parse variable name.

Character is >= 5BH - check for BASIC keyword tokens.

4FA4
CP 93H FE 93
Compare to 93H (REM token).
4FA6
If REM (Z FLAG set), JUMP to 4F65H to skip rest of line.
4FA8
CP FBH FE FB
Compare to FBH (single-quote REM token).
4FAA
If single-quote REM (Z FLAG set), JUMP to 4F65H to skip rest of line.
4FAC
CP 88H FE 88
Compare to 88H (DATA token).
4FAE
If DATA (Z FLAG set), JUMP to 4F55H to skip to end of statement.
4FB0
CP A3H FE A3
Compare to A3H (DEF token).
4FB2
If not DEF (NZ FLAG set), JUMP to 4F77H to continue scanning.

DEF token found - track its position for DEF FN handling.

4FB4
LD (5085H),DE ED 53 85 50
Store current position at 5085H to track DEF statement location.
4FB8
JUMP to 4F77H to continue scanning.

4FBAH - Handle Numeric Character

Processes numeric characters (0-9). Determines if it's part of a numeric variable name or a numeric constant to skip.

Character is >= '0' and < 'A' - check if digit or colon.

4FBA
CP 3AH FE 3A
Compare to : (colon = 3AH, statement separator).
4FBC
If >= ':' (NC FLAG set), not a digit. JUMP to 4F77H to continue.
4FBE
(Unreachable - previous JR NC always taken if this would be)

It's a digit 0-9. Parse as numeric variable or constant.

4FC0
LD HL,51CBH 21 CB 51
Load HL with 51CBH, buffer for numeric variable name.
4FC3
LD B,06H 06 06
Load B with 6 (max digits in numeric variable name).

Skip leading zeros in numeric value.

4FC5
CP 30H FE 30
Compare current digit to '0'.
4FC7
If not zero (NZ FLAG set), JUMP to 4FD9H to start storing.
4FC9
LD (HL),A 77
[LEADING ZERO LOOP] Store the '0'.
4FCA
Call 514BH to get next non-space character.
4FCD
CP 30H FE 30
Compare to '0'.
4FCF
If another zero (Z FLAG set), JUMP back to keep skipping. [LOOP END]
4FD1
CP 30H FE 30
Compare to '0' again.
4FD3
If < '0' (C FLAG set), not a digit. JUMP to 4FEEH.
4FD5
CP 3AH FE 3A
Compare to ':'.
4FD7
If >= ':' (NC FLAG set), end of digits. JUMP to 4FF2H to check for match.

Store digit and get next character.

4FD9
LD (HL),A 77
[DIGIT STORE LOOP] Store digit in buffer.
4FDA
INC HL 23
Advance buffer pointer.
4FDB
Call 514BH to get next non-space character.
4FDE
Decrement B and loop until max digits or non-digit. [LOOP END]
4FE0
DEC DE 1B
Back up one character (went one too far).

Skip any remaining digits in a decimal number.

4FE1
[DECIMAL SKIP LOOP] Get next non-space character.
4FE4
CP 3AH FE 3A
Compare to ':'.
4FE6
If >= ':' (NC FLAG set), end of number. JUMP to 4FECH.
4FE8
CP 30H FE 30
Compare to '0'.
4FEA
If >= '0' (NC FLAG set), still in number. JUMP back. [LOOP END]
4FEC
JUMP to 4F78H to continue main scan loop.

Check for decimal point in number.

4FEE
CP 2EH FE 2E
Compare to . (decimal point).
4FF0
If decimal point (Z FLAG set), JUMP to 4FE1H to continue skipping number.

4FF2H - Numeric Variable Match Check

After parsing a numeric variable name, compares it against the search target to determine if this is a reference to record.

Calculate variable name length and compare to search target.

4FF2
LD A,05H 3E 05
Load A with 5 (max length - 1).
4FF4
SUB B 90
Subtract remaining count. A = digits parsed (0-5).
4FF5
ADC 00H CE 00
Add carry (adjusts for edge case).
4FF7
LD C,A 4F
Copy length to C.
4FF8
LD HL,(51BFH) 2A BF 51
Load search variable info from 51BFH. L=length, H=first char.
4FFB
CP L BD
Compare parsed length to search length.
4FFC
If parsed shorter (C FLAG set), no match. Self-modifying: opcode changes to C2H or DAH.
4FFF
If different length (NZ FLAG set), JUMP to 501CH to check if should record anyway.

Lengths match - compare actual characters.

5001
LD A,(51CBH) 3A CB 51
Load first character of parsed variable.
5004
CP H BC
Compare to first character of search target.
5005
If different (NZ FLAG set), JUMP back to check via self-mod code.
5007
LD IY,51C1H FD 21 C1 51
Load IY with 51C1H (search variable remaining chars).
500B
Call 5189H to compare remaining characters.
500E
If no match (NZ FLAG set), JUMP back to continue scanning.

Full match found - record reference.

5010
LD HL,51D1H 21 D1 51
Load HL with 51D1H (reference count array, entry 0).
5013
LD A,(4E0CH) 3A 0C 4E
Load search mode flag.
5016
OR A B7
Test if mode 0 (all variables).
5017
If not mode 0 (NZ FLAG set), JUMP to 50D9H to record and continue.
501A
Mode 0: JUMP to 4FECH to continue scanning without recording yet.

Check if we should track this variable for "all variables" mode.

501C
LD HL,(51C5H) 2A C5 51
Load current scan variable info from 51C5H.
501F
LD A,C 79
Load parsed length into A.
5020
CP L BD
Compare to current variable length.
5021
If shorter (C FLAG set), JUMP to 5037H. Self-modifying: Operand changes.
5023
If different (NZ FLAG set), JUMP to continue scanning.
5026
LD A,(51CBH) 3A CB 51
Load first character of parsed variable.
5029
CP H BC
Compare to current scan variable first char.
502A
If different (NZ FLAG set), JUMP back to compare again.
502C
LD IY,51C7H FD 21 C7 51
Load IY with 51C7H (current variable remaining chars).
5030
Call 5189H to compare remaining characters.
5033
If no match (NZ FLAG set), JUMP back.
5035
Match found - JUMP to 5048H to check for duplicate.

New variable found that should be added to tracking.

5037
LD A,C 79
Load parsed length into A.
5038
INC C 0C
Increment for copy count.
5039
PUSH DE D5
Save scan pointer.
503A
LD HL,51CBH 21 CB 51
Load HL with source (parsed variable name).
503D
LD DE,51C6H 11 C6 51
Load DE with destination (current variable storage).
5040
LD B,00H 06 00
Clear B for 16-bit byte count.
5042
LDIR ED B0
Copy C bytes: variable name to current tracking.
5044
POP DE D1
Restore scan pointer.
5045
Call 516EH to update variable length storage.
5048
JUMP to 5101H to check reference table and record.

504BH - Handle Ampersand Prefix (&H, &O, &D)

Processes & prefixed numbers: &H for hex, &O for octal, &D for decimal. Skips the numeric constant.

Check what follows the ampersand.

504B
Call 514BH to get next non-space character after &.
504E
CP 44H FE 44
Compare to D (decimal prefix).
5050
If &D (Z FLAG set), JUMP to 4F6AH to skip to next line.
5053
CP 4FH FE 4F
Compare to O (octal prefix).
5055
If &O (Z FLAG set), JUMP to 4FE1H to skip digits.
5058
CP 48H FE 48
Compare to H (hex prefix).
505A
If not &H (NZ FLAG set), JUMP to 5070H to continue scanning.

&H found - skip hex digits (0-9, A-F).

505C
[HEX SKIP LOOP] Get next non-space character.
505F
CP 47H FE 47
Compare to G (first char after hex range).
5061
If >= 'G' (NC FLAG set), end of hex. JUMP to 5070H.
5063
CP 41H FE 41
Compare to A.
5065
If >= 'A' (NC FLAG set), it's A-F. JUMP to continue. [HEX LETTER LOOP]
5067
CP 3AH FE 3A
Compare to ':'.
5069
If >= ':' (NC FLAG set), end of digits. JUMP to 5070H.
506C
CP 30H FE 30
Compare to '0'.
506E
If >= '0' (NC FLAG set), it's 0-9. JUMP to continue. [HEX DIGIT LOOP END]
5070
JUMP to 4F78H to continue main scan.

5073H - Handle Z Variable Special Case

Processes the 'Z' character followed by 'S' which might indicate a special variable or the ZS variable name.

Check for ZS variable pattern.

5073
Call 514BH to get next non-space character.
5076
CP 53H FE 53
Compare to S.
5078
If not 'S' (NZ FLAG set), JUMP to 5094H to continue as regular variable.
507A
LD B,A 47
Save 'S' in B.
507B
Get next character after 'S'.
507E
Call 5161H to check if it's alphanumeric.
5081
If not alphanumeric (C FLAG set), JUMP to 509FH.

Check if this is inside a DEF FN statement.

5083
PUSH DE D5
Save current position.
5084
LD DE,0000H 11 00 00
Load DE with 0000H. Self-modifying target: Address at 5085H holds DEF position.
5087
LD HL,(4E7AH) 2A 7A 4E
Load current line address from 4E7AH.
508A
OR A B7
Clear carry for subtraction.
508B
SBC HL,DE ED 52
Subtract DEF position from current line. Tests if we're in same DEF statement.
508D
POP DE D1
Restore position.
508E
If not in DEF (NC FLAG set), JUMP to 509FH.
5090
LD C,A 4F
Save character in C.

5091H - Parse Alphabetic Variable Name

Parses a variable name starting with a letter (A-Z). Collects up to 2 characters plus optional type suffix ($, %, !, #).

Get next character and check for valid variable name continuation.

5091
Call 514BH to get next non-space character.
5094
LD B,20H 06 20
Load B with 20H (space = no second character).
5096
Call 515AH to check if digit or letter.
5099
If not alphanumeric (C FLAG set), JUMP to 50A4H - single char variable.
509B
LD B,A 47
Save second character in B.
509C
Get next character.
509F
Check if alphanumeric.
50A2
If alphanumeric (NC FLAG set), skip more chars. [SKIP EXTRA CHARS LOOP]

Variable name parsed. C=first char, B=second char (or space). Compare to search target.

50A4
LD HL,(51C0H) 2A C0 51
Load search variable (first 2 chars) from 51C0H. L=first, H=second.
50A7
LD A,C 79
Load first character into A.
50A8
CP L BD
Compare to search first character.
50A9
If less (C FLAG set), no match. Self-modifying: opcode changes to C2H or DAH.
50AC
If different (NZ FLAG set), JUMP to 50EEH to check for "all vars" tracking.
50AE
LD A,B 78
Load second character into A.
50AF
CP H BC
Compare to search second character.
50B0
If different (NZ FLAG set), JUMP back to self-mod check.

First two chars match. Now check type suffix ($, %, !, #).

50B2
LD A,(4E0CH) 3A 0C 4E
Load search mode flag.
50B5
OR A B7
Test if mode 0.
50B6
If mode 0 (Z FLAG set), JUMP to 50EBH to record without type check.
50B8
LD A,(DE) 1A
Load current character (potential type suffix).
50B9
LD HL,51D5H 21 D5 51
Load HL with 51D5H (reference count array entry 4).
50BC
LD IY,51B5H FD 21 B5 51
Load IY with 51B5H (type suffix table: space, #, !, %, $).
50C0
LD B,04H 06 04
Load B with 4 (check 4 suffix types).

Loop through type suffixes to find match.

50C2
CP (IY+00H) FD BE 00
[SUFFIX CHECK LOOP] Compare current char to suffix in table.
50C5
If match (Z FLAG set), JUMP to 50CDH.
50C7
DEC HL 2B
Move to previous count array entry.
50C8
DEC IY FD 2B
Move to previous suffix in table.
50CA
Decrement B and loop. [LOOP END]
50CC
DEC DE 1B
No suffix match - back up one character.
50CD
Get next character (after suffix if any).
50D0
CP 28H FE 28
Compare to ( (open parenthesis = array access).
50D2
If not '(' (NZ FLAG set), JUMP to 50D9H to record reference.

Array access: Advance HL by 5 to use array reference count slot.

50D4
LD BC,0005H 01 05 00
Load BC with 5 (offset to array count entries).
50D7
ADD HL,BC 09
Add 5 to HL to point to array count slot.
50D8
INC DE 13
Advance past the '(' character.

Record the reference by incrementing count at (HL).

50D9
INC (HL) 34
Increment reference count for this variable type.
50DA
LD HL,4F6DH 21 6D 4F
Load HL with 4F6DH (self-modifying location).
50DD
LD (HL),C3H 36 C3
Store C3H (JP opcode) to mark that we found a reference.
50DF
LD HL,(4E7AH) 2A 7A 4E
Load current line address from 4E7AH.
50E2
INC HL 23
Skip next-line pointer low byte.
50E3
INC HL 23
Skip next-line pointer high byte.
50E4
LD C,(HL) 4E
Load line number low byte into C.
50E5
INC HL 23
Advance to high byte.
50E6
LD B,(HL) 46
Load line number high byte into B. BC = line number.
50E7
LD (4E76H),BC ED 43 76 4E
Store line number at 4E76H for output display.
50EB
JUMP to 4F78H to continue main scan loop.

50EEH - Track New Variable for All-Variables Mode

When scanning all variables, this routine checks if a found variable should be added to the tracking system or if it's already being tracked.

Compare found variable to current tracking variable.

50EE
LD HL,(51C6H) 2A C6 51
Load current tracking variable (first 2 chars) from 51C6H.
50F1
LD A,C 79
Load found variable first char into A.
50F2
CP L BD
Compare to tracking first char.
50F3
If less (C FLAG set), it's a new earlier variable. JUMP to 50FEH.
50F5
If greater (NZ FLAG set), skip - we've passed this in sort order.
50F8
LD A,B 78
Load found variable second char into A.
50F9
CP H BC
Compare to tracking second char.
50FA
If different (NZ FLAG set), JUMP back to check order.
50FC
Match found - JUMP to 5101H to record reference.

New variable found that should be added to tracking.

50FE
Call 5168H to store new variable BC at 51C6H and init reference table.

5101H - Check and Record Line Reference

Checks the reference table for duplicate line numbers and adds new entries. Prevents recording the same line multiple times for the same variable.

Get reference table write pointer and check for table end.

5101
LD HL,(4EEFH) 2A EF 4E
Load reference table write pointer from 4EEFH. Self-modifying target.
5104
LD BC,4240H 01 40 42
Load BC with 4240H (end of reference table).
5107
OR A B7
Clear carry for comparison.
5108
SBC HL,BC ED 42
Subtract end address from current pointer.
510A
ADD HL,BC 09
Restore HL (add BC back).
510B
If at table end (Z FLAG set), table full. JUMP to 50EBH to continue.
510D
LD BC,4200H 01 00 42
Load BC with 4200H (start of reference table).
5110
OR A B7
Clear carry.
5111
SBC HL,BC ED 42
Subtract start address.
5113
ADD HL,BC 09
Restore HL.
5114
LD BC,(4E7AH) ED 4B 7A 4E
Load current line address from 4E7AH into BC.
5118
If table empty (Z FLAG set), JUMP to 5126H to add first entry.
511A
DEC HL 2B
Back up to last entry high byte.
511B
LD A,B 78
Load current line high byte.
511C
CP (HL) BE
Compare to last entry high byte.
511D
If different (NZ FLAG set), not duplicate. JUMP to 5125H.
511F
DEC HL 2B
Back up to last entry low byte.
5120
LD A,C 79
Load current line low byte.
5121
CP (HL) BE
Compare to last entry low byte.
5122
INC HL 23
Restore HL position.
5123
If duplicate (Z FLAG set), don't add again. JUMP to 50EBH.
5125
INC HL 23
Advance back to write position.
5126
LD (HL),C 71
Store line address low byte.
5127
INC HL 23
Advance to high byte position.
5128
LD (HL),B 70
Store line address high byte.
5129
INC HL 23
Advance to next slot.
512A
LD (4EEFH),HL 22 EF 4E
Store updated write pointer at 4EEFH.
512D
JUMP to 50EBH to continue scanning.

512FH - String Literal Match Check

When searching for a string literal (REF "string"), compares the current position against the search string stored at 51DBH.

Compare current program position to search string.

512F
LD HL,51DBH 21 DB 51
Load HL with 51DBH (search string buffer).
5132
CP (HL) BE
Compare current byte (in A) to first search char.
5133
If no match (NZ FLAG set), JUMP to 5148H to continue scanning.
5135
PUSH DE D5
Save program position.

First char matches - compare remaining characters.

5136
INC DE 13
[STRING COMPARE LOOP] Advance program pointer.
5137
INC HL 23
Advance search string pointer.
5138
LD A,(HL) 7E
Load next search character.
5139
OR A B7
Test if end of search string (00H).
513A
If not end (NZ FLAG set), JUMP to 5143H to compare next char.

Full match found - record reference.

513C
POP HL E1
Pop saved position into HL (discard).
513D
LD HL,51D1H 21 D1 51
Load HL with 51D1H (reference count array).
5140
JUMP to 50D9H to record the reference.

Compare next character in string.

5143
LD A,(DE) 1A
Load program character.
5144
CP (HL) BE
Compare to search character.
5145
If match (Z FLAG set), JUMP back to continue. [LOOP END]
5147
POP DE D1
Restore original position (no match).
5148
JUMP to 4F77H to continue scanning.

514BH - Get Next Non-Space Character

Advances DE past any spaces and returns the next non-space character in A. Also skips certain control characters.

514B
INC DE 13
[SKIP SPACE LOOP] Advance to next character.
514C
LD A,(DE) 1A
Load character into A.
514D
CP 20H FE 20
Compare to space (20H).
514F
If space (Z FLAG set), JUMP back to skip it.
5151
RET NC D0
If char > space (NC FLAG set), RETURN with char in A.
5152
CP 0BH FE 0B
Compare to 0BH (vertical tab).
5154
RET NC D0
If >= 0BH (NC FLAG set), RETURN.
5155
CP 09H FE 09
Compare to 09H (tab).
5157
RET C D8
If < tab (C FLAG set), RETURN.
5158
Tab or line feed - skip and continue. [LOOP END]

515AH - Check If Digit (0-9)

Tests if character in A is a digit 0-9. Returns C FLAG set if not a digit.

515A
CP 30H FE 30
Compare to '0' (30H).
515C
RET C D8
If < '0' (C FLAG set), RETURN - not a digit.
515D
CP 3AH FE 3A
Compare to ':' (3AH, one past '9').
515F
CCF 3F
Complement carry. If was < 3AH, now NC (is digit).
5160
RET NC D0
If digit 0-9 (NC FLAG set), RETURN.

5161H - Check If Alphanumeric

Tests if character in A is alphanumeric (A-Z or 0-9). Returns C FLAG set if not alphanumeric.

5161
CP 41H FE 41
Compare to 'A' (41H).
5163
RET C D8
If < 'A' (C FLAG set), RETURN - not a letter (might be digit, checked by caller).
5164
CP 5BH FE 5B
Compare to '[' (5BH, one past 'Z').
5166
CCF 3F
Complement carry. If was < 5BH, now NC (is letter).
5167
RET C9
RETURN with NC if letter A-Z, C if not.

5168H - Initialize Variable Tracking

Stores a new variable name at 51C6H and initializes the reference table pointers. Called when a new variable is found during "all variables" scan.

5168
LD (51C6H),BC ED 43 C6 51
Store variable name (BC = first 2 chars) at 51C6H.
516C
LD A,01H 3E 01
Load A with 1 (variable length = 1 or 2 chars).
516E
LD (51C5H),A 32 C5 51
Store length at 51C5H.

5171H - Initialize Reference Table

Sets up the reference table pointers and configures the output column width based on variable type (numeric vs alphabetic).

5171
LD A,(51C6H) 3A C6 51
Load first character of variable from 51C6H.
5174
CP 41H FE 41
Compare to 'A' (41H).
5176
LD A,18H 3E 18
Load A with 18H (24) for narrow columns (alpha vars).
5178
If >= 'A' (NC FLAG set), use narrow columns.
517A
LD A,38H 3E 38
Otherwise load A with 38H (56) for wider columns (numeric vars).
517C
LD (5021H),A 32 21 50
Self-modifying: Store column width offset at 5021H.
517F
LD HL,4200H 21 00 42
Load HL with 4200H (start of reference table).
5182
LD (4EEFH),HL 22 EF 4E
Store as write pointer at 4EEFH.
5185
LD (4EF2H),HL 22 F2 4E
Store as read pointer at 4EF2H.
5188
RET C9
RETURN.

5189H - Compare Variable Name Extension

Compares remaining characters of a variable name. IY points to search extension, HL points to current position in 51CCH buffer. B contains character count.

5189
LD A,C 79
Load length into A.
518A
OR A B7
Test if length is zero.
518B
LD B,A 47
Copy length to B for loop counter.
518C
RET Z C8
If zero length (Z FLAG set), RETURN with Z set (match).
518D
LD HL,51CCH 21 CC 51
Load HL with 51CCH (parsed variable extension buffer).

Compare loop for remaining variable name characters.

5190
LD A,(HL) 7E
[COMPARE LOOP] Load character from parsed buffer.
5191
CP (IY+00H) FD BE 00
Compare to character in search buffer.
5194
INC HL 23
Advance parsed buffer pointer.
5195
RET NZ C0
If no match (NZ FLAG set), RETURN with NZ.
5196
INC IY FD 23
Advance search buffer pointer.
5198
Decrement B and loop until all chars compared. [LOOP END]
519A
RET C9
RETURN with Z set (all characters matched).

519BH - Output Cursor Positioning

Handles cursor positioning for output. Prints newline if near end of line, or spaces to align columns.

519B
Call BASIC routine to print CR/LF (newline).
519E
Call BASIC routine to print a space.
51A1
LD A,(4020H) 3A 20 40
Load cursor position from 4020H (low 6 bits = column).
51A4
AND 3FHAND 00111111 E6 3F
Mask to get column number (0-63).
51A6
CP 35H FE 35
Compare to column 53.
51A8
If >= column 53 (NC FLAG set), JUMP back to print newline.
51AA
SUB 07H D6 07
Subtract 7 from column position.
51AC
RET NC D0
If result >= 0 (NC FLAG set), RETURN - already past minimum.
51AD
NEG ED 44
Negate A to get number of spaces needed.
51AF
JUMP to BASIC routine to print A spaces and return.

51B2H - Data Tables and Messages

Contains type suffix characters, messages, and variable storage buffers used throughout the REF command.

Type suffix table at 51B1H: (space), #, !, %, $

51B2
DB 24H 24
Data: $ - String type suffix.
51B3
DB 25H 25
Data: % - Integer type suffix.
51B4
DB 21H,23H,54H 21 23 54
Data: ! (single), # (double), T (start of "TEXT").

Message "!#$%EXT END" with CR at 51B6H

51B7
DB 45H,58H,54H 45 58 54
Data: "EXT" - Part of message.
51BA
DB 20H,45H,4EH,44H,0DH 20 45 4E 44 0D
Data: " END" + CR - End of message.

Variable storage buffers (initialized to 00H).

51BF
DB 00H,00H 00 00
51BFH-51C0H: Search variable (first 2 chars or line number).
51C1
DB 00H,00H,00H,00H 00 00 00 00
51C1H-51C4H: Search variable extension (remaining chars).
51C5
DB 00H 00
51C5H: Search variable name length.
51C6
DB 00H,00H,00H,00H,00H 00 00 00 00 00
51C6H-51CAH: Current variable being scanned.
51CB
DB 00H 00
51CBH: Current variable first character.
51CC
DB 00H,00H,00H,00H,00H 00 00 00 00 00
51CCH-51D0H: Variable name comparison buffer.
51D1
DB 00H,00H,00H,00H,00H 00 00 00 00 00
51D1H-51D5H: Reference count array (entries 0-4).
51D6
DB 00H,00H,00H,00H,00H 00 00 00 00 00
51D6H-51DAH: Reference count array (entries 5-9, for arrays).
51DB
DB 00H 00
51DBH+: String search buffer (16 bytes for search string).