Back To BASIC: Kemeny & Kurtz's book from 1985

I only just realised how rare pass-by-reference is in modern high-level languages. I knew python doesn’t have it, but I don’t think Lua and JavaScript do either (which I put in a similar mental category).

I wonder if there’s much BASIC code that uses it.

I thought of it as a fairly standard thing since I used it in Pascal, and then when I learnt C it had pointers which almost let you do the same thing.

1 Like

A few years ago, I was working on a BASIC cross-compiler and came upon this very issue.

Discussion here:

https://6502.org/forum/viewtopic.php?f=2&t=6182

and

https://talk.dallasmakerspace.org/t/a-twisted-question-about-basic/72013

2 Likes

And yet, the loop is still executed once by Atari BASIC.

1 Like

Thanks for that link, in which I learned about https://telehack.com with its purported “Dartmouth DTSS TeleBASIC”. Sadly, it’s nothing to do with Dartmouth BASIC and would have the late Professors K most annoyed. While it’s got lots of extra features, it’s missing all of MAT and it does something with FOR loops that the Dartmouth manual says they should never do. Oh well.

1 Like

Yep. I could be wrong, but I’m pretty sure FOR loops in BASIC (according to spec?) were always supposed to execute at least once.

See, that’s the thing: they’re absolutely not. But due to lack of standards and poor documentation, it mostly got ignored by the micro BASICs. Until the first ANSI BASIC standard came out in 1978, this one paragraph from the 1968 Dartmouth manual was about it:

If you write 50 FOR Z = 2 TO -2, without a negative step size, the body of the loop will not be performed and the computer will proceed to the statement immediately following the corresponding NEXT statement.

3 Likes

This feels to me like one of the very biggest distinguishers between Basics. And we’re told I think that Sinclair’s Basic for the Spectrum does honour the zero iteration FOR loop. So it’s not even as simple as all micro Basics.

3 Likes

Was this FOR loop behavior defined in it?

It looks like this is the answer (from Duck.ai) although it doesn’t say when this was specified.

The ANSI BASIC standard specifies that a FOR NEXT loop will execute at least once, regardless of whether its condition is met. This means that the loop’s body will run even if the initial condition does not allow for any iterations.

This note is for everyone:

Please: It’s not good to forward AI content in this way. If you don’t know, you can look things up, or wait for someone who knows.

This forum is a place for people to share their enthusiasm and their experience. Please bring your authentic contributions.

6 Likes

Page 18 of https://ecma-international.org/wp-content/uploads/ECMA-55_1st_edition_january_1978.pdf seems to indicate that if the starting value is past the end value, the loop will not be executed at all.

1 Like

On the FOR loop conundrum… (As someone who’s written a coupe of BASICs)
The original BASIC, as I understand it; is a compiled BASIC. (Although I’m not sure what it compiled into) So maybe easier to ‘fix’ the FOR issue. Or at least detect it.
Simple FOR/NEXT counting doesn’t work as Basic makes it too easy to:

FOR I = 1 TO 10
  IF I = 5 THEN NEXT I
  PRINT I
NEXT I
99 END

One of my Basics uses the construct CYCLE… REPEAT for all loops including FOR but even that can be thwarted as above.

So I don’t think there is an easy solution.

I do provide CONTINUE and BREAK statement for the other loops, but even that relies on the programmer being kind to the system and not having an extra REPEAT buried inside an IF statement. If you do that, then the predictable happens, but it’s not always going to be obvious.

I could scan the tokenised program at RUN time, looking for mismatched CYCLE… REPEATS but on an 8-bit micro counting and matching FOR/NEXT is going to take a little bit of time and precious ROM space - another reason to be “lazy” about it…

Could I add this check into my RTB Basic? Sure - it would be quick (I already do a scan at RUN time to build up a table of procedures & functions and goto/gosub’s - it wouldn’t be hard, but then what do I do when someone comes along and says; “This program I wrote 10 years ago now doesn’t work” …

So maybe we just accept it as it is and work with it.

-Gordon

1 Like

Agreed. Sorry about that.

1 Like

Yeah, I’ve been studying this over the last few weeks. The behaviour is all over the shop with micro BASICs. I’d meant to have a writeup by now. Maybe I’ll manage something from the RV at VCF East.

I tested 38 interpreters and one compiler (though it’s hard to tell if the DEC products were compilers or interpreters, so maybe 3-4 compilers) with release dates from 1970–1992. Every one of them from before the micro age did loops correctly. From about 1975–1984, however, it was the Wild West. After 1984, new BASICs did the right thing.

There was one under-reported event that spurred BASICs to change in the early 1980s. It wasn’t the release of a standard. It was a much more commercial issue.

What I find interesting is how BASICs from the same supplier changed. In the case of Microsoft, you can clearly see several different development/product lines, but regressions being introduced in one product that had long been fixed in others.

1 Like

Sounds interesting! A teaser…

2 Likes

I think, this will always be hard for any micro-BASIC with limited resources. Even Commodore BASIC, which allows a GOTO out of the loop, will leave a partially occupied stack behind. (This will be cleaned up, though, if there is a new FOR-loop with the given loop variable. So, if you jump out of any loop, you may want to do an empty FOR-loop on the outer-most loop variable after this.)

Do we know if any of the micro-BASICs which do allow zero-iteration loops do clean up their stack reliably?

3 Likes

I didn’t meant to leave this for so long, but I finally wrote it up:

The Broken BASIC Years – We Saw a Chicken …

Summary:

  • original BASIC didn’t execute FOR loops if the end value was less than the start value;
  • micro BASICs, starting around 1975, changed this behaviour: perhaps due to lack of RAM, or copying it from FORTRAN-66’s DO loop;
  • Microsoft BASIC-80 version 5.0 reverts to standard behaviour. A FIPS norm of late 1980 specified that all BASICs used by US Government agencies had to comply with the ANSI standard. So no government $ for Microsoft unless they complied;
  • Other micro BASICs used look-aheads to find matching NEXT statements, so didn’t always execute FOR loops. See page 96 of the Spectrum ROM disassembly for the implementation in Sinclair BASIC
  • If only Acorn had read the outline specification for the BBC Micro, they would have seen the requirement for syntax “… identical to Microsoft BASIC 5.0 (repeat 5.0) …”;
  • apart from this one time, BASIC standards counted for very little;
  • if you must, you can find all 200+ programs from the NBS test suite that backs the ANSI/FIPS standard here: bas55/tests at master · jorgicor/bas55 .

So many systems were emulated in the making of this post …

6 Likes

Very interesting indeed! My internal expectations are very much aligned to the earlier MS Basics and to BBC Basic as shipped - I’m surprised to see so many examples go the other way. But your explanation is solid.

BBC Basic fell far short of identical syntax to MS Basic 5.0! Arguably in this case it also fell short of identical semantics… still, of course, I’m fond of what I know.

Perhaps most remarkable is the change in Sinclair Basic, from the ZX80 following the microcomputer compromise, to the ZX81 changing track to the more sophisticated approach. (I was, again, surprised to see that difference, even in discussions about Basics for the present-day Spectrum Next.) As noted over on the fediverse:

It would be interesting to learn more about the change from ZX80 to ZX81 Basic - Steve Vickers was newly joined to Nine Tiles for the latter. It would also be interesting to see how much difference it makes to the interpreter to handle FOR loops in the more sophisticated way: it does seem to mandate a forward search.

(A tiny bit of relevant info at Sinclair BASIC history)

And as you replied:

@EdS tho Sinclair BASIC could never have been described as fast in any version.

p96 of the Spectrum ROM disassembly suggest a forward search
The Complete Spectrum ROM Disassembly

Also notable, I think, that Amstrad, by way of Locomotive Basic, also did the sophisticated thing for the CPC.

And finally, I’d want to highlight this note of yours in the article:

It’s also interesting to note that Microsoft didn’t change all of their BASIC interpreters after 1980. Sure, the important (as in, $$$ for Microsoft) ones like IBM PC BASIC got it, but the later MSX BASIC didn’t. MSX wasn’t a low-effort port, though: it had a new graphics system and a completely different decimal floating point package.

2 Likes

It surprises and delights me how polarized this tiny detail of home computer history can be. I grew up with Locomotive BASIC (itself a quick hack on Mallard BASIC for CP/M, familiar to BBC Z80 Second Processor users), and at first I was utterly mystified that other interpreters were “doing it wrong”.

There are BASICs that have completely unexpected behaviour, such as PDP-8 BASIC (which sets the unvisited loop variable to 0) and QL SuperBASIC, which seems to undefine the variable and gets very grumpy indeed if you ask about it later. I haven’t looked into SuperBASIC further: residual trauma from the Strathclyde QL-per-student days, I suspect.

I was hoping to find a more coherent progression in Microsoft’s interpreters, but found nothing beyond “yes, we can license you this for $$”. Developed from a common 8080 core they may have been, but MSX and Tandy 100 seem to have been shunted off onto their own decimal branch, while the Tandy/Dragon(/Thomson/Matra) 6809 code is on a track all its own.

Decimal floating point as an option (OPTION ARITHMETIC DECIMAL) is part of the Full BASIC standard, so the idea was not unheard of. The standard also proposed OPTION ANGLE DEGREES, which was perhaps less of a good idea. At least one of the Eastern European computers’ BASICs allowed switching to degrees too.

2 Likes

In my own Basic, I supported the RAD and DEG commands, but in my infinite wisdom decided to support CLOCK angles. You know; a quarter turn is 15 minutes on the clock, half a turn is 30 minutes and so on.

This was used on the Big Track toy tank in the early 80s so it seemed to make sense at the time and I was into Turtle graphics which is effectively what the Big Track was, so …

Until I found out that many millennials and a large number of Zoomers can’t tell the time on a round clock.

Bother.

-Gordon

1 Like