如何加法器执行无符号整数减法? [英] How does an adder perform unsigned integer subtraction?

查看:407
本文介绍了如何加法器执行无符号整数减法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 A B 签署的正整数,则 AB ,它的计算使用 A + 2 B的补充

Suppose that A and B are signed positive integers, then for A-B, it's calculated using A+2's complement of B.

例如,在一个4位二进制系统中,用于符号的整数,我们有
7-3 = 0111-0011 = 0111 + 1101 =(1)0100
括号内的1是进位。据对符号整数溢出的规则,我们知道有没有溢出,结果是正确的,因此

For example, in a 4-bit binary system, for signed integers, we have 7-3=0111-0011=0111+1101=(1)0100, the 1 in the bracket is the carry bit. According to the overflow rule for signed integer, we know there is no overflow and the result is therefore correct.

不过,对于无符号整数,如果将我们计算发生什么 7-3 ?如果我们用我们上面提到的一样:

However, for unsigned integers, what will happen if we calculate 7-3? If we use the same way we mentioned above:

7-3=0111-0011=0111+1101=(1)0100

然后,根据用于无符号整数溢出规则,存在溢出因为进行。换句话说, 0100 是错误的,因为有一个溢出。但事实上,我们知道结果 0100 是正确的。

then, according to the overflow rule for unsigned integers, there is an overflow because of the carry out. In another word, 0100 is wrong because there is an overflow. But in fact, we know the result 0100 is correct.

如果我的分析是正确的,是不是错用加法器来执行无符号整数减法?

If my analysis is correct, isn't it wrong to use adder to perform unsigned integer subtraction?

推荐答案

在<一个href=\"http://stackoverflow.com/questions/8034566/overflow-and-carry-flags-on-z80/8037485#8037485\">this回答以一个相关的问题有一个在C示例code,显示了如何通过加法做减法。在code将进位和溢出标志以及和包含一个简单的测试,增加和减去几号并打印出结果。这些数字是8位

In this answer to a related question there's sample code in C that shows how to do subtraction via addition. The code sets the carry and overflow flags as well and contains a simple "test" that adds and subtracts a few numbers and prints the results. The numbers are 8-bit.

编辑:正式证明,人们可以使用添加的,而不是为SUB无符号整数现货无符号溢/下溢仿佛从SUB

Formal proof that one can use ADD instead of SUB for unsigned integers AND spot unsigned overflow/underflow as if from SUB.

比方说,我们要计算 A - B ,其中 A B 4位无符号整数,我们希望通过除执行减法,并得到一个4位的差异和下溢/溢出指示时,&LT; b

Let's say we want to calculate a - b, where a and b are 4-bit unsigned integers and we want to perform subtraction via addition and get a 4-bit difference and an underflow/overflow indication when a < b.

A - B = A +(-b)结果
由于我们在模16算术操作是, -b = 16-B 。所以,结果
A - B = A +(-b)= A +(16 - B)

a - b = a + (-b)
Since we're operating in modulo-16 arithmetic, -b = 16-b. So,
a - b = a + (-b) = a + (16 - b)

如果我们执行常规的无符号加法 A 16-B 这个此外,这是溢出条件往往在其执行标志指示的CPU,将是(我们正在处理4位整数回忆):

If we perform regular unsigned addition of a and 16-b the overflow condition for this addition, which is often indicated by the CPU in its carry flag, will be this (recall that we're dealing with 4-bit integers):

A +(16 - B)> 15结果
让我们来简化这个溢出条件:结果
一个+ 16 - B> 1​​5结果
一个+ 16> 15 + B结果
一个+ 1> B时结果
A> B - 1

a + (16 - b) > 15
Let's simplify this overflow condition:
a + 16 - b > 15
a + 16 > 15 + b
a + 1 > b
a > b - 1

现在让我们回顾一下,我们正在处理的整数。因此,上述可改写为:搜索
A> = B 。结果
这是获得进位= 1后加入 A (16)-b 的条件。如果不等式不成立,我们得到矣= 0。

Let's now recall that we're dealing with integers. Therefore the above can be rewritten as:
a >= b.
This is the condition for getting carry flag = 1 after adding a and (16)-b. If the inequality doesn't hold, we get carry = 0.

现在,让我们记得,我们是来自减法感兴趣的溢/下溢(A - B)。这条件的 A&LT; b

Let's now recall that we were interested in overflow/underflow from subtraction (a - b). That condition is a < b.

好吧, A> = B &LT的完全相反; b

由此可以得出,携带您添加获取标记 A 借是减法溢出的倒数,或者,换句话说,在 >标志你减去 b 直接从 A 使用相应的减法指令(例如SUB)。

From this it follows that the carry flag that you get from adding a and (16)-b is the inverse of the subtraction overflow, or, in other words, the inverse of the borrow flag you'd get by subtracting b directly from a using the appropriate subtraction instruction (e.g. SUB).

刚刚反转进位或以相反的方式对待它。

Just invert the carry or treat it in the opposite way.

这篇关于如何加法器执行无符号整数减法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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