-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.c
166 lines (149 loc) · 2.25 KB
/
code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/** BOOTH'S ALGORITHM FOR BINARY MULTIPLICATION 8 BIT **/
#include<stdio.h>
#include <math.h>
/* Declaring Functions */
int booth[16]={0},M[8]={0},M_[8]={0},q=0;
void multiplicand(int);
void multiplier(int);
void add(int m[8]);
void arithmeticShiftRight();
void neg();
int dec();
/* Main Function */
int main(){
int a,b,n=8,i,sum=0;
printf("Enter the Multiplicand (less than 128 and greater then -129): ");
scanf("%d",&a);
printf("Enter the Multiplier: ");
scanf("%d",&b);
multiplicand(a);
multiplier(b);
neg();
while(n>0){
if(booth[15]==0&&q==1){
add(M); //AC=AC+M
}else if(booth[15]==1&&q==0){
add(M_); //AC=AC-M
}
arithmeticShiftRight();
n--;
}
printf("The output in binary is: ");
for(i=0;i<16;i++){
printf("%d",booth[i]);
}
printf("\nThe output in decimal is: ");
printf("%d",dec());
return 0;
}
/* MAIN ENDS */
/*FUNCTIONS BEGIN HERE */
/* Converts multiplier to binary and adds in booth array as Q*/
void multiplier(int a){
if(a>=0){
int i=15;
while(a>0){
int rem=a%2;
a=a/2;
booth[i]=rem;
i--;
}
}else{
a=a+128;
int i=15;
while(a>0){
int rem=a%2;
a=a/2;
booth[i]=rem;
i--;
}
booth[8]=1;
}
}
/*Converts multiplicand to binary*/
void multiplicand(int a){
if(a>=0){
int i=7;
while(a>0){
int rem=a%2;
a=a/2;
M[i]=rem;
i--;
}
}else{
a=a+128;
int i=7;
while(a>0){
int rem=a%2;
a=a/2;
M[i]=rem;
i--;
}
M[0]=1;
}
}
/*ADDS M or -M to AC*/
void add(int m[8]){
int sum=0,carry=0,i;
for(i=7;i>=0;i--){
sum=m[i]+booth[i]+carry;
if(sum==2){
sum=0;
carry=1;
}else if(sum==3){
sum=1;
carry=1;
}else{
carry=0;
}
booth[i]=sum;
}
}
/* neg() creates -M from M */
void neg(){
int i,carry=0,sum=0;
for(i=0;i<8;i++){
if(M[i]==0){
M_[i]=1;
}else{
M_[i]=0;
}
}
sum=1+M_[7];
if(sum==2){
carry=1;
sum=0;
}
M_[7]=sum;
for(i=6;i>=0;i--){
sum=M_[i]+carry;
if(sum==2){
carry=1;
sum=0;
}else{
carry=0;
}
M_[i]=sum;
}
}
void arithmeticShiftRight(){
int i=0;
q=booth[15];
for(i=15;i>0;i--){
booth[i]=booth[i-1];
}
}
int dec()
{
int result=0,i=15,b=0;
while(i>0&&b<15)
{
result+=(booth[i]*pow(2,b));
b++,i--;
}
if(booth[0]==1)
return (-32768+result);
else
return result;
}
/*FUNCTIONS END*/