DannyelPerez23

Find min MIPS32

Feb 14th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #int find_min(void* base, int size, int itemSize, int (*compare)(void*, void*))
  2. #{
  3. # int i, minIndex;
  4. # void *ptr = base, *ptrMin = base;
  5. #
  6. # minIndex = 0;
  7. # for (i=0; i<size; i++) {
  8. #
  9. # if (compare(ptr, ptrMin) < 0) {
  10. # minIndex = i;
  11. # ptrMin = ptr;
  12. # }
  13. #
  14. # ptr += itemSize;
  15. # }
  16. #
  17. # return minIndex;
  18. #}
  19.  
  20.  
  21. find_min:
  22. #Prologo
  23. addi $sp, $sp, -8
  24. sw $ra, $sp
  25. sw $a1, 4($sp)
  26.  
  27. #init i and minIndex
  28. add $t0, $zero,$zero
  29. add $t1, $zero,$zero
  30.  
  31. #init ptr amd ptrMin eq base
  32. add $t2, $zero, $a0
  33. add $t3, $zero, $a0
  34.  
  35. for:
  36. lw $t4, 4($sp)
  37. slt $t4, $t0, $t4 #i<size
  38. beq $t4,$zero, end_for #$t0==0
  39. #if
  40. add $a0, $zero, $t2
  41. add $a1, $zero, $t3
  42. jal $a3
  43. slt $t4, $v0, $zero
  44. beq $t4, $zero, end_if
  45. if:
  46. add $t1, $zero, $t0 #minIndex = i;
  47. add $t3, $zero, $t2 #ptrMin = ptr;
  48. end_if:
  49. add $t2, $t2, $a3 #ptr += itemSize;
  50. j for
  51. end_for:
  52. add $v0, $zero, $t1
  53. lw $ra, ($sp)
  54. addi $sp, $sp, 8
  55. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment