1010; ************************************************************************************************
1111; ************************************************************************************************
1212;
13- ; +16 Step (1 or 255 )
14- ; +12..+15 Terminal value (( in 2's complement format.)
13+ ; +16..+19 Step value (in 2's complement format. )
14+ ; +12..+15 Terminal value (in 2's complement format.)
1515; +8..+11 Value of index variable (in 2's complement format.)
1616; +6..+7 Address of index variable
1717; +1..5 Loop back address
2727; ************************************************************************************************
2828
2929ForCommand: ;; [for]
30- lda #STK_FOR+9 ; allocate 18 bytes on the return stack (see above).
31- jsr StackOpen
30+ lda #STK_FOR+11 ; allocate 22 bytes on the return stack (see above).
31+ jsr StackOpen
3232 ;
3333 ; Get an integer reference to Stack[0] - this is the loop variable.
3434 ;
@@ -40,19 +40,19 @@ ForCommand: ;; [for]
4040 ;
4141 ; = character
4242 ;
43- lda #KWD_EQUAL ; =
43+ lda #KWD_EQUAL ; =
4444 jsr CheckNextA
4545 ;
4646 ; The Initial value to Stack[1]
4747 ;
4848 inx
4949 jsr EvaluateInteger ; <from> in +1
5050 ;
51- ; TO or DOWNTO put on stack
51+ ; Save TO or DOWNTO in temporary memory
5252 ;
5353 .cget ; next should be DOWNTO or TO
54+ pha ; save keyword for later
5455 iny ; consume it
55- pha ; save on stack for later
5656 cmp #KWD_DOWNTO
5757 beq _FCNoSyntax
5858 cmp #KWD_TO
@@ -62,23 +62,28 @@ _FCNoSyntax:
6262 ; The Terminal value to Stack[2]
6363 ;
6464 inx
65- jsr EvaluateInteger
65+ jsr EvaluateInteger
6666 ;
67- ; Now set up the FOR Structure, starting with the code position
68- ;
69- jsr STKSaveCodePosition ; save loop back position
70- ;
71- ; Now the TO or DOWNTO
67+ ; Now set the +1 or -1 default step for the TO or DOWNTO
7268 ;
7369 pla ; restore DOWNTO or TO
74- phy ; save Y on the stack
7570 eor #KWD_DOWNTO ; 0 if DOWNTO, #0 if TO
7671 beq _FCNotDownTo
77- lda #2
72+ lda #2
7873_FCNotDownTo: ; 0 if DOWNTO 2 if TO
79- dec a ; 255 if DOWNTO, 1 if TO
74+ phy ; save current position
8075 ldy #16
81- sta (basicStack),y ; copy that out to the Basic Stack.
76+ dec a ; 255 if DOWNTO, 1 if TO
77+ sta (basicStack),y ; store low byte of step
78+ bmi _FCNegativeStep
79+ lda #0 ; next bytes are 0 for a step of 1
80+ _FCNegativeStep:
81+ iny
82+ sta (basicStack),y ; store rest of step in Basic Stack
83+ iny
84+ sta (basicStack),y
85+ iny
86+ sta (basicStack),y
8287 ;
8388 ; Copy the reference where the index goes.
8489 ;
@@ -98,11 +103,31 @@ _FCNotDownTo: ; 0 if DOWNTO 2 if TO
98103 ldx #2
99104 jsr FCIntegerToStack
100105 ;
101- ; Now copy the current value to the index reference, in standard format.
106+ ; Handle optional STEP value
102107 ;
103- jsr CopyIndexToReference
104108 ply ; restore position
105- rts
109+ .cget ; check for optional STEP keyword
110+ cmp #KWD_STEP
111+ bne _FCNoStep
112+ iny ; consume STEP
113+ ;
114+ ldx #0
115+ jsr EvaluateInteger ; get the step value
116+ ;
117+ phy ; save the new position
118+ ldy #16 ; set the step value
119+ ldx #0
120+ jsr FCIntegerToStack
121+ ply ; restore position
122+ _FCNoStep:
123+ ;
124+ ; Now set up the FOR Structure, starting with the code position
125+ ;
126+ jsr STKSaveCodePosition ; save loop back position
127+ ;
128+ ; Now copy the current value to the index reference, in standard format.
129+ ;
130+ jmp CopyIndexToReference
106131
107132_FCError:
108133 jmp TypeError
@@ -121,7 +146,7 @@ FCIntegerToStack:
121146 jsr NSMNegateMantissa ; if so 2's complement the mantissa
122147_FCNotNegative:
123148 lda NSMantissa0,x ; copy out to the basic stack
124- sta (basicStack),y
149+ sta (basicStack),y
125150 iny
126151 lda NSMantissa1,x
127152 sta (basicStack),y
@@ -141,7 +166,7 @@ _FCNotNegative:
141166
142167CopyIndexToReference:
143168 phy
144- ;
169+ ;
145170 ldy #6 ; copy address-8 to write to zTemp0
146171 sec ; (because we copy from offset 8)
147172 lda (basicStack),y
@@ -159,7 +184,7 @@ CopyIndexToReference:
159184 asl a ; into carry
160185
161186 ldy #8 ; where to copy from.
162- bcc _CITRNormal
187+ bcc _CITRNormal
163188 ;
164189 ; Copy out -ve
165190 ;
@@ -169,8 +194,8 @@ _CITRNegative: ; copy and negate simultaneously.
169194 sbc (basicStack),y
170195 sta (zTemp0),y
171196 iny
172- dex
173- bne _CITRNegative
197+ dex
198+ bne _CITRNegative
174199 dey ; look at MSB of mantissa
175200
176201 lda (zTemp0),y ; set the MSB as negative packed.
@@ -185,7 +210,7 @@ _CITRNormal:
185210 lda (basicStack),y ; copy without negation.
186211 sta (zTemp0),y
187212 iny
188- dex
213+ dex
189214 bne _CITRNormal
190215 ply ; and exit.
191216 rts
@@ -197,29 +222,35 @@ _CITRNormal:
197222; ************************************************************************************************
198223
199224NextCommand: ;; [next]
200- lda #STK_FOR+9 ; check FOR is TOS
225+ lda #STK_FOR+11 ; check FOR is TOS
201226 ldx #ERRID_FOR ; this error
202- jsr StackCheckFrame
227+ jsr StackCheckFrame
203228
204229 phy
205- ldy #16 ; get the step count
206- lda (basicStack),y
207- sta zTemp0 ; this is the sign extend
208- bmi _NCStepNeg
209- stz zTemp0 ; which is 0 or 255
210- _NCStepNeg:
230+ ;
231+ ; Set up a pointer to step value (basicStack+16) via zTemp1
232+ ; We use (zTemp1),y with the same y offsets as (basicStack),y
233+ ; so that index[y] + step[y] works (both at y=8..11, offset by 8)
234+ ;
235+ lda basicStack ; zTemp1 = basicStack + 8
236+ clc
237+ adc #8
238+ sta zTemp1
239+ lda basicStack+1
240+ adc #0
241+ sta zTemp1+1
211242 ;
212243 ; Bump the index, and update the index variable
213244 ;
214245 ldy #8 ; offset to bump
215- ldx #4 ; count to bump
246+ ldx #4 ; four bytes to add
216247 clc
217248_NCBump:
218- adc (basicStack),y ; add it
249+ lda (basicStack),y ; get index
250+ adc (zTemp1),y ; add step
219251 sta (basicStack),y
220- lda zTemp0 ; get sign extend for next time.
221252 iny ; next byte
222- dex ; do four times
253+ dex ; are we done yet?
223254 bne _NCBump
224255 jsr CopyIndexToReference ; copy it to the reference variable.
225256 ;
@@ -228,7 +259,7 @@ _NCBump:
228259 ; if TO , exit if terminal < index (e.g. 10 < 11)
229260 ; if DOWNTO, exit if index < terminal (e.g. -3 < -2)
230261 ;
231- ldy #16 ; get step count again
262+ ldy #19 ; get MSB of step value
232263 lda (basicStack),y
233264 asl a ; sign bit to carry
234265 ;
@@ -237,13 +268,13 @@ _NCBump:
237268 bcc _NCCompRev ; use if step is +ve
238269 lda #8 ; now the LHS = index value
239270_NCCompRev:
240- sta zTemp1 ; so zTemp0 is the index for LHS
241- eor #(8 ^12 ) ; and zTemp0 +1 is the index for RHS
271+ sta zTemp1 ; so zTemp1 is the index for LHS
272+ eor #(8 ^12 ) ; and zTemp1 +1 is the index for RHS
242273 sta zTemp1+1
243274 ldx #4 ; bytes to compare
244275 sec
245276
246- _NCCompare:
277+ _NCCompare:
247278 ldy zTemp1 ; do compare using the two indices
248279 lda (basicStack),y
249280 ldy zTemp1+1
@@ -261,12 +292,10 @@ _NCNoOverflow:
261292 asl a ; is bit 7 set.
262293 bcc _NCLoopBack ; if no , >= so loop back
263294 ;
264- jsr StackClose ; exit the loop
265- rts
295+ jmp StackClose ; exit the loop
266296
267297_NCLoopBack:
268- jsr STKLoadCodePosition ; loop back
269- rts
298+ jmp STKLoadCodePosition ; loop back
270299
271300 .send code
272301
@@ -278,5 +307,6 @@ _NCLoopBack:
278307;
279308; Date Notes
280309; ==== =====
310+ ; 01/03/26 Added support for optional use of STEP (from Kevin Cozens' patch)
281311;
282312; ************************************************************************************************
0 commit comments