LOAD
BASIC keyword | |
Keyword: | LOAD |
Abbreviation: | L Shift+O |
Type: | Command |
Token code: | 147/$93 |
Handling routine in BASIC ROM: |
57704–57789 $E168–E1BD |
List of all BASIC keywords |
Remark: This article describes the BASIC command LOAD in BASIC V2 at the Commodore 64.
Type: Command General programming syntax: LOAD ["<filename>" [,<device number> [,<secondary number>]]]
The BASIC command LOAD is normally used for loading program files (PRG) like BASIC, machine language programs, but actually any kind of data from datasette or disk drive into RAM. This is the common task needed before a previously stored program can be edited and run with the BASIC command RUN.
The device number instructs the computer which storage device is to be accessed using the following scheme:
- datasette - device number 1
- disk drives - device numbers 8-15, optional first disk drive 8, second disk drive 9, etc
If no device number is given, the computer defaults to device 1, usually the datasette.
The filename parameter can be used to load a specific file. In this context certain characters have special meanings, e.g. wildcard characters "*", "?" or the directory load character "$"). Examples:
LOAD "*",8 loads the first file if LOAD is called the first time or the last loaded file. LOAD "A*",8 loads the first file whose name begins with A. But note: all characters appearing after the first "*" are ignored. LOAD "A?C",8 loads the first file whose name consists of the three letters, where the first letter is "A" and the last is "C". The "?" character denotes a wildcard which matches any single character. LOAD "$",8 loads the disk directory.
The "secondary number" specifies how the data should be loaded:
,0 The program will be loaded to the start address of BASIC memory (2049/$0801). ,1 The program will be loaded absolute, namely it is stored to the location defined by the first two bytes in the PRG file image. Typically needed for machine language programs to get properly located into the memory.
If this is not specified it defaults to 0.
- If the command LOAD is entered in direct mode, all open files will be closed and the BASIC command CLR will be implicitly executed, and interpretation of the current line will end.
- LOAD executed in direct mode always changes the SOV (start-of-variables) pointer. If a ML program is loaded without relocation, SOV will point directly below it which will usually results in a BASIC area pointer mismatch, sometimes leading to a "?OUT OF MEMORY ERROR". To prevent this, a NEW command should be executed after LOAD (beside of some trickery with POKEing pointers).
- If LOAD is called from a BASIC program (program mode), it behaves like chain loader: After loading, program execution starts over from the beginning, but unlike RUN, variables and arrays are maintained and may hold state information throughout successive loads.
- If a BASIC program was loaded that is longer than the loader, variable space gets overwritten and variables get corrupted, and subsequent changes to variables corrupt the BASIC program.
- Due to this, BASIC programs typically do not get loaded in program mode. Instead, a typical practice is to PRINT
LOAD"...",8
andRUN
onto the screen, then POKE two RETURNs into the keyboard buffer (POKE631,13:POKE632,13:POKE198,2
), then end the program. After that, LOAD will get invoked automatically from direct mode now reading from the keyboard buffer.
A user can easily load from a datasette by holding SHIFT while pushing RUN/STOP . After that the PLAY button on the datasette should be pushed and the first file will be searched. After some time the name of the first encountered file will be printed on screen with the message "FOUND filename" for short while and followed by the process of being loaded into RAM. This delay can be skipped by pushing the C= key although the SPACE key is more frequently used. While loading a file from datasette the screen will be blanked so the VIC will not interfere the timing for the data transfer directly processed by the CPU. The screen remains on while loading from other storage devices where handshake protocols (like on IEC/serial bus) are able to cope with VIC's behavior.
In case the filename given doesn't match an existing one or is not of type program (PRG) BASIC emits the error message "?FILE NOT FOUND ERROR".
Examples[edit | edit source]
LOAD "FILENAME",8
(loads a program from the first disk-drive 8)
LOAD "FILENAME",9,1
(loads a program absolute from the second disk drive 9)
LOAD FILE$,1,1
(loads a program with file name stored in the string FILE$ absolute from datasette)
- Simple loader (from the 1980's)
10 REM JUMP&RUN GAME LOADER 20 A=A+1 30 IF A=1 THEN LOAD "MUSIC",8,1 40 IF A=2 THEN LOAD "SPRITES",8,1 50 IF A=3 THEN LOAD "GRAPHICS",8,1 60 IF A=4 THEN LOAD "LEVEL1",8,1
This loader program successively loads the machine code or data parts (MUSIC, SPRITES, GRAPHICS, LEVEL1) from disk drive with device number 8 into RAM. Note that (BASIC) programs that are longer that this loader cannot be loaded this way.
- Advanced loader with progress indicator
0 REM JUMP&RUN GAME LOADER 10 A=A+1 20 D=8: S=4: DATA "MUSIC", "SPRITES", "GRAPHIC", "LEVEL1" 30 READ F$ 40 IF A=0 THEN PRINT CHR$(147)"LOADING PLEASE WAIT..." 50 PRINT INT(100*A/S)"%": LOAD F$,D,1 : REM LOAD PART
The file names are taken from the DATA statement on line 20.
Variable D represents the device number.
Variable S contains the number of files to be loaded. Right before a part is loaded the progress in percent (accomplished after the part is finished) will be displayed.
Note that (BASIC) programs that are longer that this loader cannot be loaded this way.
ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT