Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. # Print the values of an array using a for loop.
  2.  
  3. .data
  4. numElements:
  5. .word 7
  6.  
  7. elements:
  8. .word 55
  9. .word 66
  10. .word 77
  11. .word 0
  12. .word -16
  13. .word -19
  14. .word 82
  15.  
  16. newline:
  17. .asciiz "\n"
  18.  
  19. .text
  20.  
  21. main:
  22. # Function prologue -- even main has one
  23. subu $sp, $sp, 24 # allocate stack space -- default of 24 here
  24. sw $fp, 0($sp) # save caller's frame pointer
  25. sw $ra, 4($sp) # save return address
  26. addiu $fp, $sp, 20 # setup main's frame pointer
  27.  
  28. # for ( i = 0; i < numElements; i++ )
  29. # print elements[i]
  30.  
  31. addi $s1, $zero, 0 # i = 0
  32.  
  33. la $t0, numElements
  34. lw $s2, 0($t0) # $s2 = numElements
  35.  
  36. la $t0, elements # $t0 = address of elements[0]
  37.  
  38. loopBegin:
  39. # test if for loop is done
  40. slt $t1, $s1, $s2 # $t1 = i < numElements
  41. beq $t1, $zero, loopEnd
  42.  
  43. # Compute address of elements[i]
  44. add $t1, $s1, $s1
  45. add $t1, $t1, $t1 # $t1 = 4 * i
  46. add $t2, $t0, $t1 # $t2 = address of elements[i]
  47. lw $a0, 0($t2) # $a0 = elements[i]
  48. addi $v0, $zero, 1
  49. syscall
  50.  
  51. # print newline
  52. la $a0, newline
  53. addi $v0, $zero, 4
  54. syscall
  55.  
  56. addi $s1, $s1, 1 # i++
  57. j loopBegin
  58.  
  59. loopEnd:
  60.  
  61. done: # Epilogue for main -- restore stack & frame pointers and return
  62. lw $ra, 4($sp) # get return address from stack
  63. lw $fp, 0($sp) # restore the caller's frame pointer
  64. addiu $sp, $sp, 24 # restore the caller's stack pointer
  65. jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement