Sei sulla pagina 1di 13

Signed Magnitude Multiplication

A PROJECT REPORT

Submitted By

SESHADRI.A (10MCA0001) ABINAYA.E (10MCA0004) ADITI KOCHHAR (`10MCA0005) AISHWARYA.V (10MCA0006) ANANDAN.E (10MCA0008)

In partial fulfillment for the award of the degree of

MASTER OF COMPUTER APPLICATION

School Of Information Technology & Engineering April 2010

Sign-magnitude representation The sign-magnitude method is the simplest way of handling negative numbers - at least from a human the point view. The idea is to set aside one bit to store the sign of the integer (either + or -) and use the remaining N-1 bits to store the magnitude of the number. With 12 bits, this method allows representing integers from -(211-1) to +(211-1), that is, from -2047 to +2047. By convention, the leftmost bit is the sign bit, and a sign bit of 1 signifies a negative number. So in a 12-bit sign-magnitude system, 111111111111 represents -2047 100000000001 represents -1 100000000000 represents 0 (so-called negative 0) 000000000000 represents 0 (so-called positive 0) 000000000001 represents +1 011111111111 represents +2047 Note that there are two different representations for the number zero! These representations are called positive zero and negative zero, but it is important to remember that they represent the same number (they are mathematically equal) and that in mathematics, zero is neither positive nor negative. The problem of overflow persists. For example, 1400 + 1500 cannot be done correctly. Neither can (1400) + (-1500).

The sign-magnitude method is convenient for human users of data, but it presents a more difficult problem for the electrical engineers who must design the electronic circuits to do arithmetic computations. To add two numbers, two cases must be distinguished: the summands are of the same sign or of different signs. In the first case, the magnitudes must be added, and the result given the sign of the summands. In the second case, the smaller magnitude must be subtracted from the larger magnitude, and the result given the sign of the summand having the larger magnitude. So the engineer must incorporate in his circuit both an adder and a subtractor, and circuits to distinguish the various cases.The process is, of course, exactly the method taught in school for adding signed numbers by hand.

Multiply

Multiplicand in BR Multiplier in QR

AC Qn+1 SC

0 0 n

=10

Qn Qn+1

=01

AC

AC-BR+1

=00 =11

AC

AC+BR

ashr(AC & QR) SC SC - 1

=0

SC

END
FIG: Booths algorithms for multiplication of signed -2s complement numbers

Source Code:
#include<iostream.h> #include<math.h> #include<stdlib.h> void showmenu(); class ALU; class Operand { friend class ALU; int length; int bits[20]; public: Operand(int l) { length=l; for(int i=0;i<length;i++) bits[i]=0; } void setBits(int *p) { for(int i=0;i<length;i++) bits[i]=*(p+i); } void setBits(int *p,int k) { for(int i=0;i<length;i++) bits[i]=*(p+i+k); } void setBits() { cout<<"\nInsert bits of length "<<length<<":"; for(int i=0;i<length;i++) { if(getchar()-48==1) bits[i]=1; else if(getchar()-48==1) bits[i]=0; else {cout<<"\nInvalid data format!\n";break;} } } void dispBits() { for(int i=0;i<length;i++) cout<<bits[i]; }

int shr()// shift right, adding 0 from left { int pop=bits[length-1];

for(int i=length-1;i>0;i--) { bits[i]=bits[i-1]; } bits[0]=0; return pop; } int shl()// shift left, adding 0 from right { int pop=bits[0]; for(int i=0;i<length-1;i++) { bits[i]=bits[i+1]; } bits[length-1]=0; return pop; } int ashr()// shift right, maintaining leftmost bit { int pop=bits[length-1]; for(int i=length-1;i>0;i--) { bits[i]=bits[i-1]; } return pop; } int ashl()// shift left, maintaining rightmost bit { int pop=bits[0]; for(int i=0;i<length-1;i++) { bits[i]=bits[i+1]; } return pop; } int shr(int j)// shift right, with given first bit { int pop=bits[length-1]; for(int i=length-1;i>0;i--) { bits[i]=bits[i-1]; } bits[0]=j; return pop; } int shl(int j)// shift left, with given first bit { int pop=bits[0]; for(int i=0;i<length-1;i++) { bits[i]=bits[i+1];

} bits[length-1]=j; return pop; } void dtb(int n)//Decimal to Binary { if(n==0) for(int i=0;i<length;i++) bits[i]=0; else if(n==1) { for(int i=0;i<length;i++) bits[i]=0; bits[length-1]=1; } else if(n>(int)pow(2,length)||n*(-1)>(int)pow(2,length)) { cout<<"\nData Overflow!\n"; return; } else if(n>0) { int i=length-1; while(n!=0) { bits[i]=n%2; i--; n=n/2; } } else if(n<0) { n=n*-1; int i=length-1; while(n!=0) { bits[i]=n%2; i--; n=n/2; } for(int k=0;k<length;k++) { bits[k]=bits[k]==1?0:1; } int c=0; if(bits[length-1]==1) { bits[length-1]=0; c=1; } else

{ bits[length-1]=1; c=0; } for(k=length-2;k>0;k--) { if(bits[k]==1&&c==1) bits[k]=0; else { bits[k]+=c; c=0; } } } } void toComplement()//convert to 2's complement form { for(int k=0;k<length;k++) { bits[k]=bits[k]==1?0:1; } int c=0; if(bits[length-1]==1) { bits[length-1]=0; c=1; } else { bits[length-1]=1; c=0; } for(k=length-2;k>0;k--) { if(bits[k]==1&&c==1) bits[k]=0; else { bits[k]+=c; c=0; } } }

}; class ALU { public:

int adder(Operand *op1,Operand *op2) { int carry=0; for(int i=op1->length;i>=0;i--) { if(op1->bits[i]==1&&op2->bits[i]==1&&carry==0) { op1->bits[i]=0; carry=1; } else if(op1->bits[i]==1&&op2->bits[i]==0&&carry==1) { op1->bits[i]=0; carry=1; } else if(op1->bits[i]==0&&op2->bits[i]==1&&carry==1) { op1->bits[i]=0; carry=1; } else if(op1->bits[i]==1&&op2->bits[i]==1&&carry==1) { } else { op1->bits[i]=op1->bits[i]+op2->bits[i]+carry; carry=0; } } return carry; }

void multiplier(Operand *br,Operand *qr) { Operand *brc=new Operand(br->length);//2's complement of br brc->setBits(&(br->bits[0])); brc->toComplement(); Operand *ac=new Operand(br->length); int q=0; int sc=qr->length; cout<<"\nQn Qn+1 Operation "; for(int i=1;i<=(br->length-2)/2;i++) cout<<" "; cout<<"AC"; for(i=1;i<=(br->length+1);i++)

cout<<" "; cout<<"QR"; for(i=1;i<=(br->length)/2+1;i++) cout<<" "; cout<<"Qn+1 Sc"; cout<<"\n"<<qr->bits[qr->length-1]<<" "<<q<<" Initialization "; ac->dispBits(); cout<<" "; qr->dispBits(); cout<<" "<<q<<" "<<sc; while(sc!=0) { if(qr->bits[qr->length-1]==1&&q==0) { adder(ac,brc); cout<<"\n"<<qr->bits[qr->length-1]<<" "<<q<<" AC<-AC+(BR 2's comlement) "; ac->dispBits(); cout<<" "; qr->dispBits(); cout<<" "<<q<<" "<<sc; } else if(qr->bits[qr->length-1]==0&&q==1) { adder(ac,br); cout<<"\n"<<qr->bits[qr->length-1]<<" "<<q<<" AC<-AC+BR ac->dispBits(); cout<<" "; qr->dispBits(); cout<<" "<<q<<" "<<sc; } q=qr->shr(ac->ashr()); cout<<"\n"<<qr->bits[qr->length-1]<<" "<<q<<" shr(AC QR) ac->dispBits(); cout<<" "; qr->dispBits(); cout<<" "<<q<<" "<<sc; sc--; }cout<<"\n";

";

";

};

int main() {

ALU *alu1=new ALU(); int len,o1,o2; char as='y'; while(as=='y') { system("cls"); cout<<"\nEnter bit size:"; cin>>len; Operand *op1=new Operand(len); Operand *op2=new Operand(len); cout<<"\nEnter decimal value of operand1:"; cin>>o1; op1->dtb(o1); cout<<"\nEnter decimal value of operand1:"; cin>>o2; op2->dtb(o2); system("cls"); alu1->multiplier(op1,op2); system("pause"); cout<<"do you want do mulitiplication again? y / n"; cin>>as; } system("pause"); }

SCREEN SHOT:

Potrebbero piacerti anche