DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   MIPS: Print array in columns (SPIM) (http://www.daniweb.com/forums/thread200508.html)

pokerstar Jun 29th, 2009 12:40 pm
MIPS: Print array in columns (SPIM)
 
I have written a code that prints array in 6 columns:
# -----
# Print array elements.


        li        $t1, 0
        la        $s0, py_sars

print_lp1:
        bgt        $t1, 59, print_end1

        lw $a0, 0($s0)                            #get the value pointed by s0
        li $v0, 1                                #print int
        syscall
         
        la        $a0, space
        li        $v0, 4                                #print space between each output
          syscall

        addi        $t1, $t1, 1                        #increment counter for next element
        addi        $s0, $s0, 4
        rem        $t5, $t1, 6                        #print areas in 6 columns
        beq        $t5, $zero, newline       
        j print_lp1

newline:
        la        $a0, new_ln
        li        $v0, 4                                #print newline
        syscall
        j print_lp1

print_end1:

I can't get the elements aligned however.
We can asume that the longest length of the integer is 8, and must be right justified.
Could you please guide me on how to do this?
Thanks.

Salem Jun 29th, 2009 1:10 pm
Re: MIPS: Print array in columns (SPIM)
 
- format integer as a string in some memory buffer
- measure it's length
- output 8-len spaces
- output buffer

wildgoose Jun 29th, 2009 4:51 pm
Re: MIPS: Print array in columns (SPIM)
 
Use a static buffer 256 or so bytes in size. Plenty of room!

You can subtract the current add new character position from the buffer base to get your index. Do a modulus operation and advance that many slots by inserting spaces.
OR you can have a moving partition.

NEXT = THIS + 12 space.
print column entry,
If THIS < NEXT
SKIP = NEX - THIS print skip spaces.

OR prefill entire buffer with spaces (0x20).
Then merely skip, no fill needed since its prefilled.
But don't forget your ASCIIz terminator after the last column!

wildgoose Jun 29th, 2009 4:52 pm
Re: MIPS: Print array in columns (SPIM)
 
You can also print character by character but your column position logic remains the same. Instead of inserting spaces, write spaces.

pokerstar Jun 30th, 2009 3:32 am
Re: MIPS: Print array in columns (SPIM)
 
Thanks all! Working on that part of my program now.


All times are GMT -4. The time now is 1:12 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC