Unit 1e

Creating the Comparison Sub-Block

Efficient Comparison in the MIPS ALU

For the comparison operations, Set on Less Than (SLT) and Set on Less Than Unsigned (SLTU), we wish to determine whether the input A is less than the input B. If it is, we wish to set the result to X"0000000000000001". If it is not, we wish to set the result to X"0000000000000000".

Let's start by looking at the calculation we wish to perform: A < B. Using a little simple algebra, this expression can be rewritten as A - B < 0. If we were to subtract B from A, then all that would remain is the relatively trivial matter of determining whether the result is less than zero. Instead of a large comparator which will consume a number of gates, we can once again reuse our single adder with a minimal amount of logic looking at the output.

First, we must make sure that our existing Arithmetic sub-block is performing a subtraction operation. This will occur when ALUOp(1 DOWNTO 0) is either "10" or "11". fortunately, we only have two comparison operations to perform. SLT will require a signed subtraction, thus it will be encoded as ALUOp(1 DOWNTO 0) = "10". SLTU will require an unsigned subtraction, thus it will be encoded as ALUOp(1 DOWNTO 0) = "11". The remaining two encodings for this sub-block will be undefined.

Now, the question is how do we determine if the result is less than zero based on the four inputs which we have defined to the Comparison sub-block: CarryOut from the subtractor, Bit 63 of the subtractor result (RSign), Bit 63 of the input A (ASign), and Bit 63 of the input B (BSign).

Lets start with the Signed version. There are four possible combinations of the signs of the inputs: both are positive; both are negative; A is positive and B is negative; A is negative and B is positive. When the signs of the inputs are different, our problem is trivial. If A is negative and B positive, then A MUST be less than B. Conversely, if A is positive and B is negative then A CAN NEVER be less than B. When the inputs have the same sign we need to look at the result of the signed subtraction. Two inputs with the same sign can never cause an overflow in subtraction, so we do not have to concern ourselves with an incorrect answer due to that. If the inputs are of the same sign, then whenever A is smaller than B the result of subtracting A - B MUST be a negative number. So, if the sign of the result, SignR, is negative we have A < B and if it is positive then we have A > B.

For the Unsigned version of the comparison we have a simple test. In unsigned representation, A and B are both positive numbers. Subtracting two positive numbers will result in a positive number if A > B or a negative number if A < B. However, we cannot represent a negative number with an Unsigned integer: if we try we will have an overflow condition. Overflow for unsigned subtraction means that the result is a negative number (it is impossible to subtract two unsigned numbers and get a result too large to represent). We can test for the occurence of overflow in unsigned subtraction by looking at the Carry Out of the most significant bit. In unsigned addition, the existence of a Carry Out indicates overflow. In unsigned subtraction, however, the situation is reversed: Carry Out high is the normal state and Carry Out low indicates overflow. Thus, since we are performing unsigned subtraction in our SLTU, if there is no Carry Out, then we had an overflow and A - B is less than zero and A < B is true. If there is a Carry Out, then A - B is greater than zero and A < B is false.

Since we are only concerned with four input bits to determine our output, we will be implementing the Comparison sub-block by creating a Truth Table view.

Creating a Truth Table View for the Comparison Sub-Block