5

试图将此 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 中运行时出现以下错误:

PC=0x00400040 发生异常
  数据/堆栈读取中的错误地址:0x00000004
PC=0x0040004c 发生异常
  inst/data fetch 中未对齐的地址:0x00000003
PC=0x00400058 发生异常
  数据/堆栈读取中的错误地址:0x00000000
尝试在 0x0040006c 处执行非指令

一些方向会很好。谢谢

4

2 回答 2

2

在初始化指向和的指针之前,您正在分支到loop( ) 。您需要将 移到代码的末尾,而不是在开头。blt $t0, $t1, loopABblt $t0, $t1, loop

我讨厌这样做,但是有太多事情错了,无法一一列举。试试这个:

.data 
A:   .word  0:100        # array of 100 integers 
B:   .word  0:100        # array of 100 integers 


.text 
main: 
    li $t0, 4       # load the value "1" into register $t0 
    li $t1, 400     # load the value "100" into register $t1 
    la $t9, B 
    la $t8, A 

loop: 
    add $t2, $t9, $t0  # $t2 = B + i
    lw $s4, 0($t9)     # $s4 = B[i]
    add $t3, $t0, -4   # $t3 = i - 1
    add $t4, $t8, $t3  # $t4 = A + i - 1
    lw $s5, 0($t4)     # $s5 = A[i - 1]
    add $t5, $t8, $t0  # $t5 = A + i
    add $t6, $s4, $s5  # $t6 = B[i] + A[i - 1]
    sw $t6, 0($t5)     # A[i] = $t6
    addi $t0, $t0, 4   # i++

    li $v0, 1 # system call for print_int 
    move $a0, $t6 # the sum to print 
    syscall # print the sum

    blt $t0, $t1, loop # branches to Loop if $t0 < 100 
于 2010-02-28T07:54:00.767 回答
1

马上, s1 和 s2 应该初始化为数组的基于堆栈的(我假设)地址。

于 2010-02-28T05:29:40.363 回答