该程序的目标是通过Visual Studio中的C中的内联汇编在X86程序集中进行插入排序。问题是它只会交换数组中的前两个数字。如果有人可以提供帮助,那么代码会很感激吗?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 20
// Function prototypes //
int isort(char* data, int size);
static void print_data(const char* array, int size);
// Global variables //
char* iarray;
int main()
{
//iarray = malloc(SIZE); // Allocate memory for the array //
int iarray[100] = { 8,5, 12, 10, 56, 22, 98, 120, 90, 4, 349, 8, 45, 37, 43, 67, 3, 18, 97, 71 };
// Load the integer array with data to sort //
/*iarray[0] = 5;
iarray[1] = 8;
iarray[2] = 12;
iarray[3] = 10;
iarray[4] = 56;
iarray[5] = 22;
iarray[6] = 98;
iarray[7] = 120;
iarray[8] = 90;
iarray[9] = 4;
iarray[10] = 349;
iarray[11] = 8;
iarray[12] = 45;
iarray[13] = 37;
iarray[14] = 43;
iarray[15] = 67;
iarray[16] = 3;
iarray[17] = 18;
iarray[18] = 97;
iarray[19] = 17;*/
//print_data(iarray, SIZE); // Display the unsorted array //
isort(iarray, SIZE); // Sort the array //
//print_data(iarray, SIZE); // Display the sorted array //
getchar();
free(iarray); // Free memory //
return EXIT_SUCCESS;
}
//*****************************************************************************//
// isort //
//*****************************************************************************//
int isort(int* data, int arr_size)
{
arr_size = arr_size - 1;
__asm
{
mov esi, 0
mov eax, data
mov edx, 0
aloop:
mov ebx, [eax+edx]
mov ecx, [eax+edx+4]
mov [eax+edx+4], ebx
mov[eax+edx], ecx
cmp esi, 12
add esi,1
add edx,4
jae loopend
jmp aloop
loopend:
}
int y = 0;
for (int z = 0; z < SIZE; z++) {
y = data[z];
printf("%d", y);
printf(" ");
}
}
static void print_data(int array, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d", iarray[i]);
printf(" ");
}
printf("\n");
return;
}
执行时打印:5 8 12 10 56 22 98 120 90 4 349 8 45 37 43 67 3 18 97 71 从阵列:8,5,12,10,56,22,98,120,90,4,349,8,45,37,43,67,3,18,97,71