开发者

How can I store numbers > 128 in a signed one byte interger?

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 com开发者_运维知识库plement 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜