Вставка сортировка ассемблерного кода (перевод с языка Си)


У меня нет глубоких знаний ассемблерного программирования.

Я пытаюсь перевести следующий код с чистого C на язык ассемблера.

Pure-C

int i,j,temp;
for ( i = 0 ; i < 10 ; i++ )
{
    temp = global_array[i];
    for ( j = i-1 ; j >= 0 ; j-- )
    {
        if ( global_array[j] <= temp ) break; //if first value is bigger than the second value
            global_array[j+1] = global_array[j];
    }
    global_array[j+1]=temp;
}

пожалуйста, проверьте, насколько хорошо я перевел с чистого Си на ассемблер?

ASM

.globl _Sort
//.type AtoI,@function

//Declaration of variables
.comm _global_array,40 //this is an array of integers
.comm _temp,4 //this is the variable temp
.comm _i,4 //used for loop
.comm _j,4 //used for loop



_Sort:
/*prolog*/
pushl %ebp
pushl %ebx
movl %esp, %ebp //move the special ptr to base ptr
subl $8, %esp //allocate space for the variables local_var and temp_var

pushl %esi //index register
pushl %edi //index register


/*put code here*/
//for ( i = 0 ; i < 10 ; i++ )
//first for loop
movl $0, _i //index i = 0

ForOne:
movl _i, %eax
movl _i, %esi //move i into index register
cmp $10, %eax //if i is less than or equal 9, continue this loop
jge return
movl _global_array, %ebx
movl (%ebx,%esi,4), _%ecx
movl %ecx, _temp //temp = global_array[i]
movl _i, %eax //i-1
subl $1, %eax
movl %eax, _j //move j into index register
jmp ForTwo //jump to second for loop

ForTwo:
movl _j, %eax
cmp $0, %eax //j >= 0
jl ForOneFn //get out of 2nd loop to end of 1st loop
movl _global_array, %ebx
cmp _temp, (%ebx,%edi,4) //if ( global_array[j] <= temp ) break;
jle ForOneFn //jump to end of first loop after break
movl (%ebx,%edi,4),%ecx
addl $1,%eax //j+1
movl _j, %edi //move j into index register 
movl _global_array, %ebx
movl %ecx, (%ebx,%edi,4) //global_array[j+1] = global_array[j];

//last line of code is jump to reloop/finish 2nd loop
jmp ForTwoFn

ForTwoFn:
subl $1, _j
jmp ForTwo

ForOneFn:
addl $1, _i
jmp ForOne


return:
/*epilog*/
movl %ebp,%esp
popl %ebx
popl %ebp
ret
1 2

1 ответ:

Первый совет: используйте регистры более интенсивно.

У вас есть 2 индексные переменные, временная переменная и адрес global_array, поэтому у вас достаточно регистров.

Код должен компилироваться, если строка:

cmp _temp, (%ebx,%edi,4) //if ( global_array[j] <= temp ) break;

Будет заменено на:

cmp eax, (%ebx,%edi,4) //if ( global_array[j] <= temp ) break;, where eax is yours temp

Если вы кодируете эту процедуру, используя только регистры, она будет меньше и легче для понимания и отладки.