| Routines

155D: THE 'MAIN-ADD' SUBROUTINE

Used by the routine at MAIN_EXEC. This subroutine allows for a new BASIC line to be added to the existing BASIC program in the program area. If a line has both an old and a new version then the old one is 'reclaimed'. A new line that consists of only a line number does not go into the program area.
Input
BC | BASIC line number
MAIN_ADD | 155D | LD ($5C49),BC | Make the new line number the 'current line' (E-PPC).
| 1561 | LD HL,($5C5D) | Fetch CH-ADD and save the address in DE.
| 1564 | EX DE,HL
| 1565 | LD HL,$1555 | Push the address of REPORT_G on to the machine stack. ERR-SP will now point to REPORT_G.
| 1568 | PUSH HL
| 1569 | LD HL,($5C61) | Fetch WORKSP.
| 156C | SCF | Find the length of the line from after the line number to the 'carriage return' character inclusively.
| 156D | SBC HL,DE
| 156F | PUSH HL | Save the length.
| 1570 | LD H,B | Move the line number to the HL register pair.
| 1571 | LD L,C
| 1572 | CALL LINE_ADDR | Is there an existing line with this number?
| 1575 | JR NZ,MAIN_ADD1 | Jump if there was not.
| 1577 | CALL NEXT_ONE | Find the length of the 'old' line and reclaim it.
| 157A | CALL RECLAIM_2
MAIN_ADD1 | 157D | POP BC | Fetch the length of the 'new' line and jump forward if it is only a 'line number and a carriage return'.
| 157E | LD A,C
| 157F | DEC A
| 1580 | OR B
| 1581 | JR Z,MAIN_ADD2
| 1583 | PUSH BC | Save the length.
| 1584 | INC BC | Four extra locations will be needed, i.e. two for the number and two for the length.
| 1585 | INC BC
| 1586 | INC BC
| 1587 | INC BC
| 1588 | DEC HL | Make HL point to the location before the 'destination'.
| 1589 | LD DE,($5C53) | Save the current value of PROG to avoid corruption when adding a first line.
| 158D | PUSH DE
| 158E | CALL MAKE_ROOM | Space for the new line is created.
| 1591 | POP HL | The old value of PROG is fetched and restored.
| 1592 | LD ($5C53),HL
| 1595 | POP BC | A copy of the line length (without parameters) is taken.
| 1596 | PUSH BC
| 1597 | INC DE | Make DE point to the end location of the new area and HL to the 'carriage return' character of the new line in the editing area (WORKSP-2).
| 1598 | LD HL,($5C61)
| 159B | DEC HL
| 159C | DEC HL
| 159D | LDDR | Now copy over the line.
| 159F | LD HL,($5C49) | Fetch the line's number (E_PPC).
| 15A2 | EX DE,HL | Destination into HL and number into DE.
| 15A3 | POP BC | Fetch the new line's length.
| 15A4 | LD (HL),B | The high length byte.
| 15A5 | DEC HL
| 15A6 | LD (HL),C | The low length byte.
| 15A7 | DEC HL
| 15A8 | LD (HL),E | The low line number byte.
| 15A9 | DEC HL
| 15AA | LD (HL),D | The high line number byte.
MAIN_ADD2 | 15AB | POP AF | Drop the address of REPORT_G.
| 15AC | JP MAIN_EXEC | Jump back and this time do produce an automatic listing.