Search code examples
assembly6502c64

Iterating over bytes and printing them to screen as ascii


So I'M attempting to print ASCII to the screen, by storing the ascciASCIIcodes in a byte array, but it just renders a lot of gunk on the screen.

; Message: hello
*=$033C        
        BYTE $48,$45,$49,$49,$4F 
*=$1000
START
    JSR            PRINT_MESSAGE
EXIT
    RTS
PRINT_MESSAGE
    LDX #$00        ; initialize x to 0
    LDA $033C,X     ; grab byte
    JSR $FFD2       ; render text in A with Subroutine:CLRCHN
    INX             ; Incriment X
    CPX #$05        ; We stop at 5
    BNE $1006       ; Else we loop
    RTS

Solution

  • Further to your own answer, three points you might or might not already know:

    1. The C64 uses PETSCII rather than ASCII - it's similar, but not identical.

    2. The memory region $033C-$03FB (192 bytes) is the cassette buffer - it's OK to use that space for temporary storage, but it'll get overwritten when a tape operation occurs.

    3. You can speed-up your routine a tiny bit by reversing the order of the bytes to be displayed and counting down in your loop rather than up - which saves you a CMP instruction on each iteration. Note that we use BPL since we want bytes 0-4, and so we wait until X drops below zero before leaving the loop. This restricts the message length to 127 bytes, but then since most of the bottleneck in your code is in the Kernal print routine at $FFD2 this is probably academic. However, the count-down-and-branch technique (instead of count-up-and-compare-and-branch) is a common trick in 6502 coding, since it saves both CPU cycles and bytes.

      ; Message: hello
      *=$033C        
          BYTE $4F,$49,$49,$45,$48
      *=$1000
      START
          JSR            PRINT_MESSAGE
      EXIT
          RTS
      PRINT_MESSAGE
          LDX #$04        ; initialize x to message length
      GETCHAR
          LDA $033C,X     ; grab byte
          JSR $FFD2       ; render text in A with Subroutine:CLRCHN
          DEX             ; decrement X
          BPL GETCHAR     ; loop until X goes negative
          RTS