Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. # Example for ECE 152 (thanks to Prof. Alvin Lebeck)
  2. # Program to add together list of 9 numbers
  3. .text # Code
  4. .align 2
  5. .globl main
  6. main: # MAIN procedure Entrance
  7. subu $sp, 40 #\ Push the stack
  8. sw $ra, 36($sp) # \ Save return address
  9. sw $s3, 32($sp) # \
  10. sw $s2, 28($sp) # > Entry Housekeeping
  11. sw $s1, 24($sp) # / save registers on stack
  12. sw $s0, 20($sp) # /
  13. move $v0, $0 #/ initialize exit code to 0
  14. move $s1, $0 #\
  15. la $s0, list # \ Initialization
  16. la $s2, msg # /
  17. la $s3, list+36 #/
  18. # Main code segment
  19.  
  20. again: # Begin main loop
  21. lw $t6, 0($s0) #\
  22. addu $s1, $s1, $t6 #/ Actual work
  23. # SPIM I/O
  24. li $v0, 4 #\
  25. move $a0, $s2 # > Print a string
  26. syscall #/
  27. li $v0, 1 #\
  28. move $a0, $s1 # > Print a number
  29. syscall #/
  30. li $v0, 4 #\
  31. la $a0, nln # > Print a string (eol)
  32. syscall #/
  33.  
  34. addu $s0, $s0, 4 #\ index update and
  35. bne $s0, $s3, again #/ end of loop
  36.  
  37.  
  38. # Exit Code
  39.  
  40. move $v0, $0 #\
  41. lw $s0, 20($sp) # \
  42. lw $s1, 24($sp) # \
  43. lw $s2, 28($sp) # \ Closing Housekeeping
  44. lw $s3, 32($sp) # / restore registers
  45. lw $ra, 36($sp) # / load return address
  46. addu $sp, 40 # / Pop the stack
  47. jr $ra #/ exit(0) ;
  48. .end main # end of program
  49.  
  50. # Data Segment
  51.  
  52. .data # Start of data segment
  53. list: .word 35, 16, 42, 19, 55, 91, 24, 61, 53
  54. msg: .asciiz "The sum is "
  55. nln: .asciiz "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement