DLCA - Exp10
DLCA - Exp10
EXPERIMENT NO: 10
EXPERIMENT NO: 10
To implement an ALU
AIM To write a program to simulate ALU operation.
LEARNING To implement the operation of ALU to simulate various logical and arithmetic
OBJECTIVE operation.
LEARNING Students can simulate the operation of ALU unit.
OUTCOME
LAB OUTCOME CSL 302.5: Ability to design and estimate the output of the basic building blocks
of a computer: ALU
PROGRAM PO1-1,
OUTCOME PO5-2,
PO8-3,
PO9-3,
PO12-2,
PSO1-1
BLOOM'S Apply
TAXONOMY
LEVEL
THEORY Arithmetic Logic Unit (ALU) Operations: The Arithmetic Logic Unit (ALU) is a
fundamental building block of the central processing unit (CPU) in computers. It
performs both arithmetic and logical operations on binary data and is crucial in
processing and executing instructions in computing systems.
The ALU uses binary number representations and carries out these opera-
tions with binary addition (including carry handling) and subtraction (using
two’s complement representation).
SOFTWARE C/C++/Java/Python
USED
STEPS TO 1. Take two 4-bit binary inputs from the user (A&B).
EXECUTE THE 2. Perform the operation as per the combination of select lines(S).
PROGRAM 3.Output F will be reflected in binary format as per the function table with the
combination of select lines(S).
PROBLEM Problem statement:
STATEMENTS Design a 4-bit ALU to perform various arithmetic and logical operations.
{
dec = (dec & mask);
}
Complement(B, 'B');
break;
case "1010":
And();
break;
case "1011":
Or();
break;
case "1100":
Nand();
break;
case "1101":
Nor();
break;
case "1110":
Xor();
break;
case "1111":
Xnor();
break;
default:
return;
}
}
}
private void DisplayLogicalOperation(string result, string gate = "Result", string sym = "=")
{
Console.WriteLine($"{gate}: ");
Console.WriteLine($"A: {DecToBin(A)}");
Console.WriteLine($"B: {DecToBin(B)}");
Console.WriteLine($" {sym,-2}----");
Console.WriteLine($" {result}");
}
private void Xnor() {
DisplayLogicalOperation(DecToBin(~(A ^ B)), "XNOR", "~^");
}
private void Xor() {
DisplayLogicalOperation(DecToBin(A ^ B), "XOR", "^");
}
private void Nand() {
DisplayLogicalOperation(DecToBin(~(A & B)), "NAND", "~&");
}
{
DisplayLogicalOperation(DecToBin(A | B), "OR", "|");
}
private void And()
{
DisplayLogicalOperation(DecToBin(A & B), "AND", "&");
}
private static void Complement(int i, char c)
{
Display(DecToBin(~i), $"Complement {c}");
}
private static void Multiplication(int a, int b)
{
var result = a * b;
Display(DecToBin(result, 8), "Multiplication");
}
private static void Subtraction(int a, int b, string text = "Subtraction")
{
var result = a - b;
Display(DecToBin(result), text);
}
private static void Addition(int a, int b, string text = "Addition")
{
var result = a + b;
Display(DecToBin(result, result > 15 ? 5 : 4), text);
}
}
OUTPUT