Silver_Smoulder

[ASM][Func]2D Array

May 14th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 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.  
  19. .globl main
  20. .text
  21.  
  22. main:
  23. #load all the arrays at once
  24. la $s0, array
  25.  
  26. #t0 is going to be our counter - when it hits 4, we newline
  27. li $t0, 0
  28. li $t2, 0
  29. #input the array into t1
  30.  
  31. process:
  32. lb $a0, ($s0) #load the value of the pointer at the array
  33. li $v0, 1 #output the integer
  34. syscall #output the number inputted from the array
  35. addi $s0, $s0, 1 #advance to the next element in the array
  36. addi $t0, $t0, 1 #increment the counter
  37. addi $t1, $t1, 1 #increment our exit counter - when this hits 12, we should be done
  38. beq $t1, 12, exit #if we reach the end of the array, exit
  39. beq $t0, 4, new_line #when t0 is equal to 4, jump to the new_line label
  40. j process #if it isn't, go back to the process and redo it
  41.  
  42. new_line:
  43. li $v0, 4
  44. la $a0, newline
  45. syscall #print a new line
  46. li $t0, 0 #reset our counter to 1
  47. j process #go back to the process
  48.  
  49.  
  50. exit:
  51. #end the program
  52. li $v0, 10
  53. syscall
Advertisement
Add Comment
Please, Sign In to add comment