Docsity

Detecting Signed Integer Inequalities in MIPS Assembly, Assignments of Computer Architecture and Organization

Toggle sidebar
bg1
3.4 -- 250 ten
3.7
addu $t2, $zero, $t3 # copy $t3 into $t2
bgez $t3, next # if $t3 >= 0 then done
sub $t2, $zero, $t3 # negate $t3 and place into $t2
Next:
3.10 Either the instruction sequence
addu $t2, $t3, $t4
sltu $t2, $t2, $t4
or
addu $t2, $t3, $t4
sltu $t2, $t2, $t3
works.
3.12 To detect whether $s0 < $s1, it๎˜™ s tempting to subtract them and look at the sign of the result.
This idea is problematic, because if the subtraction results in an overflow, an exception would
occur! To overcome this, there are two possible methods: You can subtract them as unsigned
numbers (which never produces an exception) and then check to see whether overflow would have
occurred. This method is acceptable, but it is lengthy and does more work than necessary. An
alternative would be to check signs. Overflow can occur if $s0 and (๎˜“ $s1) share the same sign; that
is, if $s0 and $s1 differ in sign. But in that case, we don๎˜™ t need to subtract them since the negative
one is obviously the smaller! The solution in pseudocode would be
if ($s0<0) and ($s1>0) then
$t0:=1
else if ($s0>0) and ($s1<0) then
$t0:=0
else
$t1:=$s0๎˜“ $s1
if ($t1<0) then
$t0:=1
else
$t0:=0

Partial preview of the text

Download Detecting Signed Integer Inequalities in MIPS Assembly and more Assignments Computer Architecture and Organization in PDF only on Docsity!

3.4 -- 250 ten

addu $t2, $zero, $t3 # copy $t3 into $t bgez $t3, next # if $t3 >= 0 then done sub $t2, $zero, $t3 # negate $t3 and place into $t Next: 3.10 Either the instruction sequence addu $t2, $t3, $t sltu $t2, $t2, $t or addu $t2, $t3, $t sltu $t2, $t2, $t works. 3.12 To detect whether $s0 < $s1, it s tempting to subtract them and look at the sign of the result. This idea is problematic, because if the subtraction results in an overflow, an exception would occur! To overcome this, there are two possible methods: You can subtract them as unsigned numbers (which never produces an exception) and then check to see whether overflow would have occurred. This method is acceptable, but it is lengthy and does more work than necessary. An alternative would be to check signs. Overflow can occur if $s0 and ( $s1) share the same sign; that is, if $s0 and $s1 differ in sign. But in that case, we don t need to subtract them since the negative one is obviously the smaller! The solution in pseudocode would be if ($s0<0) and ($s1>0) then $t0:= else if ($s0>0) and ($s1<0) then $t0:= else $t1:=$s0 $s if ($t1<0) then $t0:= else $t0:=