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.

Recommended Answers

All 4 Replies

- format integer as a string in some memory buffer
- measure it's length
- output 8-len spaces
- output buffer

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!

You can also print character by character but your column position logic remains the same. Instead of inserting spaces, write spaces.

Thanks all! Working on that part of my program now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.