Print String in Commodore 64 Assembly Language

December 27, 2022

Print String in Commodore 64 Assembly Language

When the THEC64 from Retro Games was released at the end of December 2019, I was able to spend some time making up for lost time with the so-called “breadbox”.

With a lot of interest I read through the official Commodore 64 User’s Guide and learnd some BASIC, the onboard programming language. In the last two years I have been playing with the C64 on and off and have also been looking into the 6502 assembly programming language.

In this short article I’m going to show you one way to print a string in 6502 assembly programming language.

Output in VICE

Here you can see the final result. We simply print the string COMMODORE 64 at the top left corner of the screen.

VICE Print String

In BASIC this can be done with this simple line of code (although the position will be different):

PRINT "COMMODORE 64"

Source Code

You can access the source code on my github repository here.

Notes

Loading the string with the upper code takes 470 cycles (see below how to calculate number of cycles used). I tried different other options, which in general use less space, but take significant more cycles. For example this approach, using the subroutines $e50c and $ab1e will take 5192 cycles and does exactly the same.

        ldx #$00                ; Select row
        ldy #$00                ; Select column
        jsr $e50c               ; Set cursor

print   lda #<str               ; Load lo-byte of string adress
        ldy #>str               ; Load hi-byte of string adress
        jsr $ab1e               ; Print string
        rts                     ; End.

str     .text "COMMODORE 64"
        .byte $00

How to detect number of cycles used in VICE

  1. Activate monitor: File -> Activate monitor
  2. Disassemble instructions from the start of the program: d $02a7
  3. Set two breakpoints: break $02a7 and break $02b9
  4. Close monitor
  5. Run program: SYS 679
  6. Write down STOPWATCH number
  7. Close monitor
  8. Write down STOPWATCH number
  9. Calculate cycles used: Difference of both STOPWATCH numbers

For more informations about the VICE monitor, have a look at the official manual.

References

6502 Assembly C64 Commodore 64 Retro