Sunday, February 26, 2012

Computer Organization - signed bit number (2's complement and Magnitude)

>> Algorithm and FlowChart 
multiSigned.h

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

class multiSigned {
private:
    /*
     A >>
     Q1 >>
     Q >> holding multiplicant value
     M >> holding multiplier value
     Bit >> bit number
     */
    int A,Q1,Q,M,maxRang,maxBit,Count,minRang,result;
public:

    /*  Constructor :   */
    multiSigned(int Bit , int multiplicant , int multiplier){
        minRang = -pow(2,Bit-1);   // rang lowest
        maxRang = pow(2,Bit-1)-1; // rang highest
        maxBit = pow(2,Bit-2);  // value of MSB
        Count = Bit;
        M = multiplicant;
        Q = multiplier;
        result = M*Q;
        Q1 = 0;
        A = 0;
    }
    int getResult(){
        return result;
    }
    /* Function printAsBit(int number) 
     print decimal to bianry 2's complement in 8bit   
     range { -128 ~ 127 }
     1   1  1  1  1 1 1 1
     -128 64 32 16 8 4 2 1
     */
    void printBitOf(int number){
        if(number<0)
            cout<<"1";
        else
            cout<<"0";
        for(int i=maxBit; i>0 ; i=i/2){
            if((number & i)>0){
                cout<<"1";
            }else{
                cout<<"0";
            }
        }
    }
    // Print Q only without thinking of signed of integer
    void printQ(int number){
        for(int i=maxBit<<1; i>0 ; i=i/2){
            if((number & i)>0){
                cout<<"1";
            }else{
                cout<<"0";
            }
        }
    }
    void calculate(){
        if(Count<7)
            cout<<"A\tQ\tQ[-1]\tM\n\n";
        else
            cout<<"A\t\tQ\t\tQ[-1]\tM\n\n";
        printBitOf(A);
        cout<<"\t";
        printQ(Q);
        cout<<"\t";
        cout<<Q1;
        cout<<"\t";
        printBitOf(M);
        cout<<"\tInitial Value\n\n";
        do{
            // if 1,0 => A <- A + M
            if((Q&1)==0 && Q1==1){
                A= A + M;
                printBitOf(A);
                cout<<"\t";
                printQ(Q);
                cout<<"\t";
                cout<<Q1;
                cout<<"\t";
                printBitOf(M);
                cout<<"\tA <- A + M\n";
            }
            // if 1,0 => A <- A - M
            else if((Q&1)==1 && Q1==0){
                A = A - M;
                printBitOf(A);
                cout<<"\t";
                printQ(Q);
                cout<<"\t";
                cout<<Q1;
                cout<<"\t";
                printBitOf(M);
                cout<<"\tA <- A - M\n";
            }
           
            // Arithmetic Shift-Right A,Q,Q[-1] Count-1
            // Q = xxx1 => Q[-1] = 1
            if((Q&1)==1)
                Q1=1;
            else // Q = xxx0 => Q[-1] = 0
                Q1=0;  
            // Shift-Right Q 1 bit
            Q>>=1;
            // Postcondition : A = xxx1
            if((A&1)==1){
                if(Q>=0)
                    // to set Q = 1xxx
                    Q = abs(Q)|(maxBit*2);  
            }else
                if(Q<0)
                    // to set Q = 0xxx
                    Q = Q & (maxBit*2-1);
            // Shift-Right A 1 bit
            A>>=1;
            // Print A , Q , Q1 , M
            printBitOf(A);
            cout<<"\t";
            printQ(Q);
            cout<<"\t";
            cout<<Q1;
            cout<<"\t";
            printBitOf(M);
            cout<<"\tShift\n\n";
            // decrease count --
            Count--;
        }while(Count != 0);  // count still not Zero ?
        // Print Twice bit of result from A:Q
        cout<<"A:Q :";
        printBitOf(A);
        cout<<" ";
        printQ(Q);
        // Print Result in decimal number
        cout<<" : "<<getResult()<<endl;
    }
};



No comments:

Post a Comment