打印 MIPS Int 数组的有效方法

发布于 2024-08-10 05:58:32 字数 1487 浏览 3 评论 0原文

我正在做一项家庭作业,将我们编写的 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

这是一个家庭作业项目,我真的很想完全理解一个cleaner打印数组的方法,我不想抄袭。非常感谢有关编写循环的提示,我不是在寻找有人给我代码。

I'm working on a homework assignment translating a C program we wrote to MIPS. My question is about general MIPS coding and not project specific issues though. I've run into an issue with printing my output. I have an array and output string declared as such:

array: .word 7, 2, 5, -3, 3, 6, -4, 1  
output1: .asciiz "Array: \0"

I'm trying to output the data so I have the following format:

Array: 7 2 5 -3 3 6 -4 1

Our array is hard coded, and our array length is predetermined. I've tried to come up with a loop to print it out efficiently, but dealing with the lw offset using a register was an issue.
I have come up with the following code to hardcode my output, but I still have another array I need to print, and this just seems like it's taking up way to much room. My code is fully functional, but it's just a mess! Can anyone give me tips to clean it up / refactor it?
The array is stored in $a0/$s0, the array size is stored in $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

This is a homework project and I really want to fully understand a cleaner way to print out arrays, I am not looking to plagiarize. Tips on writing the loop are greatly appreciated, I'm not looking for someone to give me the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2024-08-17 05:58:32

使用 addi 增加 $s0 可能会有所帮助,而不是手动更改偏移量 - 这样您就始终使用 lw 0($s0)< /代码>。

编辑:我想我应该补充一点,您正在循环内递增 $s0 (使用 j 进行循环)。

It might be helpful to increment $s0 with addi instead of manually changing the offset – that way you're always using lw 0($s0).

Edit: I suppose I should add that you're being incrementing $s0 within a loop (use j for the loop).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文