Q(uick)BASIC Statement: BSAVE

Quick View

BSAVE Statement

A file I/O statement transfers the contents of an area of memory to an output file or device

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • BSAVE filespec$, offset%, length&
  • BLOAD filespec$[,offset%]
Description/Parameter(s)

In binary mode, you can read or write information to any byte position in the file using GET or PUT statements.

filespec$ For BSAVE, a file to which an area of memory (a byte-for-byte memory image) is copied. For BLOAD, a memory-image file created by a previous BSAVE.
offset% For BSAVE, the offset of the starting address of the area of memory being saved. For BLOAD, the offset of the address where loading starts.
length& The number of bytes to copy (from 0 through 65,535).

The starting address of the memory area saved or loaded is determined by the offset and the most recent DEF SEG statement.

Syntax
  • BSAVE filespec,offset,length
Description/Parameter(s)
filespec A string expression containing the file or device name. Output devices other than the console (SCRN: and CONS:) are supported.
offset The offset of the starting address of the area in memory to be saved.
length The number of bytes to save. This is a numeric expression returning an unsigned integer in the range 0-65,535.

The BSAVE statement allows data or programs to be saved as memory-image files on disk. A memory-image file is a byte-for-byte copy of what is in memory along with control information used by BLOAD to load the file.

Note: Programs written in earlier versions of BASIC no longer work if they use VARPTR to access numeric arrays.

The starting address of the area saved is determined by the offset and the most recent DEF SEG statement.

If no DEF SEG statement is executed before the BSAVE statement, the program uses the default BASIC data segment (DS). Otherwise, BSAVE begins saving at the address specified by the offset and by the segment set in the most recent DEF SEG statement.

If the offset is a single- or double-precision floating-point value, it is coerced to an integer. If the offset is a negative number in the range -1 to -32,768, it is treated as an unsigned 2-byte offset.

Note: Because different screen modes use memory differently, do not load graphic images in a screen mode other than the one used when the images were created.

Differences from BASICA

  • BSAVE does not support the cassette device.
Example

This example draws a magenta cube inside a white box and then uses BSAVE to store the drawing in the file MAGCUBE.GRH. The BLOAD statement example program shows how you can retrieve and display the drawing after you create it.

DIM Cube(1 TO 675) SCREEN 1 ' Draw a white box. LINE (140, 25)-(140 + 100, 125), 3, B ' Draw the outline of a magenta cube inside the box. DRAW "C2 BM140,50 M+50,-25 M+50,25 M-50,25" DRAW "M-50,-25 M+0,50 M+50,25 M+50,-25 M+0,-50 BM190,75 M+0,50" ' Save the drawing in the array Cube. GET (140, 25)-(240, 125), Cube ' Set segment to the array Cube's segment and store the drawing ' in the file MAGCUBE.GRH. Note: 2700 is the number of bytes ' in Cube (4 bytes per array element * 675). DEF SEG = VARSEG(Cube(1)) BSAVE "MAGCUBE.GRH", VARPTR(Cube(1)), 2700 DEF SEG ' Restore default BASIC segment.
Syntax
  • BSAVE filespec$, offset%, length%
Description/Parameter(s)
filespec$ The file or device on which to save a memory-image file.
offset% The offset of the starting address of the area in memory to be saved.
length% The number of bytes to save. This is a numeric expression that returns an unsigned integer between 0 and 65,535, inclusive.

Usage Notes

  • The BSAVE statement allows data or programs to be saved as memory-image files on disk. A memory-image file is a byte-for-byte copy of what is in memory along with control information used by BLOAD to load the file.
  • The starting address of the area saved is determined by the offset and the most recent DEF SEG statement.
  • If no DEF SEG statement is executed before the BSAVE statement, the program uses the default BASIC data segment (DGROUP). Otherwise, BSAVE begins saving at the address specified by the offset and by the segment set in the most recent DEF SEG statement.
  • If the offset is a long integer, or a single- or double-precision floating-point number between -1 and -32,768, inclusive, it is treated as an unsigned 2-byte offset.

BSAVE and Expanded Memory Arrays

  • Do not use BSAVE to transfer an expanded memory array to an output file. If you start QBX with the /Ea switch, any of these arrays may be stored in expanded memory:
    • Numeric arrays < 16K in size
    • Fixed-length string arrays < 16K in size
    • User-defined-type arrays < 16K in size
  • If you want to use BSAVE to transfer an array to an output file, first start QBX without the /Ea switch. (Without the /Ea switch, no arrays are stored in expanded memory.)
  • For more information on using expanded memory, see Using Expanded Memory .

Important

  • Programs written in earlier versions of BASIC no longer work if they use VARPTR to access numeric arrays.
  • Because different screen modes use memory differently, do not load graphic images in a screen mode other than the one used when they were created.

Differences From BASICA

  • BSAVE does not support the cassette device.
Example

This example draws a magenta cube inside a white box and then uses the BSAVE statement to store the drawing in the file MAGCUBE.GRH. The BLOAD statement example program shows how you can retrieve and display the drawing.

DIM Cube(1 TO 675) SCREEN 1 'Draw a white box. LINE (140, 25)-(140 + 100, 125), 3, B 'Draw the outline of a magenta cube inside the box. DRAW "C2 BM140,50 M+50,-25 M+50,25 M-50,25" DRAW "M-50,-25 M+0,50 M+50,25 M+50,-25 M+0,-50 BM190,75 M+0,50" 'Save the drawing in the array Cube. GET (140, 25)-(240, 125), Cube 'Set segment to the array Cube's segment and store the drawing 'in the file MAGCUBE.GRH. Note: 2700 is the number of bytes 'in Cube (4 bytes per array element * 675). DEF SEG = VARSEG(Cube(1)) BSAVE "MAGCUBE.GRH", VARPTR(Cube(1)), 2700 DEF SEG 'Restore default BASIC segment.