[英]Translate C++ to MIPS Assembly
我需要此問題的最后一部分的幫助。 我基本上是想將C ++代碼翻譯成MIPS匯編語言。
假設a在$ s0中,b在$ s1中,c在$ s2中,x在$ s4中,y在$ s5中,z在$ s6中。
我幾乎完成了所有任務,但是我堅持了這兩個任務,我知道其中的某些部分,但是很難將它們作為一個整體組合在一起。 我知道的部分后面將帶有帶有匯編代碼的井號。 謝謝你的幫助。
1。
for(x = 0; x <= z; x++) # x = 0; is: addi $s4, $0, 0
y = y + 1; # addi $s5, $s5, 1
y = 0; # addi $s5, $0, 0
2。
if(y > z)
x = 0; # addi $s4, $0, 0
else x = 1; # else: addi $s4, $0, 1
如果我錯了,這是沒有主題標簽的原始問題:
1。
for(x = 0; x <= z; x++)
y = y + 1;
y = 0;
2。
if(y > z)
x = 0;
else x = 1;
再次感謝。
嘗試2,不確定是否正確。
ifLoop:
add $s5, ? , $s6
addi $s4, $0, 0
ifLoop
else:
addi $s4, $0, 1
else
練習:(假設數組p在$ s7中)
p[0] = 0;
int a = 2;
p[1] = a;
p[a] = a;
我的嘗試:
sw $0, 0($s7)
addiu $s0, $0, 2
sw $s0, 4($s7)
sll $t0, $s0, 2
addu $t1, $t0, $s7
sw $s0, 0($t1)
編輯:1.幸運的是,沒有偽指令也沒什么不同。
addi $s4, $0, 0
forLoop: sle $t1, $s4, $s6 #if x <= z, set $t1 to 1, else 0
addi $s5, $s5, 1
addi $s5, $0, 0
addi $s4, $s4 1
bne $t1, $0, forLoop #repeat loop while $t1 is not 0
這是#2。 我只是想讓您先嘗試一下,然后再給出答案。 您想使用slt指令將寄存器設置為1或0。如果為1,則比較為true(y> z)。 然后使用bne確定要跳轉到的位置。 通過將bne與0進行比較,真實代碼最終將直接位於bne指令之下。 否則,跳到標簽。
slt $t2, $s6, $s5 # if z < y, set $t2 to 1, else 0
bne $t2, $0, else # if $t2==1, do the code below, if not, go to else
addi $s4, $0, 0
j continue # need the jump instruction to skip the else below
else:
addi $s4, $0, 1
continue:
# rest of code/program
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.