gpt4 book ai didi

mips - 为什么对无符号数的 MIPS 运算会给出有符号结果?

转载 作者:行者123 更新时间:2023-12-03 21:42:03 25 4
gpt4 key购买 nike

当我尝试在 MIPS 中处理无符号整数时,我所做的每个操作的结果仍然是有符号的(也就是说,整数都是 2 的补码),即使我执行的每个操作都是无符号的:addu , multu所以第四...

当我打印范围内的数字时 [2^31, 2^32 - 1]我得到它们的“溢出”负值,就好像它们已签名一样(我猜它们是)。

不过,当我尝试这样的事情时:

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

打印的号码总是 2147483647 (2^31 - 1)
我很困惑......我错过了什么?

PS:我没有包含我的代码,因为它不是很可读(比如汇编代码),把这个问题放在一边,
似乎工作正常。如果有人觉得有必要,我会立即包括在内!

最佳答案

来自 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.



来自 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.

关于mips - 为什么对无符号数的 MIPS 运算会给出有符号结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1976793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com