-
Notifications
You must be signed in to change notification settings - Fork 60
(ASM Programming) Creating "auto launching" programs
!byte $0B, $08, $01, $00, $9E, $32, $30, $36, $31, $00, $00, $00
Bytes | Description |
---|---|
$0B $08 | Little-endian order $080B, 2-byte pointer to the next line of BASIC code |
$01 $00 | Little-endian order $0001, line number "1" |
$9E | "SYS" token in BASIC encoding |
$32 $30 $36 $31 | "2061" as a PETSCII-encoded decimal, which is $080D in hexadecimal |
$00 | End-of-line |
$00 $00 | End-of-program |
The X16 is happy to execute any machine code that is loaded anywhere into memory. To do so, one just needs to know about the SYS
command in BASIC, and provide it with the address from which it should start executing. This SYS command can also be used in BASIC programs to execute kernal functions or other highly optimized, custom code.
BASIC programs begin at address $0801, conveniently the same location that other program files are usually loaded into, meaning we can take advantage of this to insert a "header" of BASIC that the X16 kernal will treat as a loaded BASIC program. This saves our users the burden of a cryptic-looking SYS
command to some address, and allows them to just use RUN
instead. That's an "auto-launching" program.
Simply add the byte pattern described above to the beginning of your program file, followed immediately by whatever code you want to execute. Since the file is loaded into $0801, and the header is exactly 12 bytes, your program's machine code will start at $080D, which the header conveniently specifies to its SYS
call.
To launch machine code that begins somewhere other than $080D, modify the portion of the header that contains the address in PETSCII. If you need to add a byte, for instance to represent an address of "10000" ($2710) or larger, then you must also adjust the first byte of the header so that it refers to the start of the "end-of-program" token.
You can see this in some other versions of the header, such as this one which adds a "space" character between the SYS
token and the PETSCII address:
!byte $0C, $08, $01, $00, $9E, $20, $32, $30, $36, $31, $00, $00, $00
This is interpreted as follows:
Bytes | Description |
---|---|
$0C $08 | Little-endian order $080C, 2-byte pointer to the next line of BASIC code |
$01 $00 | Little-endian order $0001, line number "1" |
$9E | "SYS" token in BASIC encoding |
$20 | PETSCII "space" character |
$32 $30 $36 $32 | "2062" as a PETSCII-encoded decimal, which is $080E in hexadecimal |
$00 | End-of-line |
$00 $00 | End-of-program |
Note that accommodating the "space" character meant it also had to adjust the address given to SYS
, by one byte, because the header is now 13 bytes, instead of 12. This can be replaced with a PETSCII decimal character for addresses of 10000 or greater without having to make other adjustments to the header.