Computer Organization and Design assignements
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

quiz3.s 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. .data
  2. v: .word 3, 10, 8, 2, 7, 1, 5, 9, 6, 4
  3. # swap(int i, int j) : swaps elements of v[]
  4. # at positions i and j
  5. # $a0 = i
  6. # $a1 = j
  7. # $s0 = v
  8. .text
  9. swap:
  10. # intro - stack-frame
  11. addi $sp, $sp, -28
  12. sw $ra, 24($sp)
  13. sw $fp, 20($sp)
  14. sw $s0, 16($sp) # v
  15. sw $t0, 12($sp) # temp
  16. sw $t1, 8($sp) # *v[i]
  17. sw $t2, 4($sp) # *v[j]
  18. sw $t3, 0($sp) # buffer
  19. addi $fp, $sp, 24
  20. move $t1, $s0 # pointer assignements
  21. sll $a0, $a0, 2
  22. add $t1, $t1, $a0
  23. move $t2, $s0
  24. sll $a1, $a1, 2
  25. add $t2, $t2, $a1
  26. lw $t0, 0($t1) # temp = v[i]
  27. lw $t3, 0($t2) # v[i] = v[j]
  28. sw $t3, 0($t1)
  29. sw $t0, 0($t2) # v[j] = temp
  30. # epilogue - stack-frame
  31. lw $t3, 0($sp)
  32. lw $t2, 4($sp)
  33. lw $t1, 8($sp)
  34. lw $t0, 12($sp)
  35. lw $s0, 16($sp)
  36. lw $fp, 20($sp)
  37. lw $ra, 24($sp)
  38. addi $sp, $sp, 28
  39. jr $ra
  40. # partition(int f, int l) : partitions elements in vector v[]
  41. # between positions f and l, using as pivot the element v[l], such
  42. # that
  43. # i < p ==> v[i] <= v[p]
  44. # i > p ==> v[p] < v[i]
  45. # The function returns the position p of the pivot.
  46. # $a0 = f
  47. # $a1 = l
  48. # $s0 = v
  49. # $v0 = p
  50. .text
  51. partition:
  52. # intro - stack-frame
  53. addi $sp, $sp, -40
  54. sw $ra, 36($sp)
  55. sw $fp, 32($sp)
  56. sw $s0, 28($sp) # v
  57. sw $s1, 24($sp) # pivot
  58. sw $s2, 20($sp) # i
  59. sw $s3, 16($sp) # j
  60. sw $s4, 12($sp) # a0 (f)
  61. sw $s5, 8($sp) # a1 (l)
  62. sw $t0, 4($sp)
  63. sw $t1, 0($sp)
  64. addi $fp, $sp, 36
  65. move $s4, $a0
  66. move $s5, $a1
  67. move $t0, $s0 # make *v[l]
  68. move $t1, $a1 # t1 = l
  69. sll $t1, $t1, 2 # t1 *= 4
  70. add $t0, $t0, $t1
  71. lw $s1, 0($t0) # pivot = v[l]
  72. move $s2, $s4 # i = f;
  73. # for () {
  74. move $s3, $s4 # j = f;
  75. _part_loop0:
  76. slt $t0, $s3, $s5 # j < l
  77. beq $t0, $0, _part_loop_end
  78. move $t0, $s0 # make *v[j]
  79. move $t1, $s3
  80. sll $t1, $t1, 2
  81. add $t0, $t0, $t1
  82. lw $t1, 0($t0) # t1 = v[j]
  83. # if (v[j] < pivot)
  84. slt $t0, $t1, $s1
  85. beq $t0, $0, _part_loop_else
  86. move $a0, $s2 # swap (i, j);
  87. move $a1, $s3
  88. jal swap
  89. addi $s2, 1 # i++
  90. _part_loop_else:
  91. addi $s3, 1 # j++
  92. j _part_loop0 # }
  93. _part_loop_end:
  94. move $a0, $s2 # swap (i, l)
  95. move $a1, $s5
  96. jal swap
  97. move $v0, $s2 #return i
  98. # epilogue - stack-frame
  99. lw $t1, 0($sp)
  100. lw $t0, 4($sp)
  101. lw $s5, 8($sp)
  102. lw $s4, 12($sp)
  103. lw $s3, 16($sp)
  104. lw $s2, 20($sp)
  105. lw $s1, 24($sp)
  106. lw $s0, 28($sp)
  107. lw $fp, 32($sp)
  108. lw $ra, 36($sp)
  109. addi $sp, $sp, 40
  110. jr $ra
  111. # qsort(int f, int l) : sorts the elements of v[]
  112. # between positions f:l
  113. # $a0 = f
  114. # $a1 = l
  115. # $s0 = v
  116. .text
  117. qsort: # qsort entry point
  118. # intro - stack-frame with 4byte local var @ fp
  119. addi $sp, $sp, -28 # qsort
  120. sw $ra, 24($sp)
  121. sw $fp, 20($sp)
  122. sw $s0, 16($sp) # v
  123. sw $s2, 12($sp) # a0 (f)
  124. sw $s3, 8($sp) # a1 (l)
  125. sw $t0, 4($sp)
  126. addi $fp, $sp, 24
  127. move $s2, $a0 # get f, l
  128. move $s3, $a1
  129. # if (f < l) {
  130. slt $t0, $s2, $s3
  131. beq $t0, $0, _qsort_end
  132. move $a0, $s2 # pivot = partition(f, l);
  133. move $a1, $s3
  134. jal partition
  135. sw $v0, 0($sp)
  136. move $a0, $s2 # qsort(f, pivot - 1);
  137. lw $t0, 0($sp)
  138. addi $t0, $t0, -1
  139. move $a1, $t0
  140. jal qsort
  141. lw $t0, 0($sp) # qsort(pivot + 1, l);
  142. addi $t0, $t0, 1
  143. move $a0, $t0
  144. move $a1, $s3
  145. jal qsort
  146. # }
  147. _qsort_end:
  148. # epilogue - stack-frame
  149. lw $t0, 4($sp)
  150. lw $s3, 8($sp)
  151. lw $s2, 12($sp)
  152. lw $s0, 16($sp)
  153. lw $fp, 20($sp)
  154. lw $ra, 24($sp)
  155. addi $sp, $sp, 28
  156. jr $ra # return to caller
  157. .text
  158. .globl main
  159. main:
  160. # intro - stack-frame
  161. addi $sp, $sp, -12 # main
  162. sw $ra, 8($sp)
  163. sw $fp, 4($sp)
  164. sw $s0, 0($sp)
  165. addi $fp, $sp, 8
  166. li $t0, 10
  167. li $a0, 0
  168. li $a1, 9
  169. la $s0, v
  170. jal qsort
  171. # epilogue - stack-frame
  172. lw $s0, 0($sp)
  173. lw $fp, 4($sp)
  174. lw $ra, 8($sp)
  175. addi $sp, $sp, 12
  176. jr $ra