0% found this document useful (0 votes)
9 views14 pages

Programs

The document contains multiple C programs that implement various operations related to number systems and binary arithmetic, including conversions between binary, decimal, octal, and hexadecimal. It also covers binary addition, subtraction, multiplication, and arithmetic micro-operations using logic gates. Additionally, it includes logic and shift micro-operations, as well as signed multiplication using Booth's algorithm.

Uploaded by

Titus Martin
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)
9 views14 pages

Programs

The document contains multiple C programs that implement various operations related to number systems and binary arithmetic, including conversions between binary, decimal, octal, and hexadecimal. It also covers binary addition, subtraction, multiplication, and arithmetic micro-operations using logic gates. Additionally, it includes logic and shift micro-operations, as well as signed multiplication using Booth's algorithm.

Uploaded by

Titus Martin
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

Computer Organization LAB Programs

1. Implement a C program to convert a Hexadecimal, octal, and binary number to


decimal number vice versa.
#include <stdio.h>
int binaryToDecimal(long long binary);
long long decimalToBinary(int decimal);
int octalToDecimal(int octal);
int decimalToOctal(int decimal);
int hexToDecimal(char hex[]);
void decimalToHex(int decimal);
int main() {
int choice, decimal, octal;
long long binary;
char hex[20];
do {
printf("\n--- Number System Conversion ---\n");
printf("1. Binary to Decimal\n");
printf("2. Decimal to Binary\n");
printf("3. Octal to Decimal\n");
printf("4. Decimal to Octal\n");
printf("5. Hexadecimal to Decimal\n");
printf("6. Decimal to Hexadecimal\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Binary Number: ");
scanf("%lld", &binary);
printf("Decimal = %d\n", binaryToDecimal(binary));
break;
case 2:
printf("Enter Decimal Number: ");
scanf("%d", &decimal);
printf("Binary = %lld\n", decimalToBinary(decimal));
break;
case 3:
printf("Enter Octal Number: ");
scanf("%d", &octal);
printf("Decimal = %d\n", octalToDecimal(octal));
break;
case 4:
printf("Enter Decimal Number: ");
scanf("%d", &decimal);
printf("Octal = %d\n", decimalToOctal(decimal));
break;
case 5:
printf("Enter Hex Number: ");
scanf("%s", hex);
printf("Decimal = %d\n", hexToDecimal(hex));
break;
case 6:
printf("Enter Decimal Number: ");
scanf("%d", &decimal);
printf("Hexadecimal = ");
decimalToHex(decimal);
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid Choice!\n");
}

} while (choice != 7);

return 0;
}
// Binary to Decimal
int binaryToDecimal(long long binary) {
int decimal = 0, base = 1, rem;
while (binary > 0) {
rem = binary % 10;
decimal += rem * base;
base *= 2;
binary /= 10;
}
return decimal;
}
// Decimal to Binary
long long decimalToBinary(int decimal) {
long long binary = 0;
int rem, base = 1;
while (decimal > 0) {
rem = decimal % 2;
binary += rem * base;
base *= 10;
decimal /= 2;
}
return binary;
}
// Octal to Decimal
int octalToDecimal(int octal) {
int decimal = 0, base = 1, rem;
while (octal > 0) {
rem = octal % 10;
decimal += rem * base;
base *= 8;
octal /= 10;
}
return decimal;
}

// Decimal to Octal
int decimalToOctal(int decimal) {
int octal = 0, base = 1;
while (decimal > 0) {
octal += (decimal % 8) * base;
base *= 10;
decimal /= 8;
}
return octal;
}
// Hexadecimal to Decimal
int hexToDecimal(char hex[]) {
int decimal = 0, base = 1;
for (int i = strlen(hex) - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9')
decimal += (hex[i] - '0') * base;
else if (hex[i] >= 'A' && hex[i] <= 'F')
decimal += (hex[i] - 'A' + 10) * base;
else if (hex[i] >= 'a' && hex[i] <= 'f')
decimal += (hex[i] - 'a' + 10) * base;
base *= 16;
}
return decimal;
}
// Decimal to Hexadecimal
void decimalToHex(int decimal) {
char hex[20];
int i = 0;

while (decimal > 0) {


int rem = decimal % 16;
if (rem < 10)
hex[i] = rem + '0';
else
hex[i] = rem - 10 + 'A';
decimal /= 16;
i++;
}
// Print in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%c", hex[j]);
printf("\n");
}
2. Implement a C program to perform Binary Addition & Subtraction.
#include <stdio.h>
#include <string.h>
// Function to add two binary numbers
void binaryAddition(char bin1[], char bin2[]) {
char result[50];
int i = strlen(bin1) - 1;
int j = strlen(bin2) - 1;
int k = 0, carry = 0;
result[0] = '\0'; // initialize result as empty string
while (i >= 0 || j >= 0 || carry > 0) {
int bit1 = (i >= 0) ? bin1[i] - '0' : 0;
int bit2 = (j >= 0) ? bin2[j] - '0' : 0;
int sum = bit1 + bit2 + carry;
result[k++] = (sum % 2) + '0'; // store current bit
carry = sum / 2; // update carry
i--;
j--;
}
result[k] = '\0';
// Reverse the result
for (int x = 0; x < k / 2; x++) {
char temp = result[x];
result[x] = result[k - x - 1];
result[k - x - 1] = temp;
}
printf("Binary Addition Result = %s\n", result);
}
// Function to subtract two binary numbers (bin1 - bin2)
void binarySubtraction(char bin1[], char bin2[]) {
char result[50];
int i = strlen(bin1) - 1;
int j = strlen(bin2) - 1;
int k = 0, borrow = 0;
result[0] = '\0';
while (i >= 0) {
int bit1 = bin1[i] - '0';
int bit2 = (j >= 0) ? bin2[j] - '0' : 0;
bit1 = bit1 - borrow;
if (bit1 < bit2) {
bit1 += 2;
borrow = 1;
} else {
borrow = 0;
}
result[k++] = (bit1 - bit2) + '0';
i--;
j--;
}
// Remove leading zeros
while (k > 1 && result[k - 1] == '0')
k--;

result[k] = '\0';
// Reverse the result
for (int x = 0; x < k / 2; x++) {
char temp = result[x];
result[x] = result[k - x - 1];
result[k - x - 1] = temp;
}
printf("Binary Subtraction Result = %s\n", result);
}
int main() {
char bin1[50], bin2[50];
int choice;

do {
printf("\n--- Binary Operations ---\n");
printf("1. Binary Addition\n");
printf("2. Binary Subtraction\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1 || choice == 2) {
printf("Enter first binary number: ");
scanf("%s", bin1);
printf("Enter second binary number: ");
scanf("%s", bin2);
}
switch (choice) {
case 1:
binaryAddition(bin1, bin2);
break;
case 2:
binarySubtraction(bin1, bin2);
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid Choice!\n");
}
} while (choice != 3);

return 0;
}
3. Implement a C program to perform Multiplication of two binary numbers.
#include <stdio.h>
#include <string.h>
// Function to convert Binary to Decimal
int binaryToDecimal(long long binary) {
int decimal = 0, base = 1, rem;
while (binary > 0) {
rem = binary % 10;
decimal += rem * base;
base *= 2;
binary /= 10;
}
return decimal;
}
// Function to convert Decimal to Binary
long long decimalToBinary(int decimal) {
long long binary = 0;
int base = 1;
while (decimal > 0) {
int rem = decimal % 2;
binary += rem * base;
base *= 10;
decimal /= 2;
}
return binary;
}
int main() {
long long bin1, bin2;
int dec1, dec2, product;
long long binaryProduct;
printf("\n--- Binary Multiplication ---\n");
printf("Enter first binary number: ");
scanf("%lld", &bin1);
printf("Enter second binary number: ");
scanf("%lld", &bin2);
// Step 1: Convert to decimal
dec1 = binaryToDecimal(bin1);
dec2 = binaryToDecimal(bin2);
// Step 2: Multiply in decimal
product = dec1 * dec2;
// Step 3: Convert product back to binary
binaryProduct = decimalToBinary(product);
printf("Binary Multiplication Result = %lld\n", binaryProduct);
printf("(In Decimal = %d)\n", product);
return 0;
}
4. Implement arithmetic micro-operations using logic gates.
#include <stdio.h>
// Function to perform binary addition using logic gates
int addUsingGates(int a, int b) {
int carry;
while (b != 0) {
carry = a & b; // AND gate -> generates carry
a = a ^ b; // XOR gate -> sum without carry
b = carry << 1; // shift carry to add in next position
}
return a;
}
// Function to perform subtraction using 2's complement
int subUsingGates(int a, int b) {
// 2's complement of b = (~b + 1)
b = addUsingGates(~b, 1);
return addUsingGates(a, b);
}
// Function to increment using logic gates
int increment(int a) {
return addUsingGates(a, 1);
}
// Function to decrement using logic gates
int decrement(int a) {
return subUsingGates(a, 1);
}
int main() {
int a, b, choice;
do {
printf("\n--- Arithmetic Micro-Operations ---\n");
printf("1. Addition (A + B)\n");
printf("2. Subtraction (A - B)\n");
printf("3. Increment (A + 1)\n");
printf("4. Decrement (A - 1)\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1 || choice == 2) {
printf("Enter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);
} else if (choice == 3 || choice == 4) {
printf("Enter A: ");
scanf("%d", &a);
}
switch (choice) {
case 1:
printf("Result (A + B) = %d\n", addUsingGates(a, b));
break;
case 2:
printf("Result (A - B) = %d\n", subUsingGates(a, b));
break;
case 3:
printf("Result (A + 1) = %d\n", increment(a));
break;
case 4:
printf("Result (A - 1) = %d\n", decrement(a));
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid Choice!\n");
}
} while (choice != 5);
return 0;
}

5. Implement logic and shift micro-operations using logic gates.


#include <stdio.h>

// Function for Logic Micro-Operations


void logicOperations(int a, int b) {
printf("\n--- Logic Micro-Operations ---\n");
printf("A AND B = %d\n", a & b); // AND gate
printf("A OR B = %d\n", a | b); // OR gate
printf("A XOR B = %d\n", a ^ b); // XOR gate
printf("NOT A = %d\n", ~a); // NOT gate (1's complement)
printf("NOT B = %d\n", ~b);
}
// Function for Shift Micro-Operations
void shiftOperations(int a) {
printf("\n--- Shift Micro-Operations ---\n");
printf("Original A = %d\n", a);
printf("Logical Left Shift (A << 1) = %d\n", a << 1); // Multiply by 2
printf("Logical Right Shift (A >> 1) = %d\n", a >> 1); // Divide by 2
// Circular Left Shift (1 bit)
int bits = sizeof(a) * 8;
int circularLeft = (a << 1) | ((a >> (bits - 1)) & 1);
printf("Circular Left Shift (1 bit) = %d\n", circularLeft);
// Circular Right Shift (1 bit)
int circularRight = (a >> 1) | ((a & 1) << (bits - 1));
printf("Circular Right Shift (1 bit) = %d\n", circularRight);
}
int main() {
int a, b, choice;
do {
printf("\n--- Logic & Shift Micro-Operations ---\n");
printf("1. Logic Operations (AND, OR, XOR, NOT)\n");
printf("2. Shift Operations (Left, Right, Circular)\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value of A: ");
scanf("%d", &a);
printf("Enter value of B: ");
scanf("%d", &b);
logicOperations(a, b);
break;
case 2:
printf("Enter value of A: ");
scanf("%d", &a);
shiftOperations(a);
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 3);
return 0;
}

6. Implement a C program to perform Multiplication of two binary numbers (signed)


using Booth’s Algorithms.
#include <stdio.h>
#include <string.h>
// Function to convert binary string to integer
int binaryToDecimal(char bin[]) {
int dec = 0;
for (int i = 0; bin[i] != '\0'; i++) {
dec = dec * 2 + (bin[i] - '0');
}
return dec;
}
// Function to convert integer to binary string
void decimalToBinary(int num, char bin[], int size) {
for (int i = size-1; i >= 0; i--) {
bin[i] = (num & 1) + '0';
num >>= 1;
}
bin[size] = '\0';
}
// Function to perform Booth's Algorithm for signed multiplication
int boothsMultiplication(int multiplicand, int multiplier)
{
int m = multiplicand;
int r = multiplier;
int x = 0; // Accumulator
int q = r; // Multiplier
int q_1 = 0; // Extra bit
int n = sizeof(int) * 8; // Number of bits in integer
for (int i = 0; i < n; i++) {
int lastBit = q & 1;
if (lastBit == 1 && q_1 == 0) {
x = x - m; // Subtract multiplicand
} else if (lastBit == 0 && q_1 == 1) {
x = x + m; // Add multiplicand
}
// Arithmetic right shift of [x, q, q_1]
int combined = (x << (n+1)) | (q << 1) | q_1;
combined >>= 1;
q_1 = combined & 1;
q = (combined >> 1) & ((1 << n) - 1);
x = combined >> (n+1);
}
return (x << n) | q;
}
int main() {
char bin1[20], bin2[20];
int multiplicand, multiplier, product;
printf("Enter first binary number (signed): ");
scanf("%s", bin1);
printf("Enter second binary number (signed): ");
scanf("%s", bin2);
multiplicand = binaryToDecimal(bin1);
multiplier = binaryToDecimal(bin2);
product = boothsMultiplication(multiplicand, multiplier);
char result[40];
decimalToBinary(product, result, 32); // Assuming 32-bit result
printf("Multiplication Result (Binary) = %s\n", result);
printf("Multiplication Result (Decimal) = %d\n", product);
return 0;
}
7. Implement a C program to perform division of two binary numbers (Unsigned) using
restoring division algorithm.

#include <stdio.h>
#include <string.h>
#include <math.h>
// Function to convert binary string to decimal
unsigned int binaryToDecimal(char bin[]) {
unsigned int dec = 0;
for (int i = 0; bin[i] != '\0'; i++) {
dec = dec * 2 + (bin[i] - '0');
}
return dec;
}
// Function to convert decimal to binary string
void decimalToBinary(unsigned int num, char bin[], int size) {
for (int i = size-1; i >= 0; i--) {
bin[i] = (num & 1) + '0';
num >>= 1;
}
bin[size] = '\0';
}
// Function to perform restoring division
void restoringDivision(unsigned int dividend, unsigned int divisor) {
int n = (int)log2(dividend) + 1; // number of bits in dividend
unsigned int quotient = 0, remainder = 0;

for (int i = n-1; i >= 0; i--) {


remainder = (remainder << 1) | ((dividend >> i) & 1);
remainder = remainder - divisor;
if (remainder < 0) {
quotient = quotient << 1; // Q[i] = 0
remainder = remainder + divisor; // restore remainder
} else {
quotient = (quotient << 1) | 1; // Q[i] = 1
}
}
char qBin[32], rBin[32];
decimalToBinary(quotient, qBin, n);
decimalToBinary(remainder, rBin, n);
printf("Quotient (Binary) = %s\n", qBin);
printf("Remainder (Binary) = %s\n", rBin);
printf("Quotient (Decimal) = %u\n", quotient);
printf("Remainder (Decimal) = %u\n", remainder);
}
int main() {
char bin1[32], bin2[32];
unsigned int dividend, divisor;
printf("Enter dividend (binary): ");
scanf("%s", bin1);
printf("Enter divisor (binary): ");
scanf("%s", bin2);
dividend = binaryToDecimal(bin1);
divisor = binaryToDecimal(bin2);
if (divisor == 0) {
printf("Error: Division by zero!\n");
return 1;
}
restoringDivision(dividend, divisor);
return 0;
}
8. Implement a C program to perform division of two binary numbers (Unsigned) using
nonrestoring division algorithm.

#include <stdio.h>
#include <string.h>
#include <math.h>

// Function to convert binary string to decimal


unsigned int binaryToDecimal(char bin[]) {
unsigned int dec = 0;
for (int i = 0; bin[i] != '\0'; i++) {
dec = dec * 2 + (bin[i] - '0');
}
return dec;
}

// Function to convert decimal to binary string


void decimalToBinary(unsigned int num, char bin[], int size) {
for (int i = size-1; i >= 0; i--) {
bin[i] = (num & 1) + '0';
num >>= 1;
}
bin[size] = '\0';
}

// Function to perform non-restoring division


void nonRestoringDivision(unsigned int dividend, unsigned int divisor) {
int n = (int)log2(dividend) + 1; // number of bits in dividend
unsigned int quotient = 0, remainder = 0;

for (int i = n-1; i >= 0; i--) {


remainder = (remainder << 1) | ((dividend >> i) & 1);
if ((int)remainder >= 0) {
remainder = remainder - divisor;
quotient = (quotient << 1) | 1;
} else {
remainder = remainder + divisor;
quotient = quotient << 1;
}
}

// Adjust remainder if negative at the end


if ((int)remainder < 0) remainder += divisor;

char qBin[32], rBin[32];


decimalToBinary(quotient, qBin, n);
decimalToBinary(remainder, rBin, n);

printf("Quotient (Binary) = %s\n", qBin);


printf("Remainder (Binary) = %s\n", rBin);
printf("Quotient (Decimal) = %u\n", quotient);
printf("Remainder (Decimal) = %u\n", remainder);
}

int main() {
char bin1[32], bin2[32];
unsigned int dividend, divisor;

printf("Enter dividend (binary): ");


scanf("%s", bin1);
printf("Enter divisor (binary): ");
scanf("%s", bin2);

dividend = binaryToDecimal(bin1);
divisor = binaryToDecimal(bin2);

if (divisor == 0) {
printf("Error: Division by zero!\n");
return 1;
}

nonRestoringDivision(dividend, divisor);

return 0;
}

9. Write assembly language code for A+B*(C-D) using various instruction formats in
MASM or any open-source assembler.

.MODEL SMALL
.STACK 100H
.DATA
A DW 5 ; Example values
B DW 3
C DW 10
D DW 4
RESULT DW 0

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX ; Initialize data segment

; Step 1: Compute (C - D)
MOV AX, C ; Load C into AX
SUB AX, D ; AX = C - D

; Step 2: Multiply result by B


MOV BX, B ; Load B into BX
IMUL BX ; AX = AX * BX

; Step 3: Add A
ADD AX, A ; AX = A + B*(C-D)

; Step 4: Store result


MOV RESULT, AX

; Exit program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

10. Write assembly language code for A+B*C using various addressing modes in MASM
or any open-source assembler.

.MODEL SMALL
.STACK 100H
.DATA
A DW 5 ; Example values
B DW 3
C DW 4
RESULT DW 0

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX ; Initialize data segment

; --------------------------
; Step 1: Load B into AX (Register Direct)
MOV AX, B ; AX = B

; Step 2: Multiply AX by C (Register Indirect / Memory)


IMUL WORD PTR C ; AX = AX * C

; Step 3: Add A to AX (Direct Addressing)


ADD AX, A ; AX = AX + A

; Step 4: Store result in memory (Direct Addressing)


MOV RESULT, AX ; RESULT = AX

; Exit program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

You might also like