add和addu之间的区别 [英] Difference between add and addu

查看:620
本文介绍了add和addu之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对add和addu之间的区别感到困惑.

I am confused about the difference between add and addu.

MIPS指令参考指出:

The MIPS instruction reference says:

  • 添加(有溢出)
  • 添加未签名(无溢出)

我的理解是将带符号操作数的add和带无符号操作数的addu一起使用.

My understanding is to use add with signed operands and addu with unsigned operands.

但是让我们考虑这个示例(只有6位):

But let's consider this example (with only 6bit):


overflow
|
V
1 | 1 1 1  <- carry
  | 1 1 1 1 0 1 +
  | 1 1 1 1 1 0 =
-----------------
  | 1 1 1 0 1 1

这是我的理由:

  • 如果我考虑第一个和第二个操作数带符号的数字(二进制​​补码),那么结果是正确的(-3 + -2 = -5)并且我不希望发生溢出异常.因此,我将使用addu来避免这种异常,但是,尽管结果是相同的,但该名称建议使用addu来表示无符号数字!
  • 如果我考虑第一个和第二个操作数 unsigned 数字,那么我想引发一个异常(因为61 + 62不等于59).因此,我将使用add引发异常,而不是使用addu,正如名称所暗示的那样.
  • if I consider the first and second operand signed numbers (two's complement), then the result is correct (-3 + -2 = -5) and I don't want an overflow exception. So I would use addu to avoid this exception, but, although the result is the same, the name suggests to use addu is for unsigned numbers!
  • if I consider the first and second operand unsigned numbers, then I want an exception to be raised (because 61 + 62 is not equal to 59). So I would use add to raise the exception, and not addu, as the name would suggest to do.

现在我的问题是:

  • 假设操作数是带符号的(在上面的示例中为负),我应该使用addu(如我的推理所示)还是应该使用add(如名称所示)?
  • 假设操作数是无符号(正)数字,我应该使用加(如我的推理所示)还是加(如名称所示)?

推荐答案

指令名称具有误导性.如果您不希望在溢出时出现陷阱,请对有符号和无符号操作数都使用addu.

The instruction names are misleading. Use addu for both signed and unsigned operands, if you do not want a trap on overflow.

如果出于某种原因在溢出时需要陷阱,请使用add.大多数语言都不想在有符号的溢出上设陷阱,因此add很少有用.

Use add if you need a trap on overflow for some reason. Most languages do not want a trap on signed overflow, so add is rarely useful.

这篇关于add和addu之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆