SPO600 - Lab03
Published: 2022-02-07 19:20:36

For lab 03, we were supposed to create a basic game or calculator using 6502 assembly language.

## My idea

We the professor noticed us, Assembly language is hard, and we should keep it simple as much as we can.
My idea was to create a simple calculator that would divide any number from 0 to 9 by 2 using LSR(Logical Shift Right).

## The way it works

First I display a message to the user:
`Type a number from 0 to 9:`

I did it using `dcb` and `CHROUT`, this way:

``` 
LDY #$00

nextchar:  ;DISPLAY INITIAL MESSAGE

	LDA msg,Y
	BEQ getnumber
	JSR CHROUT
	INY
	BNE nextchar	

msg:
dcb "T","y","p","e",32,"a",32,"n","u","m","b","e","r",32,"f","r","o","m",32,"0",32,"t","o",32,"9",":",0
```

Then I get the input from the user using `CHRIN`:
``` 
getnumber:   ; ACCEPT ONLY NUMBERS

	LDY #$00
	JSR CHRIN

	CMP #$00
	BEQ getnumber

	CMP #$30
	BMI getnumber

	CMP #$39
	BPL getnumber

	LDX A ;Load x register with A 

	JSR CHROUT

```

This code means accept only numbers from 0 to 9, if user enters something different than that then it will be ignored.

After that I display the following message:
`Your number dividev by to is: `
```
dividemsg: ;DISPLAY DIVIDE MESSAGE
	
	LDA msg2,y
	BEQ result
	JSR CHROUT
	INY
	BNE dividemsg	

msg2:
dcb $0d,"Y","o","u","r",32,"n","u","m","b","e","r",32,"d","i","v","i","d","e","d",32,"b","y",32,"2",32,"i","s",":",0
```

After the message is displayed the result should be outputs in the `result` label:

```
result:  ; GET THE RESULT - LSR DIVIDE BY 2

	LDY #$00
	CPX #$00 ;Compare X with 0
	BEQ zero ;Display divide by 0 message

	LSR X   ;Divide by 2
	LDA X	; load result in A
	JSR CHROUT ;Display the result

```

> It is important to say that I could not make this part work properly, the result is not displaying to the user.

And if the user is trying to divide by 0, then a message should be displayed:

```
zero:	;MESSAGE IN CASE DIVIDING BY 0

	LDA msg3,y
	BEQ done
	JSR CHROUT
	INY
	BNE zero


done: BRK
```


The entire code can be found here:

```

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

	
	LDY	#$00

nextchar:  ;DISPLAY INITIAL MESSAGE

	LDA msg,Y
	BEQ getnumber
	JSR CHROUT
	INY
	BNE nextchar	

getnumber:   ; ACCEPT ONLY NUMBERS

	LDY #$00
	JSR CHRIN

	CMP #$00
	BEQ getnumber

	CMP #$30
	BMI getnumber

	CMP #$39
	BPL getnumber

	LDX A ;Load x register with A 

	JSR CHROUT



LDY #$00

dividemsg: ;DISPLAY DIVIDE MESSAGE
	
	LDA msg2,y
	BEQ result
	JSR CHROUT
	INY
	BNE dividemsg	


result:  ; GET THE RESULT - LSR DIVIDE BY 2

	LDY #$00
	CPX #$00 ;Compare X with 0
	BEQ zero ;Display divide by 0 message

	LSR X   ;Divide by 2
	LDA X	; load result in A
	JSR CHROUT ;Display the result

	

zero:	;MESSAGE IN CASE DIVIDING BY 0

	LDA msg3,y
	BEQ done
	JSR CHROUT
	INY
	BNE zero


done: BRK

msg:
dcb "T","y","p","e",32,"a",32,"n","u","m","b","e","r",32,"f","r","o","m",32,"0",32,"t","o",32,"9",":",0


msg2:
dcb $0d,"Y","o","u","r",32,"n","u","m","b","e","r",32,"d","i","v","i","d","e","d",32,"b","y",32,"2",32,"i","s",":",0

msg3:
dcb 32,"C","a","n","n","o","t",32,"d","i","v","i","d","e",32,"0","!",0

```