11

An interesting question has come up about how HP Time-Share BASIC handles this code:

10 PRINT "HOW MANY STARS DO YOU WANT";
20 INPUT N
30 FOR J = 1 TO N
40 PRINT "* ";
50 NEXT J
60 END

ANSI/ECMA will print no asterisks if you enter zero at the input. MS will print one because it's bottom tested.

Does anyone have a way to run this under HP?

scruss
  • 21,585
  • 1
  • 45
  • 113
Maury Markowitz
  • 19,803
  • 1
  • 47
  • 138
  • 1
    There are simh 3.x simulators for the HP 21xx series, but I presume you want something that's less work? – John Dallman Apr 23 '22 at 13:50
  • 2
    I looked at all the TS BASIC manuals I could find (there are several editions online), and none of them bother with this detail -- which seems pretty important for a programmer to know. Although perhaps the authors merely thought that it was obvious that "from 1 to 0 by 1" was the empty set; it's only broken languages that do otherwise. – dave Apr 23 '22 at 22:09
  • 1
    The manual for Acces/TS basic states on page 2-13: The statements following the FOR statement, and preceding the closest NEXT statement referencing the same control variable, are then executed none or more times… – WimC Apr 28 '22 at 19:38

1 Answers1

15

It doesn't print any:

Connected to the HP 2100 simulator MUX device, line 0

HELLO-B001,RICK READY 10 PRINT "HOW MANY STARS DO YOU WANT"; 20 INPUT N 30 FOR J=1 TO N 40 PRINT "* "; 50 NEXT J 60 END RUN

HOW MANY STARS DO YOU WANT?10


DONE RUN

HOW MANY STARS DO YOU WANT?0

DONE

I got this running in a fairly recent version of simh built from source (make hp2100) then running the 2000E Time-Shared BASIC revision 1534 Release 3 kit from HP 21xx/1000 and HP 3000 SIMH Simulators then following the Using the Disc Image instructions in the included readme.txt. This is a fiddly process, and you likely won't get it right the first couple of times.

If you get lots of Non-existent parameter errors, your simh is too old.

scruss
  • 21,585
  • 1
  • 45
  • 113
  • 1
    Very interesting. Someday I'll have to figure out how they did that... normally an interpreter wouldn't know where to go if the test fails immediately, so either they do a lookahead or they top test and then roll forward to the NEXT instead of the other way around. – Maury Markowitz Apr 23 '22 at 20:23
  • 2
    @MauryMarkowitz - I don't see why being an interpreter prevents a scan over the source code before execution starts. – dave Apr 23 '22 at 22:12
  • Interesting, the doco from the HP computing museum states: "The simple variable is assigned the value of the initial value; the value of the simple variable is increased by 1 (or the optional step value) each time the loop executes. When the value of the simple variable *passes* the final value, control is transferred to the statement following the NEXT statement." That "passes rather than is-beyond" choice seems to imply an "at least once" strategy. – paxdiablo Apr 23 '22 at 23:46
  • 1
    @MauryMarkowitz: there's also no requirement to scan up front. You could do something like have a flag indicating "I'm in a dead for loop" which reads but doesn't execute code. You stay in that mode until you get to the corresponding NEXT (so you'll probably need to stack dead FOR/NEXT pairs) then turn off the flag. Not how I would do it, I'd prefer pre-scan to speed things up. – paxdiablo Apr 23 '22 at 23:50
  • 3
    I understand the hesitation, but finding the closest NEXT forward shouldn’t be fundamentally harder than implementing GOTO to a forward line number. – Euro Micelli Apr 24 '22 at 00:12
  • 3
    @paxdiablo: "When the value of the simple variable passes the final value": the initial value has already passed the final value if N=0, so the loop is never entered. – scruss Apr 24 '22 at 02:22
  • @scruss: I read that sentence as an addendum to the preceding one, that of increasing by the step value. But you're correct that it could stand alone and "pass" even before the loop body starts. – paxdiablo Apr 24 '22 at 02:45
  • I think that's just classic tech writer looseness with language. :-) I would say "exceeds" rather than "passes"; "exceeds" doesn't include a whiff of actually-changing about it. – dave Apr 24 '22 at 21:33
  • @another-dave - the corresponding NEXT could move at any time, including during execution. Memory or line number could change. You would have to re-scan the program on every edit. In some cases, that is in text format running in a 2.5k machine so the search would be mighty expensive. – Maury Markowitz Apr 26 '22 at 01:13
  • Move during execution? That implies a BASIC program can modify the running BASIC program. I see nothing in the TSB manual that suggests it is possible. Rescan on every edit? That seems unnecessary., Just on every RUN (or equivalent) would do. But there's an interesting test to try: given 100 FOR I ... and 200 NEXT I, insert 150 NEXT J. When is it detected as erroneous? Immediately, at RUN before execution, at line 100, or line 150? – dave Apr 26 '22 at 01:40
  • FWIW, BASIC under TSS/8 on the PDP-8 can manage to loop zero times; and it detects the misplaced NEXT when the RUN command, immediately before executing any line of the program. There is clearly a scan of the source before execution. I'm pretty sure BASIC-8 is interpreted (it and the user program need to fit in 4K x 12 bit words). – dave Apr 26 '22 at 02:02
  • @MauryMarkowitz: "You would have to re-scan the program on every edit" - NEXT is on or after the current line. You don't have to restart the scan at the first line. 8-bit interpreters typically interpret every (tokenized) line as they find them – scruss Apr 27 '22 at 02:39
  • @another-dave - "during execution" may also mean pressing break, modifying the code, and CONTinuing. Execution never ended, simply paused. And of course, self-modifying code was indeed supported on any number of variants. – Maury Markowitz Apr 27 '22 at 14:12
  • @scruss - you're still scanning the program after every edit, doing so "from here forward" doesn't change the basic concept. – Maury Markowitz Apr 27 '22 at 14:19
  • @MauryMarkowitz - what BASIC does that? Most BASICs tokenize the line after a return, but they don't scan the rest of the program while editing – scruss Apr 28 '22 at 02:23
  • @another-dave Interpreted basic is not (always?) structured. The FOR .. NEXT is not a single syntactic element but simply a FOR and a NEXT statement. How could an interpreter even scan for the “correct” NEXT? It can appear anywhere and even multiple times for the same FOR statement. – WimC Apr 28 '22 at 05:19
  • Are we discussing TSB or any possible BASIC extension? TSB is one statement/line. Multiple NEXTs for one FOR are not allowed in original BASIC - it's syntactically an end-of-loop bracket, not a dynamic statement. – dave Apr 28 '22 at 12:16
  • @another-dave Was thinking about MS BASIC on Commodore 8 bit systems. – WimC Apr 28 '22 at 19:22
  • "Most BASICs tokenize the line after a return, but they don't scan the rest of the program while editing" - your point was that you only have to scan forward to find the NEXT. That is beside the point. At some point you have to scan program code. And you have to do it after every edit that might have changed layout. There's a lot of ways you might improve its performance, but every one of them is more code of that 2.5k you have to work in. – Maury Markowitz Apr 29 '22 at 16:44