0
0
VHDLprogramming~20 mins

Adder and subtractor design in VHDL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Adder-Subtractor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 4-bit Adder with Overflow
What is the output and overflow flag of this 4-bit adder when adding A = "1111" and B = "0001"?
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Adder4bit is
    Port ( A : in STD_LOGIC_VECTOR (3 downto 0);
           B : in STD_LOGIC_VECTOR (3 downto 0);
           Sum : out STD_LOGIC_VECTOR (3 downto 0);
           Overflow : out STD_LOGIC);
end Adder4bit;

architecture Behavioral of Adder4bit is
    signal A_int, B_int, Sum_int : signed(3 downto 0);
    signal temp_sum : signed(4 downto 0);
begin
    A_int <= signed(A);
    B_int <= signed(B);
    temp_sum <= ('0' & A_int) + ('0' & B_int);
    Sum_int <= temp_sum(3 downto 0);
    Sum <= std_logic_vector(Sum_int);
    Overflow <= temp_sum(4) xor temp_sum(3);
end Behavioral;
ASum = "0000", Overflow = '0'
BSum = "1111", Overflow = '0'
CSum = "0000", Overflow = '1'
DSum = "10000", Overflow = '1'
Attempts:
2 left
💡 Hint
Remember that overflow occurs when the carry into the sign bit differs from the carry out.
Predict Output
intermediate
2:00remaining
Subtractor Output for 4-bit Inputs
What is the output of this 4-bit subtractor when subtracting B = "0011" from A = "0101"?
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Subtractor4bit is
    Port ( A : in STD_LOGIC_VECTOR (3 downto 0);
           B : in STD_LOGIC_VECTOR (3 downto 0);
           Diff : out STD_LOGIC_VECTOR (3 downto 0);
           Borrow : out STD_LOGIC);
end Subtractor4bit;

architecture Behavioral of Subtractor4bit is
    signal A_int, B_int, Diff_int : unsigned(3 downto 0);
    signal temp_diff : signed(4 downto 0);
begin
    A_int <= unsigned(A);
    B_int <= unsigned(B);
    temp_diff <= signed(('0' & A_int)) - signed(('0' & B_int));
    Diff_int <= unsigned(temp_diff(3 downto 0));
    Diff <= std_logic_vector(Diff_int);
    Borrow <= '1' when temp_diff(4) = '1' else '0';
end Behavioral;
ADiff = "1110", Borrow = '1'
BDiff = "0010", Borrow = '0'
CDiff = "0010", Borrow = '1'
DDiff = "1110", Borrow = '0'
Attempts:
2 left
💡 Hint
Borrow is set when the result is negative in unsigned subtraction.
🔧 Debug
advanced
2:00remaining
Identify the Error in 8-bit Adder Design
This VHDL code is intended to add two 8-bit unsigned numbers but produces incorrect results. What is the main error?
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Adder8bit is
    Port ( A : in STD_LOGIC_VECTOR (7 downto 0);
           B : in STD_LOGIC_VECTOR (7 downto 0);
           Sum : out STD_LOGIC_VECTOR (7 downto 0);
           CarryOut : out STD_LOGIC);
end Adder8bit;

architecture Behavioral of Adder8bit is
    signal A_int, B_int : unsigned(7 downto 0);
    signal temp_sum : unsigned(8 downto 0);
begin
    A_int <= unsigned(A);
    B_int <= unsigned(B);
    temp_sum <= ('0' & A_int) + ('0' & B_int);
    Sum <= std_logic_vector(temp_sum(7 downto 0));
    CarryOut <= temp_sum(8);
end Behavioral;
ACarryOut should be assigned temp_sum(7) instead of temp_sum(8).
Btemp_sum should be signed type to handle overflow correctly.
CSum output should be assigned temp_sum(8 downto 1) instead of (7 downto 0).
Dtemp_sum is declared as unsigned(8 downto 0) but assigned sum of unsigned(7 downto 0) + unsigned(7 downto 0) causing size mismatch.
Attempts:
2 left
💡 Hint
Check the size of signals and the result of addition carefully.
📝 Syntax
advanced
2:00remaining
Syntax Error in Subtractor with Borrow
Which option contains the correct syntax to assign the borrow output in this subtractor architecture?
VHDL
Borrow <= '1' when temp_diff(4) = '1' else '0';
ABorrow <= '1' when temp_diff(4) = '1' else '0';
BBorrow := '1' if temp_diff(4) = '1' else '0';
CBorrow = '1' when temp_diff(4) = '1' else '0';
DBorrow <= '1' if temp_diff(4) = '1' else '0';
Attempts:
2 left
💡 Hint
Remember VHDL uses '<=' for signal assignment and 'when ... else' for conditional assignment.
🚀 Application
expert
3:00remaining
Design Challenge: Combined Adder-Subtractor Output
Given this VHDL snippet for a combined adder-subtractor, what is the output Sum when A = "0110", B = "0011", and Mode = '1' (subtract)?
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity AddSub4bit is
    Port ( A : in STD_LOGIC_VECTOR(3 downto 0);
           B : in STD_LOGIC_VECTOR(3 downto 0);
           Mode : in STD_LOGIC; -- '0' for add, '1' for subtract
           Sum : out STD_LOGIC_VECTOR(3 downto 0);
           Carry_Borrow : out STD_LOGIC);
end AddSub4bit;

architecture Behavioral of AddSub4bit is
    signal B_mod : STD_LOGIC_VECTOR(3 downto 0);
    signal carry_in : STD_LOGIC;
    signal temp_sum : unsigned(4 downto 0);
begin
    B_mod <= B xor ("1111" when Mode = '1' else "0000");
    carry_in <= Mode;
    temp_sum <= unsigned(('0' & A)) + unsigned(('0' & B_mod)) + to_unsigned(to_integer(unsigned(carry_in & "0000")), 5);
    Sum <= std_logic_vector(temp_sum(3 downto 0));
    Carry_Borrow <= temp_sum(4);
end Behavioral;
ASum = "0011", Carry_Borrow = '0'
BSum = "0101", Carry_Borrow = '1'
CSum = "0011", Carry_Borrow = '1'
DSum = "0101", Carry_Borrow = '0'
Attempts:
2 left
💡 Hint
Subtracting B from A is done by adding A to the two's complement of B.