#using .word or .byte define a 2-D matrix
#start assuming row-wise or column-wise and print .word
#1 2 3 4 5 6 7 8 9 10 11 12
#as
#1 2 3 4
#5 6 7 8
#9 10 11 12
.data
array: .byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
newline:
.asciiz "\n"
spacing_single:
.asciiz " "
spacing_double:
.asciiz " "
.globl main
.text
main:
#load all the arrays at once
la $s0, array
#t0 is going to be our counter - when it hits 4, we newline
li $t0, 0
process:
lb $a0, ($s0) #load the value of the pointer at the array
li $v0, 1 #output the integer
syscall #output the number inputted from the array
j format
process_2:
addi $s0, $s0, 1 #advance to the next element in the array
addi $t0, $t0, 1 #increment the newline counter
addi $t1, $t1, 1 #increment our exit counter - when this hits 12, we should be done
beq $t1, 12, exit #if we reach the end of the array, exit
beq $t0, 4, new_line #when t0 is equal to 4, jump to the new_line label
j process #if it isn't, go back to the process and redo it
format:
move $t2, $a0
div $t3, $t2, 10 #if the number is smaller than 10 (aka a single-digit)
beqz $t3, double_digit_spacing #if it's a single digit number, we need to put 2 spaces
li $v0, 4 #if it isn't (a double-digit number) then we only need one space
la $a0, spacing_single
syscall
move $a0, $t2
j process_2
double_digit_spacing:
li $v0, 4
la $a0, spacing_double
syscall
move $a0, $t2
j process_2
new_line:
li $v0, 4
la $a0, newline
syscall #print a new line
li $t0, 0 #reset our counter to 1
j process #go back to the process
exit:
#end the program
li $v0, 10
syscall
Comments