我用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