Talen Kylon
Talen Kylon

Reputation: 1958

Determining valid input using SLT in MIPS

I'm learning MIPS assembly. I want to accept values 1, 2, 3 inclusive. Does this logic make sense to accomplish this?

move  $s0, $v0       # get user input
li    $t0, 1         # minimum accepted
li    $t1, 3         # maximum accepted
slt   $t2, $s0, $t0  # if input < 1, t2 = 1.
beq   $t2, $t0,      # if t2 = 1, bad input
slt   $t2, $t1, $s0  # if 3 < input, t2 = 1
beq   $t2, $t0       # if t2 = 1, bad input

Upvotes: 0

Views: 570

Answers (1)

Michael
Michael

Reputation: 58427

You're missing the labels to jump to, and the code could be simplified a bit by using SLTI:

move  $s0, $v0        # get user input
li    $t0, 3          # maximum accepted
slti  $t1, $s0, 1     # t1 = (input < 1) ? 1 : 0
bne   $t1, $zero, bad # if t1 != 0, bad input
slt   $t1, $t0, $s0   # t1 = (3 < input) ? 1 : 0
bne   $t1, $zero, bad # if t2 != 0, bad input
ok:
# Do whatever
bad:
# Do whatever

The logic looks sound though.

Upvotes: 2

Related Questions