Commodore 64 Programming #1: A quick start guide to C-64 assembly programming on Windows

image

Update: All needed files for this tutorial can be found in the GitHub repo linked in the bottom of this page.

I noticed that a lot of people are creating graphical programs to the Commodore 64 again, so I decided to let you know of the tools I use, and how you can use them to create C-64 apps in Windows. I might write more articles on C-64 programming if there’s an interest for it. If you want more, let me know by writing a comment to this article

Note: You do not need to own a Commodore 64 to create programs. In this article, I’m going to use a cross assembler and an emulator.

Cross Assembler?
A cross assembler enables you to assemble the code on your computer, and then later transfer the code to a real C-64 or an emulator to run the program. In my opinion, this makes it a lot easier to create programs as the editors in Windows is a lot easier to use then the editors on the C-64.

The cross assembler I’m using is named DASM, and can be downloaded from here. Download this now, as this is the assembler we are going to use in this article.

Emulator?
Next you will need an emulator. The emulator will make it possible to run any Commodore 64 program on your PC. If you are creating a program for a C-64 using an emulator, remember to test the program on a real device as there might be some differences.

The emulator I use is named WinVICE, and you can get it from here. Download this now, as this is the emulator we are going to use in this article.

Set up the tools, and running them
Let’s see how the tools work. The assembler, DASM, is very simple to use. I usually add the DASM.exe to the environment variables, as well as the emulator x64.exe.

To add them to the environment variables (Microsoft Windows 7), go to control panel, and open System. Then click “Advanced system settings”:
image

Then on the Advanced tab, click the [Environment Variables…] button:
image

In the popup under System variables (or for the user if you only want it to be available for one user) find the variable Path and add the paths to the exe files behind the other paths. the semicolon is the splitter between different paths:

;C:\C64\DASM22010\DASM\bin\DOS\;C:\C64\WinVICE-2.2-x86\;

image

Now you should be able to run the emulator and assembler from wherever you are on the system. Lets try them.

Start cmd.exe and type dasm. The result should be something like below:
image

Now, the assembler is working. Next, type x64 to run the emulator. A new window will pop up with the classic C-64 screen:
image

Now, close the emulator and the cmd screen if you want.

Programming your first C-64 program
The first thing you need when writing a program for the C-64 on Windows is a text editor. I use Visual Studio 2010 for this, but feel free to use whatever text-editor you want (Notepad, UltraEdit, …).

Create a new source code file names test.asm. You should now be in a blank test.asm file and be ready to type in some code.

We are going to write some code, then compile the code to a .prg file using DASM, and then run it using x64. There are many different assemblers for compiling C-64 programs (6502 microprocessor), and as they all got their differences I suggest you learn one and stick with it.

The first thing DASM needs is to know what processor we are going to target. The C-64 got a 6502 microprocessor, so this is the processor we want to target.

The first line of our program is:
    processor   6502

Next we need to tell the compiler where in the memory the program should start. We want to start it at the memory address $1000 (hex decimal). If you convert this to the decimal system, you get 4096.
    org    $1000

We want this program to change the background of the main window. If you take a look at the image above where the emulator is running, you can see that we got a light blue border, and a dark blue “main window” area. To do this, we need to change a value that represents the color in a specific memory location. The main window color is stored in the memory located at $d021, and the border color is located at $d020.

image

We are going to loop this process and change the screen color based on the number we have in $d021 before, and increase this by using a loop.

loop:    inc $d021
jmp loop

We start by creating a label in the code named “loop” followed by a colon. This will make it possible to jump to this location from other parts of our code. Next, we increase the number that already exist in $d021, and then we jump back to the line of code that is located after the loop label.

Your code should look something like this:

Listing 1.1 – Change color of the main area.
     processor   6502
org    $1000

loop:
     inc $d021
jmp loop

Building the code
Now, all that is left is to build our program and run it on the emulator.
Start cmd again again and browse to the folder your code is located. Assuming that you correctly added the location to DASM.exe in your Paths variable, you will be able to build a program from wherever you want in your system.

To build our program, write the following command into cmd.exe
”dasm test.asm –otest.prg”

Please note the spacing before the first two lines (processor and org), dasm requires this to compile.

The program should compile without errors. If so, the output should be something like this:
image

If you type the command dir, you should be able to see your program:
image

Let’s run it in the emulator. Type the command x64 test.prg and hit [Enter]. Then  you will see the emulator starting, and loading the program TEST.PRG into the memory.
image

All that is left is to run the application from the emulator. To do this, type the command “SYS 4096” in the emulator….
image

….and hit ENTER.

The application should now be running, giving you a result that looks something like this:
image

The reason you had to type SYS 4096 in the emulator was because we specified that we want out application to start at that memory location.

Congratulations, you just made your first C-64 program!

Let’s change the color of the boarder instead of the main area. All you need to do is to change the value in  $d020 instead of $d021. Go and change this value, compile and load it in the emulator. Run it to see that we are now changing the main border instead of the main screen area.
image

An exercise for you is to change the color of both the boarder and the main area, so the result will be something like this:

image

Downloads

Download the source from GitHub:
https://github.com/petriw/Commodore64Programming/tree/master/1-Quickstart

This entry was posted in Commodore 64. Bookmark the permalink.

66 Responses to Commodore 64 Programming #1: A quick start guide to C-64 assembly programming on Windows

  1. Pingback: Windows Client Developer roundup 064 for 3/21/2011 - Pete Brown's 10rem.net

  2. Pete says:

    Excellent! Totally unexpected but nice to see in my feed this week. Always love me a little C64 🙂

  3. Scott S Sikora says:

    Fantastic article. Amazing machine that little C64 is…25+ years and it’s still interesting.

  4. marshall says:

    Love it! Thank you for
    This intro.

  5. X86GameCoder says:

    This rawks! Got my first c64 program running. Thanks!

  6. Carl says:

    Keep it going!!! We want more.. 🙂 I’d love to pick up 6502 assembly again.

  7. v4corg says:

    Ouh – loving it. Thanx for that article.

  8. Marco Pizzo says:

    Se volete vedere la differenza tra basic e linguaggio macchina
    1 for x=0to255
    2 poke 53280,x:poke 53281,x
    3 next
    4 goto1
    RUN

    Quello proposto è stato forse il mio unico programma in L.M.

  9. v4corg says:

    Funny – exactly that changing the background color thingy was the first program i made for my amiga (i think the register was $dff180) when i got hands on the amiga internal book (XMas 1988?) – sigh

  10. Andy Spencer says:

    Absolutely fantastic . . . please do a tutorial on C64 programming! 🙂

  11. Jimmy Mac says:

    Didn’t work for me:

    I entered the program as required, and ran the assembler, but got:

    C:\dasm-2.20.11\mine>dasm test.asm -otest.prg
    test.asm (1): error: Unknown Mnemonic ‘6502’.
    test.asm (2): error: Unknown Mnemonic ‘$1000’.
    test.asm (4): error: Unknown Mnemonic ‘inc’.
    test.asm (5): error: Unknown Mnemonic ‘jmp’.

    Looks like the assembler had no idea what the file was from line one onwards…. did I miss a step?

  12. deangc-oz says:

    Did heaps of coding in basic back in the day but seeing this makes me want to learn 64asm.. moar! MOAR!

  13. JaceSTK says:

    Thank you for this tutorial! I’m gonna start making demos on c64!

  14. Pingback: Super.licio.us | Superlevel

  15. Pingback: Quick Start Guide to C=64 Assembly » Ussher Press Daily

  16. Thanks for the C-64 tutorials! I used to have one back in the day (sold it sometime in 90s for a x86), but it rocked! Please keep the C-64 posts coming 🙂

    • BTW, I have also been using VICE emulator and cross-assembler to do some simple C-64 stuff…I have been playing around with making a Pascal -> 6510 compiler (runs on PC) for creating emulator programs that run using VICE, etc. Doesn’t do much yet though LOL

  17. photogea6 says:

    I’ve been doing some work with an emulator that runs off my iPad, the app isn’t finished yet and there are a few things missing such as sound and multiple colors. I was wondering how to just type up a basic text based rpg straight into the emulator and the essential code I needed to do so…

  18. Zonacas says:

    Hello I’m trying to do the tutorial step 1 but I have problems… I compiled with DASM
    ——————————–
         processor 6502
         org $ 1000
    loop: inc $ D021
                  jmp loop
    ——————————–
    and says, Complete

    when I load the TEST.PRG and put sys 4096 in the emulator does nothing … Why?

  19. Zonacas says:

    Ok is a problem with the spaces …
    —————————————
    processor 6502
    org $1000
    loop: inc $D021
    jmp loop
    —————————————-
    now if it works

  20. Pingback: C64 assembly! « *gigazarak*

  21. zapposh says:

    I love these tutorials. I hope you find time one day to make more, as they are really excellent.

  22. azaria says:

    if can you help me. nothing not sucses icant compile i try everithing

  23. SunKing2 says:

    Thanks! 🙂

  24. run says:

    yeah , you know — , i wonderd what all those people were doing at the time , “;-) ,

  25. ralf says:

    Love it gomme more, some scrolling text 🙂

  26. Andreas says:

    Had some trouble getting this to work, then I tried using another text editor, Visual studio and viola it worked, so If you have trouble, try another text-editor.

  27. Riccardo says:

    This is the code

    processor 6502
    org $1000
    loop: inc $d021
    jmp loop

    This is the reply
    DASM V2.20.10, Macro Assembler (C)1988-2004
    test1.asm (1): error: Unknown Mnemonic ‘6502’.
    test1.asm (2): error: Unknown Mnemonic ‘$1000’.
    test1.asm (4): error: Unknown Mnemonic ‘inc’.
    test1.asm (5): error: Unknown Mnemonic ‘jmp’.
    Complete.

    And the prg doesn’t obviously work (the prg file is a 0byte file)… what’s up?

    • Cynic says:

      For some unknown reason dasm requires a space before the processor line and the org line. Example:

      [SPACE] processor 6502
      [SPACE] org $810 ;sys 2064 to run

      mainloop: lda $d012 ;load the current position of the raster
      cmp #$00 ;the raster trigger point on the screen
      bne mainloop ;if != $38 then mainloop

      The prg file will be completed when the code is compiled with
      dasm source.asm -ostuff.prg

  28. Tristan says:

    Would my program be usable on a real C64? If so, how?

  29. kpkilburn says:

    Reblogged this on Writing, Arts, & Retro and commented:
    An older post about programming machine language on the Commodore 64 with modern tools in Windows, but since it’s from a fellow WordPress blogger, I thought I’d repost…

  30. lov says:

    hey can you make this one for turbo assembler format as well?

    thanks

  31. anass says:

    hello
    i found a problem on running the prg generated file
    after my code is assembled
    i run it in vice
    and i tape
    sys 4096
    but i didint see any difference in programme
    like changin border of screen color
    please can you help

    • Tech Phantom says:

      Hello this is a bit late but,

      The same happened to me when I typed in the above. You need to make sure you’ve indented correctly:

      processor 6052
      org $1000
      loop: inc $d021
      jmp loop

      If you don’t set it out like this, when you type sys 4096, nothing happens! In other assemblers like TASM the code is automatically positioned for you but if you’re using a text editor you need to take care of this yourself. I Hope this helps.

      • Tech Phantom says:

        Oops, I’ve just seen my message and the code is still incorrectly placed. Let me describe it instead. The processor 6502 (6052??? My mistake!!) and org $1000 should be indented. The loop: tag should not be indented and the inc $d021 and jmp loop should be underneath and inline with the processor and org text.

  32. Uao… now i’m 41 years old and i have programmed “directly” in ASM with my old C=64. If I remember, the ASM code is:

    A 1000 INC $D020
    A 1002 JMP $1000

  33. Pingback: C64 6501 ASM/Coding – noise();

  34. Chris says:

    Thank you for your tutorial.

  35. Lane says:

    Thanks for those trying to help with the spacing; dasm is a finicky thing isn’t it? This is the spacing that finally worked for me (an image link): http://anonpix.com/images/2016/12/03/gsoCw.png (

  36. DASM avaible for MACOS. You can install it with brew if you like.

  37. Pingback: c64 assembly – Barton.nl

  38. Pingback: C64ForTheWin – C64 Development on your Windows Machine – Be Analytics

  39. Krister Trygg says:

    super article, thanks keep it up….

  40. Pingback: Commodore 64 Programming #1: A quick start guide to C-64 assembly programming on Windows | ExtendTree

  41. Pingback: A quick start guide to Commodore 64 assembly programming on Windows (2011) – PipisCrew Official Homepage

  42. Pingback: A quick start guide to Commodore 64 assembly programming on Windows | Ace Infoway

  43. Pingback: Les liens de la semaine – Édition #230 | French Coding

  44. Pingback: Exploring Assembly Language and WebAssembly - JobberTalentTalkJobberTalentTalk

  45. Nice, thanks for posting. Currently looking at BASIC on the ZX Spectrum.

  46. Pingback: Exploring Assembly Language and WebAssembly - Dice Insights

  47. xeno says:

    Hi! DASM creates a 0 byte .prg file, which doesn’t includes anything.

    • xeno says:

      When i make the spacing before the first two lines, creates a 5 bites prg, which doesn’t run on the emulator.

      • xeno says:

        Solved. I needed the spacing by all lines, except the loop:
        processor 6502
        org $1000
        loop:
        inc $D020
        jmp loop
        (notepad texteditor)

  48. Olle says:

    Is this really the simplest idea you could come up with learning BASIC or is this something flse than BASIC?

  49. alshp says:

    Hi everyone

    What used:
    1. Windows 10 (paths added)
    2. dasm-2.20.14.1-win-x64
    3. GTK3VICE-3.7-win64

    For a long time I could not figure out the indents. It all worked when I placed them in Notepad++ like this:
    processor 6502
    org $1000
    loop: inc $d020
    inc $d021
    jmp loop

    The command to start emulation turned out to be a little different: “x64sc test.prg”.

    Thanks for the material and the opportunity to learn something new

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.