CPU如何进行减法? [英] How does the CPU do subtraction?

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

问题描述

我有一些基本的疑问,但是每次我坐下来尝试面试问题时,这些问题和我的疑问就会弹出.

I have some basic doubts, but every time I sit to try my hands at interview questions, these questions and my doubts pop up.

说A = 5,B = -2.假设A和B为4字节,CPU如何执行A + B加法?

Say A = 5, B = -2. Assuming that A and B are 4-bytes, how does the CPU do the A + B addition?

我知道A的符号位(MSB)为0表示正值 B的符号位为1表示负整数.

I understand that A will have sign bit (MSB) as 0 to signify a positive value and B will have sign bit as 1 to signify a negative integer.

现在在C ++程序中时,我要打印A + B,请先执行ALU(算术逻辑单元)的加法模块检查符号位,然后决定进行减法,然后按照减法的步骤进行操作.如何进行减法运算将是我的下一个问题.

Now when in C++ program, I want to print A + B, does the addition module of the ALU (Arithmetic Logic Unit) first check for sign bit and then decide to do subtraction and then follow the procedure of subtraction. How subtraction is done will be my next question.

A = 5
B = 2

我想做A - B.计算机将取B的2的补码,然后加上A + B的2的补码,然后返回此值(丢弃左侧多余的位后)?

I want to do A - B. The computer will take 2's complement of B and add A + 2's complement of B and return this (after discarding the extra bit on left)?

A = 2
B = 5

执行A - B.在这种情况下,计算机如何工作?

to do A - B. How does the computer do in this case?

我知道,任何if-then等条件逻辑都将在ALU内部的硬件中完成.计算2s补码等,丢弃多余的位全部在ALU内部的硬件中完成. ALU的这个组件看起来如何?

I understand that any if-then etc kind of conditional logic all will be done in hardware inside ALU. computing 2s complement etc,discarding extra bit all will be done in hardware inside ALU. How does this component of ALU look like?

推荐答案

我们使用 2的全部原因-complement 是无论数字是正数还是负数,加法都是相同的-无需考虑特殊情况,例如符号幅度表示形式.

The whole reason we use 2's-complement is that addition is the same whether the numbers are positive or negative - there are no special cases to consider, like there are with 1's-complement or signed-magnitude representations.

因此要找到A-B,我们可以取反B并加;也就是说,我们找到A + (-B),并且因为我们使用的是2's-complement,所以我们不必担心(-B)是正数还是负数,因为加法算法的工作方式相同.

So to find A-B, we can just negate B and add; that is, we find A + (-B), and because we're using 2's-complement, we don't worry if (-B) is positive or negative, because the addition-algorithm works the same either way.

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

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