# Booth's algorithm using RT l

mdaalam22
c_cpp
2 years ago
3.9 kB
37
Indexable
Never
```#include<stdio.h>
#include<conio.h>
#include<math.h>

int main(){
int decimal1,decimal2,i,z=0,n=0,j,temp=0,carry,decimalEqv=0,y_temp=0,x_temp=0,tempValue=0,y_minus=0;
int x[20],y[20],c=0,u[20]={0},v[20]={0}, y1=0;
printf("\t\tEnter multiplier and multiplicand numbers:");
scanf("%d%d",&decimal1,&decimal2);
//change if ngative to positive and flag the number
if(decimal1<0){
y_temp=1;
decimal1=decimal1*(-1);
}
if(decimal2<0){
x_temp=1;
decimal2=decimal2*(-1);
}
while(decimal1>0 || decimal2>0){
y[n]=decimal1%2;
decimal1/=2;

x[n]=decimal2%2;
decimal2/=2;

n++;

}
printf("\n\t----------------------------------------------------------------------------------------------------\n");
printf("\t condition \t micro-operation \t\t\t i \t\t  U \t V \t Y \t Y1 \n");
printf("\n\t----------------------------------------------------------------------------------------------------\n");

printf("\t 1 \t\t U<-0,y1<-0 ,i<%d\t\t",n);
printf("\t%d \t\t\ ",n);

for(j=1;j>=0;j--){
printf("%d",u[j]);
}
printf("\t");
for(j=1;j>=0;j--){
printf("%d",v[j]);
}
printf("\t");
for(j=1;j>=0;j--){
printf("%d",y[j]);
}
printf("\t");
printf("%d \t", y1);
printf("\n");
printf("\t ---------------------------------------------------------------------------------------------------");
printf("\n\n");

y[n]=0;
x[n]=0;
i=n;
//converting to 2's complement if necessary
if(y_temp){
carry=1;
for(j=0;j<=n;j++){
temp=!y[j]+carry;
y[j]=temp%2;
carry=temp/2;
}
}
if(x_temp){
carry=1;
for(j=0;j<=n;j++){
temp=!x[j]+carry;
x[j]=temp%2;
carry=temp/2;
}
}

do{
tempValue=y[0]+y_minus;
if(tempValue==1){

if(y_minus==1){
printf("\n\t Yo'Y12 \t U<-U+X \n");
carry=0;
for(j=0;j<=n;j++){
temp=u[j]+x[j]+carry;
u[j]=temp%2;
carry=temp/2;
}
}else{
printf("\n\t YoY1'2 \t U<-U+X'+1 \n");
carry=1;
for(j=0;j<=n;j++){
temp=u[j]+!x[j]+carry;
u[j]=temp%2;
carry=temp/2;
}
}
}

i--;
printf("\n\t 2 \t\t i<-i-1 \t\t\t\t");
//output formating
if(i<0)
printf("0 \t\t");
else
printf("%d \t\t",i);

for(j=n;j>=0;j--){
printf("%d",u[j]);
}
printf("\t");
for(j=n;j>=0;j--){
printf("%d",v[j]);
}
printf("\t");
for(j=n;j>=0;j--){
printf("%d",y[j]);
}
printf("\t%d \t",y1);
printf("\n\n");
if(i<0)
z=1;
for(j=0;j<n;j++){
v[j]=v[j+1];
}
v[j]=u[0];
for(j=0;j<n;j++){
u[j]=u[j+1];
}
//cir y
y_minus=y[0];
for(j=0;j<n;j++){
y[j]=y[j+1];
}
y[j]=y_minus;
printf("\t 3 \t\t ashr(UV),cir(Y),y1<-yo \t\t");
if(i<0)
printf("0 \t\t");
else
printf("%d \t\t",i);

for(j=n;j>=0;j--){
printf("%d",u[j]);
}
printf("\t");
for(j=n;j>=0;j--){
printf("%d",v[j]);
}
printf("\t");
for(j=n;j>=0;j--){
printf("%d",y[j]);
}
y1 = y[n];
printf("\t%d \t",y1);
printf("\n\n");
if(i>=0)
printf("\t Z'3 \t\t GOTO 2 \n\n");
else
printf("\t Z3 \t\t Finish<-1 \n\n");
printf("\t ---------------------------------------------------------------------------------------------------");
}while(z==0);

printf("\n\tResult in UV = ");
if(u[n]){
printf("%d",u[n]);
decimalEqv-=u[n]*pow(2,2*n+1);
}
for(j=n-1;j>=0;j--){
printf("%d",u[j]);
decimalEqv+=u[j]*pow(2,j+n+1);
}
printf("\t");
for(j=n;j>=0;j--){
printf("%d",v[j]);
decimalEqv+=v[j]*pow(2,j);
}
printf("\n\tThe decimal equivalent of the result = %d",decimalEqv);
}```