In: Computer Science
In a language of your own choosing, write a computer program that implements the Restoring Twos Complement Division process. In addition to program source code, provide a screenshot showing the tasks below. Use 8 bits for all registers.
Program in C
#include<stdio.h>
#include<stdlib.h>
void division(char x, char y)
{
register char A, M, Q;
unsigned char t, s, r;
int i;
Q = x;
M = y;
A = 0;
//display initial values in A, Q and M
printf("Initially: A = %d Q = %d M = %d\n", A, Q, M);
Q = abs(x);
M = abs(y);
r = sizeof(char)*8;
t = 1 << (r-1);
for(i=0; i < r; i++)
{
//shift left AQ
A = A <<1;
s = Q & t;
s = s >> (r-1);
Q = Q << 1;
A = A | s;
//A = A -M
A = A - M;
//MSB of A
s = A & t;
s = s >> (r-1);
//check MSB
if(s==0)
//Q[0] = 1
Q = Q | 1;
else
{
//Q[0] = 0
Q = Q | 0;
//restore A
A = A + M;
}
}
if(x<0) A = ~A + 1;
if(x<0 && y>0 || x>0 && y<0) Q = ~Q + 1;
//display Quotient and Remainder
printf("Quotient = %d\t", Q);
printf("Remainder = %d\n", A);
printf("%dR%d\n", Q, A);
}
int main()
{
int a, b;
//a. divide two positive numbers with no remainder
printf("divide two positive numbers with no remainder\n");
a = 8; b = 4;
division(a, b);
//b. divide two positive numbers with a remainder
printf("divide two positive numbers with a remainder\n");
a = 8; b = 3;
division(a, b);
//c. divide two negative numbers with no remainder
printf("divide two negative numbers with no remainder\n");
a = -8; b = -4;
division(a, b);
//d. divide two negative numbers with a remainder
printf("divide two negative numbers with a remainder\n");
a = -8; b = -3;
division(a, b);
//e. divide a positive number by a negative number with a
remainder
printf("divide a positive number by a negative number with a
remainder\n");
a = 8; b = -3;
division(a, b);
//f. divide a positive number by a negative number without a
remainder
printf("divide a positive number by a negative number with a
remainder\n");
a = 8; b = -4;
division(a, b);
//g. divide a negative number by a positive number with a
remainder
printf("divide a negative number by a positive number with a
remainder\n");
a = -8; b = 3;
division(a, b);
//h. divide a negative number by a positive number without a
remainder
printf("divide a negative number by a positive number without a
remainder\n");
a = -8; b = 4;
division(a, b);
}
Output: