Hand Assembling to Machine Code on the Commodore VIC-20

I quite enjoy designing machine language routines on paper and then hand assembling them. For many people this would have been their only option until they got a more advanced machine language monitor or assembler. I want to show how to approach this and how to enter these programs on the Vic.

An Example Program

As an example I'm going to show how to create a routine that cycles through the various screen border and background colour combinations by changing the value of $900F.

It is easier to create the routine if we use a coding sheet like the one below to record the mnemonics, operands, labels and comments. In many respects this is similar to how we would create the routine using source code on a computer which is going to be run through an assembler, but on paper.

Address Code Label Mnemonic Operand Comment
LDA$900FRecord initial screen border/background combo
LDX#$FFFirst combo is $FF
SETCOMBOSTX$900FSet screen border/background combo
LDY#$FFTime delay
DELAYDEY
BNEDELAY
DEXNext combo
BNESETCOMBO
STA$900FRestore initial screen border/background combo
RTS

Once the routine is created we can enter the addresses and enter the machine code using lookup tables for opcodes and two's complement. This routine doesn't jump to any absolute addresses and therefore can be located wherever we find suitable. Where I have filled in the addresses I have started at location $02A1 as there is free space here between $02A1 and $02FF which is reserved for user program indirects.

Address Code Label Mnemonic Operand Comment
02A1AD 0F 90LDA$900FRecord initial screen border/background combo
02A4A2 FFLDX#$FFFirst combo is $FF
02A68E 0F 90SETCOMBOSTX$900FSet screen border/background combo
02A9A0 FFLDY#$FFTime delay
02AB88DELAYDEY
02ACD0 FDBNEDELAY
02AECADEXNext combo
02AFD0 F5BNESETCOMBO
02B18D 0F 90STA$900FRestore initial screen border/background combo
02B460RTS

This leaves us with the following machine code:

AD 0F 90 A2 FF
8E 0F 90 A0 FF
88 D0 FD CA D0
F5 8D 0F 90 60

BASIC Data Statements

One of the easiest ways to enter a routine into memory is to to convert the machine code to decimal and poke it. This works well for short machine code routines, but the data statements take up quite a lot of memory and sometimes it is better to put the routine directly into memory and load it separately.

10 for a=673 to 692
20 read b
30 poke a,b
40 next a
50 data 173,15,144,162,255,142,15,144,160,255
60 data 136,208,253,202,208,245,141,15,144,96

A Simple Hex Loader

This is almost a machine language monitor and is one of the quickest and easiest ways to enter machine language directly onto a Vic. This could be used when we have created our program by hand and just want to enter the hex codes into the computer to test it. It is also useful to load the machine code for a proper monitor. The address is in decimal but each byte is entered in hex.

The program is based on one in Vic-20 Machine Code by Bruce Smith.

10 print chr$(147)
20 input "start address:";a
30 print a;":$";
40 gosub 100:h=n:print z$;
50 gosub 100:l=n:print z$
60 b=h*16+l:poke a,b
70 a=a+1:goto 30

100 get z$
110 if z$="s" then end
120 if z$>"f" then 100
130 if z$ >="a" and z$ <= "f" then n=asc(z$)-55:return
140 if z$ = "" then 100
150 n=val(z$):return

To see how to save and load memory to disk or cassette have a look at our article: Saving and Loading Memory on the VIC-20.

Video Demonstrating Hand Assembling to Machine Code

You can see the machine code being hand assembled, entered and run in the following video:

Creative Commons License
Hand Assembling to Machine Code on the Commodore VIC-20 by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post

Feedback/Discuss

Related Articles

Getting the Address of BASIC Variables on the VIC-20

Getting the address of a BASIC variable can be useful if you want to pass data to a machine code routine or want to access the bytes of a variable directly to improve speed and reduce garbage collectio...   Read More

Saving and Loading Memory on the VIC-20

Saving and loading memory is quite easy on the VIC-20 once you know how. However, it isn't obvious how to do this and therefore this article will present a few simple ways of doing it from BASIC and A...   Read More

Programming in Assembly with VICMON on the VIC-20

VICMON is a machine language monitor released by Commodore in 1982 and is great for programming the VIC-20. Its interactive nature means that it can often be quicker to develop via this rather than us...   Read More

Storing Machine Code in REM Statements on the VIC-20

BASIC programs often contain machine code routines but they take up quite a lot of space in BASIC. An interesting way to reduce the amount of space that they take is to store the machine code in REM s...   Read More

Code and Data in Display Memory on the VIC-20

The unexpanded Commodore VIC-20 only had 5K of RAM and therefore creative ways had to be found to maximize the available RAM. The display memory would use some of this memory and therefore one option ...   Read More