Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do ADD/SUB signed or unsigned integer correctly?

Tags:

assembly

arm

I saw there is an ADD instruction on ARM, does it work for both signed and unsigned int? Some status flags should be different when the instruction is specified with S suffix, right? Such as setting overflow (V) flag. I am wondering is there another version of ADD/SUB to handle one of the integer.

like image 250
Thomson Avatar asked Jan 26 '14 03:01

Thomson


People also ask

How do you write a signed integer?

Signed integers are numbers with a “+” or “-“ sign. If n bits are used to represent a signed binary integer number, then out of n bits,1 bit will be used to represent a sign of the number and rest (n - 1)bits will be utilized to represent magnitude part of the number itself.

What signed and unsigned integer?

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.

How do you store signed and unsigned integers?

Variables such as integers can be represent in two ways, i.e., signed and unsigned. Signed numbers use sign flag or can be distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.

How are signed and unsigned integers explain with an example?

Signed and unsigned are those two ways. Since every integer can be identified using it's sign such as positive (+) or negative (-). On the other hand in an unsigned integer can not store negative values, only positive values can be stored in the unsigned variable. Example of signed integer: Sign int a = -4.


2 Answers

Because ARM uses two's complement representation, signed and unsigned addition (similarly subtraction) are the same thing. The only difference is how you interpret the flags afterwards, if you set them with the s suffix.

like image 112
Notlikethat Avatar answered Oct 06 '22 01:10

Notlikethat


Twos complement means there is no difference between signed and unsigned addition. The s bit determines whether any flags are modified or not IF modified, then carry is the unsigned overflow/borrow and v is the signed overflow/borrow. This is all described in ARMs documentation.

like image 23
old_timer Avatar answered Oct 06 '22 00:10

old_timer