0

Maybe a quick fix but Im trying to get a better understanding of Mips and I have been stuck on this problem for a while.

trying to figure out how to branch off when n meets the requirements (1<= n <= 30)

I understand that I can use blez for the stuff less than 1, but how can at the same time check for if its greater than 26?

I thought I could use slt, but I dont understand how to implement it.

Looked at this link to see if slt would help.

just to sum up what Im trying to do:

$t0 = n
$t1 = 1
$t2 = 30

 if ($t1 <= $t0 <= $t2) { go to 1stloop }
 else ( go to 2ndloop)
Community
  • 1
  • 1
Conor
  • 640
  • 5
  • 10
  • 21

2 Answers2

1

Assuming that the number is in $v0, you could test if it's in the range 1-26 like this:

blez $v0,error    # if ($v0 < 1) goto error
sltiu $t0,$v0,27  # $t0 = ($v0 < 27) ? 1 : 0
blez $t0,error    # if ($v0 >= 27) goto error

# Proceed with normal operation
....

error:
# Handle out-of-range input (e.g. print a message to the user)

For more information about the slt* instructions, consult a MIPS instruction set reference.

Michael
  • 57,169
  • 9
  • 80
  • 125
0

if you want to use slt, this will work fine.

    li    $t0, n
    li    $t1, 1
    li    $t2, 30

    slt   $s0, $t0, $t1     #$s0 = ($t0 < $t1) ? 1 : 0
    beq   $s0, 1, _2ndloop
    slt   $s0, $t2, $t0
    beq   $s0, 1, _2ndloop

_1stloop:
    # do something

_2ndloop:
    # do something

better solution would be using blt and bgt, and here is a snippest:

    blt   $t0, $t1, _2ndloop     # branch to _2ndloop if($t0 < $t1)
    blt   $t2, $t0, _2ndloop
_1stloop:
    # do something

_2ndloop:
    # do something
Harry
  • 63
  • 1
  • 10