Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Set on Less Than an ALU operation

Tags:

assembly

mips

Why is slt considered an ALU operation? I thought it will just do a subtract then get the *Z*ero output from ALU?

 ALU control lines |     Function
-------------------+-------------------
       0000        |       AND
       0001        |       OR
       0010        |       add
       0110        |     subtract
       0111        | set on less than

Or is the ALU supposed to output 1 if the result of A - B (in slt $t1, A, B) is negative.

like image 578
Jiew Meng Avatar asked Oct 22 '11 08:10

Jiew Meng


1 Answers

It's supposed to output 1 if A is less than b and 0 otherwise. This often entails calculating A - B, which you need the ALU for.

Now if it was just calculating the sign bit of A - B it would be redundant, but consider the case where A = -2147483648 = 0x80000000 and B = 1 = 0x00000001, here the subtraction result will be 0x7fffffff = 2147483647, which does not have the most significant bit set even though A < B.

As the table is from "Patterson, David A.; Hennessy, John L.: Computer Organization and Design" I'll refer you to chapter 3, specifically the subsection that deals with "addition and subtraction" (3.3 in the 2nd edition, 3.2 in the 4th). It has a table dealing with overflow/underflow where the corner cases can be looked up.

Also remember that the ALU result is 32 bits, so even if the result was just the sign bit of the subtraction result it would still need a different ALU operation code to signal that 31 zeros concatenated with the result bit should be returned rather than the full subtraction result.

The "Zero" line you're probably referring to is - in the architectures discussed in the book - IIRC only used for comparisons in relation to branch equal / branch not equal. SLT/SLTI on the hand stores its result in a register.

like image 139
user786653 Avatar answered Oct 14 '22 10:10

user786653