📄 4-3.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Addition and Subtraction 加减运算</title>
<meta name="Microsoft Theme" content="arcs 001">
</head>
<body background="../chapter1/arctile.jpg" bgcolor="#FFFFFF" text="#000000" link="#3399FF" vlink="#666666" alink="#FF9900">
<h3 align="center"><font color="#000000" size="6"><b>4.3 Addition and Subtraction 加减运算</b></font></h3>
<p align="right"><font color="#000000"><a href="chapter4.htm"><img border="0" src="images/BACK.GIF" align="left" width="60" height="45"></a> <a href="4-2.htm"><img border="0" src="images/PREV.GIF" width="60" height="45"></a>
<a href="4-4.htm"><img border="0" src="images/FORW.GIF" width="60" height="45"></a> </font></p>
<p align="left"><font color="#000000" size="4">本节问题:</font></p>
<p align="left"><font color="#000000" size="4"><img border="0" src="images/SUB.GIF" width="26" height="27"><a href="#运算规则:">运算规则</a><a href="#数溢出的概念:"><br>
</a><img border="0" src="images/SUB.GIF" width="26" height="27"><a href="#数溢出的概念:">数溢出的概念overflow</a>s</font>
<p align="left"><font color="#000000" size="4">一:<a name="运算规则:">运算规则:</a></font></p>
<p align="left"><font color="#000000" size="4">
计算机中加法的规则与我们想象中的一样,数字被一位一位地从右边加到左边,同时从右向左进位,就象手工运算一样。减法的规则与加法相似,将减号运算符看成减数的符号位,然后对被减数取补码,两数相加。逢2进1,借1作2,carry表示进位。</font></p>
<p align="left"><font color="#000000" size="4">Example: Let's try add 6<sub>ten</sub> from 7<sub>ten</sub>.<br>
Answer:
</font> </p>
<p align="left"><font color="#000000" size="4">
0000 0000 0000 0000 0000 0000 0000 0111<sub>two</sub> =7<sub>ten</sub><br>
+
0000 0000 0000 0000 0000 0000 0000 0110<sub>two</sub> =6<sub>ten</sub><br>
------------------------------------------------<br>
=
0000 0000 0000 0000 0000 0000
0000 1101<sub>two</sub> =13<sub>ten</sub><br>
下面给出了加法和进位(注意箭头): </font> </p>
<p align="left"><font color="#000000" size="4"><img border="0" src="images/4-3-pic1.gif" width="415" height="87"> </font> </p>
<p align="left"><font color="#000000" size="4">也可以直接将7减去6:</font> </p>
<p align="left"><font color="#000000" size="4">
0000 0000 0000 0000 0000 0000 0000 0111<sub>two</sub> =7<sub>ten</sub><br>
-
0000 0000 0000 0000 0000 0000 0000 0110<sub>two</sub> =6<sub>ten</sub><br>
--------------------------------------------------<br>
=
0000 0000 0000 0000 0000 0000 0000 0001<sub>two</sub> =1<sub>ten</sub>yy<br>
或者可以给-6取补码,然后与7相加:</font> </p>
<p align="left"><font color="#000000" size="4">
0000 0000 0000
0000 0000 0000 0000 0111<sub>two</sub> =7<sub>ten<br>
</sub> +
1111 1111 1111 1111 1111 1111 1111 1010<sub>two </sub>=-6<sub>ten</sub><br>
---------------------------------------------------<br>
=
0000 0000 0000 0000 0000 0000 0000 0001<sub>two</sub> =1<sub>ten</sub> </font> </p>
<p align="left"><font color="#000000" size="4">二:<a name="数溢出的概念:">数溢出的概念:</a> </font> </p>
<p align="left"><font color="#000000" size="4">
计算机运算受到计算机字长的限制。在MIPS中,32位字的二进制数相加,肯定会出现相加之和太大而不能用32位字的二进制数表示。当这个情况发生时,就叫<a name="数溢出的概念:">溢出</a>overflow。例如,下面两个有符号数之和太大而不能用32bits表示。</font></p>
<p align="left"><font color="#000000" size="4">
0111 1111 1111 1111 1111 1111 1111 1111<sub>two</sub> =2,147,483,647<sub>ten</sub><br>
+
0000 0000 0000 0000 0000 0000 0000 0010<sub>two</sub> =2<sub>ten</sub><br>
---------------------------------------------<br>
= 1000 0000 0000 0000 0000 0000 0000 0001<sub>two</sub> =-2,147,483,649<sub>ten</sub>k</font>
</p>
<p align="left"><font color="#000000" size="4"> 2,147,483,647与+2之和应是2,147,483,649,但是我们按照上面方法得-2,147,483,647.原因是我们需要33bits来表示和的二进制补码,但是却受到32bits的限制,因此,
得到32bits的数字。数的溢出也发生在减法之中。同号相加,或异号相减,有可能溢出。</font>
</p>
<ol>
<li>
<p align="left"><font color="#000000" size="4">下面图表是发生溢出的条件:<br>
我们需要在硬件上增加一位检测位,通过这一位来检测溢出。<br>
</font>
</p>
<table border="1" cellpadding="0" cellspacing="0" width="80%" height="89" bordercolordark="#CC9966" bordercolorlight="#FFCC66">
<tr>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">Operation</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">Operand A</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">Operand B</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">Result</font></td>
</tr>
<tr>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">A+B</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">>0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">>0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4"><0</font></td>
</tr>
<tr>
<td width="25%" height="15">
<p align="center"><font color="#000000" size="4">A+B</font></td>
<td width="25%" height="15">
<p align="center"><font color="#000000" size="4"><0</font></td>
<td width="25%" height="15">
<p align="center"><font color="#000000" size="4"><0</font></td>
<td width="25%" height="15">
<p align="center"><font color="#000000" size="4">>0</font></td>
</tr>
<tr>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">A-B</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">>0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4"><0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4"><0</font></td>
</tr>
<tr>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">A-B</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4"><0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">>0</font></td>
<td width="25%" height="16">
<p align="center"><font color="#000000" size="4">></font></td>
</tr>
</table>
</li>
</ol>
<p align="left"><font color="#000000" size="4"><br>
2.对溢出的处理过程:<br>
在MIPS机中用exception(异常)来检测溢出位,在许多计算机系统里也叫interrupt(中断)。exception和interrupt实际上是对一个不可预计事件处理的响应。当exception发生时,溢出时的指令地址被存放在寄存器里,同时计算机的进程跳到定义好的地址中以激活异常处理程序。中断地址(称为断点)被保存起来,以便中断处理程序执行过后,
被中断程序能够继续执行。在MIPS之中,用一个寄存器exception program counter
(EPC)来存放引起异常的指令地址。用指令move from system control (mfc0系统控制传送指令)来将EPC复制到寄存器里,以便使软件能够作出判断和处理。<br>
例如:mfc0 $1 $epc #copy $epc to $1</font>
</p>
<p align="left"><font color="#000000" size="4"> 3.其他情况:<br>
我们已经看到对于二进制补码来说可以检测到溢出的产生,对于无符号数又怎样呢?很显然,无符号数也存在过大而不能用32bits表示的问题,但是这通常不被认为是溢出。这是因为无符号数通常被用来表示内存地址,由于内存地址有限,所以无符号数的大小也是有限的,不会因为过大而产生溢出.在MIPS系统之中,最大的内存地址就是4.294,967,296<sub>ten</sub>或者2<sub>32
</sub>bits。因此计算机的设计者提供了一个有时能够检测溢出,有时能够忽略溢出的方法,MIPS提供了两套数学运算指令。</font>
</p>
<p align="left"><font color="#000000" size="4"> (1).add (add) , add immediate
(addi) ,subtract (sub)能够产生溢出异常。<br>
(2).add unsigned (addu), add immediate unsigned (addiu)
,subtract unsigned (subu)不会产生溢出。</font>
</p>
<p align="left"><font color="#000000" size="4"> 因为C语言忽略了溢出,MIPS C编译器会产生无符号的数学运算指令的版本(addu,addiu,subu);在MIPS Ada编译器中能够通过操作数的类型自动选择数学运算指令的版本。 </font>
</p>
<p align="left"><font color="#000000" size="4"> </font>
</p>
<p align="left">
</p>
<p align="left">
</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -