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

Computer Organisation Lab File BCS352

Uploaded by

sharmaa73870
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)
16 views

Computer Organisation Lab File BCS352

Uploaded by

sharmaa73870
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/ 38

Anurag Sharma

BUNDELKHAND INSTITUTE OF ENGINEERING & TECHNOLOGY JHANSI

SESSION: 2024-25

Name:-Anurag Sharma
Roll. No.:- 2300430130009
Subject:- Web Designing Lab
Subject Code:- BCS 353

Department of Information Technology

Roll.No.:-2300430130009
Anurag Sharma

S.N. Practical Date Signature

IMPLEMENTING HALF ADDER, FULL ADDER USING


01. BASIC LOGIC GATES

IMPLEMENTING BINARY - TO -GRAY, GRAY -TO -


02.
BINARY CODE CONVERSIONS.

03. IMPLEMENTING 3-8 LINE DECODER.

IMPLEMENTING 4X1 AND 8X1 MULTIPLEXERS.


04.

VERIFY THE EXCITATION TABLES OF VARIOUS FLIP-


05. FLOPS.

DESIGN OF AN 8-BIT INPUT/ OUTPUT SYSTEM


06. WITH FOUR 8-BIT INTERNAL REGISTERS.

DESIGN OF AN 8-BIT ARITHMETIC LOGIC UNIT


07.

DESIGN THE DATA PATH OF A COMPUTER FROM


ITS REGISTER TRANSFERLANGUAGE
08.
DESCRIPTION.

Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-01

OBJECTIVE:-

Implementing HALF ADDER, FULL ADDER using basic logic gates.

THEORY:-

In data processing, addition of operands is one of the most basic operations performed by
different electronic devices like computers, calculators, etc. The electronic circuit that is
designed to perform the addition of two or more numbers, more specifically binary numbers,
is known as adder. As we know, the logic circuits use binary number system to perform the
operations, hence the adder is also referred to as a binary adder.

Depending on the number of binary digits that the adder circuit can add, adders (or binary
adders) are of two types:

 Half Adder
 Full Adder
In this article, we will discuss the implementation of full adder using half adder. But before
that let’s have a look into the basics of half adder and full adder.

WHAT IS A HALF ADDER?


Half adder is a combinational logic circuit that is designed to add two binary digits. The half
adder provides the output along with a carry (if any). The half adder circuit can be designed
by
connecting an XOR gate and one AND gate. It has two input terminals and two output
terminals for sum (S) and carry (C). The block diagram and circuit diagram of a half adder
are shown in Figure-1.

In the half adder, the output of the XOR gate is the sum of two bits and the output of the
AND gate is the carry bit. However, in the half-adder circuit, the carry obtained in one
addition will not be forwarded in the next addition.

3
Roll.No.:-2300430130009
Anurag Sharma

The output equation of the half adder are,

WHAT IS A FULL ADDER?


Full adder is also a combinational logic circuit that can add two binary digits (bits) and a
carry bit, and produces a sum bit and a carry bit as output.

In other words, a combinational circuit which is designed to add three binary digits and
produces two outputs (sum and carry) is known as a full adder. Thus, a full adder circuit adds
three binary digits, where two are the inputs and one is the carry forwarded from the previous
addition. The block diagram and circuit diagram of the full adder are shown in Figure-2.
It is clear that the logic circuit of a full adder consists of one XOR gate, three AND gates and
one OR gate, which are connected together as shown in Figure-2. Here, A and B are the input
bits, Cin is the carry from previous addition, S is the sum bit, and Cout is the output carry bit.
The output equations of the full adder are,

Now, let us discuss the realization of the full adder using half adders
Implementation of Full Adder using Half Adder
The logic diagram of the full adder using two half adders is shown in Figure-3:

4
Roll.No.:-2300430130009
Anurag Sharma

The block diagram of a full adder using two half adders is shown in Figure-4.

From the logic diagram of the full adder using half adders, it is clear that we require two
XOR gates, two AND gates and one OR gate for the implementation of a full adder circuit
using half-adders.However, the implementation of full adder using half adder has a major
disadvantage that is the increased propagation delay. That means, the input bits must
propagate through several gates in succession that increases the total propagation delay of the
full adder circuit.

Code
#include <stdio.h>
// Function to compute the sum and carry for a Half Adder void halfAdder(int A, int B, int *sum, int
*carry) {
*sum = A ^ B; // XOR operation for sum
*carry = A & B; // AND operation for carry
}

// Function to compute the sum and carry for a Full Adder void fullAdder(int A, int B,
int Cin, int *sum, int *carry) {

int sum1, carry1, sum2, carry2;

// First Half Adder (A + B) halfAdder(A, B, &sum1,


&carry1);// Second Half Adder (Sum1 + Cin)
halfAdder(sum1, Cin, &sum2, &carry2);
5
Roll.No.:-2300430130009
Anurag Sharma

// Calculate the final sum and carry


*sum = sum2;
*carry = carry1 | carry2; // OR operation for carry
}
int main() {
printf("Truth Table for Half Adder:\n"); printf("A | B | Sum
| Carry\n");

printf("--|---|-----|-------------------\n");

for (int A = 0; A <= 1; A++) { for (int B = 0; B


<= 1; B++) {

int sum, carry;


halfAdder(A, B, &sum, &carry);
printf("%d | %d | %d | %d \n", A, B, sum, carry);
}
}

printf("\nTruth Table for Full Adder:\n"); printf("A | B | Cin |


Sum | Cout\n"); printf(" | | | | \n");
for (int A = 0; A <= 1; A++) { for (int B = 0; B <= 1; B++) {
for (int Cin = 0; Cin <= 1; Cin++) { int sum, carry;

fullAdder(A, B, Cin, &sum, &carry);


printf("%d | %d | %d | %d | %d \n", A, B, Cin, sum, carry);
}
}
}

return 0;
}

6
Roll.No.:-2300430130009
Anurag Sharma

OUTPUT

7
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT-02

OBJECTIVE:
Implementing Binary -to -Gray, Gray -to -Binary code conversions.

THEORY:-
WHAT IS GRAY CODE?
Gray code is a sequence of binary numbers known as reflected binary code (RBC). Gray code
was introduced by Frank Gray. In gray code, two successive values differ by only 1 bit.
Conversion of binary codes to gray codes results in reducing the switching operations.
Gray Codes are unweighted codes, unlike binary codes. There are multiple names of gray
codes following:
 Unit Distance Code
 Minimum Error Code
 Reflected Binary Code (RBC)
 Cyclic Code

In the table below, 0 to 15 decimal numbers and their binary and gray codes are present.
Obviously, the table is not meant to be learned but to have a basic understanding.

CONVERTING BINARY TO GRAY CODE


Now, we will discuss how to convert the binary code to gray code. There are 3 basic steps in
order to convert a binary to gray code:

Record the Most Significant Bit (MSB) of binary code as it is.

Add the MSB to the next bit of the binary code, record the sum, and neglect the carry.
In the 2nd step, the XOR operation can also be done instead of adding MSB to the next bit
and neglecting the carry.
8
Roll.No.:-2300430130009
Anurag Sharma

Repeat step 2 again till the end of the binary code. Here’s the XOR truth table below to check
the results:
Here’s an example to understand the conversion:

CONVERTING GRAY CODE TO BINARY


Now we will discuss how to convert the gray code to binary code.

There are 3 basic steps in order to convert a gray to binary code:

1) Record the Most Significant Bit (MSB) of gray code as it is.


2) Add the MSB to the next bit of the gray code, record the sum, and neglect the carry. In
the 2nd step, the XOR operation can also be done instead of adding MSB to the next bit
and neglecting the carry.
3) Repeat step 2 again till the end of the gray code. Here’s the XOR truth table
below to check the results:

Here’s an example to understand the conversion:

9
Roll.No.:-2300430130009
Anurag Sharma

CODE

BINARY TO GRAY:-
#include <stdio.h>

// Function to convert binary to Gray code unsigned int


binaryToGray(unsigned int binary) {
return binary ^ (binary >> 1);
}

int main() {
unsigned int binary;

printf("Enter a binary number: "); scanf("%u",


&binary);

unsigned int gray = binaryToGray(binary);

printf("Binary: %u\n", binary); printf("Gray


code: %u\n", gray);

return 0;
}

OUTPUT

10
Roll.No.:-2300430130009
Anurag Sharma

GRAY TO BINARY:-
#include <stdio.h>

// Function to convert Gray code to binary code unsigned int


grayToBinary(unsigned int num) {
unsigned int binary = 0; unsigned int
mask = num; while (mask) {
binary ^= mask; mask
>>= 1;
}
return binary;
}

int main() {
unsigned int grayCode, binaryCode;

printf("Enter a Gray code: "); scanf("%u",


&grayCode);

binaryCode = grayToBinary(grayCode); printf("Binary code:

%u\n", binaryCode);

return 0;
}

OUTPUT

11
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:03
OBJECTIVE
Implementing 3-8 line DECODER.
THEORY:-
A 3-to-8 line decoder is a combinational digital circuit that takes a 3-bit binary input and
produces 8 output lines. Each output line corresponds to a specific binary combination of the
input. Only one of the output lines is active (high) at a time, based on the input value. This
type of decoder is often used in digital circuits to enable specific functions or devices based
on the input.

Let's break down how to implement a 3-to-8 line decoder:


Input: The 3-bit binary input has 2^3 = 8 possible combinations, ranging from 000 to 111. Each
unique combination of the input corresponds to one of the 8 output lines.
1) Logic: The decoder uses combinational logic to determine which output line should be
active based on the input. This logic typically consists of AND gates.
2) Output: Each output line is associated with a unique binary combination. For example:
 Output 0 corresponds to the binary input 000.
 Output 1 corresponds to the binary input 001.
 Output 2 corresponds to the binary input 010.
 Output 3 corresponds to the binary input 011.
 Output 4 corresponds to the binary input 100.
 Output 5 corresponds to the binary input 101.
 Output 6 corresponds to the binary input 110.
 Output 7 corresponds to the binary input 111.

3) Active Output: The output corresponding to the binary value of the input is active
(high), while all other outputs remain inactive (low). This is achieved by setting up AND
12
Roll.No.:-2300430130009
Anurag Sharma

gates for each output line, with one input of each AND gate connected to a specific bit of the
input and the other input connected to the inverted form of that bit.

Here's a simplified example of the decoder logic for output 0:

 Input: 000
 Output 0: Active (High)

For output 0, you would have three AND gates:

 Gate 1: Input A is connected to bit 0 (the least significant bit) of the input.
 Gate 2: Input B is connected to bit 1 of the input.
 Gate 3: Input C is connected to bit 2 (the most significant bit) of the input.
The other AND gates for the remaining outputs are constructed similarly, each with different
input connections based on the binary value they represent.

In practice, 3-to-8 decoders are used as components in larger digital circuits to control
various operations and devices. They are crucial for demultiplexing, address decoding in
memory systems, and various other applications where input selection is needed based on
binary input values.

13
Roll.No.:-2300430130009
Anurag Sharma

CODE
#include <stdio.h>

14
Roll.No.:-2300430130009
Anurag Sharma

int main() {
unsigned int input;
printf("Enter a 3-bit binary input (e.g., 000 to 111): "); scanf("%u", &input);

if (input >= 0 && input <= 7) {


// 3-to-8 line decoder logic for (int i =
0; i < 8; i++) {
if (i == input) {
printf("Output %d is active (HIGH)\n", i);
} else {
printf("Output %d is inactive (LOW)\n", i);
}
}
} else {
printf("Invalid input. Please enter a 3-bit binary number (000 to 111).\n");
}

return 0;
OUTPUT

15
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-04

OBJECTIVE:-
Implementing 4x1 and 8x1 MULTIPLEXERS.

THEORY:-
A multiplexer (MUX) is a digital logic device that allows you to select one input from multiple
inputs and route it to the output. A 4x1 MUX selects one input from four possible inputs, and an 8x1
MUX selects one input from eight possible inputs. Let's explain the implementation of both a 4x1
MUX and an 8x1 MUX in more detail:

4X1 MULTIPLEXER (4 INPUTS, 1 OUTPUT):

In a 4x1 MUX, there are four input lines and one output line. The MUX is controlled by a select
signal that determines which input is connected to the output. Here's how it's implemented:

16
Roll.No.:-2300430130009
Anurag Sharma

1. Inputs: The 4x1 MUX has four input lines (input0, input1, input2, and input3).

2. Select Signal: The select signal (select) determines which input will be routed to the output. It
can take on values 0, 1, 2, or 3, corresponding to the four input lines.

3. MUX Function (mux4x1): The function `mux4x1` takes the four input values and the select
signal as arguments. It uses a conditional statement to select the appropriate input based on the select
signal.

4. Output: The selected input is returned as the output of the MUX.

8X1 MULTIPLEXER (8 INPUTS, 1 OUTPUT):

In an 8x1 MUX, there are eight input lines and one output line. The MUX is controlled by a select
signal that determines which input is connected to the output. Here's how it's implemented:

1. Inputs: The 8x1 MUX has eight input lines (input0 to input7).

2. Select Signal: The select signal (select) determines which input will be routed to the output. It
can take on values from 0 to 7, corresponding to the eight input lines.

3. MUX Function (mux8x1): The function `mux8x1` takes an array of eight input values and the
select signal as arguments. It uses the select signal as an index to access the appropriate input from
the array.

17
Roll.No.:-2300430130009
Anurag Sharma

4. Output: The selected input from the array is returned as the output of the MUX

In both cases, the MUX selects one of the inputs based on the select signal. This functionality is used
in digital circuits for various purposes, such as data routing, address decoding in memory devices,
and signal selection. The select signal determines which input data gets passed through to the output,
and this can be a fundamental building block in digital systems for making decisions and routing
data efficiently.

CODE

IMPLEMENT A 4X1 MUX

#include <stdio.h>

// Function to implement a 4x1 MUX


int mux4x1(int input0, int input1, int input2, int input3, int select) { if (select == 0)
return input0; else if
(select == 1)
return input1; else if
(select == 2)
return input2; else if
(select == 3)
return input3; else
return -1; // Error: Invalid select value
}

int main() {
int input0, input1, input2, input3, select, result;

printf("Enter four input values (0 or 1): ");


scanf("%d %d %d %d", &input0, &input1, &input2, &input3);

printf("Enter the select value (0, 1, 2, or 3): "); scanf("%d", &select);

18
Roll.No.:-2300430130009
Anurag Sharma

result = mux4x1(input0, input1, input2, input3, select); if (result != -1)


printf("Output: %d\n", result);
else
printf("Invalid select value. Please enter 0, 1, 2, or 3.\n");

return 0;
}

OUTPUT

IMPLEMENT A 8X1 MUX

#include <stdio.h>

// Function to implement an 8x1 MUX int mux8x1(int


inputs[], int select) {
if (select >= 0 && select < 8) return
inputs[select];
else
return -1; // Error: Invalid select value
}

int main() {
int inputs[8];
int select, result;

printf("Enter eight input values (0 or 1): "); for (int i = 0; i < 8;


i++) {
scanf("%d", &inputs[i]);
}

printf("Enter the select value (0 to 7): "); scanf("%d",


&select);

result = mux8x1(inputs, select);

19
Roll.No.:-2300430130009
Anurag Sharma

if (result != -1)
printf("Output: %d\n", result); else
printf("Invalid select value. Please enter a value from 0 to 7.\n");

return 0;
}

OUTPUT

20
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-05
OBJECTIVE:-

21
Roll.No.:-2300430130009
Anurag Sharma

Verify the excitation tables of various flip-flops.


THEORY:-
Here are the excitation tables for the D flip-flop, the JK flip- flop, and the T flip-flop:

1. D FLIP-FLOP EXCITATION TABLE:

 Current State (Q)


 Next State (Q+)
 D Input
 Clock (C)

 When D is 0, the Q output remains the same.


 When D is 1, the Q output changes to the complement of its current state.

 JK FLIP-FLOP EXCITATION TABLE:Current State (Q)


 Next State (Q+)
 J Input
 K Input
 Clock (C)

 When both J and K are 0, the Q output remains the same.


 When J is 1 and K is 0, the Q output is set to 1.
 When J is 0 and K is 1, the Q output is cleared to 0.
22
Roll.No.:-2300430130009
Anurag Sharma

 When both J and K are 1, the flip-flop toggles its state.

2. T FLIP-FLOP EXCITATION TABLE:

 Current State (Q)


 Next State (Q+)
 T Input
 Clock (C)

 When T is 0, the Q output remains the same.


 When T is 1, the Q output toggles (inverts) its state.

These excitation tables describe the behavior of D, JK, and T flip-flops in terms of their
inputs and clock signals. The X in the tables represents "don't care" conditions, which means
the input values for the clock signal do not matter for determining the flip-flop's behavior in
these tables.

CODE
#include <stdio.h>

// Function to simulate D Flip-Flop int dFlipFlop(int


currentQ, int D) {
return D;
}

// Function to simulate JK Flip-Flop


int jkFlipFlop(int currentQ, int J, int K) { if (J == 0 && K
== 0) {
return currentQ;
} else if (J == 1 && K == 0) { return 1;
} else if (J == 0 && K == 1) {

23
Roll.No.:-2300430130009
Anurag Sharma

return 0; lop(int currentQ,


int T) {
} else {
if (T == 0) {
return !currentQ;
return currentQ;
}
} } else {
return !currentQ;
/
/ }
}
F
u
int main() {
n
int currentQ, D, J, K, T;
c
t
// Test D Flip-Flop
i
printf("D Flip-Flop
o
Excitation Table:\n");
n
currentQ = 0;
t D = 0;
o printf("Q=%d D=%d => Q+=%d\n",
currentQ, D, dFlipFlop(currentQ, D));
s D = 1;
i printf("Q=%d D=%d => Q+=%d\n",
m currentQ, D, dFlipFlop(currentQ, D));
u
l // Test JK Flip-Flop
a printf("\nJK Flip-Flop
t Excitation Table:\n"); currentQ
e = 0;
T J = 0;
K = 0;
F
l printf("Q=%d J=%d K=%d => Q+=%d\n",
i currentQ, J, K, jkFlipFlop(currentQ, J, K));
p
- J = 1;
F K = 0;
l printf("Q=%d J=%d K=%d => Q+=%d\n",
o currentQ, J, K, jkFlipFlop(currentQ, J, K));
p
J = 0;
i K = 1;
n
printf("Q=%d J=%d K=%d => Q+=%d\n",
t
currentQ, J, K, jkFlipFlop(currentQ, J, K));
t J = 1;
F K = 1;
l
printf("Q=%d J=%d K=%d => Q+=%d\n",
i
currentQ, J, K, jkFlipFlop(currentQ, J, K));
p
F
24
Roll.No.:-2300430130009
Anurag Sharma

// Test T Flip-Flop Q = 0;
p T = 0;
r
printf("Q=%d T=%d => Q+=%d\n",
i
currentQ, T, tFlipFlop(currentQ, T));
n
t T = 1;
f printf("Q=%d T=%d => Q+=%d\n",
( currentQ, T, tFlipFlop(currentQ, T));
"
\ return 0;
n
T }

F OUTPUT
l
i
p
-
F
l
o
p

E
x
c
i
t
a
t
i
o
n

T
a
b
l
e
:
\
n
"
)
;

c
u
r
r
e
n
t
25
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-06

OBJECTIVE:-

26
Roll.No.:-2300430130009
Anurag Sharma

Design of an 8-bit Input/ Output system with four 8-bit Internal Registers.
THEORY:-
Designing an 8-bit Input/Output system with four 8-bit internal registers can be a complex
task, and the specific requirements and components may vary depending on your application.
However, I can provide you with a simplified conceptual design for such a system.

COMPONENTS NEEDED:

1. Microcontroller or Processor Unit: This serves as the central processing unit and
manages the overall operation of the system.

2. Input/Output Interfaces: These can be GPIO (General- Purpose Input/Output) pins,


UART, SPI, or any other interface suitable for your application.

3. Internal Registers: You need four 8-bit internal registers to store data temporarily. These
can be implemented using flip- flops or memory elements.

4. Data Bus: An 8-bit data bus is required to transfer data between the microcontroller,
internal registers, and I/O devices.

5. Address Bus: An address bus is needed to select the appropriate register when reading
or writing data.

6. Control Logic: This controls the data flow between the registers, I/O devices, and the
processor. It also manages the timing and synchronization of operations.

DESIGN OVERVIEW:

1. Registers:
 You have four 8-bit internal registers, which can be labeled as Reg0, Reg1, Reg2, and
Reg3.
 These registers store data temporarily for processing by the microcontroller.

27
Roll.No.:-2300430130009
Anurag Sharma

2. Data Bus:
 The 8-bit data bus allows data transfer between the
microcontroller, internal registers, and I/O devices.
 Each register is connected to the data bus.
3. Address Bus:
 You need an address bus to select which internal register to access.
 The address bus should have enough bits to address the four registers (e.g., 2 bits for 4
registers).
4. Control Logic:
 The control logic is responsible for managing data transfer between registers and I/O
devices.
 It handles read and write operations based on the address and control signals.
5. Input/Output Interfaces:
 You can have various I/O devices (e.g., sensors, displays) connected to the
input/output interfaces.
 The microcontroller communicates with these devices using appropriate protocols, such
as GPIO, UART, or SPI.
OPERATIONS:
 Data can be loaded into or read from the internal registers using the address bus and
data bus.
 The microcontroller controls the data flow and the operations of the system through the
control logic.
 Input data from external devices can be read into the registers for processing.
 Processed data can be written to the registers and then sent to output devices.

This is a simplified conceptual design. In a real-world application, you'll need to consider


specific requirements, microcontroller selection, voltage levels, clocking, addressing, and
communication protocols. Additionally, the control logic might involve finite state machines
or other more complex circuitry depending on your system's functionality.

#include <stdio.h>
// Define four 8-bit internal registers unsigned char
registers[4] = {0, 0, 0, 0};

// Read data from an internal register


unsigned char readRegister(int registerNumber) {
if (registerNumber >= 0 && registerNumber < 4) { return
registers[registerNumber];
} else {
printf("Invalid register number\n"); return 0; // Or
some error value

}
}

28
Roll.No.:-2300430130009
Anurag Sharma

// Write data to an internal register


void writeRegister(int registerNumber, unsigned char data) { if (registerNumber >= 0 &&
registerNumber < 4) {
registers[registerNumber] = data;
} else {
printf("Invalid register number\n");
}
}

int main() {
int registerNum = 2;
unsigned char dataToWrite = 0xAB;

// Write data to an internal register writeRegister(registerNum,


dataToWrite); printf("Data 0x%02X written to Register %d\n",
dataToWrite, registerNum);

// Read data from an internal register


unsigned char readData = readRegister(registerNum); printf("Data read from
Register %d: 0x%02X\n",
registerNum, readData);

return 0;
}
OUTPUT

29
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-07
OBJECTIVE:-

30
Roll.No.:-2300430130009
Anurag Sharma

Design of an 8-bit ARITHMETIC LOGIC UNIT.

THEORY:-
Designing an 8-bit Arithmetic Logic Unit (ALU) is a complex task, and the specific design
may vary based on your requirements and constraints. However, I can provide you with a
simplified conceptual design for an 8-bit ALU.

Components Needed:
1. 8-bit Inputs (A and B): The two 8-bit operands that the ALU will perform operations on.
2. Operation Selector (Control Inputs): These control inputs determine the operation to be
performed (addition, subtraction, bitwise AND, OR, etc.).
3. 8-bit Output (Result): The result of the operation.
4. Flags: ALU typically sets flags like zero, carry, and
overflow to provide additional information about the result.
Design Overview:
1. Addition and Subtraction:
a. The ALU should be able to perform both addition and subtraction.
b. Subtraction can be implemented by taking the two's complement of the B
operand and adding it to A.
2. Bitwise Operations:
 The ALU can perform bitwise AND, OR, XOR, and other operations.
 These operations operate on each pair of corresponding bits of A and B.
3. Overflow Detection:
 For addition and subtraction, overflow can be detected by examining the carry-out of
the most significant bit.
4. Zero Flag:
 The zero flag is set if the result is all zeros.
5. Carry Flag:
 The carry flag is set if there's a carry-out from the most significant bit during
addition.

31
Roll.No.:-2300430130009
Anurag Sharma

CODE
#include <stdio.h>

// ALU operations enum


ALUOperation {
ADDITION,
SUBTRACTION,
BITWISE_AND
};

// 8-bit ALU function


unsigned char alu_8bit(unsigned char A, unsigned char B, enum ALUOperation
operation, int* carry, int* overflow, int* zero) {
unsigned char result = 0;

switch (operation) { case


ADDITION: result = A +
B;
*carry = (result < A);
*overflow = ((*carry) ^ (A >> 7)) && ((*carry) ^ (B
>> 7));
break;

case SUBTRACTION:
result = A - B;
*carry = (A < B);
*overflow = ((*carry) ^ (A >> 7)) && ((*carry) ^ (B
>> 7));
break;

case BITWISE_AND: result = A


& B; break;
// Add more cases for other
operations

default:
printf("Unsupported operation\n");
}

*zero = (result == 0);

return result;
}
int main() {
32
Roll.No.:-2300430130009
Anurag Sharma

unsigned char operandA = 0b11011011; unsigned char


operandB = 0b10101010;
enum ALUOperation operation = ADDITION; int carry, overflow, zero;

unsigned char result = alu_8bit(operandA, operandB, operation, &carry,


&overflow, &zero);

printf("Result: 0x%02X\n", result); printf("Carry: %d\


n", carry); printf("Overflow: %d\n", overflow);
printf("Zero: %d\n", zero);

return 0;
}
OUTPUT

33
Roll.No.:-2300430130009
Anurag Sharma

EXPERIMENT:-08
OBJECTIVE:-

Design the data path of a computer from its register transfer language description.

THEORY:-
Designing a complete data path for a computer system based on a register transfer language
description is a complex task, and the specific design would depend on various factors,
including the computer architecture, the instruction set, and performance requirements.
Here, I'll provide a simplified conceptual overview of the data path components and their
interactions based on a hypothetical description.

REGISTER TRANSFER LANGUAGE DESCRIPTION:


In a typical computer's RTL description, you'd have instructions that specify the transfer of
data between various registers, as well as operations on those data. The RTL would describe
the micro-operations that happen in a single instruction cycle.

DATA PATH COMPONENTS:


1. Registers:
 General-Purpose Registers (e.g., R0, R1, ... Rn): Used for data storage and manipulation.
 Special Purpose Registers (e.g., PC, IR, MAR, MDR, etc.): Used for specific purposes
like program counter, instruction register, memory address register, memory data
register, etc.
2. ALU (Arithmetic Logic Unit):
 Performs arithmetic and logic operations on data from registers.
 Receives inputs from registers or immediate values,
performs operations, and produces results.
3. Memory Unit:
 Consists of Memory Address Register (MAR) and Memory Data Register (MDR).
 Used to read from and write to memory (RAM, ROM).
4. Control Unit:
 Generates control signals based on the instruction being executed.
 Controls the data path components to execute instructions.

34
Roll.No.:-2300430130009
Anurag Sharma

 Data Bus:
 Connects all the data path components and allows data to be transferred between
them.

DATA PATH OPERATION:


1. Fetch Phase:
 The program counter (PC) holds the address of the next instruction to be executed.
 The PC value is placed on the address bus, and the instruction is fetched from memory
into the instruction register (IR).
 The PC is incremented to point to the next instruction.
2. Decode Phase:
 The control unit decodes the instruction in the IR to determine the required micro-
operations.
 It generates control signals for the data path components.
3. Execution Phase:
 Based on the decoded instruction, data is transferred
between registers.
 The ALU performs operations (addition, subtraction, logic operations, etc.) on data in
registers.
 Data can be loaded from memory (or stored into memory) using the MAR and MDR.
4. Write Back Phase:
 The result of an operation can be written back to registers if required by the instruction.
 The PC can be updated to point to the next instruction.

EXAMPLE RTL DESCRIPTION:


Here's a simplified RTL description for a hypothetical
instruction:
ADD R1, R2, R3 ;
Add the contents of R1 and R2; store the result
in R3;

In the data path, this RTL description would involve:


 Loading data from R1 and R2 into the ALU inputs.
 Performing the addition operation in the ALU.
 Storing the result in R3.

Please note that a complete real-world computer's data path would be much more complex,
supporting various addressing modes, multiple instructions, pipelining, and other advanced
features. The design can vary significantly based on the specific computer architecture and
requirements.

35
Roll.No.:-2300430130009
Anurag Sharma

CODE
#include <stdio.h>

// Define registers and memory int registers[4]; //


R0, R1, R2, R3
int memory[256]; // Simulated memory

// Define special-purpose registers int PC; //


Program Counter
int IR; // Instruction Register
int MAR; // Memory Address Register int MDR; //
Memory Data Register

// ALU operation codes #define


ALU_ADD 0
#define ALU_SUB 1
// ALU function
int ALU(int operand1, int operand2, int operation) {
switch (operation) { case ALU_ADD:
return operand1 + operand2; case
ALU_SUB:
return operand1 - operand2;
// Add more ALU operations as needed default:
printf("Unsupported ALU operation\n"); return 0;
}
}

// Memory read function


int memoryRead(int address) { return
memory[address];
}

// Memory write function


void memoryWrite(int address, int data) { memory[address] =
data;
}

// Execute instruction function void


executeInstruction() {
// Fetch instruction from memory MAR = PC;
MDR = memoryRead(MAR); IR = MDR;

// Decode instruction
int opcode = IR >> 4; // Extract opcode from the higher 4 bits
int operand1 = IR & 0xF; // Extract operand1 from the lower 4 bits

36
Roll.No.:-2300430130009
Anurag Sharma

int operand2 = IR & 0xF; // Extract operand2 from the lower 4 bits

// Execute instruction switch


(opcode) {
case ALU_ADD:
case ALU_SUB:
// ALU operation
registers[operand1] = ALU(registers[operand1], registers[operand2],
opcode);
break;
// Add more opcodes for other instructions

default:
printf("Unsupported opcode\n");
}

// Increment program counter for the next instruction PC++;


}

int main() {
// Initialize registers, memory, and program counter registers[0] = 0;
registers[1] = 5;
registers[2] = 10;
registers[3] = 0;

memory[0] = 0x01; // Instruction: ALU_ADD R0, R1, R2 PC = 0;


// Execute instruction
executeInstruction();
// Print register values after
execution printf("Registers
after execution:\n"); for (int i =
0; i < 4; i++) {
printf("R%d: %d\n", i, registers[i]);
}

return 0;
}
OUTPUT

37
Roll.No.:-2300430130009
Anurag Sharma

38
Roll.No.:-2300430130009

You might also like