TASK 4 You are required to modify and fill in the body of the functions to imple
ID: 3890836 • Letter: T
Question
TASK 4
You are required to modify and fill in the body of the functions to implement
a divider with unsign numbers.
*/
// write your code here.
}
/*****************************************************************************
Function Description:
unsigned_div. this function simulat a divider with signed numbers. it will
save the results in Remainder and Quotient.
Arguments:
IN Dividend : int - the Dividend
IN Divisor : int - the Divisor
OUT Remainder : int * - the Remainder
OUT Quotient : int * - the Quotient
Return Value:
void
*****************************************************************************/
void signed_div(IN int Dividend, IN int Divisor,
OUT int * Remainder, OUT int * Quotient){
BOOL DividendBS = get_bit(Dividend, WIDTH - 1);
if(DividendBS == TRUE)
Dividend = neg(Dividend);
BOOL DivisorBS = get_bit(Divisor, WIDTH - 1);
if(DivisorBS == TRUE)
Divisor = neg(Divisor);
unsigned_div(Dividend, Divisor, Remainder, Quotient);
BOOL RemainderBS = DividendBS;
BOOL QuotientBS = xor_gate(DividendBS, DivisorBS);
if(RemainderBS == TRUE)
*Remainder = neg(*Remainder);
if(QuotientBS == TRUE)
*Quotient = neg(*Quotient);
}
Explanation / Answer
void signed_div(IN int Dividend, IN int Divisor,
OUT int * Remainder, OUT int * Quotient){
BOOL DividendBS = get_bit(Dividend, WIDTH - 1);//get_bit function
if(DividendBS == TRUE)
Dividend = neg(Dividend);// neg fucntion
BOOL DivisorBS = get_bit(Divisor, WIDTH - 1);//get_bit function
if(DivisorBS == TRUE)
Divisor = neg(Divisor);//neg function
unsigned_div(Dividend, Divisor, Remainder, Quotient);//unsigned_div function
BOOL RemainderBS = DividendBS;
BOOL QuotientBS = xor_gate(DividendBS, DivisorBS);//xor_gate function
if(RemainderBS == TRUE)
*Remainder = neg(*Remainder);//neg function
if(QuotientBS == TRUE)
*Quotient = neg(*Quotient);//neg function
}
//filling body of functionss
bool get_bit(int dividend,int width)//returns true if number is negative,,,otherwise false
{
if(dividend<0)return true;//if number is negative
return false;//if not negative...
}
int neg(int num)//changes the sign of the number
{
num = num*-1;//changing sign
return num;//returning number
}
unsigned_div(int dividend,int divisor,int *remainder,int *quotient)//divides...dividend and divisor..stores remainder and quotient..
{
*remainder= dividend%divisor;//storing remainder...
*quotient = dividend/divisor;//storing quotient...
}
bool xor_gate(bool dividend,bool divisor)//performs xor between dividend and divisor
{
if(dividend==true && divisor ==true)//if both positive
return false;
else if(dividend==false && divisor ==false)//if both negative
return false;
else//if one of them is negative..
return true;//then result is negative...so returning true...
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.