349,210 views
38 votes
38 votes
1. For this assignment you will print the steps for 8-bit by 8-bit multiplication.

2. Your program should read in two unsigned numbers from stdin, the first being the multiplicand and the second being the multiplier. Your program should verify that both values are within the range 0 to 255. If not, the program should print an appropriate error message and exit.
3. Your program should then initialize the simulated registers and echo the input values in decimal and also in binary.
4. Then your program should illustrate the eight steps required for multiplication using the same type of step diagrams as given in the lecture notes.
5. Finally, your program should print a check section that shows a summary of the multiplication in binary as well as in decimal. See the example below.
6. You may write your program in C, C++, or Java. If you choose to work in C, here is an example function that will allow you to represent an n-bit value within an int data type and print out the binary value in "length" bits. It uses the shift right operator and the bitwise and. (You can also choose to use unsigned int as the data type; since you are using only the lower 8 of the 32 bits in an int, there will be no apparent difference between using int and using unsigned int. However, if you choose to use char as the data type, you should use unsigned char to ensure that the compiler generates logical rather than arithmetic shifts.)
void prt_bin( int value, int length ){
int i;
for( i=(length-1); i>=0; i--){
if((value>>i)&1)
putchar('1');
else
putchar('0');
}
}
For example, if you declare acc as an int, then you could call prt_bin(acc,8) to print the 8-bit value in the accumulator.
You should format your output to exactly match the output below. 10% of the grade will be awarded for following the same format.
Sample Run is given below:
multiplicand: 33
multiplier: 55
c and acc set to 0
mq set to multiplier = 55 decimal and 00110111 binary
mdr set to multiplicand = 33 decimal and 00100001 binary
---------------------------------------------------
step 1: 0 00000000 00110111
+ 00100001 ^ add based on lsb=1
----------
0 00100001 00110111
>> shift right
0 00010000 10011011
---------------------------------------------------
step 2: 0 00010000 10011011
+ 00100001 ^ add based on lsb=1
----------
0 00110001 10011011
>> shift right
0 00011000 11001101
---------------------------------------------------
step 3: 0 00011000 11001101
+ 00100001 ^ add based on lsb=1
----------
0 00111001 11001101
>> shift right
0 00011100 11100110
---------------------------------------------------
step 4: 0 00011100 11100110
+ 00000000 ^ no add based on lsb=0
----------
0 00011100 11100110
>> shift right
0 00001110 01110011
---------------------------------------------------
step 5: 0 00001110 01110011
+ 00100001 ^ add based on lsb=1
----------
0 00101111 01110011
>> shift right
0 00010111 10111001
---------------------------------------------------
step 6: 0 00010111 10111001
+ 00100001 ^ add based on lsb=1
----------
0 00111000 10111001
>> shift right
0 00011100 01011100
---------------------------------------------------
step 7: 0 00011100 01011100
+ 00000000 ^ no add based on lsb=0
----------
0 00011100 01011100
>> shift right
0 00001110 00101110
---------------------------------------------------
step 8: 0 00001110 00101110
+ 00000000 ^ no add based on lsb=0
----------
0 00001110 00101110
>> shift right
0 00000111 00010111
---------------------------------------------------
check: binary decimal
00100001 33
x 00110111 x 55
---------------- ------
0000011100010111 1815

User DooBLER
by
3.0k points

1 Answer

13 votes
13 votes

Answer:

Code:

#include<bits/stdc++.h>

using namespace std;

char* prt_bin( int value, int length, char s[]){

for(int i=(length-1); i>=0; i--){

if((value>>i)&1) {

s[length-1-i] = '1';

// putchar('1');

}

else {

s[length-1-i] = '0';

// putchar('0');

}

}

s[length] = '\0';

return s;

}

int main(){

int nbits = 8, c, acc;

char s[nbits+1];

int multiplicand, multiplier, mq, mdr;

cout<<"Multiplicand: ";

cin>>multiplicand;

cout<<"Multiplier: ";

cin>>multiplier;

if(multiplier<0 || multiplier>255 ||multiplicand<0 || multiplicand>255){

cout<<"Given Multiplicand or multiplier is incorrect. Exiting the program.\\";

return 0;

}

c = 0; acc = 0;

cout<<"c and acc set to 0\\";

mq = multiplier;

mdr = multiplicand;

cout<<"mq set to multiplier = "<<multiplier<<" decimal and "<<prt_bin(multiplier, nbits, s)<<" binary\\";

cout<<"mdr set to multiplicand = "<<multiplicand<<" decimal and "<<prt_bin(multiplicand, nbits, s)<<" binary\\";

printf("---------------------------------------------------\\");

for(int i=1;i<=nbits;i++){

printf("step %d: %d %s ",i, c, prt_bin(acc, nbits, s));

printf("%s\\",prt_bin(mq, nbits, s));

char lsb = s[nbits-1];

if(lsb=='1'){

printf(" + %c %s ^add based on lsb=%c\\", c, prt_bin(mdr, nbits, s), lsb);

printf(" ----------\\");

acc = acc + multiplicand;

}

else{

printf(" + %c %s ^no add based on lsb=%c\\", c, prt_bin(0, nbits, s), lsb);

printf(" ----------\\");

}

printf(" %d %s ", c, prt_bin(acc, nbits, s));

printf("%s\\",prt_bin(mq, nbits, s));

printf(" >> shift right\\");

mq = mq>>1;

if(acc%2==1){

mq = mq+128;

}

acc = acc>>1;

printf(" %d %s ", c, prt_bin(acc, nbits, s));

printf("%s\\",prt_bin(mq, nbits, s));

printf("---------------------------------------------------\\");

}

char sr[nbits*2+1];

printf("check: binary decimal\\");

printf(" %s %d\\", prt_bin(multiplicand, nbits, s), multiplicand);

printf(" x %s x %d\\", prt_bin(multiplier, nbits, s), multiplier);

printf(" ---------------- ------\\");

printf(" %s %d\\", prt_bin(multiplier*multiplicand, nbits*2, sr), multiplier*multiplicand);

return 0;

}

User Mishap
by
2.6k points