DEV Community

Michael Brackett
Michael Brackett

Posted on

Week 3 - Part 1

Inside of week 3 we learn about math, but not just any math, math inside the 6502 emulator. We also learnt about creating the equivalent of a constant inside the 6502 which is called a Define Constant Byte (dcb).

What's actually cool about the dcb is that you can store anything inside of one. Want to store a smiley face? map out the colours and store it inside of a dcb! Blue box? Samething!

You can also store variables using the 'define' keyword. A good use case example of this is like storing a colour inside of a define keyword, something like

define WHITE $01

Now onto math, I won't go super into detail about this but I think addition was pretty cool when Chris was explaining it.

Basically, we use the 'ADC' (add with carry) instruction where it will add whatever you want to the accumulator + the carry flag.

I think a good example of this is on the cdot wiki here

LDA #$F0      ; A=$F0
CLC           ; C=0
ADC #$20      ; Result is $F0+$20+C = $110; therefore A=$10 and C=1
LDA #$30      ; A=$30
ADC #$01      ; Value is $30+$01+C = $32; therefore A=$32 and C=0
Enter fullscreen mode Exit fullscreen mode

Where this code adds $30F0 and $0120 together and stores it.

You may be wondering a couple of things, what is LOWBYTE/HIGHBYTE, well in a previous blog post you can find my explanation of what that does and why it's here (6502 is a little endian, my explanation is here)

You might also be wondering, what dos CLC (or C) mean? that's the carry flag, when something goes above what a byte can represent you need to turn on the carry flag so that the program knows that we had somewhat of an 'overflow'

Addition is simple, but we can also do subtraction, division, multiplication which make up the basics of math operations!

Top comments (0)