Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. .data
  2.  
  3. array: .word 0:20
  4.  
  5. endline: .asciiz "\n"
  6. firstnumber: .asciiz "Give me the first number of the sequence"
  7. step: .asciiz "Give me the step of this sequence"
  8. space: .asciiz " "
  9. sum: .asciiz "The sum of the 20 first terms of the sequence is:"
  10.  
  11.  
  12. .text
  13.  
  14. main:
  15.  
  16. la $t0, array #we are gonna use $t0 as a pointer
  17.  
  18.  
  19. la $a0, firstnumber
  20. li $v0, 4
  21. syscall #Print firstnumber message
  22.  
  23. li $v0, 5 #Get First integer from keyboard
  24. syscall
  25. sw $a0, ($t0) #saves the first integer to the first array Cell
  26.  
  27.  
  28.  
  29. la $a0, step
  30. li $v0, 4
  31. syscall #Print step message
  32.  
  33.  
  34. li $v1, 5 #Get step from keyboard
  35. syscall
  36. move $t1 , $v1
  37.  
  38.  
  39. li $t2 , 0 #counter for array iteration
  40. li $t3 , 20 #limit for array iteration
  41. li $t5, 0 #initialize sum of sequence
  42.  
  43. firstloop:
  44.  
  45. blt $t2,$t3,secondloop #if counter is out of bounds, do to second loop
  46.  
  47. lw $t4, array + $t2 #load number from iterator's position
  48.  
  49. add $t2,$t2,4 #ready to iterate to the next array cell
  50.  
  51. add $t4, $t4, $t1 #calculate next sequence number
  52.  
  53. sw $t4, array + $t2 #store next number to the next array cell
  54.  
  55. j firstloop
  56.  
  57.  
  58.  
  59. secondloop:
  60.  
  61. bltz $t2, result
  62.  
  63. sub $t2, $t2, 4
  64.  
  65. lw $t4, array + $t2
  66.  
  67. move $a0, $t4
  68. li $v0, 1
  69.  
  70. add $t5, $t5, $t4
  71.  
  72. j secondloop
  73.  
  74. result:
  75.  
  76. li $v0, 10 #exit system call
  77. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement