Lab 02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CS 201P COMPUTER ORGANIZATION LAB

Experiment 02.

You may use VHDL/Verilog or any other Hardware Description Languages for your hardware
modeling.

1. The gcd( a , b ) function returns the greatest common divisor (GCD) of two positive integers, a
and b . The gcd function can be obtained by using subtraction, which is based on the equation:
 a , a  b

gcd( a , b )   gcd a  b , b , a  b
 gcd a , b  a , a  b

Suppose the GCD computing circuit is modeled as a finite state machine. A pseudo-code for
implementing the finite state machine is given below:

Algorithm GCD_compute (output: r; input: a_in, b_in)


a = a_in;
b = b_in;
swap: if (a = b) then
goto stop;
else
if (a < b) then
swap (a, b);
end if;
a = a – b;
goto swap;
end if;
stop: r = a;

The state machine is synchronized with the help of a clock signal. The system starts operating
when the start signal is activated. An asynchronous reset signal is used for system initialization.
An external ready status signal is asserted when the circuit is idle and ready to accept new inputs.
Assume that the inputs and output are of 8 bits. Model the GCD computing circuit with a
hardware description language.

2. In problem 1, the number of clock cycles needed to compute the GCD depends on the input
value. It requires more time if only a small value is subtracted each time. The calculation of
 
gcd 1,28  1 represents the worst case scenario. The loop has to be repeated 28  1 times until
the two values are equal. For a circuit with an N bit input, the computation time is of the order
 
O 2 N , and thus this is not an effective design. One way to improve the design is to take
advantage of binary number system. For a binary number, we can tell whether it is odd or even by
checking the LSB. Based on the LSB of the two inputs, several simplification rules can be applied
in the derivation of the GCD function:
a b
 If both a and b are even, gcd a , b   2 gcd  , .
 2 2
 b
 If a is odd and b is even, gcd a , b   gcd  a ,  .
 2
a 
 If a is even and b is odd, gcd a , b   gcd  , b  .
2 

Since the divide by 2 operation involves right shifting by one bit, it can easily be implemented in
hardware. The previous gcd function can be rewritten as:

 a , a  b
 a b
 2 gcd  ,  , a  b  a , b : even
  2 2
  b
 gcd  a , 2  , a  b  a : odd  b : even
gcd a , b     
 gcd  , b  , a  b  a : even  b : odd
 a
 2 
 gcd a  b , b , a  b  a , b : odd

 gcd a , b  a , a  b  a , b : odd


Repeat problem number 1 with the revised algorithm.

You might also like