1 #include <iostream>
2 #include
<windows.h>
3 #include
<fstream>
4 #include
<math.h>
5 #define pause
system("pause");
6 using namespace
std;
7 const int no_of_bits = 32;
8 /* this Program
uses Following notations.... first bit for signed and then it goes like this 1
2 4 8 16 32 64......
9 /*
----------------------------------------global variables
----------------------------------
10 */
11 int Q1=0; /// extra bit
holder
12 int A[no_of_bits]; /// Accumulator
storing array
13 int Q[no_of_bits];/// Multiplier
storing Array.
14 int N,Qsize;/// no of bits
in multiplier
15 int X; /// no of bits
in multiplicand.
16 int M[no_of_bits]; /// M value
17 int M2Complement[no_of_bits];/// -M
18 int temp[no_of_bits]; /// for
temporary use
19 ofstream out;
20
21 /*
22 ...............................................now
function ....................................
23
24 */
25 int binary_convert(int,int m[]);
26 void ARS(); /// arithmetic
Right Shift ...
27
28 void Add(int[],int []); /// used for
Function two's complement... and adding of two array
29 void intialiser(int ,int ); /// used to
initialize A Q N and all
30 a
31
32 /*
33
34
35 Main Function
Starts
36
37
38 */
39 int main ()
40 {int multiplicand,multiplier,result=0;
41 out.open("Booth's
Multiplier.txt",ios::out);
42 out<<"++++++++++++++++++++++++++++++++++++++++Welcome
To booth's Multiplier++++++++++++++++++++++++++++\n";
43 cout<<endl;
44 /// m is the
multiplicand
45 /// is
multiplier....
46 cout<<" Booth's
multiplier
\n\n\n\a\n";
47 cout<<"============================================================================\n";
48 cout<<"Enter the
Multiplicand ---> ";
49 out<<"\n\n\n\n============================================================================\n";
50 out<<"Enter the
Multiplicand ---> ";
51
52 cin>>multiplicand;
53 out<<multiplicand;
54 cout<<"Enter the
Multiplier ---> ";
55 cin>>multiplier;
56 cout<<"Displaying
Result of "<<multiplicand<<"X"<<multiplier<<endl;
57 out<<"\nEnter the
Multiplier ---> "<<multiplier<<endl;;
58 out<<"Displaying
Result of "<<multiplicand<<"X"<<multiplier<<endl;
59
60 out<<"============================================================================\n";
61
62 cout<<"============================================================================\n";
63
64 int opr=0;
65 /*
66 opr code ...
67 opr code =1
shifting
68 opr =2 Subs
69 opr=3 Add...
70 */
71 int counter=0;
72
intialiser(multiplicand,multiplier);
73
74
75
76 cout<<"============================================================================\n\n ::StepS::\n\n";
77 cout<<"============================================================================\n";
78
79 out<<"============================================================================\n\n ::StepS::\n\n";
80 out<<"============================================================================\n";
81
82
83
84 cout<<"Index\t
A\t\tQ\t\tQ1\tN\t operation\n\n";
85 cout<<"0\t";
86 out<<"Index\t
A\t\tQ\t\tQ1\tN\t operation\n\n";
87 out<<"0\t";
88 for(int i=no_of_bits-X;i<no_of_bits;i++){
89 cout<<A[i];out<<A[i];}cout<<"\t\t"; out<<"\t\t";
90 for(int i=no_of_bits-N;i<no_of_bits;i++){
91 cout<<Q[i];out<<Q[i];}
92 cout<<"\t\t"<<Q1<<"\t"<<N<<"\t
------------";; out<<"\t"<<Q1<<"\t"<<N<<"\t -------------";
93
94
95
96
97
98 while(N>0){
99
100
101
102
103 if(Q[no_of_bits-1]==1&&Q1==0) /// it is of form 10
104 {Add(A,M2Complement);opr=2;
105
106 }
107 else if (Q[no_of_bits-1]==0&&Q1==1) /// it is of
form 01
108 {
109 Add(A,M);opr=3;
110 }
111 ARS(); /// shifting at last..
112 cout<<"\n\a____________________________________________________________________________________________________________________\n";
113 cout<<++counter<<"\t";
114 out<<"\n\a____________________________________________________________________________________________________________________\n";
115 out<<++counter<<"\t";
116
117
118 for(int i=no_of_bits-X;i<no_of_bits;i++){out<<A[i];
119 cout<<A[i];} cout<<"\t\t";out<<"\t\t";
120 for(int k=(no_of_bits-Qsize);k<no_of_bits;k++) {cout<<Q[k];out<<Q[k];}
121 cout<<"\t\t"<<Q1<<"\t"<<N<<"\t";out<<"\t\t"<<Q1<<"\t"<<N<<"\t";
122 if(opr==1){cout<<"\tShifting"<<endl;out<<"\tShifting"<<endl;}
123 else if(opr==2){ cout<<"\tSubtracting
and Arithmetic Shifting....\n";out<<"\tSubtracting and Arithmetic
Shifting....\n";}
124 else {cout<<"\tAdding and
Shifting....\n";out<<"\tAdding and Shifting....\n";}
125 out<<"\n\a____________________________________________________________________________________________________________________\n";
126 cout<<"\n\a____________________________________________________________________________________________________________________\n";
127
128 }
129 int power=0;
130 if(A[0]==0) /// it is a +ve result
131 {
132
133 for(int k=no_of_bits-1; k>=(no_of_bits-Qsize);k--)
134
135 {result=result+(pow(2,power++)*Q[k]);
136
137 }
138 for(int k=no_of_bits-1; k>=(no_of_bits-X);k--)
139 {
140 result=result+(pow(2,power++)*A[k]);
141
142 }
143
144
145
146 }
147 else
/// if number is negative
148 {int product[X+Qsize],tempproduct[X+Qsize];
149 int prosize=X+Qsize,sum=0,carry=0;
150 cout<<" Answer is
negative so Taking its Two's Complement..\n";
151
152 for(int i=0;i<X+Qsize;i++) tempproduct[i]=0; temp[(X+Qsize)-1]=1;
153
154 for(int k=0;k<Qsize;k++) product[--prosize]=Q[(no_of_bits-1)-k];
155 for(int k=0;k<X;k++) product[--prosize]=A[(no_of_bits-1)-k];
156
157
158 for(int i=(Qsize+X-1);i>=0;i--)
159 if(product[i]==0)product[i]=1;
160 else product[i]=0;
161 for(int i=(X+Qsize-1);i>=0;i--)
162 {sum=0;
163 sum=carry+product[i]+tempproduct[i];
164 if(sum==3)
165 {
166 sum=1;carry=1;
167 }else if(sum==2)
168 {
169 sum=0;carry=1;
170 }
171 else{
172 carry=0;
173 }
174 product[i]=sum;
175
176 }
177 int power=0;
178 for(int i=(Qsize+X)-1;i>=0;i--)
179 {
180 result=result+(product[i]*pow(2,power++));
181 }
182
183
184 result=(result*-1)-1;
185 }
186
187 cout<<"Result=="<<result<<endl;
188 out<<"Result=="<<result<<endl;
189
190 out.close();
191 cout<<"\n";
192 pause;
193
194
195 }
196 void intialiser(int m ,int q)
197 {
198
199 for(int i=0;i<no_of_bits;i++)
200 {
201 Q[i]=0;
202 A[i]=0;
203 temp[i]=0;
204 M[i]=0;
205 M2Complement[i]=0;
206
207 }
208
209
210
211 X=binary_convert(m,M);
212
213 N=binary_convert(q,Q);
214 Qsize=N;
215 m=m*(-1);
216 X=binary_convert(m,M2Complement);
217
218 out<<"\n==========================================================================\n";
219 out<<"\n M =\t";
220
221 cout<<"\n==========================================================================\n";
222 cout<<"\n M
=\t";
223 for(int i=no_of_bits-X;i<no_of_bits;i++){
224 cout<<M[i];out<<M[i];}
225 cout<<"\n Q
=\t";
226 out<<"\n Q =\t";
227
228 for(int i=no_of_bits-N;i<no_of_bits;i++){
229 cout<<Q[i];out<<Q[i];}
230 cout<<"\n A
=\t";
231 out<<"\n A =\t";
232
233 for(int i=no_of_bits-X;i<no_of_bits;i++){
234 cout<<A[i];out<<A[i];}
235 cout<<"\n-M
=\t";
236 out<<"\n-M =\t";
237
238 for(int i=no_of_bits-X;i<no_of_bits;i++)
239 {
240 cout<<M2Complement[i];out<<M2Complement[i];}cout<<"\n==========================================================================\n";
241
242 out<<"\n==========================================================================\n";
243
244 }
245
246
247
248
249
250
251
252 void ARS()
253 {
254
255 Q1=Q[no_of_bits-1];
256 for(int i=no_of_bits-1;i>0;i--)
257 {
258 Q[i]=Q[i-1];
259 }
260 for(int i=0;i<=(no_of_bits-Qsize);i++)
261 Q[i]=A[no_of_bits-1];
262 for(int i=no_of_bits-1;i>0;i--)
263 {
264 A[i]=A[i-1];
265 }
266
267
268
269 N--; /// decreasing N value.
270 }
271
272 int binary_convert(int num,int m[])
273 {bool ispositive=false;int counter=(no_of_bits-1),bitsize=0; /// counter
will be used to set current bit value..
274
275 if(num>0)
276 {
277 ispositive=true;
278 }
279
280 if(ispositive==true) m[0]=0; /// setting
signed bit ;;
281 else
282
283 {
284 m[0]=1;
285 num=num*(-1);
286 }
287 while(num>0)
288 {bitsize++;
289 if(num%2==0) m[counter]=0;
290 else m[counter]=1; /// reminder
ones....
291
292 num=num/2;
293
294 counter--;
295
296 }
297 if(ispositive==false) /// i.e number
is negative
298
299 {
300 for(int counter=1;counter<no_of_bits;counter++)
301
302 {
303 if(m[counter]==0) m[counter]=1;
304 else m[counter]=0;
305
306 } /// taking One's Complement...
307
308 for(int i=0;i<no_of_bits;i++)
309
310 {if(i==(no_of_bits-1)) temp[i]=1;
311 else temp[i]=0;
312
313
314 } /// adding one...
315 Add(m,temp);
316
317 }
318
319 return (bitsize+1);
320 }
321 void Add(int FirstNumber[],int SecondNumber[] )
322 {int carry=0,sum=0;
323
324 for(int i=(no_of_bits-1);i>=0;i--)
325 {sum=0;
326 sum=FirstNumber[i]+SecondNumber[i]+carry;
327
328 if(sum==3)
329 {
330 sum=1;carry=1;
331 }else if(sum==2)
332 {
333
334 sum=0; carry=1;
335 }
336 else
337 {
338 carry=0;
339 }
340
341 FirstNumber[i]=sum;
342 }
343 }
get the source code
get the executable.
.EXE
https://drive.google.com/file/d/0BwyIMs3NcEzYSTk1Y3lGOXM0LU0/view?usp=sharing
.EXE
https://drive.google.com/file/d/0BwyIMs3NcEzYSTk1Y3lGOXM0LU0/view?usp=sharing
Comments
Post a Comment
share your thoughts ....