Prev: 1CF0 Up: Map Next: 1D86
1D03: 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 1D03 CP $CD Jump forward unless a 'STEP' is given.
1D05 JR NZ,F_USE_1
1D07 RST $20 Advance CH-ADD and fetch the value of the STEP.
1D08 CALL CLASS_06
1D0B CALL CHECK_END Move on to the next statement if checking syntax; otherwise jump forward.
1D0E JR F_REORDER
There has not been a STEP supplied so the value '1' is to be used.
F_USE_1 1D10 CALL CHECK_END Move on to the next statement if checking syntax.
1D13 RST $28 Otherwise use the calculator to place a '1' on the calculator stack.
1D14 DEFB $A1 stk_one
1D15 DEFB $38 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 1D16 RST $28 v, l, s
1D17 DEFB $C0 st_mem_0: v, l, s (mem-0=s)
1D18 DEFB $02 delete: v, l
1D19 DEFB $01 exchange: l, v
1D1A DEFB $E0 get_mem_0: l, v, s
1D1B DEFB $01 exchange: l, s, v
1D1C DEFB $38 end_calc
A FOR control variable is now established and treated as a temporary calculator memory area.
1D1D CALL LET The variable is found, or created if needed (v is used).
1D20 LD ($5C68),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.
1D23 DEC HL Fetch the variable's single character name.
1D24 LD A,(HL)
1D25 SET 7,(HL) Ensure bit 7 of the name is set.
1D27 LD BC,$0006 It will have six locations at least.
1D2A ADD HL,BC Make HL point after them.
1D2B RLCA Rotate the name and jump if it was already a FOR variable.
1D2C JR C,F_L_S
1D2E LD C,$0D Otherwise create thirteen more locations.
1D30 CALL MAKE_ROOM
1D33 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 1D34 PUSH HL The pointer is saved.
1D35 RST $28 l, s
1D36 DEFB $02 delete: l
1D37 DEFB $02 delete: -
1D38 DEFB $38 end_calc: DE still points to 'l'
1D39 POP HL The pointer is restored and both pointers exchanged.
1D3A EX DE,HL
1D3B LD C,$0A The ten bytes of the LIMIT and the STEP are moved.
1D3D LDIR
The looping line number and statement number are now entered.
1D3F LD HL,($5C45) The current line number (PPC).
1D42 EX DE,HL Exchange the registers before adding the line number to the FOR control variable.
1D43 LD (HL),E
1D44 INC HL
1D45 LD (HL),D
1D46 LD D,(IY+$0D) The looping statement is always the next statement whether it exists or not (increment SUBPPC).
1D49 INC D
1D4A INC HL
1D4B 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.
1D4C CALL NEXT_LOOP Is a 'pass' possible?
1D4F RET NC Return now if it is.
1D50 LD B,(IY+$38) Fetch the variable's name from STRLEN.
1D53 LD HL,($5C45) Copy the present line number (PPC) to NEWPPC.
1D56 LD ($5C42),HL
1D59 LD A,($5C47) Fetch the current statement number (SUBPPC) and two's complement it.
1D5C NEG
1D5E LD D,A Transfer the result to the D register.
1D5F LD HL,($5C5D) Fetch the current value of CH-ADD.
1D62 LD E,$F3 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 1D64 PUSH BC Save the variable's name.
1D65 LD BC,($5C55) Fetch the current value of NXTLIN.
1D69 CALL LOOK_PROG The program area is now searched and BC will change with each new line examined.
1D6C LD ($5C55),BC Upon return save the pointer at NXTLIN.
1D70 POP BC Restore the variable's name.
1D71 JR C,REPORT_I If there are no further NEXTs then give an error.
1D73 RST $20 Advance past the NEXT that was found.
1D74 OR $20 Allow for upper and lower case letters before the new variable name is tested.
1D76 CP B
1D77 JR Z,F_FOUND Jump forward if it matches.
1D79 RST $20 Advance CH-ADD again and jump back if not the correct variable.
1D7A 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 1D7C RST $20 Advance CH-ADD.
1D7D LD A,$01 The statement counter in the D register counted statements back from zero so it has to be subtracted from '1'.
1D7F SUB D
1D80 LD ($5C44),A The result is stored in NSPPC.
1D83 RET Now return - to STMT_RET.
Report I - FOR without NEXT.
REPORT_I 1D84 RST $08 Call the error handling routine.
1D85 DEFB $11
Prev: 1CF0 Up: Map Next: 1D86