我正在学习MIPS组装。我想接受1,2,3的值。这个逻辑有意义吗?
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
答案 0 :(得分:2)
您错过了要跳转到的标签,使用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
虽然逻辑看起来很合理。