Search code examples
assemblyx8664-bitx86-64

x64 asm: Moving a negative value from a register to memory


Suppose I have defined a byte in .data called val. Suppose also that I have a number in register r10, let's say it is 12.

I am trying to get a 2s compliment of the number in the r10 and store it for future use in val.

So I do this:

neg r10
mov [val], r10b

But when I try to use the value in val later (or prod around with a debugger), it gets stored and treated as an unsigned value, so if r10 was -12, val is now 244, which is the unsigned equivalent.

How can I move the register to memory while preserving the sign? Alternatively, how do I set the sign after I move that memory back to a different register?


Solution

  • A signed 8-bit integer, i.e. -12 is stored as 244. When you try to read the value with a debugger, it will treat the number as an unsigned byte and will show 244. If you tell your debugger to treat the memory as signed bytes, it will work properly.