我正在做一项家庭作业,将我们编写的 C 程序翻译成 MIPS。我的问题是关于一般 MIPS 编码而不是项目具体问题。我遇到了打印输出的问题。我有一个数组和输出字符串声明如下:
array: .word 7, 2, 5, -3, 3, 6, -4, 1
output1: .asciiz "Array: \0"
我正在尝试输出数据,因此我具有以下格式:
Array: 7 2 5 -3 3 6 -4 1
我们的数组是硬编码的,我们的数组长度是预先确定的。我试图想出一个循环来有效地打印出来,但是lw
使用寄存器处理偏移是一个问题。
我想出了以下代码来硬编码我的输出,但我还有另一个数组需要打印,这似乎占用了很多空间。我的代码功能齐全,但只是一团糟!谁能给我一些清理/重构它的提示?
数组存储在 中$a0/$s0
,数组大小存储在$a1/$s1
la $a0, output1 # print the "Array: " string
li $v0, 4
syscall
# Huge Oversized Print Statement to print out the original Array:
li $v0, 1 # print the array
lw $a0, 0($s0)
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
li $v0, 1
lw $a0, 4($s0)
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 8($s0)
li $v0, 1
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 12($s0)
li $v0, 1
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 16($s0)
li $v0, 1
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 20($s0)
li $v0, 1
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 24($s0)
li $v0, 1
syscall
la $a0, space #print the space between elements
li $v0, 4
syscall
lw $a0, 28($s0)
li $v0, 1
syscall
这是一个家庭作业项目,我真的很想完全了解一种更简洁的打印数组的方法,我不想抄袭。非常感谢编写循环的提示,我不是在找人给我代码。