C 到 MIPS 的转换
尝试将此c 代码转换为MIPS 并在SPIM 中运行。
int A[100], B[100];
for(i=1; i<100; 1++){
A[i] = A[i-1] + B[i];
}
到目前为止,这就是我所拥有的。
# comments are delimted by has marks
.data
A: .word 0:100 # array of 12 integers
B: .word 0:100 # array of 12 integers
.text
main:
li $v0, 1 # load the value "1" into register $v0
li $t0, 1 # load the value "1" into register $t0
li $t1, 100 # load the value "100" into register $t1
blt $t0, $t1, loop # branches to Loop if $t0 < 100
la $t9, B
la $t8, A
loop:
sll $t0, $t0, 2
add $t2, $t9, $t0
lw $s4, 0($t9)
add $t3, $t0, -1
add $t4, $t8, $t3
lw $s5, 0($t4)
add $t5, $t2, $s5
add $t6, $s0, $t0
sw $t7, 0($t5)
addi $t0, $t0, 1
li $v0, 1 # system call for print_int
move $a0, $t0 # the sum to print
syscall # print the sum
在 SPIM 中运行时,我收到以下错误:
Exception occurred at PC=0x00400040 Bad address in data/stack read: 0x00000004 Exception occurred at PC=0x0040004c Unaligned address in inst/data fetch: 0x00000003 Exception occurred at PC=0x00400058 Bad address in data/stack read: 0x00000000 Attempt to execute non-instruction at 0x0040006c
某些方向会很好。谢谢
Trying to convert this c code into MIPS and run it in SPIM.
int A[100], B[100];
for(i=1; i<100; 1++){
A[i] = A[i-1] + B[i];
}
So far this is what I have.
# comments are delimted by has marks
.data
A: .word 0:100 # array of 12 integers
B: .word 0:100 # array of 12 integers
.text
main:
li $v0, 1 # load the value "1" into register $v0
li $t0, 1 # load the value "1" into register $t0
li $t1, 100 # load the value "100" into register $t1
blt $t0, $t1, loop # branches to Loop if $t0 < 100
la $t9, B
la $t8, A
loop:
sll $t0, $t0, 2
add $t2, $t9, $t0
lw $s4, 0($t9)
add $t3, $t0, -1
add $t4, $t8, $t3
lw $s5, 0($t4)
add $t5, $t2, $s5
add $t6, $s0, $t0
sw $t7, 0($t5)
addi $t0, $t0, 1
li $v0, 1 # system call for print_int
move $a0, $t0 # the sum to print
syscall # print the sum
When running in SPIM I get the following errors:
Exception occurred at PC=0x00400040 Bad address in data/stack read: 0x00000004 Exception occurred at PC=0x0040004c Unaligned address in inst/data fetch: 0x00000003 Exception occurred at PC=0x00400058 Bad address in data/stack read: 0x00000000 Attempt to execute non-instruction at 0x0040006c
Some direction would be nice. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在初始化指向
A
和B
的指针之前,您将分支到loop
(blt $t0, $t1, Loop
)代码>.您需要将blt $t0, $t1, Loop
移至代码末尾,而不是将其放在开头。我讨厌这样做,但错误的事情太多,无法一一列举。试试这个:
You are branching to
loop
(blt $t0, $t1, loop
) before you initialize the pointers toA
andB
. You need to move theblt $t0, $t1, loop
to the end of your code, not have it at the beginning.I hate to do this, but there are too many things wrong to list them all. Try this:
立即, s1 和 s2 应该初始化为数组的基于堆栈的(我假设)地址。
Right off the bat, s1 and s2 should be initialized to the stack-based (I assume) address of your arrays.