开发者

How to calculate a negative number in assembly

开发者 https://www.devze.com 2023-01-26 19:42 出处:网络
I\'m a newbie in assembly and I have a question about how to represent negative numbers I have three 开发者_运维知识库DWORDS variable, let say:

I'm a newbie in assembly and I have a question about how to represent negative numbers I have three 开发者_运维知识库DWORDS variable, let say:

result DWORD 0
i DWORD 3
j DWORD 5

and I want to calculate this formula: result = i - j + 8 but, when i do the i-j, the result will be a very high number because of the sign so how do I make the result ok in the end?


For 32 bit DWORD the integer range is from –2147483648 to 2147483647 or in hex -0x80000000 to 0x7FFFFFFF.

So the number -1 is present like 0xFFFFFFFF. (Like counter underflow)

If the high (31) bit is set then the number is negative. To make positive number from negative (negation) you must make compement of number and add 1.

Example:

    0xFFFFFFFE   //-2
xor 0xFFFFFFFF   //binary complement 
---------------
    0x00000001   //result of complement
+   0x00000001   //add 1
---------------
    0x00000002   //Result of negation is 2


Notice:

If you compare two integers you use other jump command than comparing absolute numbers:

Comparing absolute numbers:

jg (jump if greater)
jl (jump if less)

Comparing integers (which can be negative or positive):

ja (jump if greater)
jb (jump if less)


Negative numbers are represented in Two's Complement in assembly. In order to obtain Two's Complement of a number you have two options:

  • to complement all it's bits and add one.
  • to complement all it's bits until the last 1
0

精彩评论

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