DEV Community

Gustavo Tavares
Gustavo Tavares

Posted on

SPO600 - W3

In week 3 was time to learn about:

  • Math
  • Characters
  • and strings!
  • Branches, Jumps and Procedures

About Math

6502 Assembly language can perform calculations on binary and decimal mode.

In binary, it will perform the operations on a 8-bite value.
In decimal, the bytes are treated as 2 decimal digits.

The operations are:

  • ADC (Add with Carry)
    This will perform the following: the value in the accumulator + the specified byte + the carry flag.

  • SBC (Subtract with Carry)
    This will perform the following: the value in the accumulator - the specified byte - (not Carry)

  • LSR (Logical Shift Right)
    This will perform a division by 2.

  • ASL (Arithmetic Shift Left)
    This will perform a multiplication by 2.

About Characters and Strings

In 6502, we can use dcb to make a string. Keep in mind that if you want to use enter you need to use $0d and 32 for space, for example:

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
Enter fullscreen mode Exit fullscreen mode

The 0 at the end is to control the end of the string.

Having this, we can use JSR CHROUT to output the values to the screen.

LDY #$00
nextchar:  ;DISPLAY INITIAL MESSAGE

    LDA msg,Y
    BEQ getnumber
    JSR CHROUT
    INY
    BNE nextchar    
Enter fullscreen mode Exit fullscreen mode

For Input, we can use CHRIN to get input from user.

getnumber:   ; ACCEPT ONLY NUMBERS

    LDY #$00
    JSR CHRIN

    CMP #$00
    BEQ getnumber

    CMP #$30
    BMI getnumber

    CMP #$39
    BPL getnumber

    JSR CHROUT
Enter fullscreen mode Exit fullscreen mode

This code will accept only a number from 0 to 9 from the user.

Branches, Jumps and Procedures

Branches, Jumps and Procedures are how we can control the flow of our program.
Branches can be used for conditional jumps
For example:
We can use CMP #$00 (Compare to 0) and then BNE branchName (Branch if not equal) or BEQ branchName (branch if eqqual).

Jumps can be used to perform a change in the branch independently of any conditions.
For Exemple:
JMP done will bring you to the done label.

The procedures will transfer the control to a subroutine.

For example, if you want to execute a subroutine you can:
JSR myfunction

Myfunction: …
RTN

Note that in your subroutine you need to return.

The system have some already pre defined sobroutines:

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
Enter fullscreen mode Exit fullscreen mode

This was it for W3

Thank you so much for reading!

Top comments (0)