SPO600 Lab 3 - Math and Strings - Guessing Numbers.
Tags: beginners, tutorial
Published: 2022-09-27 11:01:08

Assembly language is a type of low-level programming language that is intended to communicate with a computer's hardware more easily than high level languages. A high level language require compiler or interpreter to convert the language to binary code.

I will be using [6502 Emulator](https://wiki.cdot.senecacollege.ca/wiki/6502_Emulator) available at [http://6502.cdot.systems/](http://6502.cdot.systems/).

In lab 3 the task was to write either a game or a program that calculates or converts a value using 6502 Emulator.

I have decided to code a number guessing game where the player has to guess a random number. The goal is to guess the number with least amout of clues.

Two major tasks are:

- Accept number from the player.
- Compare two numbers.
- Generate a random number and store it.
- Compare the number accepted by the user with the random number.

```
; Comparing two numbers

; ROM routine entry points
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

; zeropage variables
define        PRINT_PTR    $10
define        PRINT_PTR_H    $11
define        value        $14
define        value_h        $15

; absolute variables
define        GETNUM_1    $0080
define        GETNUM_2    $0081

; constants

; --------------------------------------------------------

        jsr SCINIT
        jsr CHRIN

        jsr PRINT

dcb "C","o","m","p","a","r","i","n","g",32,"t","w","o",32,
dcb "n","u","m","b","e","r","s",00

start:        jsr PRINT

dcb $0d,$0d,"E","n","t","e","r",32,"a",32,"n","u","m","b","e","r"
dcb "(","0","-","9","9",")",":"
dcb 32,32,32,32,32,32,32,32,00

        lda #$00
        sta value_h

        jsr GETNUM
        sta value

        jsr PRINT

dcb "E","n","t","e","r",32,"a","n","o","t","h","e","r"
dcb 32,"n","u","m","b","e","r",32,"(","0","-","9","9",")",":",32,00

        jsr GETNUM

        sed        ; set decimal
        cmp value
        cld        ; clear decimal flag (switches into binary math mode)

        bcc toohigh    ; if second value is less than the first value, go to toohigh
            beq identical    ; if second value is same as first one, 
        inc value_h    ; increment memory
toolow:           pha        ; push the accumulator
            jsr PRINT

dcb "N","u","m","b","e","r",32,"o","n","e",32,
dcb "i","s",32,"s","m","a","l","l","e","r",32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 00
            jsr start

identical:        pha        ; push the accumulator
            jsr PRINT

dcb "C","o","r","r","e","c","t",32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 00
            jsr start

toohigh:        pha        ; push the accumulator
            jsr PRINT

dcb "N","u","m","b","e","r",32,"o","n","e",32,
dcb "i","s",32,"g","r","e","a","t","e","r",32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 32,32,32,32,32,32,32
dcb 00
            jsr start

; --------------------------------------------------------
; Print a message
; 
; Prints the message in memory immediately after the 
; JSR PRINT. The message must be null-terminated and
; 255 characters maximum in length.

PRINT:        pla    ; pull the accumulator
        clc    ; clear carry flag (C) - required before using ADC
        adc #$01    ; add with carry
        ;sec ; set carry flag (C) - required before using SBC
        ;sbc #$01
        sta PRINT_PTR
        pla
        sta PRINT_PTR_H

        tya    ; transfer Y to A
        pha    ; push the accumulator

        ldy #$00
print_next:    lda (PRINT_PTR),y
        beq print_done

        jsr CHROUT
        iny
        jmp print_next

print_done:    tya
        clc
        adc PRINT_PTR
        sta PRINT_PTR

        lda PRINT_PTR_H
        adc #$00
        sta PRINT_PTR_H

        pla
        tay

        lda PRINT_PTR_H
        pha
        lda PRINT_PTR
        pha

        rts

; ---------------------------------------------------
; GETNUM - get a 2-digit decimal number
;
; Returns A containing 2-digit BCD value

GETNUM:        txa        ; transfer X to accumulator
        pha        ; push the accumulator
        tya        ; transfer Y to A
        pha

        ldx #$00    ; count of digits received
        stx GETNUM_1    ; store the X register
        stx GETNUM_2


getnum_cursor:    lda #$a0    ; black square
        jsr CHROUT
        lda #$83    ; left cursor
        jsr CHROUT

getnum_key:    jsr CHRIN
        cmp #$00
        beq getnum_key

        cmp #$08    ; BACKSPACE
        beq getnum_bs

        cmp #$0d    ; ENTER
        beq getnum_enter

        cmp #$30    ; "0"
        bmi getnum_key

        cmp #$3a    ; "9" + 1
        bmi getnum_digit

        jmp getnum_key

getnum_enter:    cpx #$00
        beq getnum_key

        lda #$20
        jsr CHROUT
        lda #$0d
        jsr CHROUT

        lda GETNUM_1

        cpx #$01
        beq getnum_done

        asl
        asl
        asl
        asl
        ora GETNUM_2

getnum_done:    sta GETNUM_1
        pla
        tay
        pla
        tax
        lda GETNUM_1

        rts

getnum_digit:    cpx #$02
        bpl getnum_key
        pha
        jsr CHROUT
        pla
        and #$0f
        sta GETNUM_1,x
        inx
        jmp getnum_cursor

getnum_bs:    cpx #$00
        beq getnum_key
        lda #$20
        jsr CHROUT
        lda #$83
        jsr CHROUT
        jsr CHROUT
        lda #$20
        jsr CHROUT
        lda #$83
        jsr CHROUT
        dex
        lda #$00
        sta GETNUM_1,x
        jmp getnum_cursor
```
Here is the result for the program.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzbksxrpdqhske3i89nd.png)

So in this blog 2 out of 4 tasks have been completed.
I will write one more blog to complete the guessing game.