views:

954

answers:

1

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.

+3  A: 

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

Isaac Hodes
If you don't want to overwrite $s0, just use another general purpose register to walk through the array.
Carl Norum
Great idea! I had been trying to use a register to store the offset, but that's not valid MIPS from my understanding. Always using a 0 offset and instead incrementing the register itself seems like the way to go.I'll try it out!
Aaron
Worked like a charm, turned that whole mess into about 10-12 lines that can be reused for my other output, will continue to refactor after I've got the program fully functional.
Aaron