Silver_Smoulder

[ASM}[Func]2d Array w/Formatting

May 16th, 2019
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #using .word or .byte define a 2-D matrix
  2. #start assuming row-wise or column-wise and print .word
  3.  
  4. #1 2 3 4 5 6 7 8 9 10 11 12
  5. #as
  6. #1 2 3 4
  7. #5 6 7 8
  8. #9 10 11 12
  9.  
  10. .data
  11.  
  12. array: .byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
  13.  
  14.  
  15.  
  16. newline:
  17. .asciiz "\n"
  18. spacing_single:
  19. .asciiz " "
  20. spacing_double:
  21. .asciiz " "
  22.  
  23. .globl main
  24. .text
  25.  
  26. main:
  27. #load all the arrays at once
  28. la $s0, array
  29.  
  30. #t0 is going to be our counter - when it hits 4, we newline
  31. li $t0, 0
  32.  
  33.  
  34. process:
  35. lb $a0, ($s0) #load the value of the pointer at the array
  36. li $v0, 1 #output the integer
  37. syscall #output the number inputted from the array
  38. j format
  39.  
  40. process_2:
  41. addi $s0, $s0, 1 #advance to the next element in the array
  42. addi $t0, $t0, 1 #increment the newline counter
  43. addi $t1, $t1, 1 #increment our exit counter - when this hits 12, we should be done
  44. beq $t1, 12, exit #if we reach the end of the array, exit
  45. beq $t0, 4, new_line #when t0 is equal to 4, jump to the new_line label
  46. j process #if it isn't, go back to the process and redo it
  47.  
  48. format:
  49. move $t2, $a0
  50. div $t3, $t2, 10 #if the number is smaller than 10 (aka a single-digit)
  51. beqz $t3, double_digit_spacing #if it's a single digit number, we need to put 2 spaces
  52. li $v0, 4 #if it isn't (a double-digit number) then we only need one space
  53. la $a0, spacing_single
  54. syscall
  55. move $a0, $t2
  56. j process_2
  57.  
  58. double_digit_spacing:
  59. li $v0, 4
  60. la $a0, spacing_double
  61. syscall
  62. move $a0, $t2
  63. j process_2
  64.  
  65. new_line:
  66. li $v0, 4
  67. la $a0, newline
  68. syscall #print a new line
  69. li $t0, 0 #reset our counter to 1
  70. j process #go back to the process
  71.  
  72.  
  73. exit:
  74. #end the program
  75. li $v0, 10
  76. syscall
Advertisement
Add Comment
Please, Sign In to add comment