the C++ version is
#include <iostream>
using namespace std;
int x[10] = {0};
int pos[6] = {1, 3, 6, 9, -1, -1};
int data[6] = {73, 47, 23, 26, -1, -1};
int main() {
for (int i=0; i<6; i++) {
if (pos[i] >= 0) {
int temp = pos[i];
x[temp] = data[i];
}
}
for (int i=0; i<10; i++) {
cout << x[i] << endl;
}
}
.data
x: .word 0:10
pos: .word 1
.word 3
.word 6
.word 9
.word -1
.word -1
data: .word 73
.word 47
.word 23
.word 26
.word -1
.word -1
endl: .asciiz "\n"
# i $s0
# &x[0] $s1
# &pos[0] $s2
# &data[0] $s3
# 6, 10 $s6
.text
main: la $s1, x
la $s2, pos
la $s3, data
li $s6, 6
li $s0, 0 # for (int i=0; i<6; i++) {
for: mul $t0,$s0,4 # if (pos[i] >= 0) {
add $t0,$s2,$s0
lw $t1,($t0)
blt $t1,0,loop
add $t2, $s1,$t1 # int temp = pos[i]
lw $t3,4($t2) # x[temp] = data[i];
add $t4, $s3,$s0
lw $t5, 4($t4)
sw $t3, ($t5)
loop: addi $s0, $s0,1 # }
blt $s0, $s6, for # }
我一直试图解决这个问题,我得到了#34;没有对齐字边界错误&#34;而我无法弄清楚为什么在第一次迭代后错误似乎从lw $ t3,4($ t2)开始,预期的输出是 0 73 0 47 0 0 23 0 0 26