Converting a sorting program from C to Assembly

I have to convert a sorting program which is written in C to assembly (Mips 32). I tried to do so but I doesn't work.

Here is the C code :

#include <stdio.h>

int tab[10] = {3, 33, 49, 4, 23, 12, 46, 21, 48, 2};

void display(int * tab, int size) 
{
    int i;
    for (i = 0; i < size; i++) 
    {
        printf("%d", tab[i]);
        printf("\n");
    }
}

void swap(int * tab, int i, int j) 
{
    int tmp;
    tmp = tab[i];
    tab[i] = tab[j];
    tab[j] = tmp;
}

void sort(int * tab, int size) 
{
    int valmax;
    int i, imax;

    if (size < 2) 
    {
        return;
    }

    valmax = 0;
    for (i = 0; i < size; i++) 
    {
        if (tab[i] > valmax) 
        {
            valmax = tab[i];
            imax = i;
        }
    }
    swap(tab, imax, size - 1);
    sort(tab, size - 1);
}

int main(void) 
{
    int size;
    size = 10;
    display(tab, size);
    sort(tab, size);
    display(tab, size);
    return 0;
}

And what I've already written :

.data
tab: .word 3,33,49,4,23,12,46,21,48,2
return: .asciiz "\n"
str: .asciiz "-----------\n"

.text
#--main--#
#---#
main:
addiu $29, $29, -16
sw $31, 12($29)

#---#
la $4, str
ori $2, $0, 4
syscall

la $4, tab #tab[]
ori $5, $0, 10 #size

jal display
jal sort

la $4, return
ori $2, $0, 4
syscall

la $4, tab #tab[]

jal display

ori $2, $0, 10
syscall

#---#
lw $31, 12($29)
addiu $29, $29, 16
jr $31



##display(int tab[], int size)##
#---#
display:
addiu $29, $29, -8
sw $31, 4($29)
sw $4, 8($29)
sw $5, 12($29)

#---#
li $8, 0
sw $8, 0($29) #i
loop1:
lw $5, 12($29)
beq $5, $8, endloop1
  lw $4, 8($29)
  sll $8, $8, 2 #i *= 4
  addu $8, $8, $4

  lw $4,($8)
  ori $2, $0, 1
  syscall

  la $4, return
  ori $2, $0, 4
  syscall

  lw $8, 0($29)
  addi $8, $8, 1
  sw $8, 0($29)
  j loop1

endloop1:

#---#
lw $31, 4($29)
lw $4, 8($29)
lw $5, 12($29)
addiu $29, $29, 8
jr $31



##swap(int tab[], int i, int j)##
#---#
swap:
addiu $29, $29, -8
sw $31, 4($29)
sw $4, 8($29)
sw $5, 12($29)
sw $6, 16($29)

#---#
sll $8, $5, 2 #i*4
add $8, $4, $8 #@tab[i]
lw $9, ($8) #tab[î]
sll $10, $6,  2 #j*4
add $10, $4, $10 #@tab[j]
lw $11, ($10) #tab[j]
sw $9, ($10)
sw $11, ($8)


#---#
lw $31, 4($29)
addiu $29, $29, 8
jr $31



##sort(int tab[], int size)##
#---#
sort:
addiu $29, $29, -36
sw $31, 32($29)
sw $16,  28($29)
sw $17, 24($29)
sw $18, 20($29)
sw $19, 16($29)
sw $4, 36($29)
sw $5, 40($29)
sw $6, 44($29)

#---#
slti $11, $5, 2
bne $11, $0, endif1

ori $17, $0, 0 #i

loop2:
beq $17, $5, endloop2

  sll $8, $17, 2 #4*i
  add $8, $4, $8 #@tab[i]
  lw $18,($8) #tab[i]

  ori $16, $0, 0 #valmax

  slt $11, $16, $18
  bne $11, $0, endif2

  ori $16, $18, 0
  ori $19, $17, 0 #imax

  endif2:
  addi $17, $17, 1
  j loop2

endloop2:
#swap+recursion
addi $6, $5, -1 #size-1
ori $5, $19, 0 #imax
jal swap

ori $5, $6, 0 #size-1
jal tri

#---#
endif1:
lw $31, 32($29)
lw $16, 28($29)
lw $17, 24($29)
lw $18, 20($29)
lw $19, 16($29)
lw $4, 36($29)
lw $5, 40($29)
lw $6, 44($29)
addiu $29, $29, 36
jr $31

But it doesn't work : the array is not correctly sorted. I guess that the problem is in the recursion, but I've not found. Could you help me to fix it please ?

Thanks :) xDra

1 answer

  • answered 2018-10-11 19:46 Kyrill

    For reference you can try compiling your program and inspecting the generated assembly to see what the flow looks like. There are even online compilers these days that are handy for this kind of experimentation. Have a look at https://godbolt.org/ for example.