Reputation: 23
So, I'm new to assembly programming, as well as the concept of "two's complement" representation of negative numbers, and I'm a little confused.
In the book that I'm reading on the subject of assembly (and on every online resource I've consulted), it is explained that you can tell whether or not a number is negative by looking at its most significant bit; if that bit is 1, it's negative (signed), otherwise, it's positive (unsigned). This doesn't make sense to me, however.
To illustrate my confusion, imagine the unsigned representation of 255: 11111111
That's a positive, unsigned value, but its most significant bit is set. According to the two's complement rules I've been reading about, that should make this number negative rather than positive, but it IS possible to have unsigned values like this with the most significant bit set (if you couldn't do this, an unsigned 8-bit integer could only represent the numbers 0-127). So how does this work? How can I tell if a number is signed or simply unsigned with the most significant bit set? And, possibly more importantly, how can a computer tell the difference? Am I just missing something?
Upvotes: 2
Views: 612
Reputation: 9272
It is the operation performed on value which defines whether it is signed or unsigned, not the value itself.
For example, if you perform mul
or div
, then the value is treated as unsigned and the most significant bit is the part of the number (thus allowing to perform operation on numbers 0 to 255), but imul
or idiv
treat the most significant bit as sign (allowing numbers from -128 to 127).
The same for conditional jumps
cmp al, 0x81
jb some_label_1 ; unsigned comparison
jl some_label_2 ; signed comparison
Upvotes: 3