为什么我们要用addiu代替addi

为什么我们要用addiu代替addi

本文介绍了为什么我们要用addiu代替addi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MIPS组件中,使用addiu而不是addi有什么好处? addiu是不是无符号的(会破坏我们的计算?)

In MIPS assembly, what is the benefit of using addiu over addi? Isn't addiu unsigned (and will ruin our calculations?)

推荐答案

不,MIPS使用 二进制补码 ,因此相同的加/减指令可用于有符号和无符号运算.结果没有差异.

No, MIPS uses two's complement, hence the same instruction for addition/subtraction can be used for both signed and unsigned operations. There's no difference in the result.

对于按位指令,非扩展乘法和许多其他运算也是如此.见

That's also true for bitwise instructions, non-widening multiplication and many other operations. See

  • Which arithmetic operations are the same on unsigned and two's complement signed numbers?
  • Difference between signed and unsigned on bitwise operations

它们之间的唯一区别是,addi在溢出时会生成陷阱,而addiu不会.因此,addi及其溢出系列(addsub ...)通常是无用的.实际上,它很少使用,因此在MIPSr6 中删除了 addi其他指令的宝贵操作码空间

The only difference between them is that addi generates a trap when overflow while addiu doesn't. So addi and its overflow family (add, sub...) is often useless. In fact it's so rarely used that addi was removed in MIPSr6 to release valuable opcode space to other instructions

在这里,指令名称极具误导性,因为它实际上不是未签名"的加法.立即数仍然是符号扩展,而不是零扩展.因此addiu $1, $2, 0xFFFF实际上将从$2减去1,而不是添加65535.

Here the instruction name is extremely misleading, because it's not actually an "unsigned" addition. The immediate is still sign extended instead of zero extended. So addiu $1, $2, 0xFFFF will actually subtract 1 from $2 instead of adding 65535 to it.

计算机组织和设计:硬件/软件接口 大卫·帕特森(David A.Patterson),约翰·轩尼诗(John L.Hennessy)

Computer Organization and Design: The Hardware/software Interface By David A. Patterson, John L. Hennessy

了解更多信息 add和addu之间的区别

这篇关于为什么我们要用addiu代替addi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:36