English 中文(简体)
Why do MIPS operations on unsigned numbers give signed results?
原标题:

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!

最佳回答

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.

问题回答

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.





相关问题
Assembly Coding Standards / Best Practices

I ve know 8086 Assembly and now I m learning MIPS Assembly by reading the books MIPS Assembly Language Programming and See MIPS Run, but I never stopped to think about the coding standards/best ...

How to write a Hello World Bootloader for MIPS?

I m learning MIPS Assembly by the book MIPS Assembly Language Programming, but my I ve just started learning MIPS because I want to build a MIPS OS, but now as I can see, there isn t any documentation ...

Beginner Errors On When Compiling a MIPS Assembly Source

I m trying to learn MIPS Assembly by learning MIPS Assembly Language Programming. In the book I have this code(extracted from the page 37 of the book): .data prompt: .asciiz " Please Input a Value: ...

MIPS Assemblysign HELP!

我最亲密的朋友正在通过EE课程(我最后希望:......),我知道大约7年前的贾瓦,但他(直线)最近的EE方案拟订任务是使用MIPS......。

How do I compile mips gnu?

I am not even sure my thread title is correct or not. Here is my story. I visited western digital website to check for a new firmware of wdtv live. I found source code of wdtv live OS is available to ...

Efficient Way to Print MIPS Int Array

I m working on a homework assignment translating a C program we wrote to MIPS. My question is about general MIPS coding and not project specific issues though. I ve run into an issue with printing my ...

热门标签