为什么对无符号数字进行MIPS操作会得到带符号的结果? [英] Why do MIPS operations on unsigned numbers give signed results?
问题描述
当我尝试在MIPS中处理无符号整数时,即使我执行的每个操作都是无符号运算,我做的每一个运算的结果都保持有符号(即整数全为2的补码):addu
, multu
等等...
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...
当我打印范围在[2^31, 2^32 - 1]
内的数字时,我得到它们的溢出"负值,就好像它们是带符号的(我猜是这样).
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
打印的号码始终为2147483647 (2^31 - 1)
我很困惑...我想念什么?
I'm confused... What am I missing?
PS:我没有包含我的代码,因为它不易读(例如汇编代码),并且抛开了这个问题, 似乎工作正常.如果有人认为有必要,我会立即将其包括在内!
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!
推荐答案
来自 Wikipedia :
MIPS32指令集指出,作为加减指令的一部分未签名的单词是不正确的. 命令的有符号版本和无符号版本之间的区别不是操作数的符号扩展(或没有符号扩展),而是控制是在溢出(例如,添加)时执行陷阱还是忽略溢出(添加无符号)强>.这些指令的立即操作数CONST始终进行符号扩展.
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.
从 MIPS指令参考:>
所有算术立即数都用符号扩展[...]有符号和无符号指令之间的唯一区别是,有符号指令可以生成溢出异常,而无符号指令则不能.
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.
这篇关于为什么对无符号数字进行MIPS操作会得到带符号的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!