title: Why are characters being offset by 0x40 on my Commodore 64 emulator?
body: <p>I have some 6502 code to print a string to the screen memory after clearing the screen. Unfortunately if I print a string, for example "HELLO WORLD", it will come out in garbled characters. I've found that this is because the upper case characters start at 0x01, not 0x41 as I thought from the PETSCII codes <a href="http://upload.wikimedia.org/wikipedia/commons/c/c4/C64_Petscii_Charts.png" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I can fix this by subtracting 0x40 from my string, but then everything other than letters are incorrect, for example, spaces. I'm just not sure why the character generator is turning 0x01 into the character 'A' and not 0x41. It turns 0x41 into an inverted spade sign (like on a deck of cards) and everything above it seems to be border characters and weird symbols.</p>
  
  <p>After looking around for a while I found this quote on the wikipedia page for PETSCII which seemed to state the problem I'm trying to solve, but I'm not sure how to fix it and can't find any information anywhere...</p>
  
  <blockquote>
    <p>The actual character generator ROM used a different set of
    assignments. For example, to display the characters "@ABC" on screen
    by directly POKEing the screen memory, one would POKE the decimal
    values 0, 1, 2, and 3 rather than 64, 65, 66, and 67.</p>
  </blockquote>
  
  <p>I am running on the VICE x64 emulator on Mac OS X, and I'm assembling with an OS X port of 64tass.</p>
  
  <p>This is the assembly code without subtracting 0x40:</p>
  
  <pre><code>*=$c000
  
  BORDER = $d020
  INNER = $d021
  
  start   lda #0
          sta BORDER
          lda #0
          sta INNER
  
          jsr clear
          jsr string
  
  loop    
          jmp loop
  
  
  clear   ; clear screen
          lda #$00
          tax
          lda #$20
  clrloop 
          sta $0400, x ; clear each memory "row"
          sta $0500, x
          sta $0600, x
          sta $0700, x
          dex
          bne clrloop  ; clear if x != 0
          rts
  
  string  ; load string
          ldx #$0
  strloop lda hello, x ; load each byte in turn
          cmp #0       ; if we reached a null byte, break
          beq strexit
          sta $0400, x
          inx
          jmp strloop
  strexit rts
  
  
  hello   .text "HELLO WORLD"
          .byte 0
  </code></pre>
  
  <p>Here is a screenshot of the output</p>
  
  <p><img src="https://i.stack.imgur.com/9Fz49.png" alt="Here is a screenshot of the output"></p>
  
  <p>Thanks to everyone in the comments!</p>
  
  <p><img src="https://i.stack.imgur.com/nHIqM.png" alt=""></p>
  
  <h2>Side note to help others</h2>
  
  <p>You can set which row and column CHROUT will output to by setting the cursor position with PLOT</p>
  
  <p><a href="http://sta.c64.org/cbm64krnfunc.html" rel="nofollow noreferrer">http://sta.c64.org/cbm64krnfunc.html</a></p>