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.

mips1.asm 4.1 KiB

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