我有一个问题,我应该在MIPS中写一个if-else。问题最初是用C ++编写的,我必须转换它。如果有人可以,请帮助。我特别需要知道如何设置mips中的switch语句
问题:
result = "";
switch (x)
{
case 2: result = result + "bbb"; break;
case 3: result = result + "ccc"; break;
case 4: result = result + "ddd"; break;
default: result = result + "eee";
}
cout << "3.\t" << result << endl;
答案 0 :(得分:0)
切换MIPS可以通过以下方式实现:
# register to be switched is in $s0
switch:
addi $t0, $zero, 2
bne $s0, $t0, case3
# write code for case 2 here
case3:
addi $t0, $zero, 3
bne $s0, $t0, case4
# write code for case 3 here
case4:
addi $t0, $zero, 4
bne $s0, $t0, default
# write code for case 4 here
default:
# write default code here
另请参阅广泛使用的MIPS Green Card,我确信可以帮助您解决在此过程中可能遇到的其他问题。
答案 1 :(得分:0)
.data
msg2: asciiz "bbb"
msg3: asciiz "ccc"
msg4: asciiz "ddd"
def: asciiz "eee"
.text
main:
#get x
li $v0,5
syscall
move $t1,$v0
#x in t1
addi $t0, $zero, 2
beq $t0,$t1,printb
addi $t0, $zero, 3
beq $t0,$t1,printc
addi $t0, $zero, 4
beq $t0,$t1,printd
#if we have come this far and not branched else where means $t0 has value
4 and didnt went to printd branch print eee
li $v0,4
la $a0,default
syscall
#end this function
li $v0,10
syscall
printb:
li $v0,4
la $a0,msg2
syscall
printc:
li $v0,4
la $a0,msg3
syscall
printd:
li $v0,4
la $a0,msg4
syscall
在数据中定义消息,然后从上到下进行输入比较。如果到达最后意味着它没有去任何分支只执行默认情况。