Tuesday, February 14, 2012

Computer Organization - Multiplication Unsigned integer number [Code c++]

Unsigned.h

#include <iostream>
#include <math.h>
using namespace std;
/*
 multiple by 2 numbers as in 2's complement
 */

class multiUnsigned {
private:
    /*
     C >> holding protential carry bit
     A >> 0
     Q >> holding multiplicant value
     M >> holding multiplier value
     Bit >> bit number
     */
    unsigned int C,A,Q,M,maxRang,maxBit,Count;
public:

    /*  Constructor : 
        C,A<-0
        M<-Multiplicand
     */
    multiUnsigned(int Bit , int Multiplicand , int multiplier){
        maxRang = pow(2,Bit)-1; // rang highest
        maxBit = pow(2,Bit-1);  // MSB
        Count = Bit;
        M = Multiplicand;
        Q = multiplier;
        C = 0;
        A = 0;
    }
    /* Function printAsBit(int number)
     e.g.print decimal to bianry 2's complement in 8bit  
     range { 0 ~ 255 }
     1   1  1  1  1 1 1 1
     128 64 32 16 8 4 2 1
     */
    void printBitOf(int number){
        for(int i=maxBit; i>0 ; i=i/2){
            if((number & i)>0){
                cout<<"1";
            }else{
                cout<<"0";
            }
        }
    }
    void calculate(){
        /*  Initial Value   */
        // print title C A Q M
        if(Count>7) // set tab space fix to lenght of bit
            cout<<"\tC\tA\t\tQ\t\tM\n";
        else
            cout<<"\tC\tA\tQ\tM\n";
        /*  Print Value of C,A,Q,M as binary in Initial Value   */
        cout<<"init:\t";
        cout<<C<<"\t";
        printBitOf(A);
        cout<<"\t";
        printBitOf(Q);
        cout<<"\t";
        printBitOf(M);
        cout<<"\tInitial Value\n\n";
        /*
         if(Q[1]==1)
            C,A = A + M
         Shift-right C,A,Q
         Count--
         */
        int round = Count;
        while(Count>0){
            if((Q&1)==1){
                A = A + M;
            /* if A bigger than range
                then C = 1 and A = A mod range
             */
                if(A>maxRang){
                    C = 1;
                    A %= (maxRang+1); // A mod MaxRang
                }
        /*  Print Value of C,A,Q,M as binary after C,A<-A+M   */
                cout<<round-Count+1<<"a:\t";
                cout<<C<<"\t";
                printBitOf(A);
                cout<<"\t";
                printBitOf(Q);
                cout<<"\t";
                printBitOf(M);
                cout<<"\tAdd\n";
            }
            // Shift Right Q,A,C 1 bit

            if(A%2==1){
                Q>>=1;          // move bit of A e.g 1101 -> 0110
                Q = Q | maxBit; // change to 1 on MSB of Q 0110 | 1000 -> 1110
            }else{
                Q>>=1;
            }if(C==1){
                A>>=1;            // move bit of A  : e.g. 0011 -> 0001
                A = A | maxBit;//change MSB of A to 1 e.g. 0001|1000 -> 1001
            }else{
                A>>=1;
            }
            C>>=1;      // Shilf-Right C 1 bit
          
        /*  Print Value of C,A,Q,M as binary After Shift-Right 1bit   */
            cout<<round-Count+1<<"s:\t";
            cout<<C<<"\t";
            printBitOf(A);
            cout<<"\t";
            printBitOf(Q);
            cout<<"\t";
            printBitOf(M);
            cout<<"\tShift\tCycle\n\n";
            Count--;
        }
        /*  Print Value of A:Q as Result   */
        cout<<"Answer is = ";
        printBitOf(A);
        printBitOf(Q);
        cout<<"\n\n";
    }
};


Main.cpp

#include "multiUnsigned.h"

int main(){
  
    unsigned int multiplicand;
    unsigned int multiplier;
    int Bit;
    //-------------------------------------------
    cout<<"**** multiplicand * multiplier = ? ****\n";
    cout<<"Number of bit (1 Bit ~ 32 Bit):";
    cin>>Bit;
    while(!(Bit<=32 && Bit>0)){
        cout<<"Number of bit (1 Bit ~ 32 Bit) :";
        cin>>Bit;
    }
    cout<<"**** "<<Bit<<" bit unsigned\t\t****\n";
    cout<<"**** Rang { 0 ~ "<<pow(2,Bit)-1<<" }\t\t****\n";
    cout<<"Input multiplicand(0~"<<pow(2,Bit)-1<<") :";
    cin>>multiplicand;
    while (!(multiplicand>0 && multiplicand<=pow(2,Bit)-1)) {
        cout<<"Input multiplicand(0~"<<pow(2,Bit)-1<<") :";
        cin>>multiplicand;
    }
    cout<<"Input multiplier(0~"<<pow(2,Bit)-1<<")  :";
    cin>>multiplier;
    while (!(multiplier>0 && multiplier<=pow(2,Bit)-1)) {
        cout<<"Input multiplicand(0~"<<pow(2,Bit)-1<<") :";
        cin>>multiplier;
    }
  
/*  Create object and sent Bit , Q , M  */
    multiUnsigned multi(Bit,multiplicand,multiplier);
  
/*  Calculation the multiple    */
    multi.calculate();
  
    return 0;
}


No comments:

Post a Comment