0% found this document useful (0 votes)
2 views

DLCA - Exp10

Uploaded by

Oaish Qazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DLCA - Exp10

Uploaded by

Oaish Qazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Don Bosco Institute of Technology, Kurla

Academic Year 2024-25

EXPERIMENT NO: 10

Title: To implement an ALU

Name of the student: Qazi Mohd Oaish


Roll No: 68
Batch: C
Division: B

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

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.

1. Arithmetic Operations: The ALU performs basic arithmetic operations


such as:
o Addition: Adds two binary numbers.
o Subtraction: Subtracts one binary number from another.
o Multiplication and Division: In some advanced ALUs, these opera-
tions are also supported.
o Increment/Decrement: Increases or decreases a binary value by 1.

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).

2. Logical Operations: Logical operations are essential in decision-making


processes within a CPU. The ALU performs bitwise logical operations on
data, such as:
o AND: Compares two bits and outputs 1 if both bits are 1, otherwise
outputs 0.
o OR: Outputs 1 if at least one of the bits is 1, otherwise outputs 0.
o XOR (Exclusive OR): Outputs 1 if the bits are different, otherwise
outputs 0.
o NOT: Inverts the bits, converting 1 to 0 and vice versa.
o Shift Operations: The ALU can shift bits to the left or right, which

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

is used in multiplication and division by powers of 2.


3. Comparison Operations: The ALU can compare two binary numbers to
determine whether one is greater than, less than, or equal to the other. These
comparison results are vital for making decisions in programs (e.g., branch-
ing or conditional instructions).
4. Control Signals: The ALU operates based on control signals from the CPU.
These signals tell the ALU which operation to perform (arithmetic or lo-
gical) and ensure that the correct inputs are used. The ALU then outputs the
result of the operation and updates the status flags, which are used by the
CPU to handle conditions like overflows or zero results.
5. ALU’s Role in a CPU: The ALU is integrated into the execution unit of the
CPU and works in tandem with registers to store intermediate results and
perform computations efficiently. Every instruction in a program, whether it
involves data manipulation, comparisons, or branching, passes through the
ALU at some stage of execution.

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.

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

CODE using System;


using System.Text.RegularExpressions;

public class Alu


{
private int A { get; set; }
private int B { get; set; }
private int SelectLines { get; set; }

public static void Main(string[] args) {


new Alu().Start();
}

private static int BinToDec(string? bin)


{
if (bin == null) throw new ArgumentNullException(nameof(bin));
if (!Regex.IsMatch(bin, "^[01]{4}$")) throw new FormatException("Invalid Input");
const int mask = (1 << 4) - 1;
var res = Convert.ToInt32(bin, 2) & mask;
return res;
}

private static string DecToBin(int dec, int pad = 4)


{
var mask = (1 << pad) - 1;
if (dec < 0)

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

{
dec = (dec & mask);
}

var binary = Convert.ToString(dec, 2).PadLeft(pad, '0');


return binary[^pad..];
}

private void Initialize()


{
Console.WriteLine("[ Enter all inputs in the form XXXX where X can be 0 or 1 ]");
Console.Write("Enter A: ");
A = BinToDec(Console.ReadLine());
Console.Write("Enter B: ");
B = BinToDec(Console.ReadLine());
}

public void Start()


{
Initialize();
while (true)
{
Console.Write("Enter SelectLines: ");
SelectLines = BinToDec(Console.ReadLine());
switch (DecToBin(SelectLines))
{
case "0000":
Addition(A, B);
break;
case "0001":
Subtraction(A, B);
break;
case "0010":
Addition(A, 1, "Increment A");
break;
case "0011":
Subtraction(A, 1, "Decrement A");
break;
case "0100":
Addition(B, 1, "Increment B");
break;
case "0101":
Subtraction(B, 1, "Decrement B");
break;
case "0110":
Display(DecToBin(A), "Transfer");
break;
case "0111":
Multiplication(A, B);
break;
case "1000":
Complement(A, 'A');
break;
case "1001":

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

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 static void Display(string result, string text = "Result")


{
Console.WriteLine($"{text}: {result}");
}

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", "~&");
}

private void Nor()


{
DisplayLogicalOperation(DecToBin(~(A | B)), "NOR", "~|");
}
private void Or()

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

{
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

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

CONCLUSION We understood that,


1. 16 different operations of ALU are Multiplexed. 4 select lines are used to
select the operation to perform
2. Addition of two 4-bit number may return a 5-bit result
3. Multiplication of two 4-bit number may return an 8-bit result

REFERENCES 1. William Stallings, “Computer Organization and Architecture: Designing for

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25

Performance”, Pearson Publication, 10th Edition, 2013


2. B. Govindarajulu, “Computer Architecture and Organization: Design Principles
and Applications”, Second Edition, Mc GrawHill (India)

Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra


Subject: DLCA Lab

You might also like