DEV Community

Gustavo Tavares
Gustavo Tavares

Posted on

SPO600 W2 - Introduction to 6502!

6502!

For week 2, we learned how to start programming in assembly language using 6502!

Assembly language is the more readable way of writing machine language.

It looks something like this:

Example from: 6502 Assembly Language Lab - CDOT Wiki (senecacollege.ca)

    lda #$00    ; set a pointer at $40 to point to $0200
    sta $40
    lda #$02
    sta $41

    lda #$07    ; colour number

    ldy #$00    ; set index to 0

loop:   sta ($40),y ; set pixel at the address (pointer)+Y

    iny     ; increment index
    bne loop    ; continue until done the page

    inc $41     ; increment the page
    ldx $41     ; get the current page number
    cpx #$06    ; compare with 6
    bne loop    ; continue until done all pages

Enter fullscreen mode Exit fullscreen mode

Lets try to read what is written there.

The Instructions

The firs 3 letters that you see like lda, sta or ldy are called instructions.
They work like commands and will perform a certain action on the memory( which is something like #$00).

The list of instructions are:

ADC AND ASL BCC BCS BEQ BIT BMI BNE BPL BRK BVC BVS CLC
CLD CLI CLV CMP CPX CPY DEC DEX DEY EOR INC INX INY JMP
JSR LDA LDX LDY LSR NOP ORA PHA PHP PLA PLP ROL ROR RTI
RTS SBC SEC SED SEI STA STX STY TAX TAY TSX TXA TXS TYA
Enter fullscreen mode Exit fullscreen mode

A total of 56 instructions are available.
The ones used in our code are:
Lda (load a)
Sta (store a)
Ldy (load y)
Iny(increment y)
Inc (increment a)
Ldx (load x)
Cpx (compare x)
Bne (branch if not equal)

Something else that we need to understand are where the values are stored. In our case, we use Register-Memory A, X and Y.
Sta - this means store the accumulator
Stx- Store the X register
Sty - Store the Y register

In our example we can see:
Ld (load) a and y (lda and ldy), these mean load values from a and y registers.

We also can see a label on our code: loop

The label will work together with the bne instruction. Thebne means branch if not equal, and it works like a while true. Every time the condition is not equal, it will branch to the label, which means it will go back to the start of the loop.

Finally

This was a really short introduction to 6502 assembly language, but I hope it was helpful to get you to understand how to start on it.
It looks really confusing at first, but when you start to understand how the instructions work it start to make sense and gets easier and enjoyable.

Thank you for reading!

Top comments (0)