There are many books, tutorials etc dedicated to teaching C64 programming, many of them start by teaching BASIC. I had an MSX as a kid so I'm pretty familiar with this flavor of BASIC (Microsoft BASIC). On the other hand, most assembly programming tutorials begin with number systems, binary numbers and other pretty basic stuff for someone who's already familiar with assembly programming in other platforms.
What I wanted was to create the smallest programs in assembly that could run on the machine and show some effect. After looking around for a bit I've found this one:
lda #$00 sta $d020 sta $d021 rts
This sets the contents of the accumulator to 0 and then store this zero at memory locations 0xD020 and 0xD021. These two memory locations control the color of the screen background (0xD021) and the border (0xD020). As expected, setting them to zero will make the whole screen black (except for the text).
Now the next step is how to run this program. The ideal way would be to assemble the program and then run on the machine or emulator, and for that we could generate a program in the very simple PRG format and transfer it to the machine. However, I wanted something more immediate and then thought about doing it the way magazines and books did in the old days when users had access to BASIC but not an assembler. This is by using the POKE instruction from BASIC to set memory locations to arbitrary values. After looking at some examples, it seems address 0xC000 (49152) is a popular spot for placing machine language subroutines to be called from BASIC. Using a web-based assembler, the above program is translated to the following bytes in 6502 machine language:
A9 00 8D 20 D0 8D 21 D0 60
So the only thing left is to convert all bytes to decimal and create the POKEs that will put the program at the right location, then transfer to it:
10 POKE 49152, 169 20 POKE 49153, 00 30 POKE 49154, 141 40 POKE 49155, 32 50 POKE 49156, 208 60 POKE 49157, 141 70 POKE 49158, 33 80 POKE 49159, 208 90 POKE 49160, 96 100 SYS 49152
The SYS command at the last line calls a "system" subroutine at the specified address. Note that the assembly code ends with a RTS (return from subroutine) instruction that will return control to the BASIC interpreter.
Type and run this code at the C64 BASIC and voila, the screen turns black:
The next step could be to create a PRG file by hand with this program, load it from (virtual) disk and run it. Maybe next time.
Comments
Post a Comment