/*******************************************************/
       /*           BOOTH'S MULTIPLICATION                    */
       /*   Source code written by: A. Yavuz Oruç             */
       /*           E-mail: yavuz@eng.umd.edu                 */                
       /*           Last revised:  October 1998               */
       /*  Input:  16-bit multiplier, and 16-bit multiplicand */  
       /*  Output: 32-bit product                             */      
       /*                                                     */
       /*******************************************************/


// AYO: This program computes a 32-bit  product (p,r) upon receiving 
// two 16-bit operands p and r, expressed in 2's complement notation.
// It is based on Booth's multiplication algorithm. Both operands
// are entered using a 4-digit hex notation.

#include <iostream.h>
#include <stdio.h> 
#include <limits.h> 

void main(void)
{ int  p,r,i,temp;
  Boolean rbit,ebit;
  while(true)
  { 
 //AYO: Here read in the multiplier and multiplicand in hex.
  printf("\nEnter multiplier: ");
  scanf("%04X", &r);
  printf("Enter multiplicand: ");
  scanf("%04X", &p);
  printf("\nFor multiplier, you entered: "); 
  printf("%04X",r); 
  printf("\nFor multiplicand, you entered: "); 
  printf("%04X",p);
 //AYO: Here initialize the partial product to 0, multiplicand to p, and extension bit to 0.
  temp =  p;  rbit = 0; p = 0; 
 //AYO: Here enter the multiplication loop. Either add (if r[0],rbit = 01) or subtract 
 //(if r[0],rbit = 10) multiplicand to the partial product, p, and shift (p,r) right, 
 //or just shift (p,r) right (if r[0],rbit = 00 or 11).
 for(i = 1; i <= 16; i++)
  { 
  if (((r & 1) == 0)&& (rbit == 1))  p = p + temp; //AYO:add
  if (((r & 1) == 1) && (rbit == 0))  p = p + ~temp + 1; //AYO:subtract
  ebit = rbit; rbit = r & 1; r = r >> 1;  r =  (r & 0x7FFF) | (p << 15);
  p = p >> 1; 
   if(temp != 0x8000) p =  p | ( (p & 0x4000) << 1); 
  else if ((rbit == 1) && (ebit == 0))  p =  p &  0x7FFF; //AYO:handle most -ve multiplicand

  }
  printf("\nThe product is: "); 
  printf("%04X",p);  printf("%04X",r);
  printf("\n"); 
 //AYO: The final product is stored in the 32-bit vector (p,r). 
 }
} 



Return to my home page

Return to software page