2016-03-15 141 views
2

我正在用MIPS編寫一個程序,但無法圍繞下面的語句寫下下面的語句。我如何在MIPS指令集中編寫這樣的邏輯語句?將C轉換爲MIPS

return a > b ? a : b; 
+0

查找'SLT'和'BEQ'在MIPS指令集的參考。 – Michael

回答

1
  • return:一些值返回到被叫方(如果表達呈現)。
  • A ? B : C:這是條件運算符。如果A爲真(非零),則評估B。否則,評估C

如果ab簽署的32位整數,它應該是這樣的:

# assuming 
# a = $t0 
# b = $t1 
# return value = $v0 

slt $t2, $t1, $t0  # $t2 = (b < a) 
beq $t2, $zero, nottrue # if (!(a > b)) goto nottrue 
addui $v0, $t0, $zero # return value = a (not harmful even if executed when jump is taken) 
jr $ra     # return 
sll $zero, $zero, 0  # nop: prevent instruction after branch from being executed 
nottrue: 
addui $v0, $t1, $zero # return value = b 
jr $ra     # return 
sll $zero, $zero, 0  # nop: prevent instruction after branch from being executed 
+0

謝謝邁克,你是一個巫師 –