DEV Community


Posted on

6502 Math and Strings Part II

This is part 2 in a series on writing a program in assembly for the 6502 processor. You can find part 1 here.

Picking up from where we left off last time, the last requirements to satisfy for this program are that it:

  • Must accept user input from the keyboard in some form.
  • Must use some arithmetic/math instructions (to add, subtract, do bitwise operations, or rotate/shift)

Character input can, much like character output, be performed in a variety of ways. One way without using the CHRIN ROM routine can be done like so:

Image description

The code in full:

; let the user type on the first page of character screen
; has blinking cursor!
; does not use ROM routines
; backspace works (non-destructive), arrows/ENTER don't

next:     ldx #$00
idle:     inx
          cpx #$10
          bne check
          lda $f000,y
          eor #$80
          sta $f000,y

check:    lda $ff
          beq idle

          ldx #$00
          stx $ff

          cmp #$08 ; bs
          bne print

          lda $f000,y
          and #$7f
          sta $f000,y

          jmp next

print:    sta $f000,y
          jmp next
Enter fullscreen mode Exit fullscreen mode

(Many of the code examples I'm using here are from this page if you want to inspect the code in full.)

Otherwise generally one uses the CHRIN ROM routine to get each character and then manipulates them from there. You can see a full example of that here where I've recreated a program from a lecture that allows for text input with a tracked cursor, responsive to backspace and enter characters and stores and prints the user's input.

The final piece to cover before diving into coding our program is math on the 6502. There are two different ways of performing math, binary or decimal, which is decided by setting or clearing the decimal flag in the status register. the SED instruction sets it, while the CLD instruction clears it. Decimal mode treats each byte as two decimal digits, the lower 5 bits representing the lower digit and the upper 4 bits the upper ones. Numbers are treated as positive and values greater than 9 are invalid.

Special care must be taken to clear the carry flag before the low-byte portion of a multi-byte addition, or before a single-byte operation. If a multi-byte addition is performed by adding the low-byte first, the carry flag will correctly carry bits forward from one byte to the next. Subtraction is performed similarly, with the carry flag set before performing subtraction on the lowest byte of a single or multi-byte subtraction, with subtraction then performed on each byte in sequence up to the highest byte.

Multiplication and division are not generally available, but a Logical Shift right or left effectively performs a division or multiplication, respectively. Similarly, rotations perform the same function but the rotate left instruction will move the highest bit to the carry flag and the carry flag to the lowest bit. The opposite is true of rotate right.

More bitwise operations can also be found here.

I'll leave it at that for this post, and with all the last two posts have covered we'll be ready to dive into coding in earnest for our program in the next post.

Top comments (0)