Перевод с C на MIPS: пузырьковая сортировка - PullRequest
0 голосов
/ 03 марта 2019

Мне дали код на C и попросили перевести его на MIPS.

##struct Ingredient {
##  unsigned ing_type;
##  unsigned amount;
##};
##
##struct Request {
##  unsigned length;
##  Ingredient ingredients [11];
##};
##
##//Performs a bubble sort on the given request using the given comparison function
##void bubble_sort(Request* request, int (*cmp_func) (Ingredient*, Ingredient*)) {
##    for (int i = 0; i < request->length; ++i) {
##        for (int j = 0; j < request->length - i - 1; ++j) {
##            if (cmp_func(&request->ingredients[j], &request->ingredients[j + 1]) > 0) {
##                Ingredient temp = request->ingredients[j];
##                request->ingredients[j] = request->ingredients[j + 1];
##                request->ingredients[j + 1] = temp;
##            }
##        }
##    }
##}

Это то, что у меня есть, но я борюсь с использованием int (* cmp_func).Я знаю, что это указатель на функцию компаратора, но как сделать цикл?

    li $t0, 0            # set i = 0
    li $t1, 0            # set j = 0
    add $t2, $a0, 0  # request->length

for1:
    beq $t0, $t2, end    # if i >= request->length, go to between_loops
    li $t1, 0                    # reset j to 0 after each iteration
    addi $t0, $t0, 1         # ++i
    j for2

for2:
    sub $t3, $t2, $t0           # t3 = request->length - i
    sub $t3, $t3, 1             # t3 = request->length - i - 1
    beq $t1, $t3, for1    # if j >= request->length - i - 1, go to for1

    # if statement goes here

    addi $t1, $t1, 1         # ++j
    j for2
...