6502 Math and Strings Lab

The 6502 processor, an 8-bit microprocessor prominent in early computing, adeptly performs basic arithmetic like addition and subtraction. However, its simplicity poses challenges for complex mathematical tasks, often requiring multiple instructions.

In string manipulation, the absence of dedicated commands necessitates manual coding with load, store, and compare instructions. Programmers navigate character arrays for operations like concatenation, showcasing resource-conscious techniques in the absence of specialized string-handling instructions.

Code for plotting a graph:

; ROM routines

define SCINIT $ff81 ; initialize/clear screen

define CHRIN $ffcf ; input character from keyboard

define CHROUT $ffd2 ; output character to screen

define SCREEN $ffed ; get screen size

define PLOT $fff0 ; get/set cursor coordinates


; MEMORY LOCATION

define INPUT_X $2000

define INPUT_Y $3000


; Drawing X and Y axis units.

LDA #$0E

STA $0200

STA $0300

STA $0400

STA $0500

STA $05e0


STA $05e8

STA $05f0

STA $05f8

STA $05ff


JSR SCINIT ; initialize and clear the screen

LDY #$00

LDX #$00


; Prompting to enter 5 co-ordinates for X and Y axis.

GO_TO_X:

INX

LDA #$20

JSR CHROUT

LDY #$00

LDA #$0A

JSR CHROUT

JSR CHROUT

PRINT_MSG_X:

LDA GET_MSG_X, Y

JSR CHROUT


INY

CPY #$15

BNE PRINT_MSG_X


; Getting X co-ordinates from user

LDY #$00


LDA #$A0

JSR CHROUT

LDA #$83

JSR CHROUT

GET_X:

JSR CHRIN

CMP #$00

BEQ GET_X


CMP #$0D

BEQ GO_TO_Y


CMP #$30 ; C=1 IF 0>=30; C=0 IF 0<30 ; 0 is decimal and 30 is hex

BCC GET_X ; IF LOWER THAN 0 THEN GET ANOTHER NUMBER

CMP #$35

BCS GET_X


CPY #$01

BEQ GET_X


STA INPUT_X, X

JSR CHROUT


LDA #$A0

JSR CHROUT

LDA #$83

JSR CHROUT


INY

JMP GET_X


; Getting Y co-ordinates from user

GO_TO_Y:

LDA #$20

JSR CHROUT

LDY #$00

LDA #$0A

JSR CHROUT

PRINT_MSG_Y:

LDA GET_MSG_Y, Y

JSR CHROUT


INY

CPY #$15

BNE PRINT_MSG_Y


LDY #$00

LDA #$A0

JSR CHROUT

LDA #$83

JSR CHROUT

GET_Y:

JSR CHRIN

CMP #$00

BEQ GET_Y


CMP #$0D

BEQ GO_TO_X


CMP #$30 ; C=1 IF 0>=30; C=0 IF 0<30 ; 0 is decimal and 30 is hex

BCC GET_Y ; IF LOWER THAN 0 THEN GET ANOTHER NUMBER

CMP #$35

BCS GET_Y


CPY #$01

BEQ GET_Y


STA INPUT_Y, X

JSR CHROUT


LDA #$A0

JSR CHROUT

LDA #$83

JSR CHROUT

CPX #$05

BEQ PLOT_GRAPH_0


INY

JMP GET_Y



GET_MSG_X:

dcb "E","N","T","E","R",$20

dcb "X",$20,"C","O","-","O"

dcb "R","D","I","N","A","T"

dcb "E",":",$00

GET_MSG_Y:

dcb "E","N","T","E","R",$20

dcb "Y",$20,"C","O","-","O"

dcb "R","D","I","N","A","T"

dcb "E",":",$00

PLOT_GRAPH_0:

LDX #$00


PLOT_GRAPH:


LDA #$00

STA $40

LDA #$05

STA $41


TXA          ; Load the value in the X register into the accumulator

CLC            ; Clear the carry flag

ADC $3001          ; Add the value in the $3000 to the accumulator

STA $200          ; Store the result in the $200

LDY $200

CPY #$30

BEQ YValueIs0

CPY #$31

BEQ YValueIs1

CPY #$32

BEQ YValueIs2

CPY #$33

BEQ YValueIs3

CPY #$34

BEQ YValueIs4


YValueIs0:

LDA #$e0

STA $40

LDA #$05

STA $41

JMP XPLOT

YValueIs1:

LDA #$05

STA $41

JMP XPLOT

YValueIs2:

LDA #$04

STA $41

JMP XPLOT

YValueIs3:

LDA #$03

STA $41

JMP XPLOT

YValueIs4:

LDA #$02

STA $41

JMP XPLOT

XPLOT:

LDY $2001


TXA          ; Load the value in the X register into the accumulator

CLC            ; Clear the carry flag

ADC $2001          

STA $200          

LDY $200

CPY #$30

BEQ XValueIs0

CPY #$31

BEQ XValueIs1

CPY #$32

BEQ XValueIs2

CPY #$33

BEQ XValueIs3

CPY #$34

BEQ XValueIs4


XValueIs0:

LDY #$00

JMP PLOTING

XValueIs1:

LDY #$08

JMP PLOTING

XValueIs2:

LDY #$10

JMP PLOTING

XValueIs3:

LDY #$18

JMP PLOTING

XValueIs4:

LDY #$1f

JMP PLOTING


PLOTING:

LDA #$07

STA ($40),Y

INX

CPX #$05

JMP PLOT_GRAPH


In the above code, I am taking input from the user for X and Y co-ordinates from the user and based on the input I am plotting the co-ordinates with yellow dots.

The blue dots on the left and bottom edges are single units on X and Y axis.

String uses:
    I am using strings to prompt the user to enter 5 co-ordinates ranges from 0 to 4.

Limitations - No backspace allowed
    Once the co-ordinate is set, it cannot be changed
    Only 0 to 4 inclusive numbers can be entered

Math uses:

TXA            ; Load the value in the X register into the accumulator

CLC            ; Clear the carry flag

ADC $2001          

STA $200          

LDY $200

In my code, I am using math to determine the location where to put the yellow dot.

$2001 memory location stores the first input for X co-ordinate.
X register value increments for each loop to switch to another X co-ordinate.
The final result is then stored in the $200 location and then to Y register.

After this we are checking for values for the X co-ordinate and determining the low bit which decides the location for the yellow dot on X axis.

Similarly, $3001 is storing Y axis co-ordinates and determining the high bit to decide on the yellow dot's Y axis location.

In the example above, I am using the following co-ordinates: (0,0), (1,1), (2,2), (3,3), (4,4). These co-ordinates makes a straight line at 45 degree angle.

Comments

Popular posts from this blog

Project stage 2

Project Stage 1 Inspection

AArch64 and x86_64 assembler code