How to do FOR without structured programming (retro investigation)

You can talk about almost anything that you want to on this board.

Moderator: Moderators

User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

How to do FOR without structured programming (retro investigation)

Post by aa-dav »

A couple of days ago I discovered some interesting (imho) thing about BASIC interpreter of old ZX Spectrum PC from 80-s.
What do you think will print this program:

Code: Select all

10 FOR i=1 TO 1
20 PRINT i
30 NEXT i
40 PRINT i
50 NEXT i
60 PRINT i
70 NEXT i
80 PRINT i
?
Well, it will give no error and print:

Code: Select all

1
2
3
4
Why?
Key idea is absence of structured programming blocks in this interpreter and FOR/NEXT block-like behaviour in fact are imitated by nature of 'control for-loop variable':
Image
FOR initializes it's variable as a special kind of variable (like strings or arrays are different kinds of variables too) and it keeps all parameters including line/statement for looping too!
Then NEXT is executed it strictly needs variable name to update it's value by Step and check condition of Limit. If Limit is not reached - it just GOTOs to looping line/statement!
That is FOR and NEXT are not have to be paired in any way, but just imitate structured blocks in this hidden manner. :) Special case is then FOR detects reaching of Limit in first interation - then it skips chain of operators for the next NEXT with the same looping variable. But if it is not the case - all looping job is done by NEXT i. :)
Interesting...
(I also tried to replicate this behaviour on C64 emulator, but it's BASIC seems to be smarter than that and behaves like destroing looping variable after Limit is reached. However details are unknown for me.)
zzo38
Posts: 1096
Joined: Mon Feb 07, 2011 12:46 pm

Re: How to do FOR without structured programming (retro investigation)

Post by zzo38 »

At first, I would expect an error message "NEXT without FOR" or something similar, but the output you mentioned there would be my second guess if it is not an error.
(Free Hero Mesh - FOSS puzzle game engine)
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: How to do FOR without structured programming (retro investigation)

Post by Drag »

Music engines are similar! They interpret music data just like a BASIC interpreter interprets lines, and a common way to implement a LOOP command (i.e., the equivalent to FOR/NEXT in music) is like this:
  • On song start, let LoopCount = 0.
  • When the "LOOP <label> <count>" command is encountered:
    • If LoopCount == 0, initialize LoopCount to <count>, then jump to <label>.
    • Else if LoopCount == 1, set LoopCount to 0 and don't jump.
    • Else, decrease LoopCount by 1, then jump to <label>.
There's usually only one LoopCount variable per channel, so LOOPs do not nest.

Capcom's SNES music engine has 4 different LOOP commands, so each channel can keep track of 4 separate loops.
User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

Re: How to do FOR without structured programming (retro investigation)

Post by aa-dav »

zzo38 wrote: Tue Jan 24, 2023 1:10 pm At first, I would expect an error message "NEXT without FOR" or something similar, but the output you mentioned there would be my second guess if it is not an error.
Yea, there are no many options. :)

How about this?

Code: Select all

10 FOR i=1 TO 0
20 FOR j=1 TO 5
30 NEXT i
40 PRINT i
50 IF i=1 THEN i=2: GO TO 20
60 NEXT j
:)
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: How to do FOR without structured programming (retro investigation)

Post by Oziphantom »

There is a Structured programming add on for CBM BASIC 2.0 by Waterloo, I was curious how it did it. Turns out it modifies the BASIC program as it runs to achieve the loop points etc
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How to do FOR without structured programming (retro investigation)

Post by Pokun »

Just tried it on MSX (in the openMSX emulator): "NEXT without FOR in 50" as expected.
I guess Family BASIC and most other interpreters would return a similar error.
User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

Re: How to do FOR without structured programming (retro investigation)

Post by aa-dav »

Pokun wrote: Thu Jan 26, 2023 12:22 pm Just tried it on MSX (in the openMSX emulator): "NEXT without FOR in 50" as expected.
I guess Family BASIC and most other interpreters would return a similar error.
I tried C64 Basic with the same result. MSX and C64 have Microsoft Basic. So, Microsoft implementation is smarter than that.
Indeed ZX Specrum BASIC is not derivation from Microsoft Basic (and this is rare thing).
P.S.
It always wonders me then I realized that Microsoft always was dominator on the market of PC OSes. It was so from the very start because MS Basic burned in ROM of 8-bit machines was some kind of OS in that times. LOAD "" 8,,1 - it's like DOS command to load and execute program. :)
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: How to do FOR without structured programming (retro investigation)

Post by Oziphantom »

try BBC basic, the BBCers love to brag about their fantastic BASIC. I also think the CPC's BASIC is unique.
User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

Re: How to do FOR without structured programming (retro investigation)

Post by aa-dav »

Oziphantom wrote: Fri Jan 27, 2023 3:10 am try BBC basic
But... it WAS STRUCTURED from the start.
It's not one of them who are discussed there.
Am I wrong?
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: How to do FOR without structured programming (retro investigation)

Post by Oziphantom »

kind of. It does have REPEAT UNTIL and you can define PROCEDURES but it also still has line numbers and GOTO and GOSUB with line numbers.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How to do FOR without structured programming (retro investigation)

Post by Pokun »

Yeah BASIC is one of the things that makes these 8-bit computers beautiful. It's a programming language and simple IDE that doubles as a simple OS, it allows a layman to program his own computer however he wants provided that it comes with a good manual to teach him it. These 8-bit computers themselves are also simple enough for a layman to understand how they work.


Anyway I tried the code in Family BASIC V3 (using Mesen) for fun. It uses NS-HuBASIC v3.0 which is programmed by Hudson for Sharp and Nintendo, but it's designed to be largely compatible with Microsoft's BASIC dialect and is similarly capable so I expect similar errors.
But surprisingly I got "?SN ERROR IN 30" which is syntax error. It turns out that the NEXT command in NS-HuBASIC doesn't expect any arguments. I removed the argument from all NEXT lines and this time I got the expected output:

Code: Select all

  1
  2
  ?NF ERROR IN 50
NF-error is of course the "NEXT without FOR error".
User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

Re: How to do FOR without structured programming (retro investigation)

Post by aa-dav »

Oziphantom wrote: Fri Jan 27, 2023 7:16 am kind of. It does have REPEAT UNTIL and you can define PROCEDURES but it also still has line numbers and GOTO and GOSUB with line numbers.
As far as I know BBC Basic is real beast.
It has DEF PROC defining named procedures instead of silly GO SUB.
It even has inline assembly! :D That is amazing!
User avatar
aa-dav
Posts: 220
Joined: Tue Apr 14, 2020 9:45 pm
Location: Russia

Re: How to do FOR without structured programming (retro investigation)

Post by aa-dav »

Pokun wrote: Fri Jan 27, 2023 2:51 pm ...
It's interesting that Famicom Basic supress variable name in NEXT statemant... It's a real step forward structured programming with nested blocks.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How to do FOR without structured programming (retro investigation)

Post by Pokun »

It should keep track of them anyway so nested FOR...NEXT-loop blocks are still possible.

Family BASIC doesn't have an assembler, but you can at least call subroutines you have written in machine code (usually by POKEing the hand-assembled bytes directly to RAM) with the CALL command, it's pretty fun.
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: How to do FOR without structured programming (retro investigation)

Post by Drag »

It still nests, it's just that "NEXT" would always refer to the most recently encountered "FOR", implying that they're implemented with a stack, which forces nesting to work correctly and prevents you from interleaving (i.e., FOR A, FOR B, NEXT A, NEXT B) like the ZX allows you to do.
Post Reply