English 中文(简体)
Efficient Way to Print MIPS Int Array
原标题:

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: "

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.

最佳回答

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

问题回答

暂无回答




相关问题
List of suspected Malicious patterns

I am doing an anti-virus project by disassembling its code and analyzing it. So I want a list of the Suspected Malicious pattern codes, so I can observe which is suspected and which is not? So I want ...

Prefetch for Intel Core 2 Duo

Has anyone had experience using prefetch instructions for the Core 2 Duo processor? I ve been using the (standard?) prefetch set (prefetchnta, prefetcht1, etc) with success for a series of P4 ...

How are mutex and lock structures implemented?

I understand the concept of locks, mutex and other synchronization structures, but how are they implemented? Are they provided by the OS, or are these structures dependent on special CPU instructions ...

Installing GNU Assembler in OSX

No matter how hard I google, I can t seem to find a (relatively) easy-to-follow instruction on how to install the GNU Assembler on a mac. I know I can use gcc -c (Apple Clang on a Mac) to assemble .s ...

8086 assembler,INT 16,2

I got stuck at this thing,I want to see if right shift button has been pressed,so I have this assambler code: mov ah,2 int 16h ;calling INT 16,2 - Read Keyboard Flags interrupt mov ah,...

Translate a FOR to assembler

I need to translate what is commented within the method, to assembler. I have a roughly idea, but can t. Anyone can help me please? Is for an Intel x32 architecture: int secuencia ( int n, ...

热门标签