DEV Community

Michael Brackett
Michael Brackett

Posted on

3 2

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
STA LOWBYTE
LDA #$30      ; A=$30
ADC #$01      ; Value is $30+$01+C = $32; therefore A=$32 and C=0
STA HIGHBYTE
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!

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay