views:

29

answers:

2

I am reading the book art of assembly language. There I came across this paragraph.

 If the H.O. bit is zero, then the number is positive and is stored as a
 standard binary     value. If the H.O. bit is one, then the number is
 negative and is stored in the two’s comple-ment form. To convert a
 positive number to its negative, two’s complement form, you use the
 following algorithm:

    1) Invert all the bits in the number, i.e., apply the logical NOT function.

    2) Add one to the inverted result.

    For example, to compute the eight bit equivalent of -5:

    0000 0101  Five (in binary)

    1111 1010  Invert all the bits.

    1111 1011  Add one to obtain result.
    .

Here I want to know if 0000 0101 is 5 in decimals and 1111 1011 is -5 then how we represent 251?

Is not the same 1111 1011? How the computer distinguishes between -5 and 251?

+3  A: 

When you are representing signed numbers in 8 bits, the 8th bit (the HO bit) is the sign bit. Therefore you can only use 7 bits to store the value of the number. The range for a signed number in 8 bits is -128..127. 251 can't be represented, unless you use more than 8 bits.

RD
by the way, that is a great book :)p
RD
Oh, this was so easy. Did not come across my mind. Thanks a lot for quick reply.
@RD : agree, great book.
+2  A: 

The computer doesn't distinguish between the two -- it's up to you to use the appropriate instructions.

For example, on the x86, you use JA (jump if Above) and JB (jump if below) for unsigned numbers and JG (jump if greater) and JL (jump if less than) for signed numbers. Likewise you use IMUL and IDIV to multiply/divide signed numbers and MUL and DIV for unsigned numbers.

Jerry Coffin