[英]<= and >= Mips representation
也許可以快速解決,但我試圖更好地了解Mips,並且我在這個問題上停留了一段時間。
試圖弄清楚當n滿足要求時如何分支(1 <= n <= 30)
我知道我可以對小於1的東西使用blez,但是如何同時檢查其是否大於26?
我以為可以使用slt,但是我不知道如何實現它。
總結一下我想做的事情:
$t0 = n
$t1 = 1
$t2 = 30
if ($t1 <= $t0 <= $t2) { go to 1stloop }
else ( go to 2ndloop)
假設數字在$v0
,您可以測試它是否在1-26范圍內,如下所示:
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)
有關slt*
指令的更多信息,請參考MIPS指令集參考 。
如果您想使用slt
,它將正常工作。
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
更好的解決方案是使用blt
和bgt
,這是一個最簡潔的方法:
blt $t0, $t1, _2ndloop # branch to _2ndloop if($t0 < $t1)
blt $t2, $t0, _2ndloop
_1stloop:
# do something
_2ndloop:
# do something
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.