Quantcast
Channel: stardot.org.uk
Viewing all articles
Browse latest Browse all 5780

programming • Why did BBC BASIC store PRINT# strings backwards?

$
0
0
In BBC BASIC (including 8-bit BASIC2 and ARM BASICV, also Matrix Brandy), strings are written to file last byte to first. For example, the output of the program at the end of this post is:

Code:

Wrote string "Hello" to file   "hlo"Read string  "Hello" from file "hlo"Read bytes   "..olleH" from file "hlo"
I understand that the file contains a type byte, then the length byte, then the string bytes … only backwards. This seems an unusual way to do it, and the naïve user might suspect that PRINT# would write bytes in customary order. Why did Acorn choose to do it this way?

I've tried a few other interpreters (including GWBASIC, Commodore BASIC v2, Locomotive BASIC; even Richard Russell's BBCSDL) and they all PRINT# bytes to file in the correct order.

NB: I am intrigued by this decision, not annoyed or belittling it. If I ever have to deal with it, I know now that I have to BPUT# strings one character at a time. Somebody at Acorn must've had a really good reason to write strings this way, and I'm wondering if anyone here knows why.

The code in question (slightly naff 'cos I wanted it to be portable rather than pretty):

Code:

   10 f$="hlo"   20 h$="Hello"   30 r$=""   40 b=0   50 REM write string to file   60 f=OPENOUT f$   70 PRINT #f, h$   80 PRINT "Wrote string "+CHR$(34)+h$+CHR$(34)+" to file   "+CHR$(34)+f$+CHR$(34)   90 CLOSE #f  100 REM read string from file  110 f=OPENIN f$  120 INPUT #f, r$  130 PRINT "Read string  "+CHR$(34)+r$+CHR$(34)+" from file "+CHR$(34)+f$+CHR$(34)  140 CLOSE #f  150 REM read bytes from file  160 f=OPENIN f$  170 r$=""  180 b=BGET #f  190 IF b>31 AND b<127 THEN r$=r$+CHR$(b)  200 IF b<32 OR b>126 THEN r$=r$+"."  210 IF NOT(EOF #f) THEN GOTO 180  220 PRINT "Read bytes   "+CHR$(34)+r$+CHR$(34)+" from file "+CHR$(34)+f$+CHR$(34)  230 CLOSE #f  240 END

Statistics: Posted by scruss — Mon Jun 02, 2025 10:26 pm



Viewing all articles
Browse latest Browse all 5780

Trending Articles