0

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

Lynn
  • 121
  • 8
  • 25
  • You can check the sign bit of the result but you will also need to detect overflow. You need to add stuff to your circuit. – Jester Jul 04 '20 at 18:28
  • @Jester thanks for your response, okay i get how checking the sign will tell me if rs is greater than imm, (0 means its a positive number so if rs-imm gives me a positive number it means that rs>imm) but why do i need to detect overflow? and what kind of stuff do i need to add? how do i look at the first sign bit of the result? – Lynn Jul 04 '20 at 18:33
  • Try it on 0x7FFFFFFF with -12 for the immediate. The subtraction yields 0x80000011. This appears negative and if you don't realize this is overflow, you would think -12 is larger than maxint. – Erik Eidt Jul 04 '20 at 18:52
  • @ErikEidt okay, but i have no idea how to control overflow or even how to look if my result is negative or positive – Lynn Jul 04 '20 at 19:07

1 Answers1

5

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.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • Okay, I understand why i have to take care of the overflow that my substractions can generate. I also understand the approach you explained, but in my case i am doing a substraction. So when you say "check the signs and if they are opposite" when given a positive number we subtract a negative number and the result is negative, i need to check if the two inputs were positive and negative and also check if the result was negative to conclude that the positive one is larger. – Lynn Jul 04 '20 at 20:00
  • Also, i dont know how to check if a value is positive or negative in paper.. is there a component that can do that? – Lynn Jul 04 '20 at 20:01
  • 2
    The sign bit, A[31], tells us whether A is positive or negative. – Erik Eidt Jul 04 '20 at 20:23
  • okay thanks, i have a last question, is there a proper way to draw the fact that i'm taking the sign bit of my substraction s? i'm not sure about how to say: I'm taking s[31] only and then negate it to put it in rd. – Lynn Jul 05 '20 at 16:29
  • 1
    Yes, do it using the [31] label on a line, see for reference the way the output of the Instruction Memory (MI) is connected to various things like Control labeled [31..26] and Registers [25..21]. – Erik Eidt Jul 05 '20 at 19:14