News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Signed or Unsigned?

Started by unktehi, February 22, 2009, 08:17:55 PM

Previous topic - Next topic

unktehi

I'm taking a class right now on the intel assembly language and I still have a concept I'm not quite grasping in pertaining to the sign bit.  If an 8bit binary digit ends with a 1 (10101010) it is always negative - correct? What if someone was trying to represent the number 170? Wouldn't it also be 10101010? In short, if the last bit is always the sign bit, then how would you represent 170?

Thanks in advanced.

BogdanOntanu

#1
It is a matter of convention. The way you consider a number as being "signed" on "unsigned" depends on your program logic.

The CPU has this "SIGN" flag and some specific instructions to help you with this but ultimately it does depend on you.

For example the conditional jumps instructions have a version that is "unsigned" and another version that is "signed".

That is why in a high level language you have to specify if a variable is signed or not in order for the compiler to choose the correct instructions.

In ASM you can choose the right instruction yourself ... or another option is to instruct MASM to  make a "signed" compare by using "SDWORD PTR" or "SWORD PTR" or "SBYTE PTR" keywords.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

GregL

unktehi,

Data Type Range of Values

Signed SBYTE:    –128 to 127
Unsigned BYTE:   0 to 255
Signed SWORD:    –32,768 to 32,767
Unsigned WORD:   0 to 65,535
Signed SDWORD:   –2,147,483,648 to 2,147,483,647
Unsigned DWORD:  0 to 4,294,967,295

If you wanted to represent 170 as signed, you would need to use an SWORD.

tenkey

Quote from: unktehi on February 22, 2009, 08:17:55 PM
If an 8bit binary digit ends with a 1 (10101010) it is always negative - correct? What if someone was trying to represent the number 170? Wouldn't it also be 10101010? In short, if the last bit is always the sign bit, then how would you represent 170?
A 1 does not always mean negative.

However when a 1 means negative (and you get to choose whether it does or not) ...

The true negative number does not "end" with a 1. The bits that you see end with a 1 because the machine can only handle a limited number of bits.

The true value of -1 (in two's complement) is ...11111111 where "..." is an infinite number of 1's, just as the true value of 0 is ...00000000 where "..." is an infinite number of 0's. The sign bit is an abbreviation for the infinite number of 1's or 0's to the left of the other bits. That's why you need to "sign extend" when you convert a signed number from one data size to a larger data size.

You can't fit the value of decimal 512 in 8 bits, so you use a larger data size to hold the value. You can't fit 170 in 8 bits if you intend to hold negative numbers in 8 bits. The solution is the same -- use a larger data size.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8