0% found this document useful (0 votes)
7 views40 pages

Co Practicals

Uploaded by

dnr departments
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)
7 views40 pages

Co Practicals

Uploaded by

dnr departments
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/ 40

1.

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


number vice versa.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Function to convert Hexadecimal to Decimal
int hexToDec(char hex[]) {

int len = strlen(hex);


int base = 1; // 16^0
int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
dec_val += (hex[i] - 48) * base;

} else if (hex[i] >= 'A' && hex[i] <= 'F') {


dec_val += (hex[i] - 55) * base; // 'A' is 10 in decimal
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
dec_val += (hex[i] - 87) * base; // 'a' is 10 in decimal
}
base = base * 16;

}
return dec_val;
}
// Function to convert Octal to Decimal
int octalToDec(char octal[]) {
int len = strlen(octal);

int base = 1; // 8^0


int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
dec_val += (octal[i] - '0') * base;
base = base * 8;
}

return dec_val;
}
// Function to convert Binary to Decimal
int binaryToDec(char binary[]) {
int len = strlen(binary);
int base = 1; // 2^0

int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
if (binary[i] == '1') {
dec_val += base;
}
base = base * 2;

}
return dec_val;
}
// Function to convert Decimal to Hexadecimal
void decToHex(int decimal) {
char hex[100];

int i = 0;
while (decimal != 0) {
int temp = decimal % 16;
if (temp < 10) {
hex[i] = temp + 48;
} else {
hex[i] = temp + 55;
}
i++;

decimal /= 16;
}
// Reverse the array for correct order
for (int j = i - 1; j >= 0; j--) {
printf("%c", hex[j]);
}

}
// Function to convert Decimal to Octal
void decToOctal(int decimal) {
char octal[100];
int i = 0;
while (decimal != 0) {

octal[i] = (decimal % 8) + '0';


i++;
decimal /= 8;
}
// Reverse the array for correct order
for (int j = i - 1; j >= 0; j--) {

printf("%c", octal[j]);
}
}
// Function to convert Decimal to Binary
void decToBinary(int decimal) {
char binary[100];
int i = 0;
while (decimal != 0) {
binary[i] = (decimal % 2) + '0';

i++;
decimal /= 2;
}
// Reverse the array for correct order
for (int j = i - 1; j >= 0; j--) {
printf("%c", binary[j]);

}
}
// Driver function to test the conversions
int main() {
int choice;
char input[100];

int decimal;
do {
printf("\nNumber Conversion Menu:\n");
printf("1. Hexadecimal to Decimal\n");
printf("2. Octal to Decimal\n");
printf("3. Binary to Decimal\n");

printf("4. Decimal to Hexadecimal\n");


printf("5. Decimal to Octal\n");
printf("6. Decimal to Binary\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a Hexadecimal number: ");

scanf("%s", input);
printf("Decimal: %d\n", hexToDec(input));
break;
case 2:
printf("Enter an Octal number: ");
scanf("%s", input);

printf("Decimal: %d\n", octalToDec(input));


break;
case 3:
printf("Enter a Binary number: ");
scanf("%s", input);
printf("Decimal: %d\n", binaryToDec(input));

break;
case 4:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Hexadecimal: ");
decToHex(decimal);

printf("\n");
break;
case 5:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Octal: ");
decToOctal(decimal);
printf("\n");
break;

case 6:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Binary: ");
decToBinary(decimal);

printf("\n");
break;
case 0:
printf("Exiting...\n");
break;
default:

printf("Invalid choice. Please try again.\n");


}
} while (choice != 0);
return 0;
}

OUTPUT
Number Conversion Menu:
1. Hexadecimal to Decimal
2. Octal to Decimal
3. Binary to Decimal
4. Decimal to Hexadecimal
5. Decimal to Octal
6. Decimal to Binary
0. Exit
Enter your choice: 1

Enter a Hexadecimal number: 1A3F


Decimal: 6719
2. Implement a C program to perform Binary Addition & Subtraction.
#include <stdio.h>
#include <string.h>
// Function to convert a binary string to a decimal integer

int binaryToDecimal(char binary[]) {


int decimal = 0;
int base = 1; // 2^0
int len = strlen(binary);
for (int i = len - 1; i >= 0; i--) {
if (binary[i] == '1') {

decimal += base;
}
base = base * 2;
}
return decimal;
}

// Function to convert a decimal integer to a binary string


void decimalToBinary(int decimal, char binary[]) {
int index = 0;
// Handle the special case of decimal == 0
if (decimal == 0) {
strcpy(binary, "0");
return;
}
while (decimal > 0) {

binary[index++] = (decimal % 2) + '0';


decimal /= 2;
}
binary[index] = '\0';
// Reverse the binary string to correct the order
for (int i = 0; i < index / 2; i++) {

char temp = binary[i];


binary[i] = binary[index - i - 1];
binary[index - i - 1] = temp;
}
}
// Function to perform binary addition

void binaryAddition(char bin1[], char bin2[], char result[]) {


int dec1 = binaryToDecimal(bin1);
int dec2 = binaryToDecimal(bin2);
int sum = dec1 + dec2;
decimalToBinary(sum, result);
}

// Function to perform binary subtraction


void binarySubtraction(char bin1[], char bin2[], char result[]) {
int dec1 = binaryToDecimal(bin1);
int dec2 = binaryToDecimal(bin2);
int diff = dec1 - dec2;
// Handle negative results for subtraction
if (diff < 0) {
printf("Subtraction result is negative. Binary subtraction is not valid for negative
numbers.\n");
return;
}
decimalToBinary(diff, result);
}

// Driver function
int main() {
char bin1[100], bin2[100], result[100];
int choice;
do {

printf("\nBinary Addition & Subtraction Menu:\n");


printf("1. Perform Binary Addition\n");
printf("2. Perform Binary Subtraction\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the first binary number: ");
scanf("%s", bin1);
printf("Enter the second binary number: ");
scanf("%s", bin2);

binaryAddition(bin1, bin2, result);


printf("Binary Addition Result: %s\n", result);
break;
case 2:
printf("Enter the first binary number: ");
scanf("%s", bin1);

printf("Enter the second binary number: ");


scanf("%s", bin2);
binarySubtraction(bin1, bin2, result);
if (result[0] != '\0') {
printf("Binary Subtraction Result: %s\n", result);
}

break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");

}
} while (choice != 0);
return 0;
}

OUTPUT
Binary Addition & Subtraction Menu:

1. Perform Binary Addition


2. Perform Binary Subtraction
0. Exit
Enter your choice: 1
Enter the first binary number: 1010
Enter the second binary number: 1101
Binary Addition Result: 10111

3.Implement a C program to perform Multiplication of two binary numbers.


#include <stdio.h>

#include <string.h>
// Function to convert a binary string to decimal integer
int binaryToDecimal(char binary[]) {
int decimal = 0;
int base = 1; // 2^0
int len = strlen(binary);

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


if (binary[i] == '1') {
decimal += base;
}
base = base * 2;
}

return decimal;
}
// Function to convert a decimal integer to a binary string
void decimalToBinary(int decimal, char binary[]) {
int index = 0;
// Handle the case of decimal = 0

if (decimal == 0) {
strcpy(binary, "0");
return;
}
while (decimal > 0) {
binary[index++] = (decimal % 2) + '0';
decimal /= 2;
}
binary[index] = '\0';

// Reverse the binary string to correct the order


for (int i = 0; i < index / 2; i++) {
char temp = binary[i];
binary[i] = binary[index - i - 1];
binary[index - i - 1] = temp;
}

}
// Function to perform binary multiplication
void binaryMultiplication(char bin1[], char bin2[], char result[]) {
int dec1 = binaryToDecimal(bin1);
int dec2 = binaryToDecimal(bin2);
int product = dec1 * dec2;

// Convert the product back to binary


decimalToBinary(product, result);
}
// Driver function
int main() {

char bin1[100], bin2[100], result[200];


printf("Enter the first binary number: ");
scanf("%s", bin1);
printf("Enter the second binary number: ");
scanf("%s", bin2);
// Perform binary multiplication
binaryMultiplication(bin1, bin2, result);
printf("Binary Multiplication Result: %s\n", result);
return 0;

OUTPUT
Enter the first binary number: 1010
Enter the second binary number: 1101
Binary Multiplication Result: 11011010

4.Implement arithmetic micro-operations using logic gates.


#include <stdio.h>

// Function to perform binary addition using bitwise operators


int binaryAddition(int a, int b) {
int carry;
while (b != 0) {

// Find carry and shift it to the left


carry = a & b;
// Sum without carry
a = a ^ b;
// Shift carry by 1 to add to the next higher bit
b = carry << 1;

}
return a;
}
// Function to perform binary subtraction using 2's complement
int binarySubtraction(int a, int b) {
// Take 2's complement of b and add it to a
return binaryAddition(a, binaryAddition(~b, 1));
}
int main() {

int a, b, choice;
printf("Enter two numbers (in decimal form):\n");
scanf("%d %d", &a, &b);
printf("Select operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");

scanf("%d", &choice);

if (choice == 1) {
printf("Result of Binary Addition: %d\n", binaryAddition(a, b));
} else if (choice == 2) {
printf("Result of Binary Subtraction: %d\n", binarySubtraction(a, b));

} else {
printf("Invalid choice!\n");
}
return 0;
}

5.Implement a c program to perform a arithmetic micro-operations using logic gates


#include <stdio.h>
// Logic gates simulation
int AND(int a, int b) {
return a & b;
}
int OR(int a, int b) {
return a | b;
}
int XOR(int a, int b) {

return a ^ b;
}
int NOT(int a) {
return ~a & 1; // Ensure it's a single bit
}
// Half adder implementation using logic gates

void halfAdder(int a, int b, int* sum, int* carry) {


*sum = XOR(a, b);
*carry = AND(a, b);
}
// Full adder implementation using logic gates
void fullAdder(int a, int b, int cin, int* sum, int* carry) {

int sum1, carry1, carry2;


// First half adder
halfAdder(a, b, &sum1, &carry1);
// Second half adder with carry-in
halfAdder(sum1, cin, sum, &carry2);
// Final carry out

*carry = OR(carry1, carry2);


}
// Function to perform binary addition of two 4-bit numbers using full adders
void binaryAddition(int A[], int B[], int result[], int n) {
int carry = 0; // Carry-in initially 0
for (int i = n - 1; i >= 0; i--) {
int sum, carryOut;
fullAdder(A[i], B[i], carry, &sum, &carryOut);
result[i] = sum;

carry = carryOut;
}
printf("Carry Out: %d\n", carry);
}
// Function to perform 2's complement for subtraction
void twosComplement(int B[], int n) {

int carry = 1; // Start with carry 1 to add for 2's complement


// Invert the bits
for (int i = 0; i < n; i++) {
B[i] = NOT(B[i]);
}

// Add 1 to the inverted number to get 2's complement


for (int i = n - 1; i >= 0; i--) {
int sum, carryOut;
fullAdder(B[i], 0, carry, &sum, &carryOut);
B[i] = sum;
carry = carryOut;

}
}
// Function to perform binary subtraction A - B using 2's complement method
void binarySubtraction(int A[], int B[], int result[], int n) {
// Get the 2's complement of B
twosComplement(B, n);

// Perform binary addition of A and 2's complement of B


binaryAddition(A, B, result, n);

}
void printBinary(int bin[], int n) {
for (int i = 0; i < n; i++) {
printf("%d", bin[i]);
}
printf("\n");

}
int main() {
int n = 4; // Define number of bits for simplicity (4-bit binary numbers)
int A[4], B[4], result[4];

// Take input for two 4-bit binary numbers

printf("Enter first 4-bit binary number (A): ");


for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
printf("Enter second 4-bit binary number (B): ");
for (int i = 0; i < n; i++) {

scanf("%d", &B[i]);
}
int choice;
printf("Select operation:\n1. Addition\n2. Subtraction\n");
scanf("%d", &choice);
if (choice == 1) {
// Perform binary addition
printf("Performing Binary Addition (A + B):\n");
binaryAddition(A, B, result, n);

printf("Result (A + B) = ");
printBinary(result, n);
} else if (choice == 2) {
// Perform binary subtraction
printf("Performing Binary Subtraction (A - B):\n");
binarySubtraction(A, B, result, n);

printf("Result (A - B) = ");
printBinary(result, n);
} else {
printf("Invalid choice!\n");
}

return 0;
}

OUTPUT
Enter first 4-bit binary number (A): 1 0 1 0
Enter second 4-bit binary number (B): 0 1 1 0
Select operation:

1. Addition
2. Subtraction
1
Performing Binary Addition (A + B):
Carry Out: 0
Result (A + B) = 11000
Select operation:
1. Addition
2. Subtraction

2
Performing Binary Subtraction (A - B):
Carry Out: 1
Result (A - B) = 0100

6.Implement a c program to perform a logic and shift micro-operations using logic gates.
#include <stdio.h>

// Logic gates simulation


int AND(int a, int b) {
return a & b;
}
int OR(int a, int b) {
return a | b;

int XOR(int a, int b) {


return a ^ b;
}
int NOT(int a) {

return ~a & 1; // Ensure it's a single bit


}
// Function to perform bitwise AND operation on two 4-bit binary numbers
void logicAND(int A[], int B[], int result[], int n) {
for (int i = 0; i < n; i++) {
result[i] = AND(A[i], B[i]);
}
}
// Function to perform bitwise OR operation on two 4-bit binary numbers

void logicOR(int A[], int B[], int result[], int n) {


for (int i = 0; i < n; i++) {
result[i] = OR(A[i], B[i]);
}
}
// Function to perform bitwise XOR operation on two 4-bit binary numbers

void logicXOR(int A[], int B[], int result[], int n) {


for (int i = 0; i < n; i++) {
result[i] = XOR(A[i], B[i]);
}
}
// Function to perform bitwise NOT operation on a 4-bit binary number

void logicNOT(int A[], int result[], int n) {


for (int i = 0; i < n; i++) {
result[i] = NOT(A[i]);
}
}
// Function to perform a logical shift left (LSL)

void shiftLeft(int A[], int result[], int n) {


for (int i = 0; i < n - 1; i++) {
result[i] = A[i + 1]; // Shift left by moving bits to the left
}
result[n - 1] = 0; // Fill the last bit with 0
}
// Function to perform a logical shift right (LSR)
void shiftRight(int A[], int result[], int n) {
for (int i = n - 1; i > 0; i--) {

result[i] = A[i - 1]; // Shift right by moving bits to the right


}
result[0] = 0; // Fill the first bit with 0
}
// Function to perform an arithmetic shift right (ASR)
void arithmeticShiftRight(int A[], int result[], int n) {

int signBit = A[0]; // Preserve the sign bit


for (int i = n - 1; i > 0; i--) {
result[i] = A[i - 1]; // Shift right like logical shift
}
result[0] = signBit; // Copy the sign bit to the first position
}

// Function to perform rotate left (ROL)


void rotateLeft(int A[], int result[], int n) {
int firstBit = A[0]; // Save the first bit
for (int i = 0; i < n - 1; i++) {
result[i] = A[i + 1]; // Shift left
}

result[n - 1] = firstBit; // Set the first bit to the last position


}
// Function to perform rotate right (ROR)
void rotateRight(int A[], int result[], int n) {
int lastBit = A[n - 1]; // Save the last bit
for (int i = n - 1; i > 0; i--) {
result[i] = A[i - 1]; // Shift right
}
result[0] = lastBit; // Set the last bit to the first position

}
// Function to print a 4-bit binary number
void printBinary(int bin[], int n) {
for (int i = 0; i < n; i++) {
printf("%d", bin[i]);
}

printf("\n");
}
int main() {
int n = 4; // Number of bits for binary numbers
int A[4], B[4], result[4];
// Input two 4-bit binary numbers

printf("Enter first 4-bit binary number (A): ");


for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}

printf("Enter second 4-bit binary number (B): ");

for (int i = 0; i < n; i++) {


scanf("%d", &B[i]);
}
int choice;
printf("\nSelect operation:\n");
printf("1. AND\n");
printf("2. OR\n");
printf("3. XOR\n");
printf("4. NOT (A)\n");

printf("5. Logical Shift Left (A)\n");


printf("6. Logical Shift Right (A)\n");
printf("7. Arithmetic Shift Right (A)\n");
printf("8. Rotate Left (A)\n");
printf("9. Rotate Right (A)\n");
scanf("%d", &choice);

switch (choice) {
case 1:
logicAND(A, B, result, n);
printf("Result (A AND B): ");
printBinary(result, n);
break;

case 2:
logicOR(A, B, result, n);
printf("Result (A OR B): ");
printBinary(result, n);
break;
case 3:

logicXOR(A, B, result, n);


printf("Result (A XOR B): ");
printBinary(result, n);
break;
case 4:
logicNOT(A, result, n);
printf("Result (NOT A): ");
printBinary(result, n);
break;

case 5:
shiftLeft(A, result, n);
printf("Result (Logical Shift Left A): ");
printBinary(result, n);
break;
case 6:

shiftRight(A, result, n);


printf("Result (Logical Shift Right A): ");
printBinary(result, n);
break;
case 7:
arithmeticShiftRight(A, result, n);

printf("Result (Arithmetic Shift Right A): ");


printBinary(result, n);
break;
case 8:
rotateLeft(A, result, n);
printf("Result (Rotate Left A): ");

printBinary(result, n);
break;
case 9:
rotateRight(A, result, n);
printf("Result (Rotate Right A): ");
printBinary(result, n);
break;
default:
printf("Invalid choice!\n");

break;
}
return 0;
}

OUTPUT
Enter first 4-bit binary number (A): 1 0 1 0

Enter second 4-bit binary number (B): 0 1 1 0


Select operation:
1. AND
2. OR
3. XOR
4. NOT (A)

5. Logical Shift Left (A)


6. Logical Shift Right (A)
7. Arithmetic Shift Right (A)
8. Rotate Left (A)
9. Rotate Right (A)
1

Result (A AND B): 0010

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


Booth’s Algorithms.
#include <stdio.h>

#include <math.h>
// Function to convert decimal to binary (signed 8-bit representation)
void decimalToBinary(int num, int bin[], int n) {
int i;
for (i = 0; i < n; i++) {
bin[i] = num % 2;

num = num / 2;
}
}
// Function to convert binary to decimal (signed 8-bit representation)
int binaryToDecimal(int bin[], int n) {
int decimal = 0;

for (int i = 0; i < n; i++) {


decimal += bin[i] * pow(2, i);
}
return decimal;
}
// Function to perform binary addition of two binary numbers

void binaryAddition(int A[], int M[], int n) {


int carry = 0;
for (int i = 0; i < n; i++) {
int sum = A[i] + M[i] + carry;
A[i] = sum % 2;
carry = sum / 2;

}
}
// Function to perform binary subtraction (A = A - M)
void binarySubtraction(int A[], int M[], int n) {
int temp[n];
// Compute 2's complement of M
for (int i = 0; i < n; i++) {
temp[i] = (M[i] == 0) ? 1 : 0; // Invert the bits
}

int one[8] = {1, 0, 0, 0, 0, 0, 0, 0}; // To add 1 for two's complement


binaryAddition(temp, one, n); // Add 1 to get 2's complement
binaryAddition(A, temp, n); // Perform binary addition
}
// Function to shift the accumulator (A), multiplier (Q), and Q-1
void arithmeticShiftRight(int A[], int Q[], int *Q_1, int n) {

int lastA = A[n - 1];


*Q_1 = Q[0]; // Shift right the multiplier's LSB to Q-1
for (int i = 0; i < n - 1; i++) {
Q[i] = Q[i + 1];
}
Q[n - 1] = A[0]; // Shift right the last bit of A to the first bit of Q

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


A[i] = A[i + 1];
}
A[n - 1] = lastA; // Keep the sign bit unchanged
}
// Booth's algorithm implementation for signed multiplication

void boothsAlgorithm(int M[], int Q[], int n) {


int A[8] = {0}; // Initialize accumulator A to 0
int Q_1 = 0; // Initialize Q-1 to 0
// Perform n iterations (since we are multiplying two n-bit numbers)
for (int i = 0; i < n; i++) {
// Check last two bits (Q[0] and Q-1) for Booth's condition
if (Q[0] == 1 && Q_1 == 0) {
binarySubtraction(A, M, n); // A = A - M
} else if (Q[0] == 0 && Q_1 == 1) {

binaryAddition(A, M, n); // A = A + M
}
// Perform arithmetic shift right (ASR)
arithmeticShiftRight(A, Q, &Q_1, n);
}
// Print the final result in binary (A and Q combined)

printf("Result in binary: ");


for (int i = n - 1; i >= 0; i--) {
printf("%d", A[i]);
}
for (int i = n - 1; i >= 0; i--) {
printf("%d", Q[i]);

}
printf("\n");
// Combine A and Q for the final result
int result[16];
for (int i = 0; i < n; i++) {
result[i] = Q[i];

}
for (int i = 0; i < n; i++) {
result[i + n] = A[i];
}
// Convert the result from binary to decimal
int decimalResult = binaryToDecimal(result, 2 * n);
printf("Result in decimal: %d\n", decimalResult);
}
int main() {

int M[8], Q[8]; // M = multiplicand, Q = multiplier


int multiplicand, multiplier;
int n = 8; // 8-bit numbers
// Input signed multiplicand and multiplier
printf("Enter multiplicand (signed integer): ");
scanf("%d", &multiplicand);

printf("Enter multiplier (signed integer): ");


scanf("%d", &multiplier);
// Convert multiplicand and multiplier to binary (8-bit signed representation)
decimalToBinary(multiplicand, M, n);
decimalToBinary(multiplier, Q, n);
printf("\nBooth's Algorithm:\n");

boothsAlgorithm(M, Q, n);
return 0;
}
OUTPUT
Enter multiplicand (signed integer): -6
Enter multiplier (signed integer): 3

Booth's Algorithm:
Result in binary: 110111110010
Result in decimal: -18

8.Implement a C program to perform division of two binary numbers (Unsigned) using


restoring division algorithm.
#include <stdio.h>
#include <math.h>
// Function to convert decimal to binary (unsigned n-bit representation)
void decimalToBinary(unsigned int num, int bin[], int n) {
for (int i = 0; i < n; i++) {

bin[i] = (num >> (n - 1 - i)) & 1; // Extract bit by bit


}
}
// Function to convert binary to decimal (unsigned n-bit representation)
unsigned int binaryToDecimal(int bin[], int n) {
unsigned int decimal = 0;

for (int i = 0; i < n; i++) {


decimal += bin[i] * (1 << (n - 1 - i));
}
return decimal;
}
// Function to shift the binary array left by 1 bit

void shiftLeft(int A[], int Q[], int n) {


// Shift A to the left by 1 bit
for (int i = 0; i < n - 1; i++) {
A[i] = A[i + 1];
}
A[n - 1] = Q[0]; // Leftmost bit of Q goes to the last bit of A

// Shift Q to the left by 1 bit


for (int i = 0; i < n - 1; i++) {
Q[i] = Q[i + 1];
}
Q[n - 1] = 0; // Fill LSB of Q with 0
}
// Function to perform binary addition of two binary numbers
void binaryAddition(int A[], int B[], int n) {
int carry = 0;

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


int sum = A[i] + B[i] + carry;
A[i] = sum % 2;
carry = sum / 2;
}
}

// Function to perform binary subtraction (A = A - M)


void binarySubtraction(int A[], int M[], int n) {
int temp[n];
// Compute 2's complement of M
for (int i = 0; i < n; i++) {
temp[i] = (M[i] == 0) ? 1 : 0; // Invert the bits

}
int one[8] = {0, 0, 0, 0, 0, 0, 0, 1}; // To add 1 for two's complement
binaryAddition(temp, one, n); // Add 1 to get 2's complement
binaryAddition(A, temp, n); // Perform binary addition
}
// Restoring Division Algorithm

void restoringDivision(int dividend[], int divisor[], int Q[], int n) {


int A[8] = {0}; // Initialize A to 0
for (int i = 0; i < n; i++) {
// Step 1: Shift left A and Q
shiftLeft(A, Q, n);
// Step 2: A = A - M
binarySubtraction(A, divisor, n);
// Step 3: If A is negative, restore A (A = A + M) and set Q[LSB] = 0
if (A[0] == 1) {

binaryAddition(A, divisor, n); // Restore A (A = A + M)


Q[n - 1] = 0; // Set Q's LSB to 0
} else {
Q[n - 1] = 1; // Set Q's LSB to 1
}
}

// Print the final result


printf("Quotient in binary: ");
for (int i = 0; i < n; i++) {
printf("%d", Q[i]);
}
printf("\n");

printf("Remainder in binary: ");


for (int i = 0; i < n; i++) {
printf("%d", A[i]);
}
printf("\n");
// Convert the quotient and remainder to decimal

unsigned int quotient = binaryToDecimal(Q, n);


unsigned int remainder = binaryToDecimal(A, n);
printf("Quotient in decimal: %u\n", quotient);
printf("Remainder in decimal: %u\n", remainder);
}
int main() {
int dividend[8], divisor[8], Q[8]; // Dividend, divisor, quotient
unsigned int dividend_dec, divisor_dec;

int n = 8; // Number of bits for binary numbers (8-bit unsigned)


// Input unsigned dividend and divisor
printf("Enter dividend (unsigned integer): ");
scanf("%u", &dividend_dec);
printf("Enter divisor (unsigned integer): ");
scanf("%u", &divisor_dec);

// Convert dividend and divisor to binary (8-bit unsigned representation)


decimalToBinary(dividend_dec, dividend, n);
decimalToBinary(divisor_dec, divisor, n);
// Perform restoring division
restoringDivision(dividend, divisor, Q, n);
return 0;

OUTPUT:
Enter dividend (unsigned integer): 25
Enter divisor (unsigned integer): 3
Quotient in binary: 00001000
Remainder in binary: 00000001

Quotient in decimal: 8
Remainder in decimal: 1

9.Implement a C program to perform division of two binary numbers (Unsigned) using


nonrestoring division algorithm.
#include <stdio.h>
#include <math.h>
// Function to convert decimal to binary (unsigned n-bit representation)
void decimalToBinary(unsigned int num, int bin[], int n) {
for (int i = 0; i < n; i++) {

bin[i] = (num >> (n - 1 - i)) & 1; // Extract bit by bit


}
}
// Function to convert binary to decimal (unsigned n-bit representation)
unsigned int binaryToDecimal(int bin[], int n) {
unsigned int decimal = 0;

for (int i = 0; i < n; i++) {


decimal += bin[i] * (1 << (n - 1 - i));
}
return decimal;
}
// Function to shift the binary array left by 1 bit

void shiftLeft(int A[], int Q[], int n) {


// Shift A to the left by 1 bit
for (int i = 0; i < n - 1; i++) {
A[i] = A[i + 1];
}
A[n - 1] = Q[0]; // Leftmost bit of Q goes to the last bit of A

// Shift Q to the left by 1 bit


for (int i = 0; i < n - 1; i++) {
Q[i] = Q[i + 1];
}
Q[n - 1] = 0; // Fill LSB of Q with 0
}

// Function to perform binary addition of two binary numbers


void binaryAddition(int A[], int B[], int n) {

int carry = 0;
for (int i = n - 1; i >= 0; i--) {
int sum = A[i] + B[i] + carry;
A[i] = sum % 2;
carry = sum / 2;
}

}
// Function to perform binary subtraction (A = A - M)
void binarySubtraction(int A[], int M[], int n) {
int temp[n];
// Compute 2's complement of M
for (int i = 0; i < n; i++) {

temp[i] = (M[i] == 0) ? 1 : 0; // Invert the bits


}
int one[8] = {0, 0, 0, 0, 0, 0, 0, 1}; // To add 1 for two's complement

binaryAddition(temp, one, n); // Add 1 to get 2's complement


binaryAddition(A, temp, n); // Perform binary addition

}
// Non-Restoring Division Algorithm
void nonRestoringDivision(int dividend[], int divisor[], int Q[], int n) {
int A[8] = {0}; // Initialize A to 0
for (int i = 0; i < n; i++) {
// Step 1: Shift left A and Q
shiftLeft(A, Q, n);

// Step 2: A = A - M (if A is positive or zero)

if (A[0] == 0) {
binarySubtraction(A, divisor, n);
} else {
// Step 3: A = A + M (if A is negative)
binaryAddition(A, divisor, n);
}

// Step 4: Set Q[LSB]


if (A[0] == 0) {
Q[n - 1] = 1; // If A is positive or zero, set Q's LSB to 1
} else {
Q[n - 1] = 0; // If A is negative, set Q's LSB to 0
}

}
// Final correction if A is negative, add divisor back to A
if (A[0] == 1) {
binaryAddition(A, divisor, n);
}
// Print the final result

printf("Quotient in binary: ");


for (int i = 0; i < n; i++) {
printf("%d", Q[i]);
}
printf("\n");
printf("Remainder in binary: ");
for (int i = 0; i < n; i++) {
printf("%d", A[i]);
}

printf("\n");
// Convert the quotient and remainder to decimal
unsigned int quotient = binaryToDecimal(Q, n);
unsigned int remainder = binaryToDecimal(A, n);
printf("Quotient in decimal: %u\n", quotient);
printf("Remainder in decimal: %u\n", remainder);

}
int main() {
int dividend[8], divisor[8], Q[8]; // Dividend, divisor, quotient
unsigned int dividend_dec, divisor_dec;
int n = 8; // Number of bits for binary numbers (8-bit unsigned)
// Input unsigned dividend and divisor

printf("Enter dividend (unsigned integer): ");


scanf("%u", &dividend_dec);
printf("Enter divisor (unsigned integer): ");
scanf("%u", &divisor_dec);
// Convert dividend and divisor to binary (8-bit unsigned representation)
decimalToBinary(dividend_dec, dividend, n);

decimalToBinary(divisor_dec, divisor, n);


// Perform non-restoring division
nonRestoringDivision(dividend, divisor, Q, n);
return 0;
}
OUTPUT
Enter dividend (unsigned integer): 25
Enter divisor (unsigned integer): 3

Quotient in binary: 00001000


Remainder in binary: 00000001
Quotient in decimal: 8
Remainder in decimal: 1

10.Write assembly language code for A+B*(C-D) using various instruction formats in
MASMor any open-source assembler
.model small
.stack 100h
.data
A dw 10 ; Define A = 10

B dw 5 ; Define B = 5
C dw 8 ; Define C = 8
D dw 3 ; Define D = 3
result dw 0 ; To store the final result
.code
main proc

; Set up the data segment


mov ax, @data
mov ds, ax
; Load the values of C and D into registers
mov ax, C ; AX = C
sub ax, D ; AX = C - D

; Multiply B with (C - D)
mov bx, B ; BX = B
imul bx ; AX = B * (C - D), result in AX
; Add A to the result of the multiplication
add ax, A ; AX = A + B * (C - D)

; Store the result in memory


mov result, ax
; Exit the program
mov ah, 4Ch
int 21h
main endp

end main

output
A = 10, B = 5, C = 8, D = 3
A + B * (C - D) = 10 + 5 * (8 - 3) = 10 + 5 * 5 = 35
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 10 ; Define A = 10

B dw 5 ; Define B = 5
C dw 8 ; Define C = 8
result dw 0 ; To store the final result
.code
main proc
; Set up the data segment

mov ax, @data


mov ds, ax
; Load the values into registers using various addressing modes
; Using direct addressing mode to load A into AX
mov ax, A ; AX = A

; Using direct addressing mode to load B into BX


mov bx, B ; BX = B
; Using direct addressing mode to load C into CX
mov cx, C ; CX = C
; Perform multiplication (B * C)
imul bx, cx ; BX = B * C (result in BX)

; Add A to the result (A + B * C)


add ax, bx ; AX = A + B * C
; Store the result in memory
mov result, ax
; Exit the program
mov ah, 4Ch

int 21h
main endp
end main
output
A = 10, B = 5, C = 8
A + B * C = 10 + 5 * 8 = 10 + 40 = 50

You might also like