2

I'm currently attempting to write an NES emulator through .NET and I have a question about the particular opcodes that do decrementing and incrementing...

Since X, and Y registers are 8 bits, in terms of implementation, is it an unsigned or signed byte? That is, is the value range of the X and Y registers from -128 to 127 or 0-255?

I am confused by this because if the X and Y registers are initialized as 0, what happens when a DEX is performed? Or is it up to the programmer to actually worry about that?

Thanks in advance for the help everyone.

urbanspr1nter
  • 1,347
  • 2
  • 16
  • 25
  • 1
    It's not directly part of your question, and Dougvj's point about two's complement arithmetic is worth absorbing, but X and Y are considered to be unsigned since e.g. `LD X,$FF` then `LD $5200,X` will load from $52FF, not from $51FF. – Tommy Aug 17 '12 at 19:55

1 Answers1

4

Interestingly enough with two's complement signed numbers there is no difference when performing arithmetic, therefore DEX is agnostic as to whether the register contains a signed or unsigned number. For example, the bits representing -1 are the same as those representing 255. So 0 - 1 = 255 or -1 depending on your interpretation. The decrementation doesn't care.

Dougvj
  • 6,409
  • 2
  • 23
  • 18
  • I see, so this means it is actually the SIGN bit from the status register that gets read that determines if it is actually a negative? – urbanspr1nter Aug 17 '12 at 03:24
  • 1
    Yes, which is equivalent to the highest order bit (so 127 = 01111111; 128 - 256 = 1XXXXXXX = -126 - -1) Does that make sense? – Dougvj Aug 17 '12 at 05:33
  • Yes, this definitely does make sense to me now. Thank you for your answer! – urbanspr1nter Aug 17 '12 at 23:17