User Tools

Site Tools


reading_20and_20writing_20nonstandard_20values

Reading and writing nonstandard values

by J.G.Harston, July 2009
Read a 16-bit integer

        DEF FNrd16(I%):=BGET#I%+256*BGET#I%

Read a 32-bit integer (same as INPUT#I%,A% but as a function, see notes)

        DEF FNrd32(I%):LOCAL A%:DIM zp% LOCAL 3:FOR A%=0 TO 3:zp%?A%=BGET#I%:NEXT:=!A%

Read a length-specified string (string length followed by string of up to 255 characters)

        DEF FNrdStrN(I%):LOCAL A%,A$:A%=BGET#I%:IFA%=0:=""
        REPEATA$=A$+BGET#I%:A%=A%-1:UNTILA%<1:=A$

Read a length-specified reversed string (string length followed by reversed string of up to 255 characters)

        DEF FNrdStrR(I%):LOCAL A%,A$:A%=BGET#I%:IFA%=0:=""
        REPEATA$=BGET#I%+A$:A%=A%-1:UNTILA%<1:=A$

Read a bit7-terminated string (final character has bit 7 set)

        DEF FNrdStrB7(I%):LOCAL A%,A$:REPEAT:A%=BGET#I%:A$=A$+CHR$(A%AND127):UNTILA%>127:=A$

Write a 16-bit integer

        DEF PROCwr16(O%,A%):DIM zp% LOCAL 3:!zp%=A%:FOR A%=0 TO 1:BPUT#O%,zp%?A%:NEXT:ENDPROC

Write a 32-bit integer (same as PRINT#O%,A%, see notes)

        DEF PROCwr32(O%,A%):DIM zp% LOCAL 3:!zp%=A%:FOR A%=0 TO 3:BPUT#O%,zp%?A%:NEXT:ENDPROC

Write a length-specified string (string length followed by string of up to 255 characters)

        DEF PROCwrStrN(O%,A$):LOCAL A%:BPUT#O%,LENA$:IFA$="":ENDPROC
        FOR A%=1 TO LEN A$:BPUT#O%,ASCMID$(A$,A%,1):NEXT:ENDPROC

Write a length-specified reversed string (string length followed by reversed string of up to 255 characters)

        DEF PROCwrStrN(O%,A$):LOCAL A%:BPUT#O%,LENA$:IFA$="":ENDPROC
        FOR A%=LEN A$ TO 1 STEP -1:BPUT#O%,ASCMID$(A$,A%,1):NEXT:ENDPROC

Write a bit7-terminated string (final character has bit 7 set)

        DEF PROCwrStrB7(O%,A$):BPUT#O%,LEFT$(A$,LENA$-1);:BPUT#O%,ASCRIGHT$(A$,1)OR128:ENDPROC

Notes

Different varieties of BBC BASIC write and read data with PRINT# and INPUT# in different ways. You can only be certain that INPUT# will read data written by PRINT# with the same BBC BASIC. These functions allow you to impose the exact structure on the data you are writing, regardless of what platform BBC BASIC the program is running or where the data is going to or coming from.

Comments by Richard Russell

I don't believe that INPUT#I%,A% reads four bytes, nor that PRINT#O%,A% writes four bytes, in any version of BBC BASIC. In BBC BASIC for Windows they read/write five bytes (in *FLOAT40 mode) or eight bytes (in *FLOAT64 mode).

FNrd32 is more straightforwardly implemented as follows (it should also be considerably faster than the code listed above):

        DEF FNrd32(I%)=BGET#I%+(BGET#I%<<8)+(BGET#I%<<16)+(BGET#I%<<24)

The 'length-specified' string routines appear to assume a maximum string length of 255 characters, but this is not stated.

In several instances the routines listed could be simplified at the expense of compatibility with old versions of BBC BASIC (e.g. 6502 and Z80), for example by using the += and –= operators.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
reading_20and_20writing_20nonstandard_20values.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1