How do I use negative numbers as a whole?

I have an arithmetic program in assembly, but when I add, subtract, multiply negative numbers, it does not produce the desired result.

Example

input:

-1+2=66675 (should be 1)

-1-1=656745 (should be -2)

-1*-1=66757 (should be 1) 

      

Questions:

  • how would i handle (-) and (1) as one?

  • how to perform arithmetic operation on signed numbers?

any advice please ...

+3


source to share


1 answer


I recommend reading 2 compliments and the difference between signed and unsigned ints. The value you are showing looks suspiciously like a signed int value negative that gets converted to unsigned int value without conversion. Negative ints have the most significant bit, which is set to 1. If you stuff that value into an unsigned int without first masking, then you end up with a much larger number than expected.

An example in 8-bit representation:



signed value = -1 
unsigned value = 255 
binary = 1111 1111

Take the twos compliment: 
       1111 1111
XOR    0000 0000
equals 0000 0000
add1   0000 0001
dec value = 1

      

You can read more here (they have an example to add two additions you can look at): http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

+1


source







All Articles