0

I'm new to 6502 Assembly and hexadecimal in general, so I'm really confused about how arithmetics work.

For example:

LDA #$c0
TAX
INX
ADC #$c4
BRK

c0 + c4 is too big for a byte, implying that it's 192 + 196. Thus setting the carry flag and resulting in the hex value 84.

But on the other hand, doing only LDA #$c0 sets the negative flag, implying that c0 isn't actually 92 but -64. So the operation becomes -64 + -60. That result is -124. That would "fit" as it's still the hex value 84.

I just can't wrap my head around it. Does 6502 Assembly use 0 to 255 for hex values or -127 to 127? Why does the code above set the carry flag?

Styn
  • 191
  • 2
  • 9
  • The CPU doesn't care what it is, it just does binary operations that produce both sets of flags. It's up to you to write code that looks at the relevant flags for signed vs. unsigned numbers. And BTW, the 2's complement 8-bit range is from -12**8** to +127. – Peter Cordes Feb 28 '22 at 11:31
  • 1
    "Compared to other systems for representing **signed** numbers (e.g., ones' complement), two's complement has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are **identical** to those for **unsigned** binary numbers ... This property makes the system simpler to implement, especially for higher-precision arithmetic." (In this case, higher precision means for > 1 byte.) [Wikipedia](https://en.wikipedia.org/wiki/Two%27s_complement) – Nick Westgate Feb 28 '22 at 12:07
  • Aside: `ADC` adds in the present `C`arry flag, as well as setting it as part of the result. So you need `SEC` or `CLC` first, unless you want to add in its previous value. Other instructions affect `C` too. The 6502 does not have an `ADD` instruction. – Weather Vane Feb 28 '22 at 17:43

0 Answers0