Thursday 27 February 2020

C64 quickie - autorun assembly code


One thing my previously mentioned Snake 1K game didn't do was automatically launch. Typing in SYS 4096 every time is not a huge task, but it nonetheless gets old fast.

It's actually a very simple issue to solve, though - so I thought I'd hammer out a very quick explanation of how to do this for any assembly code program.

On the C64, BASIC starts at memory location 2048 (or $0800 in Hex). All we need to do to create a launcher for our code is to "assemble" in some data at that location that forms a BASIC program, that in turn runs our code.

This doesn't take that much effort to do:

You can easily change the ASCII values for the SYS address to any address you wish to place your code at. Each value is simply represented as Hex code $3x, where 'x' is the value between 0 and 9, forming (in this case) 2062.

If you assemble a program built with this at 2048, the .prg (or a.out) file produced will automatically create the BASIC code when loaded. If you drag the file into VICE's window, you won't even need to type RUN as VICE does this automatically in this situation.


Anyway, I hope that is useful! Until next time :)

[Copy-Paste-able version of code now follows...]

   processor 6502

    org 2048 ; start of BASIC program area in memory

    ; BASIC launcher

    .byte $00                ; first byte has to be zero for BASIC program to work
    .byte $0c, $08           ; pointer to next BASIC line in memory, i.e. $080C
    .byte $0a, $00, $9e, $20 ; the code for: 10 SYS (includes trailing space)
    .byte $32, $30, $36, $32 ; ASCII version of SYS address, i.e. 2062
    .byte $00                ; line terminator
    .byte $00, $00           ; the pointer above points to this place in memory
                             ; as we have no other line, we leave both bytes zero
                             ; end of program

    ; memory location 2062 comes next, which our SYS command above calls

    ; (i.e. 2048 + 14 bytes for our BASIC program above)

start:
    ; your C64 assembly program goes here
    ; to demonstrate, this simple program just changes the screen color

    jsr $e544 ; clear the screen
    lda #$00
    sta $d020 ; border to black
    lda #$05
    sta $d021 ; background to green
    lda #$00  ; Black text
    sta $0286

    rts ; back to BASIC!


No comments:

Post a Comment