Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

ADC = A + V + C

SBC = A - V - (1 - C)

SBC = A + (-1 * (V - (1 - C)))

SBC = A + (-V + (1 - C))

SBC = A + -V + 1 + C

I am not able to follow the mathemagic behind the third line. Is it normal algebra or its in 1's complement?



I think it looks odd, too. If you go with this:

    ADC = A + V + C
    SBC = A - V - (1 - C)
    SBC = A + (-1 * (V - (C - 1)))
    SBC = A + (-V + (C - 1))
    SBC = A + -(V + 1) + C
"The SBC instruction subtracts a value from the accumulator register, with an extra 1 subtracted from the result if the carry flag is NOT set."

The explanation fits though. If the carry flag is not set (C == 0) then you subtract an extra 1. If C == 1 then it's A-V.


Wow, yeah, that's a bit off. And incorrect. It should end up being equivalent to:

SBC = A - V - 1 + C

Thanks. I'll fix this up :)


> but the - 1 is taken care of because of the fact that ADC and SBC do the opposite thing with the carry flag.

Also, this line is bothersome, they do opposite things but that's why you have already done (1 - C).

Maybe the fact that -V is actually (!V + 1) (in 2's complement) that eats up the extra -1 is the correct explanation?

So,

  SBC = A - V - 1 + C

  SBC = A + !V + 1 - 1 + C

  SBC = A + !V + C


Ah yeah you’re right. I had a brain fart and got my -V and !V confused.

At the time of writing I was sure I had it right too!

I’ll fix that up. Thanks :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: