Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #int find_min(void* base, int size, int itemSize, int (*compare)(void*, void*))
- #{
- # int i, minIndex;
- # void *ptr = base, *ptrMin = base;
- #
- # minIndex = 0;
- # for (i=0; i<size; i++) {
- #
- # if (compare(ptr, ptrMin) < 0) {
- # minIndex = i;
- # ptrMin = ptr;
- # }
- #
- # ptr += itemSize;
- # }
- #
- # return minIndex;
- #}
- find_min:
- #Prologo
- addi $sp, $sp, -8
- sw $ra, $sp
- sw $a1, 4($sp)
- #init i and minIndex
- add $t0, $zero,$zero
- add $t1, $zero,$zero
- #init ptr amd ptrMin eq base
- add $t2, $zero, $a0
- add $t3, $zero, $a0
- for:
- lw $t4, 4($sp)
- slt $t4, $t0, $t4 #i<size
- beq $t4,$zero, end_for #$t0==0
- #if
- add $a0, $zero, $t2
- add $a1, $zero, $t3
- jal $a3
- slt $t4, $v0, $zero
- beq $t4, $zero, end_if
- if:
- add $t1, $zero, $t0 #minIndex = i;
- add $t3, $zero, $t2 #ptrMin = ptr;
- end_if:
- add $t2, $t2, $a3 #ptr += itemSize;
- j for
- end_for:
- add $v0, $zero, $t1
- lw $ra, ($sp)
- addi $sp, $sp, 8
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment