6502 Math and Strings Lab
Lab 3
Tim Lin
In this LAB, I learn how to write a simple assembler. Since assembly language is complex, I need to refer to the sample code. Example:Etch-a-Sketchtm Style Drawing.
The function of this code is that clicking the C key will display a pixel dot in the middle of the screen.
I modified it so that only the four uppercase letters H,E,L,O can be clicked to display the pixel dots. In the process, I struggled to find out the meaning of various characters. For example, the letter O is 4F in computer characters, in the form of a number + letter.
; zero-page variable locations
define ROW $20 ; current row
define COL $21 ; current column
define POINTER $10 ; ptr: start of row
define POINTER_H $11
; constants
define DOT $01 ; dot colour
define CURSOR $e ; blue colour
ldy #$00 ; put help text on screen
print: lda help,y
beq setup
sta $f000,y
iny
bne print
setup: lda #$0f ; set initial ROW,COL
sta ROW
sta COL
draw: lda ROW ; ensure ROW is in range 0:31
and #$1f
sta ROW
lda COL ; ensure COL is in range 0:31
and #$1f
sta COL
ldy ROW ; load POINTER with start-of-row
lda table_low,y
sta POINTER
lda table_high,y
sta POINTER_H
ldy COL ; store CURSOR at POINTER plus COL
lda #CURSOR
sta (POINTER),y
getkey: lda $ff ; get a keystroke
beq getkey
ldx #$00 ; clear out the key buffer
stx $ff
cmp #$48
beq clear
cmp #$45
beq clear
cmp #$4C
beq clear
cmp #$4F
beq clear
lda #DOT ; set current position to DOT
sta (POINTER),y
pla ; restore A
cmp #$80 ; check key == up
bne check1
dec ROW ; ... if yes, decrement ROW
jmp done
check1: cmp #$81 ; check key == right
bne check2
inc COL ; ... if yes, increment COL
jmp done
check2: cmp #$82 ; check if key == down
bne check3
inc ROW ; ... if yes, increment ROW
jmp done
check3: cmp #$83 ; check if key == left
bne done
dec COL ; ... if yes, decrement COL
clc
bcc done
clear: lda table_low ; clear the screen
sta POINTER
lda table_high
sta POINTER_H
ldy #$00
tya
c_loop: sta (POINTER),y
iny
bne c_loop
inc POINTER_H
ldx POINTER_H
cpx #$06
bne c_loop
done: clc ; repeat
bcc draw
; these two tables contain the high and low bytes
; of the addresses of the start of each row
table_high:
dcb $02,$02,$02,$02,$02,$02,$02,$02
dcb $03,$03,$03,$03,$03,$03,$03,$03
dcb $04,$04,$04,$04,$04,$04,$04,$04
dcb $05,$05,$05,$05,$05,$05,$05,$05,
table_low:
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
; help message for the character screen
help:
dcb "T","y","p","e",32,"t","h","e",32,"f","o","l","l","o","w","i","n","g",32,"c","a","p","i","t","a","l",32,"l","e","t","t","e","r","s",32,"H","/","E","/","L","/","L","/","O",32,"o","n",32,"t","h","e",32,"k","e","y","b","o","a","r","d",".",32,"T","h","e",32,"p","i","x","e","l","s",32,"w","i","l","l",32,32,32,"f","l","a","s","h"
dcb 00
The difficulties encountered at present are: **Out of range branch on line 107 (branches are limited to -128 to +127): bcc draw**. This is the bug that the emulator shows when I want the keyboard to detect the sixth letter, and no solution has been found so far.
In the end, I found this assembly language if you want to display the sentence on the screen, you need the form "" + letter to display. This takes a lot of time. I actually envisioned the program as where you click on a letter and it will show up on the screen, like, type A or a and the monitor will show an A. This is beyond my capabilities. Assembly language is more complex and cumbersome than I thought. But I'm going to keep trying.
Comments
Post a Comment