【发布时间】:2015-09-19 09:41:19
【问题描述】:
项目是在汇编中创建一个冒泡排序算法,它将对给定的整数列表进行排序。我已经下降并且我的输出在某种程度上是正确的。似乎在组合数字顺序时会混淆,这就是我的意思:
10 -20 5 12 30 -5 -22 55 52 0
整数个数 = 10 Ascend_or_Descend = 1
0 5 10 12 30 52 55 -22 -20 -5
这是包含用于测试的主要方法的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
int sorter (int* list, int count, int opcode)
{
__asm
{
mov eax, 0 ; zero out the result
mov ebx, opcode ; move opcode to ebx for comparison
mov ecx, count; //OuterLoop counter
cmp ecx, 1; //check length
jle Return;
OuterLoop:
mov edi, list; //move in list
push ecx;
mov ecx, count;
dec ecx; //Decrement inner loop
InnerLoop:
mov eax, [edi]; //First Value in list
cmp ebx, 1;
je Ascending;
cmp eax, [edi+4]; //next int is 4 bits away
jb Swap;
jmp Continue;
Ascending:
cmp eax, [edi+4]; //Again compare with next int
ja Swap; //if no swap
jmp Continue;
Swap:
mov edx, [edi+4]; //Move to temp register
mov [edi+4], eax; //Move stored value there
mov [edi], edx; //Return temp value to list
Continue:
add edi, 4; //Increment to move to next int
loop InnerLoop;
pop ecx; //reset counter
loop OuterLoop;
Return:;
}
}
int main(int argc, char** argv)
{
int numlist[1000], asc;
FILE *fptr;
int i = 0;
if (argc != 3)
{
printf("Usage: %s filename asc_des\n", argv[0]);
return 1;
}
asc = atoi (argv[2]);
asc = (asc == 1) ? 1 : 2;
printf("\n");
fptr = fopen((argv[1]), "rtc");
if (fptr == NULL)
printf( "File %s was not opened\n",argv[1] );
else
{
/* Set pointer to beginning of file: */
fseek( fptr, 0L, SEEK_SET );
/* Read data from file: */
while ( fscanf(fptr, "%d", &numlist[i]) != EOF )
{
printf( "%d ", numlist[i] );
i++;
}
printf( "\n\nNumber of integer = %d\n", i );
printf( "Ascend_or_Descend = %d\n\n", asc );
fclose( fptr );
}
sorter( numlist, i, asc);
for (int j = 0; j < i; j++)
printf( "%d ", numlist[j] );
printf("\n");
return 0;
}
【问题讨论】:
标签: sorting assembly bubble-sort