QBASIC - Statements - BSAVE, BLOAD
BLOAD
Loads the contents of a file anywhere in memory. The file must be saved with BSAVE - or be constructed with the BSAVE-format.
BLOAD filename [,offset]
filename  STRING: file saved with BSAVE
If no extension is given, .BAS is assumed
offset INTEGER: 0..65535 (0..&HFFFF): offset in segment specified by DEF SEG=.
Default is the offset specified in the file-header (the location of the data when saved with BSAVE).

>maskinkode

 
BSAVE
Saves a portion of memory in a file.
BSAVE filename ,offset, length
filename  STRING: name of saved file
If no extension is given, .BAS is used
NB: this is not a basic program, so it is better to specify an extension like .DAT
offset INTEGER: offset in segment specified by DEF SEG.
length INTEGER: length in bytes
 
The data are saved in a file with this header:
  offset    length    field    contents    comment 
0 1 ID FDh = 253d BSAVE/BLOAD ID 
1 2   segment  ? segment defined with DEF SEG
3 2 offset ? BSAVE , offset
5 2 length ? BSAVE , , length
NB: The saved segment, offset and length are used when the file is loaded with
BLOAD filename
But specifying an offset:
BLOAD filename, offset
will make BLOAD ignore the segment and offset specified in the header - and use the current DEF SEG
 
>MASM: Qbasic interface
   
Example: Save and load screen buffer
In graphic modes SCREEN 1 and SCREEN 2, the screen buffer is placed in segment B800h:
 
SCREEN 1 (320 x 200, 4 colors):
The 200 pixel-lines are alternately stored in the first and the second half of the screen buffer:
Lines 0, 2, 4, ..198 are stored from offset 0
Lines 1, 3, 5, ..199 are stored from offset 2000h (8192 = 8K)
Each pixel uses 2 bits (4 colors)
100 lines use 100 x 320 x 2 = 64000 bits = 8000 bytes
The full screen uses 2 x 8000 = 16000 bytes.
When saving the two screen-halves in one file, the 192 bytes 'slack' in the first buffer must also be saved, so the saved data length must be at least 16192 bytes.
Often, the full 16KB screen buffer is saved: 2 x 8192 = 16384 bytes.
NB: It is better so save only 16192 bytes, because the disk cluster size is often 4 KB (4096 bytes). When the 7 byte header is added, 16192+7 bytes only use 4 clusters while 16384+7 bytes use 5 clusters wasting 4K of disk space!
 
SCREEN 2: (640 x 200, 2 colors)
This screen mode uses the same buffer area as Screen 1. Each pixel uses 1 bit.
 
DEF SEG = &HB800
  BSAVE "screen.dat", 0, 16192
DEF SEG
---
BLOAD "screen.dat"
 
file length: 16192 + 7 bytes header = 16199 bytes