Search code examples
assemblymipscpucpu-architecturealu

Add instruction greater than immediate to MIPS


This is a homework question and i am stuck because i dont know how to evaluate if a value is greater than another.

Here is the instruction i need to add:

The sgti instruction - set on greater than immediate - is an instruction which allows setting a register to 1 when one value is greater than the other:

sgti rd, rs, imm #if R[rs] > ext32 (imm) then R[rd] = 1 otherwise R[rd] = 0

Here is my circuit:

Here is my circuit: I know for example how to evaluate if a value == another value by doing a substraction in the UAL and looking if the zero is set, but i dont know how to check if greater than. Thanks


Solution

  • For signed addition, overflow happens

    • when adding a positive number with a positive number and the result is negative
    • when adding a negative number with a negative number and the result is positive

    For signed subtraction, overflow happens

    • when given a positive number we subtract a negative number and the result is negative
    • when given a negative number we subtract a positive number and the result is positive

    A quick read of these should make mathematical sense (the sum of two positive numbers cannot be negative).

    You can check for these conditions to determine overflow.

    When overflow happens, then you simply compare the signs of the inputs to determine the result.


    So, here's an approach:

    • check the signs and if they are opposite, then the positive one is larger.

      • in other words, XOR the signs and if the XOR result is 1, they have differing (opposite) signs.
        • the answer to sgti is then the sign of the immediate, which is also the sign of rd negated.
    • otherwise they are either both positive or both negative, and the subtraction should yield the proper result without overflowing

      • so the answer is in the sign bit of the subtraction result specifically, the sign of the result negated is the answer for sgti.

    The sign bit is the high bit, the MSB, aka bit 31.