DEV Community

Cover image for Easiest program in 6502 emulator part-3
Yukti Manoj Mulani
Yukti Manoj Mulani

Posted on

Easiest program in 6502 emulator part-3

Hii
Welcome to the final reveal!! If you have not done so already read my last blog so you get some context.In this blog I am going to reveal why does my easiest program in 6502 assembly says that 1+2 is not 3 but 4.

Here is some background

The 6502 processor is limited to very simple math operations, and various processor flags affect these operations. Addition and subtraction only.

Addition
The ADC (add with carry) instruction adds together:

the value in the accumulator + the specified byte + the carry flag
If the unsigned value overflows a single byte, the carry flag will be set.

It is therefore important to clear the carry flag (with CLC) before adding the lowest byte of a single or multi-byte value. If a multi-byte addition is performed starting with the lowest byte and proceeding to the highest byte, the carry flag will correctly carry bits forward from one byte to the next. For example, this code adds $30F0 and $0120:

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

The Overflow (V) flag is set if the signed result of the operation is above 127 ($7f) or below -128 ($80).

Subtraction

The SBC (subtract with carry) operation subtracts:

the value in the accumulator - the specified byte - (not Carry)
Where the not operation inverts the value of the carry flag.

The carry flag (which can be viewed as a “borrow” flag for subtraction) is cleared if the result underflows $00 and set otherwise (in other words, if doing unsigned math, the Carry flag is cleared if a bit must be “borrowed”).

Normally, you will set the carry flag (with SEC) before performing a subtraction on the lowest byte of a single- or multi-byte subtraction, and then perform subtraction on each byte in sequence up to the highest byte. The borrows will automatically be carried from one byte to the next.

The Overflow (V) flag is set if the signed result of the operation is above 127 ($7f) or below -128 ($80).

Final Reveal

I think most of you might have guessed it by now that this program adds the carry flag along with adding 1 and 2 so technically it is performing 1+2+1=4.
So simple right!! The code that I provided in the last blog does not really work at this stage because it still misses the arithmatic part. I am still figuring somethings out like what would be its name?

I would love your suggestion in the comment section. However, I have come up with a name "Dumb Daniel". Sorry no offense for the Daniels out there but it just goes with the name.
So, let me know what do you think about the name and the program. Could it be more crazier or dumb?

Thank you for reading through I will keep you updated.😊

Top comments (0)