Question

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.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top