Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. .data
  2. .align 2 # Let's make sure that it's aligned
  3.  
  4. Z: .word 10, 11, 4, 10, 24, 1, 16, 17, 18, 19, 10
  5.  
  6. X: .word 0
  7. # above we allocated and initialized
  8. # twenty elements of array A
  9. .space 320 # here we allocated space here for...
  10. # ...how many more elements?
  11.  
  12. message2: .asciiz "Enter k value above 0 and less than 9\nK = " # string to print
  13. message3: .asciiz "\nM = " # string to print
  14. message4: .asciiz "\nTotal = " # string to print
  15.  
  16. .text
  17.  
  18. .globl main
  19.  
  20. main: # main has to be a global label
  21.  
  22. addu $s7, $0, $ra # save the return address
  23. # in a global register
  24.  
  25. #la $s2, X # set h to 40 for the examples
  26. #la $s3, Z # $s3 has the starting address of A
  27.  
  28. .text
  29. li $v0, 4 # print_str (system call 4)
  30. la $a0, message2 # takes the address of string as an argument
  31. syscall
  32.  
  33. li $v0, 5 # read_int (system call 5)
  34. syscall
  35.  
  36. # STORE VALUE
  37. add $s1, $0, $v0 # s1 = k
  38.  
  39. li $v0, 4 # print_str (system call 4)
  40. la $a0, message3 # takes the address of string as an argument
  41. syscall
  42.  
  43. li $v0, 5 # read_int (system call 5)
  44. syscall
  45.  
  46. # STORE VALUE
  47. add $s4, $0, $v0 # s4 = m
  48.  
  49. # LOOPS
  50. la $a0, Z # $a0 has the starting address of Z
  51. move $t0, $zero # i = 0
  52.  
  53. #la $s5, Z # $s5 has the starting address of Z
  54.  
  55. loop1:
  56. add $t1,$t0,$t0 # $t1 = 2i
  57. add $t1,$t1,$t1 # $t1 = 4i
  58. add $s5,$a0,$t1 # $s5 = array[i]
  59.  
  60. addi $t0,$t0,1 # i = i + 1
  61. slt $t3, $t0, $s1 # $t3 = (i < size0)
  62. bne $t3, $zero, loop1
  63.  
  64. lw $t4, 0($s5) #t0 = Z[k]
  65. #la $s5, Z # $s5 has the starting address of Z
  66.  
  67. add $s4, $s4, $s1 # s4 = m + k
  68.  
  69. loop2:
  70. add $t1,$t0,$t0 # $t1 = 2i
  71. add $t1,$t1,$t1 # $t1 = 4i
  72. add $s5,$a0,$t1 # $s5 = array[i]
  73.  
  74. addi $t0,$t0,1 # i = i + 1
  75. slt $t3, $t0, $s4 # $t3 = (i < size0)
  76. bne $t3, $zero, loop2
  77.  
  78. lw $t0, 0($s5) # t0 = Z[m+k]
  79.  
  80. add $t0, $t0, $t4 # t0 = Z[k] + Z[m+k]
  81.  
  82. li $v0, 4 # print_str (system call 4)
  83. la $a0, message4 # message
  84. syscall
  85.  
  86. li $v0, 1 # print_int (system call 1)
  87. add $a0, $0, $t0 # put value g in $a0 for printing
  88. syscall
  89.  
  90.  
  91.  
  92.  
  93. #--------------------------------------Usual stuff at the end of the main
  94. addu $ra, $0, $s7 # restore the return address
  95. jr $ra # return to the main program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement