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

Page Customization

Description:

SYS13 provides two essential functions for NEWDOS/80 BASIC: error message display and the first part of the RENUM (renumber) command. This overlay must be present on the system diskette whenever BASIC is active.

Error Message Handler (4D0CH-4D45H): When BASIC encounters an error, the error code is passed in Register E. This routine translates the error code into an index, locates the corresponding null-terminated error message string in the error table (4D47H-50FCH), and prepares it for display. The routine handles three error code ranges: DOS errors (9CH-A1H) which trigger a call to 5D5EH for DOS-specific handling, standard BASIC errors (2FH-62H), and extended errors (63H+). Error codes are mapped through a calculation that converts them to table indices.

Error Message Table (4D47H-50FCH): Contains 52 null-terminated ASCII strings for BASIC error messages, from "NEXT WITHOUT FOR" through "BAD FILE POSITIONING PARAM". Messages are indexed sequentially, with the lookup algorithm counting null terminators to find the correct string.

RENUM Command Handler (50FEH-51E7H): Implements the first part of BASIC's RENUM (renumber) command. This routine parses up to four optional parameters: new starting line number, increment, old starting line, and old ending line. It validates that the line number range is valid (not exceeding 65529/FFF9H) and prepares the renumbering operation. The actual line number modification is handled by subsequent code.

Routine Overview:

AddressPurpose
4D00H-4D0BHData area (parameters/work area)
4D0CH-4D45HError message display routine (entry point)
4D46HStart of error message table (00H terminator before strings)
4D47H-50FCHError message strings (you've already converted these)
50FDHFinal 00H terminator
50FEH-51E7HRENUM command handler (Part 1)

Variables:

AddressBytesPurpose
4D00H-4D01H2RENUM: New starting line number (default 10)
OTHER: Current memory top (2 bytes)
4D02H-4D03H2RENUM: Line increment value (default 10)
OTHER: Buffer size (2 bytes)
4D04H-4D05H2RENUM: Old starting line number (default 0)
OTHER: Low memory limit (2 bytes)
4D06H-4D07H2RENUM: Old ending line number (default FFFFH)
OTHER: High memory limit (2 bytes)
4D08H-4D09H2RENUM: Working storage for calculations
OTHER: Memory top backup (2 bytes)
4D0AH1RENUM: "U" flag - Update line references
4D0BH1RENUM: "X" flag - Extended mode
4D46H1Error table start marker (00H)
4D47H-50FCH950Error message string table (null-terminated strings)
50FDH1Error table end marker (00H - double null terminator)
409AH1BASIC system flag (cleared for error code 02H)
4317H1COMPRESS work variable (cleared by RENUM)
40A4H2TXTTAB - Start of BASIC program text

Key Routines:

AddressPurpose
4D00H-4D0BSystem variables/flags area
4D0CH-4D45Error message lookup routine
  • Entry point at 4D0CH
  • Checks for error code 4FH (special case → 50FEH)
  • Translates error codes to message table index
  • Walks null-terminated string table at 4D47H
4D46Null terminator before message table
4D47H-50FCError message string table (null-terminated strings)
50FDFinal null terminator (double 00H ends table)
50FEH-513EAdditional error/CLEAR related code (not fully analyzed)
513FH-515ACLEAR command parameter parsing
  • Checks for 'U' (55H) and 'X' (58H) flags
  • Validates memory boundaries (low < high)
  • Ensures minimum 7 bytes free space
515CH-516FFile buffer chain initialization
  • BC = buffer counter (starts at 0)
  • HL = (40A4H) = TXTTAB = buffer chain head
5162H-51B2Buffer chain walk loop
  • Reads next buffer pointer from chain
  • Increments buffer count
  • Checks 4D0AH flag (skip adjustment if set)
  • Validates buffer address against memory limits
  • Updates memory top at 4D00H
  • Calls 5CF3H for buffer processing
  • Error 72H (BAD FILE#) if chain corrupted
  • Error 8CH (OUT OF MEMORY) if overflow
  • Error 96H (BAD FILE DATA) if buffer addr = 0
51B5H-51C0Buffer block calculation
  • Converts buffer count to 128-byte blocks
  • Loop: SBC HL,DE where DE=0080H
51C2H-51CCDOS buffer initialization
  • DE = 64CAH (FCB work area)
  • B = 80H (128 bytes)
  • HL = (40A4H) buffer chain
  • A = 2DH (DOS function)
  • RST 28H to call DOS
51CDH-51E7Reserved space (27 NOPs)

Self-Modifying Code Locations:

AddressSet ByPurpose
576EH5774HSVC return value storage
57B8H5DADHLine number storage for GOSUB
57E5H5E19HConditional return opcode
5A72H59D4HCOMPRESS space handling (28H/00H)
5A95H59D8HCOMPRESS REM handling (00H/C9H)
5BA3H5B91HFebruary days (1CH=28 or 1DH=29)
5C25H5C12HVariable type code
5C2AH5C16HVariable name length
5C30H5C1AHVariable first character
5C9EH5C67HCLEAR memory difference
5CB4H5C81HCLEAR new SP value
5E13H5E09HError code for delayed error
61F1H61FDHPosition bounds value
624DH6454HPosition update enable (00/non-zero)
63A9H63AFHGuard flag (00H=clear, C9H=RET)
63BDH63B6HGuard flag backup
63EBH63F6HLine number for display
6421H6515HSystem flag for chaining
644EH64C1HCallback address (modified to 64BDH)
6447H → 6482H6438H/6445HPrint width (20H or 18H)
6529H(init value)Maximum files count

SYS11 (RENUM):

4D04HMultipleNew line number (in LD HL,nnnn)
4D08HMultipleLine increment (in LD DE,nnnn)
4D4FH4D37HSearch line number
4E60H4D80HJP target for completion
4E89H4E5AHCurrent line being processed

SYS12 (REF):

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)

Disassembly:

4D00H - RENUM Parameter Storage Area

This 12-byte data area stores the RENUM command parameters and working variables. The first 8 bytes hold the four line number parameters (new start, increment, old start, old end), followed by working storage and option flags.

4D00
LD A,(BC) / NOP 0A 00
RENUM new starting line number - Default value 000AH (10 decimal). When executed as code, these bytes are harmless NOPs.
4D02
LD A,(BC) / NOP 0A 00
RENUM increment value - Default value 000AH (10 decimal). Lines will be renumbered in steps of this value.
4D04
NOP / NOP 00 00
RENUM old starting line - Default 0000H. Renumbering begins at this line number in the existing program.
4D06
LD SP,HL / RST 38H F9 FF
RENUM old ending line - Default FFF9H (65529). Renumbering stops at this line. Value FFF9H is the maximum valid BASIC line number.
4D08
NOP / NOP 00 00
Working storage - Used during RENUM calculations to store intermediate values.
4D0A
NOP 00
"U" flag - When non-zero, RENUM updates line number references (GOTO, GOSUB, etc.) within the program.
4D0B
NOP 00
"X" flag - Extended mode flag for RENUM operation.

4D0CH - Error Message Display Entry Point

This is the main entry point for displaying BASIC error messages. Register E contains the error code when this routine is called. The routine determines which error message to display by converting the error code to a table index, then walks through the null-terminated string table to find the correct message.

Entry point: Register E contains the error code to display. Error codes fall into three ranges: DOS errors (9CH-A1H), standard BASIC errors (2FH-62H), and extended errors (63H+).

4D0C
CP 4FH FE 4F
Compare Register A with 4FH (79 decimal). If error code is 4FH, this is the special "CMD O" entry that jumps to the RENUM handler.
4D0E
If the Z FLAG is set (A equals 4FH), JUMP to 50FEH - the RENUM command entry point. This provides dual-purpose entry: error display or RENUM execution.
4D11
LD A,E 7B
Load Register A with the error code from Register E. The error code was passed in E by the calling routine.
4D12
PUSH AF F5
Save the error code on the stack. We'll need it again after checking for DOS errors.

Check if this is a DOS-specific error (codes 9CH-A1H). These require special handling through the DOS error routine.

4D13
SUB 9CH D6 9C
Subtract 9CH from the error code. DOS error codes start at 9CH, so this normalizes them to 0-based.
4D15
CP 06H FE 06
Compare with 06H. If the normalized value is less than 6, this is a DOS error (codes 9CH-A1H are the 6 DOS-specific errors).
4D17
If Carry is set (error code was 9CH-A1H), CALL 5D5EH in BASIC/CMD to handle DOS-specific error processing before displaying the message.
4D1A
POP AF F1
Restore the original error code from the stack back into Register A.

Now determine which error message table index to use. Error codes are mapped: 2FH-62H → standard errors, 63H+ → use code 5AH (which maps to "UNPRINTABLE ERROR").

4D1B
CP 63H FE 63
Compare error code with 63H (99 decimal). Codes 63H and above are outside the normal error range.
4D1D
If No Carry (error code >= 63H), JUMP to 4D25H to process as extended error (will be converted to 5AH).
4D1F
CP 2FH FE 2F
Compare error code with 2FH (47 decimal). Standard BASIC errors start at code 2FH.
4D21
If Carry set (error code < 2FH), JUMP to 4D27H. These are low-numbered errors that use the code directly as an index.
4D23
LD A,5AH 3E 5A
Load Register A with 5AH. Error codes in the range 2FH-62H but not recognized get mapped to 5AH ("UNPRINTABLE ERROR").
4D25
SUB 34H D6 34
Subtract 34H from the error code. This converts the code range 34H-5AH to table indices 00H-26H.

Convert the adjusted error code to a table index. The RRCA divides by 2 (each error entry is indexed by increments of 2 in the original error code scheme).

4D27
RRCA 0F
Rotate Register A right through Carry. This effectively divides the error code by 2, converting it to a 0-based message index.
4D28
LD B,A 47
Load Register B with the message index. B will be the counter for walking through the error message table.
4D29
LD HL,4D46H 21 46 4D
Load HL with 4D46H - the address of the error message table start marker (a 00H byte before the first message).
4D2C
INC B 04
Increment B. Since we count null terminators to find messages, we need B+1 nulls to reach message B.
4D2D
DEC A 3D
Decrement Register A. Check if error code was 02H (A becomes 01H after RRCA, then 00H here).
4D2E
If NZ FLAG (A was not 01H, meaning error was not code 02H), JUMP to 4D33H to begin table search.
4D30
LD (409AH),A 32 9A 40
Store A (now 00H) to 409AH - BASIC system flag. Error code 02H clears this flag (special system state handling).

[LOOP START] - Walk through the error table counting null terminators until we've found the Bth message.

4D33
LD A,(HL) 7E
Load Register A with the byte at (HL) - current position in the error message table.
4D34
OR A B7
OR A with itself. Sets Z FLAG if A is 00H (null terminator found).
4D35
INC HL 23
Increment HL to point to the next byte in the table.
4D36
If NZ FLAG (not a null terminator), JUMP back to 4D33H to continue scanning through current message.

Found a null terminator. Check if we've found enough nulls to reach our target message.

4D38
OR (HL) B6
OR A with next byte at (HL). If this is also 00H, we've hit the double-null end of table marker.
4D39
If Z FLAG (double null = end of table), JUMP to 4D23H to use "UNPRINTABLE ERROR" as fallback.
4D3B
Decrement B and JUMP to 4D33H if not zero. Continue counting null terminators until B reaches 0.

[LOOP END] - HL now points to the start of the target error message string.

4D3D
EX DE,HL EB
Exchange DE and HL. DE now contains the pointer to the error message string.
4D3E
POP AF F1
Pop the return address from the stack into AF. This removes the caller's return address.
4D3F
LD HL,1A01H 21 01 1A
Load HL with 1A01H - ROM address for the error message display routine that will print "?XXX ERROR IN LINE nnnn".
4D42
EX (SP),HL E3
Exchange (SP) with HL. This puts 1A01H on the stack as the return address and retrieves what was there into HL.
4D43
EX DE,HL EB
Exchange DE and HL. HL now points to the error message, DE has the old stack value.
4D44
PUSH AF F5
Push AF back onto the stack (restoring stack structure for proper return).
4D45
RET C9
Return. Due to stack manipulation, this returns to ROM routine 1A01H with HL pointing to the error message string.
4D46
NOP 00
Error table start marker - This 00H byte precedes the first error message string. The search loop counts this as the first null terminator.

4D47H - Error Message String Table

This table contains 52 null-terminated ASCII strings for all BASIC error messages. Each message ends with a 00H byte. The table is terminated by a double 00H (the final message's terminator followed by an additional 00H at 50FDH). Error messages are indexed 0-51, corresponding to error codes after translation by the lookup algorithm at 4D0CH.

4D47-4D57
DEFM "NEXT WITHOUT FOR" + 00H 4E 45 58 54 20 57 49 54 48 4F 55 54 20 46 4F 52 00
4D58-4D64
DEFM "SYNTAX ERROR" + 00H 53 59 4E 54 41 58 20 45 52 52 4F 52 00
4D65-4D79
DEFM "RETURN WITHOUT GOSUB" + 00H 52 45 54 55 52 4E 20 57 49 54 48 4F 55 54 20 47 4F 53 55 42 00
4D7A-4D85
DEFM "OUT OF DATA" + 00H 4F 55 54 20 4F 46 20 44 41 54 41 00
4D86-4D9B
DEFM "ILLEGAL FUNCTION CALL" + 00H 49 4C 4C 45 47 41 4C 20 46 55 4E 43 54 49 4F 4E 20 43 41 4C 4C 00
4D9C-4DA4
DEFM "OVERFLOW" + 00H 4F 56 45 52 46 4C 4F 57 00
4DA5-4DC3
DEFM "OUT OF MEMORY" + 00H 4F 55 54 20 4F 46 20 4D 45 4D 4F 52 59 00
4DC4-4DDA
DEFM "SUBSCRIPT OUT OF RANGE" + 00H 53 55 42 53 43 52 49 50 54 20 4F 55 54 20 4F 46 20 52 41 4E 47 45 00
4DDB-4DEE
DEFM "REDIMENSIONED ARRAY" + 00H 52 45 44 49 4D 45 4E 53 49 4F 4E 45 44 20 41 52 52 41 59 00
4DEF-4DFF
DEFM "DIVISION BY ZERO" + 00H 44 49 56 49 53 49 4F 4E 20 42 59 20 5A 45 52 4F 00
4E00-4E0E
DEFM "ILLEGAL DIRECT" + 00H 49 4C 4C 45 47 41 4C 20 44 49 52 45 43 54 00
4E0F-4E1C
DEFM "TYPE MISMATCH" + 00H 54 59 50 45 20 4D 49 53 4D 41 54 43 48 00
4E1D-4E30
DEFM "OUT OF STRING SPACE" + 00H 4F 55 54 20 4F 46 20 53 54 52 49 4E 47 20 53 50 41 43 45 00
4E31-4E40
DEFM "STRING TOO LONG" + 00H 53 54 52 49 4E 47 20 54 4F 4F 20 4C 4F 4E 47 00
4E41-4E5B
DEFM "STRING FORMULA TOO COMPLEX" + 00H 53 54 52 49 4E 47 20 46 4F 52 4D 55 4C 41 20 54 4F 4F 20 43 4F 4D 50 4C 45 58 00
4E5C-4E6A
DEFM "CAN'T CONTINUE" + 00H 43 41 4E 27 54 20 43 4F 4E 54 49 4E 55 45 00
4E6B-4E74
DEFM "NO RESUME" + 00H 4E 4F 20 52 45 53 55 4D 45 00
4E75-4E89
DEFM "RESUME WITHOUT ERROR" + 00H 52 45 53 55 4D 45 20 57 49 54 48 4F 55 54 20 45 52 52 4F 52 00
4E8A-4E9B
DEFM "UNPRINTABLE ERROR" + 00H 55 4E 50 52 49 4E 54 41 42 4C 45 20 45 52 52 4F 52 00
4E9C-4EAB
DEFM "MISSING OPERAND" + 00H 4D 49 53 53 49 4E 47 20 4F 50 45 52 41 4E 44 00
4EAC-4EB9
DEFM "BAD FILE DATA" + 00H 42 41 44 20 46 49 4C 45 20 44 41 54 41 00
4EBA-4ECC
DEFM "DISK BASIC FEATURE" + 00H 44 49 53 4B 20 42 41 53 49 43 20 46 45 41 54 55 52 45 00
4ECD-4EE4
DEFM "UNDEFINED USER FUNCTION" + 00H 55 4E 44 45 46 49 4E 45 44 20 55 53 45 52 20 46 55 4E 43 54 49 4F 4E 00
4EE5-4EF3
DEFM "FIELD OVERFLOW" + 00H 46 49 45 4C 44 20 4F 56 45 52 46 4C 4F 57 00
4EF4-4F02
DEFM "INTERNAL ERROR" + 00H 49 4E 54 45 52 4E 41 4C 20 45 52 52 4F 52 00
4F03-4F0D
DEFM "BAD FILE#" + 00H 42 41 44 20 46 49 4C 45 23 00
4F0E-4F1D
DEFM "FILE NOT FOUND" + 00H 46 49 4C 45 20 4E 4F 54 20 46 4F 55 4E 44 00
4F1E-4F2A
DEFM "BAD MODE" + 00H 42 41 44 20 4D 4F 44 45 00
4F2B-4F3C
DEFM "FILE ALREADY OPEN" + 00H 46 49 4C 45 20 41 4C 52 45 41 44 59 20 4F 50 45 4E 00
4F3D-4F3E
DEFM "?" + 00H 3F 00
4F3F-4F48
DEFM "DOS ERROR" + 00H44 4F 53 20 45 52 52 4F 52 00
4F49-4F5C
DEFM "FILE ALREADY EXISTS." + 00H46 49 4C 45 20 41 4C 52 45 41 44 59 20 45 58 49 53 54 53 00
4F5D-4F5E
DEFM "?" + 00H 3F 00
4F5F-4F60
DEFM "?" + 00H 3F 00
4F61-4F6A
DEFM "DISK FULL" + 00H44 49 53 4B 20 46 55 4C 4C 00
4F6B-4F79
DEFM "INPUT PAST END" + 00H49 4E 50 55 54 20 50 41 53 54 20 45 4E 44 00
4F7A-4F86
DEFM "BAD RECORD #" + 00H42 41 44 20 52 45 43 4F 52 44 20 23 00
4F87-4F94
DEFM "BAD FILE NAME" + 00H42 41 44 20 46 49 4C 45 20 4E 41 4D 45 00
4F95-4FA2
DEFM "MODE MISMATCH" + 00H4D 4F 44 45 20 4D 49 53 4D 41 54 43 48 00
4FA3-4FBB
DEFM "DIRECT STATEMENT IN FILE" + 00H44 49 52 45 43 54 20 53 54 41 54 45 4D 45 4E 54 20 49 4E 20 46 49 4C 45 00
4FBC-4FCA
DEFM "TOO MANY FILES" + 00H54 4F 4F 20 4D 41 4E 59 20 46 49 4C 45 53 00
4FCB-4FDF
DEFM "DISK WRITE PROTECTED" + 00H44 49 53 4B 20 57 52 49 54 45 20 50 52 4F 54 45 43 54 45 44 00
4FE0-4FF2
DEFM "FILE ACCESS DENIED" + 00H46 49 4C 45 20 41 43 43 45 53 53 20 44 45 4E 49 45 44 00
4FF3-5001
DEFM "SEQ # OVERFLOW" + 00H53 45 51 23 20 4F 56 45 52 46 4C 4F 57 00
5002-5011
DEFM "RECORD OVERFLOW" + 00H52 45 43 4F 52 44 20 4F 56 45 52 46 4C 4F 57 00
5012-5028
DEFM "ILLEGAL TO EXTEND FILE" + 00H49 4C 4C 45 47 41 4C 20 54 4F 20 45 58 54 45 4E 44 20 46 49 4C 45 00
5029-502A
DEFM "?" + 00H 3F 00
502B-5045
DEFM "PREVIOUSLY DISPLAYED ERROR" + 00H50 52 4F 47 52 41 4D 20 50 52 45 56 49 4F 55 53 4C 59 20 4C 4F 41 44 45 44 00
5046-505A
DEFM "CAN'T PROCESS LINE 0" + 00H43 41 4E 27 54 20 50 52 4F 43 45 53 53 20 4C 49 4E 45 20 30 00
505B-5068
DEFM "BAD FILE TYPE" + 00H42 41 44 20 46 49 4C 45 20 54 59 50 45 00
5069-507A
DEFM "IGEL SYNTAX ERROR" + 00H49 4C 4C 45 47 41 4C 20 53 59 4E 54 41 58 20 45 52 52 4F 52 00
507B-5091
DEFM "IGEL ITEM TAX ERROR" + 00H49 4C 4C 45 47 49 54 45 4D 20 54 41 58 20 45 52 52 4F 52 00
5092-50B6
DEFM "BAD/ILLEGAL/MISSING IGEL ITEM PREFIX " + 00H42 41 44 20 2F 49 4C 4C 45 47 41 4C 2F 4D 49 53 53 49 4E 47 20 46 49 4C 45 20 52 45 46 00
50B7-50B8
DEFM "?" + 00H3F 00
50B9-50CA
DEFM "BAD RECORD LENGTH" + 00H42 41 44 20 52 45 43 4F 52 44 20 4C 45 4E 47 54 48 00
50CB-50E1
DEFM "STATEMENT USES 2 FILE AREAS" + 00H53 54 41 54 45 4D 45 4E 54 20 53 45 51 55 45 4E 43 45 20 45 52 52 4F 52 20 41 53 00
50E2-50FC
DEFM "BAD FILE POSITIONING PARAM" + 00H42 41 44 20 46 49 4C 45 20 50 4F 53 49 54 49 4F 4E 49 4E 47 20 50 41 52 41 4D 00

50FDH - Error Table End Marker

The final null byte marking the end of the error message table. Combined with the preceding message's null terminator, this creates a double-null (00H 00H) sequence that signals the end of the table to the search algorithm.

50FD
NOP 00
Error table end marker - This 00H byte, following the last message's null terminator, creates the double-null end-of-table signal checked at 4D38H.

50FEH - RENUM Command Entry Point

This is the entry point for BASIC's RENUM (renumber) command. The RENUM command syntax is: RENUM [new][,inc][,old][,oldend][,U][,X]. This routine parses the optional parameters and validates the line number range before proceeding with the renumbering operation. Default values are: new=10, increment=10, old=0, oldend=65529.

Entry: A=4FH (from the CP 4FH / JP Z at 4D0CH). HL points to command line after RENUM token.

50FE
XOR A AF
Clear Register A to 00H. This will be used to initialize the COMPRESS work variable and option flags.
50FF
LD (4317H),A 32 17 43
Store 00H to 4317H - COMPRESS work variable. Clear any previous compression state before RENUM operation.
5102
LD BC,4D00H 01 00 4D
Load BC with 4D00H - address of the RENUM parameter storage area. BC will be used to store parsed parameters.
5105
LD A,(HL) 7E
Load Register A with the byte at (HL) - the first character after the RENUM command token.
5106
OR A B7
OR A with itself. Sets Z FLAG if the byte is 00H (end of statement - no parameters provided).
5107
If Z FLAG (no parameters), JUMP to 5130H - syntax error. RENUM requires at least end-of-line, handled differently.
5109
LD A,04H 3E 04
Load Register A with 04H - maximum number of numeric parameters to parse (new, inc, old, oldend).
510B
EX AF,AF' 08
Exchange AF with AF'. Store the parameter counter (4) in the alternate A register for safekeeping.

[PARAMETER PARSE LOOP] - Parse up to 4 numeric parameters separated by commas.

510C
DEC HL 2B
Decrement HL to back up one character (RST 10H will advance it).
510D
RST 10H D7
Call ROM routine via RST 10H - get next character, skipping spaces. Returns character in A, advances HL.
510E
If Carry set (character is a digit 0-9), JUMP to 5116H to parse the number.
5110
CP 2CH FE 2C
Compare A with , (2CH). Check if this is a comma (parameter separator with missing value).
5112
If Z FLAG (comma found), JUMP to 5126H - skip this parameter (use default) and continue.
5114
Otherwise JUMP to 5139H - check for U or X option flags.
5116
Call ROM routine 1E5AH - parse decimal number from command line. Returns value in DE.
5119
LD A,E 7B
Load Register A with the low byte of the parsed number from Register E.
511A
LD (BC),A 02
Store the low byte at (BC) - current parameter storage location.
511B
INC BC 03
Increment BC to point to the high byte storage location.
511C
LD A,D 7A
Load Register A with the high byte of the parsed number from Register D.
511D
LD (BC),A 02
Store the high byte at (BC+1).
511E
DEC BC 0B
Decrement BC back to point to the low byte (for next iteration's advance).
511F
OR E B3
OR A (high byte) with E (low byte). Sets Z if the entire parsed number was zero.
5120
If Z FLAG (number was zero), JUMP to 5130H - syntax error. Zero is not valid for RENUM parameters.
5122
DEC HL 2B
Decrement HL to back up one character.
5123
RST 10H D7
Call ROM routine via RST 10H - get next character after the number.
5124
If Z FLAG (end of statement), JUMP to 5148H - all parameters parsed, validate and execute.
5126
RST 08H CF
Call ROM routine via RST 08H - syntax check. Expects the byte following to match current character.
5127
INC L 2C
Embedded data: 2CH - This is the comma character that RST 08H checks for. (INC L is the opcode 2CH).
5128
INC BC 03
Increment BC by 1 (first of two increments to advance to next parameter slot).
5129
INC BC 03
Increment BC again. BC now points to the next 2-byte parameter storage slot.
512A
EX AF,AF' 08
Exchange AF with AF'. Retrieve the parameter counter from alternate A.
512B
DEC A 3D
Decrement the parameter counter. We've processed (or skipped) one parameter.
512C
If NZ FLAG (more parameters allowed), JUMP back to 510BH to parse the next parameter.
512E
All 4 numeric parameters parsed. JUMP to 5139H to check for U/X option flags.

Syntax error handler - invalid RENUM syntax detected.

5130
JUMP to ROM routine 1997H - ?SN ERROR (Syntax Error). Invalid RENUM command syntax.

Handle U or X flag parsing - store flag value then continue checking.

5133
LD (DE),A 12
Store the non-zero value (character code) at (DE) - the flag storage location. Non-zero means flag is set.
5134
RST 10H D7
Call ROM routine via RST 10H - get next character after the flag letter.
5135
If Z FLAG (end of statement), JUMP to 5148H - done parsing, validate and execute.
5137
RST 08H CF
Call ROM routine via RST 08H - syntax check for comma after flag.
5138
INC L 2C
Embedded data: 2CH - Comma character for RST 08H syntax check.

Check for U flag (update line references) or X flag (extended mode).

5139
LD DE,4D0AH 11 0A 4D
Load DE with 4D0AH - address of the "U" flag storage byte.
513C
LD A,(HL) 7E
Load Register A with the current character from the command line.
513D
CP 55H FE 55
Compare A with U (55H). Check for the "U" (Update references) flag.
513F
If character was U (55H from prior CP), JUMP to 5133H to store the flag at (DE) - enables "U" memory reservation mode
5141
LD DE,4D0BH 11 0B 4D
Load DE with address 4D0BH - second flag byte location for "X" parameter
5144
CP 58H FE 58
Compare A with X (58H). If equal, Zero flag is set
5146
If character was X, JUMP to 5133H to store the flag - enables "X" extended memory mode

Neither 'U' nor 'X' found - check for end of statement

5148
OR A B7
Test if A = 0 (end of line/statement terminator)
5149
If NOT zero (unexpected character), JUMP to 5130H → JP 1997H for ?SN ERROR

End of statement reached - validate memory boundaries before proceeding

514B
LD DE,(4D04H) ED 5B 04 4D
Load DE with low memory limit from 4D04H (string space bottom)
514F
LD HL,(4D06H) 2A 06 4D
Load HL with high memory limit from 4D06H (top of usable memory)
5152
RST 18H DF
Call ROM compare routine - compares HL with DE
5153
If HL < DE (high < low = invalid range), JUMP to 5130H for error
5155
EX DE,HL EB
Exchange: HL = low limit, DE = high limit
5156
LD HL,FFF9H 21 F9 FF
Load HL with -7 (FFF9H) - minimum required free space for system overhead
5159
RST 18H DF
Call ROM compare routine - check if enough space exists
515A
If insufficient space, JUMP to 5130H for error

515CH - File Buffer Chain Initialization

Initializes the file buffer chain scan. Sets up a counter and retrieves the pointer to the first file buffer from BASIC's TXTTAB pointer area. This begins the process of walking through all allocated file buffers.

515C
LD BC,0000H 01 00 00
Load BC with 0 - initialize file buffer counter to zero
515F
LD HL,(40A4H) 2A A4 40
Load HL with pointer to first file buffer from TXTTAB (40A4H) - start of buffer chain

5162H - File Buffer Chain Walk Loop

[LOOP START] Walks through the linked list of file buffers. Each buffer contains a pointer to the next buffer (or 0000H for end of chain). Counts buffers and validates each one's memory position against the CLEAR limits.

5162
LD E,(HL) 5E
Load E with low byte of next buffer pointer from (HL)
5163
INC HL 23
Increment HL to point to high byte
5164
LD D,(HL) 56
Load D with high byte of next buffer pointer - DE now = next buffer address
5165
LD A,D 7A
Load A with D (high byte)
5166
OR E B3
OR with E - test if DE = 0000H (end of chain)
5167
If Zero (DE=0, end of chain), JUMP to 51B5H to finalize buffer allocation
5169
PUSH DE D5
Save next buffer pointer on stack
516A
INC BC 03
Increment buffer count in BC

Check reserved memory flag - if set, skip memory recalculation

516B
LD A,(4D0AH) 3A 0A 4D
Load A with reserved memory flag from 4D0AH (set by CLEAR ,U parameter)
516E
OR A B7
Test if flag is non-zero
516F
If flag is set (NZ), JUMP to 51A9H - skip memory adjustment, call 5CF3H directly

5171H - Buffer Memory Validation

Validates that the current buffer's address falls within the acceptable memory range. Retrieves the buffer address from offset +2/+3 and compares against the low and high memory limits established by CLEAR parameters.

5171
PUSH HL E5
Save current buffer pointer on stack
5172
INC HL 23
Increment HL to point to buffer address field (offset +2)
5173
LD E,(HL) 5E
Load E with low byte of buffer's data address
5174
INC HL 23
Increment HL to high byte
5175
LD D,(HL) 56
Load D with high byte - DE = buffer's actual data address
5176
LD A,D 7A
Load A with D (high byte)
5177
OR E B3
OR with E - test if buffer address = 0000H
5178
LD A,96H 3E 96
Load A with error code 96H (150 decimal) - "BAD FILE DATA" error
517A
If buffer address = 0 (invalid), JUMP to 5E09H error handler

Buffer address is valid - check against memory limits

517D
LD HL,(4D04H) 2A 04 4D
Load HL with low memory limit from 4D04H
5180
EX DE,HL EB
Exchange: DE = low limit, HL = buffer address
5181
RST 18H DF
Call ROM compare - compare buffer address (HL) with low limit (DE)
5182
EX DE,HL EB
Exchange back: HL = low limit, DE = buffer address
5183
If buffer < low limit (outside range), JUMP to 51A4H - skip this buffer
5185
LD HL,(4D06H) 2A 06 4D
Load HL with high memory limit from 4D06H
5188
RST 18H DF
Call ROM compare - compare high limit (HL) with buffer address (DE)
5189
If high limit < buffer (outside range), JUMP to 51A4H - skip this buffer

518BH - Memory Pointer Adjustment

Buffer is within valid range. Updates memory tracking pointers by adding the buffer size to the current memory top. Saves backup and checks for overflow conditions.

518B
LD HL,(4D00H) 2A 00 4D
Load HL with current memory top from 4D00H
518E
LD (4D08H),HL 22 08 4D
Save current memory top to 4D08H as backup
5191
EX DE,HL EB
Exchange: HL = buffer address, DE = memory top
5192
LD HL,FFF9H 21 F9 FF
Load HL with -7 (FFF9H) - buffer overhead/minimum space check
5195
RST 18H DF
Call ROM compare - check if space available
5196
LD A,8CH 3E 8C
Load A with error code 8CH (140 decimal) - "OUT OF MEMORY" error
5198
If Carry (insufficient space), JUMP to 5E09H error handler
519B
LD HL,(4D02H) 2A 02 4D
Load HL with buffer size from 4D02H
519E
ADD HL,DE 19
Add memory top (DE) to buffer size (HL) - calculate new top
519F
LD (4D00H),HL 22 00 4D
Store new memory top at 4D00H
51A2
JR C,5196H 38 F2
If Carry (overflow), JUMP back to 5196H for OUT OF MEMORY error

51A4H - Update Buffer Pointer and Continue

Updates the buffer pointer in the chain with the new address and calls the buffer processing routine. Then pops the next buffer address and continues the chain walk loop.

51A4
POP HL E1
Restore current buffer pointer from stack
51A5
LD (HL),D 72
Store high byte of new address at (HL)
51A6
DEC HL 2B
Decrement HL to low byte position
51A7
LD (HL),E 73
Store low byte of new address at (HL)
51A8
INC HL 23
Restore HL to original position
51A9
CALL 5CF3H - buffer processing/validation routine
51AC
POP DE D1
Restore next buffer pointer from stack (saved at 5169H)
51AD
RST 18H DF
Call ROM compare - compare HL with DE
51AE
If equal (valid chain), JUMP back to 5162H [LOOP] to process next buffer
51B0
LD A,72H 3E 72
Load A with error code 72H (114 decimal) - "BAD FILE#" error
51B2
JUMP to 5E09H error handler - corrupted buffer chain

51B5H - Buffer Allocation Finalization

End of buffer chain reached. Calculates the total buffer space required by counting how many 128-byte blocks are needed. Prepares for DOS buffer initialization call.

51B5
LD H,B 60
Load H with B (high byte of buffer count)
51B6
LD L,C 69
Load L with C (low byte) - HL = total buffer count from BC
51B7
LD C,A 4F
Load C with A - preserve current A value (block counter)
51B8
LD DE,0080H 11 80 00
Load DE with 128 (80H) - standard buffer block size

[LOOP START] Calculate number of 128-byte blocks needed

51BB
INC C 0C
Increment C (block counter)
51BC
SBC HL,DE ED 52
Subtract 128 from HL (HL = HL - DE - Carry)
51BE
If Zero (exact fit), JUMP to 51C2H - done counting
51C0
If No Carry (HL >= 0), JUMP back to 51BBH [LOOP] - continue counting

51C2H - DOS Buffer Initialization Call

Sets up parameters for the DOS RST 28H call to initialize file buffers. Loads the FCB work area address, buffer size, and file buffer chain pointer, then invokes DOS function 2DH.

51C2
LD DE,64CAH 11 CA 64
Load DE with 64CAH - FCB/buffer work area address
51C5
LD B,80H 06 80
Load B with 128 (80H) - buffer size for initialization
51C7
LD HL,(40A4H) 2A A4 40
Load HL with file buffer chain head from TXTTAB (40A4H)
51CA
LD A,2DH 3E 2D
Load A with 2DH (45 decimal) - DOS function code for buffer initialization
51CC
RST 28H EF
Call DOS via RST 28H - execute buffer initialization function

51CDH - Reserved/Unused Space

NOP padding from 51CDH through 51E7H. This is unused reserved space at the end of the SYS13 overlay, likely preserved for future expansion or alignment purposes.

51CD-51E7
NOP (27 bytes) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Unused space - 27 bytes of NOPs (00H) reserved for future use or overlay alignment