# Sign

How the assembler knows if value in register is signed or unsigned???

• There are really no "Signed" registers, but the x86 CPU can be

made to act as if there was. To simulate a sign, the highest

bit of the register is either 0 for positive or 1 for negative.

To do mathematic operations using signed numbers, you need to use

different commands, i.e. using IDIV instead of DIV treates the

numbers as signed integer values.

I'm not sure how you do the addition/subtraction, but I've never used

signed variables via assembly myself...

URL:http://acheronx.ml.org/home/

• Well, that's almost correct

OK... Pretend we're working with a signed word.

A number (signed) is negative if it is between

32768 and 65535. 65535 is -1. This is handy

because 0 - 1 = 65535 because of underflow!!!

32768 is -32768! Experiment to see how it is

that way. It just simplifies things because

with addition and subtraction, you don't need

to worry about the sign - 3 + (-6) = 65533 = -3!!

It is true for multiplication/division that you

need to use IDIV and IMUL. Experiment to see

for yourself!!! (9 / -3 = -3; 3 / 65533 = ??)

*it might work to do MUL /w signed nums IN SOME

CASES!*

Get it??? Mail me if you need more help...

- SlycerMan

• : There are really no "Signed" registers, but the x86 CPU can be

: made to act as if there was. To simulate a sign, the highest

: bit of the register is either 0 for positive or 1 for negative.

: To do mathematic operations using signed numbers, you need to use

: different commands, i.e. using IDIV instead of DIV treates the

: numbers as signed integer values.

: I'm not sure how you do the addition/subtraction, but I've never used

: signed variables via assembly myself...

You are correct, the assembler and even the processor do not know

or even care whether you are working with signed or unsigned numbers.

It is all your job to remember, and use the special IMUL and IDIV

instructions.

The x86 stores negative numbers in what's called "twos complement."

To get the negative of a number, you do a "ones complement", changing

every 0 bit to 1 and 1 to 0, and then you add 1.

Thus 1 (0000000000000001) turns into

-1 (1111111111111111) .

And you could work out the rest for yourself.

They do this because with signed numbers kept this way, addition and

subtraction do not have to be recoded, they work just the same as always.

See for yourself:

1 (0000000000000001)

+ -1 (1111111111111111)

_____

0 (0000000000000000) with a carried 1

You ignore the carry and voila: 1 + (-1) = 0.

Same for all the other negative numbers.

Twos complement also has the use of following the "one in the high bit

for negative" behavior. So that you can quickly determine if a number

is positive or negative by examining the high bit.

As you may see, multiplication and division to not work the same, so

we have special instructions for them.

Am I helping?