四位二进制加法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

四位全加器可对两个多位二进制数进行加法运算,同时产生进位。当两个二进制数相加时,较高位相加时必须加入较低位的进位项(Ci),以得到输出为和(S)和进位(C0)。
(一) 半加器
VHDL语言描述语句为:
so<=a xor b;
co<=a and b
程序设计:
library ieee;
use ieee.std_logic_1164.all;
entity h_adder is
port (a,b:in std_logic;
so,co:out std_logic); ――定义输入、输出端口
end h_adder;
architecture bh of h_adder is
begin
so<=a xor b; ――“异或”运算
co<=a and b; ――“与”运算
end bh;
四位全加器程序代码如下:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity add4 is
port(cin:in std_logic;
a,b:in std_logic_vector(3 downto 0);
s:out std_logic_vector(3 downto 0);
cout:out std_logic);
end add4;
architecture beh of add4 is
signal sint:std_logic_vector(4 downto 0);
signal aa,bb:std_logic_vector(4 downto 0);
begin
aa<='0' & a(3 downto 0); --4位加数矢量扩为5位,提供进位空间
bb<='0' & b(3 downto 0);
sint<=aa+bb+cin;
s(3 downto 0)<=sint(3 downto 0);
cout<=sint(4);
end beh;
四位全加器 Verilog HDL语言代码如下:
module adder4(cout,sum,a,b,cin);
output[3:0] sum;
output cout;
input[3:0] a,b;
input cin;
assign {cout,sum}=a+b+cin;
endmodule

相关文档
最新文档