我想试着把一个C程序转换成MIPS汇编代码
下面是C语言程序:
int x=2;
int index;
for(index = 0;index<4;index++){
x=x+index;
}以下是我对MIPS汇编代码的尝试:
li $8,4 # the limit
li $9,2 #x = 2
li $10,0 #index, starts at 0
forLoop:
slt $11,$10,$8 #if index <4 then $11 = true =1
beq $11,$0,Exit #if $11 = 0 = false means reached 4, then exit
add $9,$9,$10 #adding the index with the value in x
addi $10,1 # add 1 to the index if didnt reach the limit
j forLoop # repeat the loop
Exit:
nop #end 需要问你们所有人这是否正确,因为我没有mips模拟器,不确定如何结束程序,nop是有效的退出计划吗?
发布于 2013-06-19 09:33:52
这是一个将C代码转换为MIPS的简单版本:
注意:我使用SPIM来做这件事。
main:
li $t0, 2 # $t0 = x = 2
li $t1, 0 # $t1 = index = 0
li $t2, 4 # $t2 = indexLimit = 4
jal forLoop # jump and link the forLoop label
move $a0, $t0 # move the result into $a0 for printing
li $v0, 1 # load print integer code
syscall # tell system to do it
li $v0, 10 # load exit code
syscall # clean exit
forLoop:
bge $t1, $t2, exit # if index >= 4 goto exit label
add $t0, $t0, $t1 # x = x + index
addi $t1, $t1, 1 # index++
j forLoop # continue loop by jumping back up
exit:
jr $ra # jump and return the return address回答你的问题:nop什么也不做。你可以将它用于计时等目的。这里有一个维基百科链接,可以进一步阅读http://en.wikipedia.org/wiki/NOP。请注意,要结束MIPS程序,请将10加载到$v0中,然后加载syscall。
编辑:
作为对您的评论的回应:您是在正确的轨道上,但不要忘记添加一个主标签,然后从主标签跳到forLoop,然后让您的退出标签终止程序(如果需要,首先打印整数)。
以下是在MIPS中进行编程的两个有用链接:http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm
发布于 2013-06-19 11:45:44
也许您需要查看这个How do you get assembler output from C/C++ source in gcc?,并使用您的交叉编译器来生成汇编文件。
https://stackoverflow.com/questions/17181445
复制相似问题