Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do MIPS operations on unsigned numbers give signed results?

Tags:

unsigned

mips

When I try working on unsigned integers in MIPS, the result of every operation I do remains signed (that is, the integers are all in 2's complement), even though every operation I perform is an unsigned one: addu, multu and so fourth...

When I print numbers in the range [2^31, 2^32 - 1] I get their 'overflowed' negative value as if they were signed (I guess they are).

Though, when I try something like this:

li $v0, 1
li $a0, 2147483648                # or any bigger number
syscall

the printed number is always 2147483647 (2^31 - 1)

I'm confused... What am I missing?

PS : I haven't included my code as it isn't very readable (such is assembly code) and putting aside this problem, seems to be working fine. If anyone feels it is necessary I shall include it right away!

like image 753
dankilman Avatar asked Dec 29 '09 20:12

dankilman


People also ask

What is the difference between signed and unsigned in MIPS?

The only difference between signed and unsigned instructions is that signed instructions can generate an overflow exception and unsigned instructions can not.

What is the difference between a signed and an unsigned number?

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.

What is the largest unsigned number that you can store in a MIPS word?

In MIPS, the largest unsigned number is 232–1, or about 4.3 billion. Hexadecimal is frequently used as a shorthand for binary numbers, because one hex digit corresponds to four bits.

What causes overflow in MIPS?

Arithmetic overflow occurs during the execution of an add, addi, or sub instruction. If the result of the computation is too large or too small to hold in the result register, the Overflow output of the ALU will become high during the execute state. This event triggers an exception.


2 Answers

From Wikipedia:

The MIPS32 Instruction Set states that the word unsigned as part of Add and Subtract instructions, is a misnomer. The difference between signed and unsigned versions of commands is not a sign extension (or lack thereof) of the operands, but controls whether a trap is executed on overflow (e.g. Add) or an overflow is ignored (Add unsigned). An immediate operand CONST to these instructions is always sign-extended.

From the MIPS Instruction Reference:

ALL arithmetic immediate values are sign-extended [...] The only difference between signed and unsigned instructions is that signed instructions can generate an overflow exception and unsigned instructions can not.

like image 91
Pedro Silva Avatar answered Oct 21 '22 18:10

Pedro Silva


It looks to me like the real problem is the syscall that you are using to print numbers. It appears to and to always interpret what you pass as signed, and to possibly to bound as as well.

like image 27
John Knoeller Avatar answered Oct 21 '22 18:10

John Knoeller