0

I have the entire code done for this assignment but I cannot for the life of me figure out how to align it into columns so it better represent a multiplication table. Looking for some assistance in making this output look more readable by left aligning it or something of that nature.

.text

main:
    move    $s0 ,   $zero
    lw  $s1 ,   MAXROW 
    lw  $s2 ,   MAXCOL
    li  $t0 ,   1
    li  $t1 ,   1

oLoop:
    bgt $t1 ,   $s2 ,   done
    li  $t0 ,   1
    li  $v0 ,   4
    la  $a0 ,   break
    syscall
    
iLoop:
    bgt     $t0 ,   $s1 ,   oLoop2  
    mul $s0 ,   $t0 ,   $t1
    li  $v0 ,   1
    la  $a0 ,   ($s0)
    syscall
    li  $v0 ,   4
    la  $a0 ,   space
    syscall
    addi    $t0 ,   $t0 ,   1
    b   iLoop

oLoop2:
    addi    $t1 ,   $t1 ,   1
    j oLoop

done:
    li  $v0 ,   10
    syscall
    

.data

MAXROW:        .word    20
MAXCOL:        .word    16
break:         .ascii "\n"
space:         .ascii " "

Current output

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 
 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 
 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 
 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 
 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 
 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 
 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 
 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 
 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 
 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 187 198 209 220 
 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240 
 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247 260 
 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266 280 
 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285 300 
 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

MARS toy system calls for output don't have formatting options (unlike if you were on Linux MIPS for example, passing args to printf like "%5d" to pad the number to 5 spaces wide). They also don't have any cursor-position movement or query functions.

So to achieve the same thing, you'll need to figure out how many decimal digits are in each number and manually print the right number of spaces to get a fixed width. (e.g. start with w=1, multiply by 10 repeatedly until its > your number. But that fails for big numbers where the next power of 10 doesn't fit in a register.)

Or manually format your numbers into strings starting from the end of a fixed-width buffer that was filled with spaces to start with, and use the MARS print-string syscall. (Repeated division / remainder by 10, like the C function in How do I print an integer in Assembly Level Programming without printf from the c library?)

(You'd probably want to right justify within each field so the 1's place is at the same column for each number.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847