我正在将C代码转换为Mips代码
C代码是
int main(void) {
int i;
int data [10] = { 10, -2, 5, 22, 99, 0, -5, 8, 30, 7};
for(i=0; i<10; i++){
printf("%d\n", data[i]);
}
return 0;
}
我的Mips代码是
.data
data: .space 10
enter: .asciiz "\n"
.text
.globl main
main:
addi $s0, $zero, 10
add $t0, $zero, $zero
sw $s0, data($t0)
addi $s0, $zero, -2
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 5
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 22
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 99
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 0
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, -5
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 8
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 30
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 7
addi $t0, $t0, 4
sw $s0, data($t0)
addi $t0, $zero, 0
Loop: slti $t1, $t0, 10
beq $t1, $zero, Exit
mul $t2, $t0, 4
li $v0, 1
lw $a0, data($t2)
syscall
li $v0, 4
la $a0, enter
syscall
addi $t0, $t0, 1
j Loop
Exit:
end:
li $v0, 10
syscall
s1用于保存值。 t0用于索引 t1是slt的标志 t1乘以4是t2
您应该只关注
li $v0, 4
la $a0, enter
syscall
它打印 10-2522990-58307
dddddddddddddddddddddddddddddddddddasdfasdfasdfasdfasdfasdfasdf
请帮助我dddddddddddddddddddddddddddddddddddddd
答案 0 :(得分:0)
您没有为data
阵列保留足够的内存。 .space 10
保留10个字节的内存,但是您正尝试存储10个 words (40个字节)。因此,您最终会覆盖换行符。
如果将声明更改为data: .space 40
,则应该获得预期的输出。