汇编MIPS:初始化和汇总数组

时间:2016-11-08 17:48:50

标签: assembly mips

我们假设我们有一个包含10个数字的数组,我们希望将这些数字加到这样的变量中:

int arr[10]= {1, 15, 0, -3, 99, 48, -17, -9, 20, 15};

sum = 0;

for(i=0; i<10; i++)

   sum = sum + arr[i];

当我尝试&#34;组装mips&#34;这个,一条特定的线路出现了:

.data
arr: .word 1, 12, 0, -3, 99, 48, -17, -9, 20, 15

.text
.globl main
main:

add $t0, $zero, $zero   #counter i
add $t1, $zero, $zero   #sum

la  $t2, arr      #loading the address of the array to a register

loop:
slti    $t3, $t0, 10        #i<10
beq $t3, $zero, EXIT

add $t1, $t1, $t2($t0)     #wrong wrong very wrong

addi    $t0, $t0, 1        #i++
j loop
EXIT:

li  $v0, 10
syscall

我在理解如何表达&#34; arr [i]&#34;指向大会。

它的正确表达方式是什么?

我是否必须为数组的每个数字采用另一个注册表?

1 个答案:

答案 0 :(得分:0)

感谢上面的Jester,正确的代码如下:

If the app crashes using a real device this is printed:
warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.