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

Experiment 1 and 2

The document outlines two experiments focused on simulating Arithmetic Logic Unit (ALU) operations and status flags. The first experiment involves basic ALU operations such as addition, subtraction, AND, OR, and XOR, with sample C code provided for implementation. The second experiment introduces status flags like zero, negative, carry, and overflow, demonstrating how these flags can be set based on the results of ALU operations.

Uploaded by

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

Experiment 1 and 2

The document outlines two experiments focused on simulating Arithmetic Logic Unit (ALU) operations and status flags. The first experiment involves basic ALU operations such as addition, subtraction, AND, OR, and XOR, with sample C code provided for implementation. The second experiment introduces status flags like zero, negative, carry, and overflow, demonstrating how these flags can be set based on the results of ALU operations.

Uploaded by

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

Experiment 1: Arithmetic Logic Unit operations simulation

The goal of this experiment is to allow students to simulate the basic operations of an ALU,
which include addition, subtraction, AND, OR, and XOR operations.

Objective:

 Understand the basic arithmetic and logic operations.


 Implement ALU functionality in code.

Experiment Steps:

1. Introduction to ALU Operations: Explain the basic operations supported by an ALU:


o Addition: Adds two numbers.
o Subtraction: Subtracts one number from another.
o AND: Performs bitwise AND.
o OR: Performs bitwise OR.
o XOR: Performs bitwise XOR.

2. Sample Code for Simulating ALU in C:

#include <stdio.h>

// Function to perform arithmetic and logical operations


unsigned int ALU(unsigned int A, unsigned int B, unsigned int
opcode) {
unsigned int result; // Variable to store the result of the
operation

// Perform the operation based on the opcode


switch (opcode) {
case 0: // Addition operation
result = A + B;
break;
case 1: // Subtraction operation
result = A - B;
break;
case 2: // Bitwise AND operation
result = A & B;
break;
case 3: // Bitwise OR operation
result = A | B;
break;
default: // Default case, if an unsupported opcode is
given
result = 0; // Set result to 0 for undefined
operations
break;
}

// Return the result of the operation


return result;
}

int main() {
// Input values for testing the ALU function
unsigned int A = 8, B = 7; // Example inputs
unsigned int opcode = 0; // Opcode: 0 corresponds to
addition
unsigned int result = ALU(A, B, opcode); // Call ALU
function with inputs

// Print the result of the operation


printf("Result of operation: %u\n", result);
return 0; // Exit the program
}
Exercise:
1. Generate results for subtraction, AND and OR
2. Add code for implementing NOT and XOR

Experiment 2: ALU and Status Flags Simulation

This experiment introduces the concept of status flags that are commonly set in a CPU after
performing an ALU operation. These flags can indicate conditions such as "zero", "negative", or
"carry out".

Objective:

 Simulate status flags for arithmetic operations.


 Implement flags like Zero, Negative, Carry, and Overflow.

C Code for ALU with Status Flags:

#include <stdio.h>

// Define a structure to hold the status flags


typedef struct {
int zero; // Indicates if the result is zero
int negative; // Indicates if the result is negative
int carry; // Indicates if there is a carry (for 8-bit
calculations)
int overflow; // Indicates if there is an arithmetic overflow
} Flags;
// ALU function performs various operations based on the input
operation type
int ALU(char operation, int operand1, int operand2, Flags *flags) {
int result = 0; // Variable to store the computation result

// Perform operation based on the input character


switch (operation) {
case 'a': // Addition
result = operand1 + operand2;
break;
case 's': // Subtraction
result = operand1 - operand2;
break;
case 'm': // Multiplication
result = operand1 * operand2;
break;
case 'A': // Bitwise AND
result = operand1 & operand2;
break;
case 'O': // Bitwise OR
result = operand1 | operand2;
break;
default: // Handle invalid operations
printf("Invalid operation\n");
return -1;
}

// Update status flags based on the result


flags->zero = (result == 0) ? 1 : 0; // Zero flag is set if the
result is 0
flags->negative = (result < 0) ? 1 : 0; // Negative flag is set if
the result is negative
flags->carry = (result > 255) ? 1 : 0; // Carry flag is set for
results > 255 (8-bit assumption)
flags->overflow = ((operand1 > 0 && operand2 > 0 && result < 0) ||
(operand1 < 0 && operand2 < 0 && result > 0)) ?
1 : 0; // Overflow logic

return result; // Return the computed result


}

// Function to display the status flags


void displayFlags(Flags *flags) {
printf("Zero Flag: %d, Negative Flag: %d, Carry Flag: %d, Overflow
Flag: %d\n",
flags->zero, flags->negative, flags->carry, flags-
>overflow);
}

// Main function to demonstrate the ALU operations


int main() {
int operand1 = 200, operand2 = 100; // Example operands
Flags flags = {0}; // Initialize flags to 0

// Perform addition and display the result and flags


printf("Addition: %d + %d = %d\n", operand1, operand2, ALU('a',
operand1, operand2, &flags));
displayFlags(&flags);

// Perform subtraction and display the result and flags


operand1 = 50;
operand2 = 100;
printf("Subtraction: %d - %d = %d\n", operand1, operand2, ALU('s',
operand1, operand2, &flags));
displayFlags(&flags);

return 0; // End of program


}

Explanation:

 The Flags struct keeps track of the status flags: zero, negative, carry, and overflow.
 The ALU function now also sets these flags based on the result of the operation. For
example:
o Zero flag is set if the result is zero.
o Negative flag is set if the result is negative.
o Carry flag is set if the result exceeds the limit of a byte (assuming 8-bit).
o Overflow flag is set when the result overflows for signed operations.

You might also like