| Routines

07427: THE 'FOR' COMMAND ROUTINE

The address of this routine is found in the parameter table. This command routine is entered with the VALUE and the LIMIT of the FOR statement already on the top of the calculator stack.
Input
A | Code of the next character in the statement
FOR | 07427 | CP 205 | Jump forward unless a 'STEP' is given.
| 07429 | JR NZ,F_USE_1
| 07431 | RST 32 | Advance CH-ADD and fetch the value of the STEP.
| 07432 | CALL CLASS_06
| 07435 | CALL CHECK_END | Move on to the next statement if checking syntax; otherwise jump forward.
| 07438 | JR F_REORDER
There has not been a STEP supplied so the value '1' is to be used.
F_USE_1 | 07440 | CALL CHECK_END | Move on to the next statement if checking syntax.
| 07443 | RST 40 | Otherwise use the calculator to place a '1' on the calculator stack.
| 07444 | DEFB 161 | stk_one
| 07445 | DEFB 56 | end_calc
The three values on the calculator stack are the VALUE (v), the LIMIT (l) and the STEP (s). These values now have to be manipulated.
F_REORDER | 07446 | RST 40 | v, l, s
| 07447 | DEFB 192 | st_mem_0: v, l, s (mem-0=s)
| 07448 | DEFB 2 | delete: v, l
| 07449 | DEFB 1 | exchange: l, v
| 07450 | DEFB 224 | get_mem_0: l, v, s
| 07451 | DEFB 1 | exchange: l, s, v
| 07452 | DEFB 56 | end_calc
A FOR control variable is now established and treated as a temporary calculator memory area.
| 07453 | CALL LET | The variable is found, or created if needed (v is used).
| 07456 | LD (23656),HL | Make it a 'memory area' by setting MEM.
The variable that has been found may be a simple numeric variable using only six locations in which case it will need extending.
| 07459 | DEC HL | Fetch the variable's single character name.
| 07460 | LD A,(HL)
| 07461 | SET 7,(HL) | Ensure bit 7 of the name is set.
| 07463 | LD BC,6 | It will have six locations at least.
| 07466 | ADD HL,BC | Make HL point after them.
| 07467 | RLCA | Rotate the name and jump if it was already a FOR variable.
| 07468 | JR C,F_L_S
| 07470 | LD C,13 | Otherwise create thirteen more locations.
| 07472 | CALL MAKE_ROOM
| 07475 | INC HL | Again make HL point to the LIMIT position.
The initial values for the LIMIT and the STEP are now added.
F_L_S | 07476 | PUSH HL | The pointer is saved.
| 07477 | RST 40 | l, s
| 07478 | DEFB 2 | delete: l
| 07479 | DEFB 2 | delete: -
| 07480 | DEFB 56 | end_calc: DE still points to 'l'
| 07481 | POP HL | The pointer is restored and both pointers exchanged.
| 07482 | EX DE,HL
| 07483 | LD C,10 | The ten bytes of the LIMIT and the STEP are moved.
| 07485 | LDIR
The looping line number and statement number are now entered.
| 07487 | LD HL,(23621) | The current line number (PPC).
| 07490 | EX DE,HL | Exchange the registers before adding the line number to the FOR control variable.
| 07491 | LD (HL),E
| 07492 | INC HL
| 07493 | LD (HL),D
| 07494 | LD D,(IY+13) | The looping statement is always the next statement whether it exists or not (increment SUBPPC).
| 07497 | INC D
| 07498 | INC HL
| 07499 | LD (HL),D
The NEXT_LOOP subroutine is called to test the possibility of a 'pass' and a return is made if one is possible; otherwise the statement after for FOR - NEXT loop has to be identified.
| 07500 | CALL NEXT_LOOP | Is a 'pass' possible?
| 07503 | RET NC | Return now if it is.
| 07504 | LD B,(IY+56) | Fetch the variable's name from STRLEN.
| 07507 | LD HL,(23621) | Copy the present line number (PPC) to NEWPPC.
| 07510 | LD (23618),HL
| 07513 | LD A,(23623) | Fetch the current statement number (SUBPPC) and two's complement it.
| 07516 | NEG
| 07518 | LD D,A | Transfer the result to the D register.
| 07519 | LD HL,(23645) | Fetch the current value of CH-ADD.
| 07522 | LD E,243 | The search will be for 'NEXT'.
Now a search is made in the program area, from the present point onwards, for the first occurrence of NEXT followed by the correct variable.
F_LOOP | 07524 | PUSH BC | Save the variable's name.
| 07525 | LD BC,(23637) | Fetch the current value of NXTLIN.
| 07529 | CALL LOOK_PROG | The program area is now searched and BC will change with each new line examined.
| 07532 | LD (23637),BC | Upon return save the pointer at NXTLIN.
| 07536 | POP BC | Restore the variable's name.
| 07537 | JR C,REPORT_I | If there are no further NEXTs then give an error.
| 07539 | RST 32 | Advance past the NEXT that was found.
| 07540 | OR 32 | Allow for upper and lower case letters before the new variable name is tested.
| 07542 | CP B
| 07543 | JR Z,F_FOUND | Jump forward if it matches.
| 07545 | RST 32 | Advance CH-ADD again and jump back if not the correct variable.
| 07546 | JR F_LOOP
NEWPPC holds the line number of the line in which the correct NEXT was found. Now the statement number has to be found and stored in NSPPC.
F_FOUND | 07548 | RST 32 | Advance CH-ADD.
| 07549 | LD A,1 | The statement counter in the D register counted statements back from zero so it has to be subtracted from '1'.
| 07551 | SUB D
| 07552 | LD (23620),A | The result is stored in NSPPC.
| 07555 | RET | Now return - to STMT_RET.
Report I - FOR without NEXT.
REPORT_I | 07556 | RST 8 | Call the error handling routine.
| 07557 | DEFB 17