/* C Program For Implementation Of Signed Multiplication
OR
C Program for the Implementation Of Booths Algorithm */
#include< stdio.h>
#include< conio.h>
#include< process.h>
#include< math.h>
int get(int a)
{
char ch='B';
int flag=0;
if(a==1)
ch='A';
do
{
printf("│ ENTER VALUE OF %c: ",ch);
scanf("%d",&a);
if(a< 0)
{
a = a * -1;
flag = 1;
}
if(9< =a)
printf("│\n\t!INVALID NUMBER.ENTER VALUE (-9 < A < 9)!");
}while(9< =a);
if(flag)
a = a *-1;
return(a);
}
void add(int *a,int *b)
{
int x,i,c=0;
for(i=3;i>=0;i--)
{
x=a[i];
a[i]=c^x^b[i];
if(((c==1)&&(x==1))||((x==1)&&(b[i]==1))||((b[i]==1)&&(c==1)))
c = 1;
else
c = 0;
}
}
void binary(int x,int*arr)
{
int i,p=x,c[4]={0,0,0,1};
for(i=0;i< 4;i++)
arr[i] = 0;
if(x < 0)
x = x *-1;
i = 3;
do
{
arr[i]=x%2;
x = x/2;
i--;
}while(x!=0);
if(p< 0)
{
for(i=0;i< 4;i++)
arr[i]=1-arr[i];
add(arr,c);
}
printf("\n\nTHE BINARY EQUIVALENT OF %d IS : ",p);
for(i=0;i< 4;i++)
printf("%d",arr[i]);
}
void rshift(int x,int *y)
{
int i;
for(i=3;i>0;i--)
y[i] = y[i-1];
y[0] = x;
}
void main()
{
int q=0,i,j,a,b,A[4]={0,0,0,0},C[4]={0,0,0,1},C1[8]={0,0,0,0,0,0,0,1};
int s=0,z=0,Q[4],M[4],temp,temp1[4],ans[8],y,x=0,c=0;
clrscr();
printf("\n│----------------------------------------------------\n");
a = get(1);
b=get(0);
printf("\n│---------------------------------------------------\n");
binary(a,M);
binary(b,Q);
printf("\n\n---------------------------------------------------\n");
printf(" OPERATION\t\t A\t Q\tQ'\t M");
printf("\n\n INITIAL\t\t");
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
for(j=0;j< 4;j++)
{
if((Q[3]==0)&&(q==1))
{
printf("\n A:=A+M \t\t");
add(A,M);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
if((Q[3]==1)&&(q==0))
{
printf("\n A:=A-M \t\t");
for(i=0;i< 4;i++)
temp1[i] = 1-M[i];
add(temp1,C);
add(A,temp1);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n Shift \t\t\t");
y = A[3];
q = Q[3];
rshift(A[0],A);
rshift(y,Q);
for(i=0;i< 4;i++)
printf("%d",A[i]);
printf("\t");
for(i=0;i< 4;i++)
printf("%d",Q[i]);
printf("\t");
printf("%d\t",q);
for(i=0;i< 4;i++)
printf("%d",M[i]);
}
printf("\n\n---------------------------------------------------\n");
printf("\nTHE ANSWER IN BINARY IS : ");
for(i=0;i< 4;i++)
ans[i]=A[i];
for(i=0;i< 4;i++)
ans[i+4]=Q[i];
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
{
for(i=0;i< 8;i++)
ans[i]=1-ans[i];
for(i=7;i>=0;i--)
{
x = ans[i];
ans[i]=c^x^C1[i];
if(((c==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(c==1)))
c=1;
else
c=0;
}
}
for(i=0;i< 8;i++)
printf("%d",ans[i]);
for(i=7;i>=0;i--)
{
s = s + (pow(2,z) * ans[i]);
z = z+1;
}
if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))
printf("\nTHE ANSWER IN DECIMAL IS : -%d\n",s);
else
printf("\nTHE ANSWER IN DECIMAL IS : %d\n",s);
getch();
}
/********************** OUTPUT **********************
│-------------------------------------------------------------
│ ENTER VALUE OF A: 7
│ ENTER VALUE OF B: -3
│-------------------------------------------------------------
THE BINARY EQUIVALENT OF 7 IS : 0111
THE BINARY EQUIVALENT OF -3 IS : 1101
---------------------------------------------------
OPERATION A Q Q' M
INITIAL 0000 1101 0 0111
A:=A-M 1001 1101 0 0111
Shift 1100 1110 1 0111
A:=A+M 0011 1110 1 0111
Shift 0001 1111 0 0111
A:=A-M 1010 1111 0 0111
Shift 1101 0111 1 0111
Shift 1110 1011 1 0111
---------------------------------------------------
THE ANSWER IN BINARY IS : 00010101
THE ANSWER IN DECIMAL IS : -21
Subscribe to:
Post Comments (Atom)
18 comments:
Very well developed
very well organized code...
lovely bro..thx a lot
A nice and successfull attempt..!
very nice but do this algorithm for 16bit
Hi
thanks:)
it's excellent !
Have a good time my dear friend.
Hi
thanks:)
it's excellent !
Have a good time my dear friend.
excellent ...but can u also post it in java??
Doing a great job...
Keep going.
Great job doing programs...
I also like it..
Keep going...
simple program for restoring division------------------> http://rapidshare.com/files/391477277/restoring_division.txt
simple program for non-restoring division------------------>
http://rapidshare.com/files/391479232/non_restoring_division.txt
u r a awesome bro...can u make the code about booth algorithm for addition & substraction.. hope for it..
Hi, great work! can you provide the code for booth in assembly for 8051? it would be great help :)
Thanks
Nikhil
hi, great job. can you provide the code in 8051 assembly also?
thanks
nikhil
Super macha......(^_~)
Super macha...(~_^)
Really Helped me a lot...Thumbs up
Man see you output. Id two signed bit numbers are multiplied the result must b the signed-representation. So the signed representationof :21 is wrong.
One.possiblity is that u just copied this program from the dumps available on the internet...
Try to upload your own cody...
Post a Comment