User Tools

Site Tools


reading_20and_20writing_20nonstandard_20values

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

reading_20and_20writing_20nonstandard_20values [2018/03/31 13:19]
127.0.0.1 external edit
reading_20and_20writing_20nonstandard_20values [2018/04/17 18:27] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by J.G.Harston,​ July 2009//​\\ ​ Read a 16-bit integer\\ ​ //by J.G.Harston,​ July 2009//​\\ ​ Read a 16-bit integer\\ ​
 +<code bb4w>
         DEF FNrd16(I%):​=BGET#​I%+256*BGET#​I%         DEF FNrd16(I%):​=BGET#​I%+256*BGET#​I%
 +</​code>​
 Read a 32-bit integer (same as **INPUT#​I%,​A%** but as a function, see [[/​Reading%20and%20writing%20nonstandard%20values#​notes1|notes]])\\ ​ Read a 32-bit integer (same as **INPUT#​I%,​A%** but as a function, see [[/​Reading%20and%20writing%20nonstandard%20values#​notes1|notes]])\\ ​
 +<code bb4w>
         DEF FNrd32(I%):​LOCAL A%:DIM zp% LOCAL 3:FOR A%=0 TO 3:​zp%?​A%=BGET#​I%:​NEXT:​=!A%         DEF FNrd32(I%):​LOCAL A%:DIM zp% LOCAL 3:FOR A%=0 TO 3:​zp%?​A%=BGET#​I%:​NEXT:​=!A%
 +</​code>​
 Read a length-specified string (string length followed by string of up to 255 characters)\\ ​ Read a length-specified string (string length followed by string of up to 255 characters)\\ ​
 +<code bb4w>
         DEF FNrdStrN(I%):​LOCAL A%,​A$:​A%=BGET#​I%:​IFA%=0:​=""​         DEF FNrdStrN(I%):​LOCAL A%,​A$:​A%=BGET#​I%:​IFA%=0:​=""​
         REPEATA$=A$+BGET#​I%:​A%=A%-1:​UNTILA%<​1:​=A$         REPEATA$=A$+BGET#​I%:​A%=A%-1:​UNTILA%<​1:​=A$
 +</​code>​
 Read a length-specified reversed string (string length followed by reversed string of up to 255 characters)\\ ​ Read a length-specified reversed string (string length followed by reversed string of up to 255 characters)\\ ​
 +<code bb4w>
         DEF FNrdStrR(I%):​LOCAL A%,​A$:​A%=BGET#​I%:​IFA%=0:​=""​         DEF FNrdStrR(I%):​LOCAL A%,​A$:​A%=BGET#​I%:​IFA%=0:​=""​
         REPEATA$=BGET#​I%+A$:​A%=A%-1:​UNTILA%<​1:​=A$         REPEATA$=BGET#​I%+A$:​A%=A%-1:​UNTILA%<​1:​=A$
 +</​code>​
 Read a bit7-terminated string (final character has bit 7 set)\\ ​ Read a bit7-terminated string (final character has bit 7 set)\\ ​
 +<code bb4w>
         DEF FNrdStrB7(I%):​LOCAL A%,​A$:​REPEAT:​A%=BGET#​I%:​A$=A$+CHR$(A%AND127):​UNTILA%>​127:​=A$         DEF FNrdStrB7(I%):​LOCAL A%,​A$:​REPEAT:​A%=BGET#​I%:​A$=A$+CHR$(A%AND127):​UNTILA%>​127:​=A$
 +</​code>​
 Write a 16-bit integer\\ ​ Write a 16-bit integer\\ ​
 +<code bb4w>
         DEF PROCwr16(O%,​A%):​DIM zp% LOCAL 3:​!zp%=A%:​FOR A%=0 TO 1:​BPUT#​O%,​zp%?​A%:​NEXT:​ENDPROC         DEF PROCwr16(O%,​A%):​DIM zp% LOCAL 3:​!zp%=A%:​FOR A%=0 TO 1:​BPUT#​O%,​zp%?​A%:​NEXT:​ENDPROC
 +</​code>​
 Write a 32-bit integer (same as **PRINT#​O%,​A%**,​ see [[/​Reading%20and%20writing%20nonstandard%20values#​notes1|notes]])\\ ​ Write a 32-bit integer (same as **PRINT#​O%,​A%**,​ see [[/​Reading%20and%20writing%20nonstandard%20values#​notes1|notes]])\\ ​
 +<code bb4w>
         DEF PROCwr32(O%,​A%):​DIM zp% LOCAL 3:​!zp%=A%:​FOR A%=0 TO 3:​BPUT#​O%,​zp%?​A%:​NEXT:​ENDPROC         DEF PROCwr32(O%,​A%):​DIM zp% LOCAL 3:​!zp%=A%:​FOR A%=0 TO 3:​BPUT#​O%,​zp%?​A%:​NEXT:​ENDPROC
 +</​code>​
 Write a length-specified string (string length followed by string of up to 255 characters)\\ ​ Write a length-specified string (string length followed by string of up to 255 characters)\\ ​
 +<code bb4w>
         DEF PROCwrStrN(O%,​A$):​LOCAL A%:​BPUT#​O%,​LENA$:​IFA$="":​ENDPROC         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         FOR A%=1 TO LEN A$:​BPUT#​O%,​ASCMID$(A$,​A%,​1):​NEXT:​ENDPROC
 +</​code>​
 Write a length-specified reversed string (string length followed by reversed string of up to 255 characters)\\ ​ Write a length-specified reversed string (string length followed by reversed string of up to 255 characters)\\ ​
 +<code bb4w>
         DEF PROCwrStrN(O%,​A$):​LOCAL A%:​BPUT#​O%,​LENA$:​IFA$="":​ENDPROC         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         FOR A%=LEN A$ TO 1 STEP -1:​BPUT#​O%,​ASCMID$(A$,​A%,​1):​NEXT:​ENDPROC
 +</​code>​
 Write a bit7-terminated string (final character has bit 7 set)\\ ​ Write a bit7-terminated string (final character has bit 7 set)\\ ​
 +<code bb4w>
         DEF PROCwrStrB7(O%,​A$):​BPUT#​O%,​LEFT$(A$,​LENA$-1);:​BPUT#​O%,​ASCRIGHT$(A$,​1)OR128:​ENDPROC         DEF PROCwrStrB7(O%,​A$):​BPUT#​O%,​LEFT$(A$,​LENA$-1);:​BPUT#​O%,​ASCRIGHT$(A$,​1)OR128:​ENDPROC
 +</​code>​
 ===== Notes ===== ===== 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.\\ \\  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 ==== ==== 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):​\\ ​  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):​\\ ​
 +<code bb4w>
         DEF FNrd32(I%)=BGET#​I%+(BGET#​I%<<​8)+(BGET#​I%<<​16)+(BGET#​I%<<​24)         DEF FNrd32(I%)=BGET#​I%+(BGET#​I%<<​8)+(BGET#​I%<<​16)+(BGET#​I%<<​24)
 +</​code>​
 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. 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.
reading_20and_20writing_20nonstandard_20values.txt · Last modified: 2018/04/17 18:27 by tbest3112