在汇编中排序整数
Sorting Integers in Assembly
项目是在程序集中创建一个冒泡排序算法,该算法将对给定的整数列表进行排序。我已经下降了,我的输出在某种程度上是正确的。似乎在组合数字顺序时混淆了,这就是我的意思:
10 -20 5 12 30 -5 -22 55 52 0
Number of integer = 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;
}
来自英特尔的手册:
The terms “above” and “below” are associated with the CF
flag and refer to the relation between two unsigned
integer values. The terms “greater” and “less” are associated with the SF
and OF
flags and refer to the relation
between two signed integer values
通过使用 jb
和 ja
,您将比较结果视为无符号比较的结果,因此有符号数字最终出现在排序数组的末尾(例如 -22 解释为无符号 32 位值时为 4294967274).
您应该使用 jl
而不是 jb
,并使用 jg
而不是 ja
。
项目是在程序集中创建一个冒泡排序算法,该算法将对给定的整数列表进行排序。我已经下降了,我的输出在某种程度上是正确的。似乎在组合数字顺序时混淆了,这就是我的意思:
10 -20 5 12 30 -5 -22 55 52 0
Number of integer = 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;
}
来自英特尔的手册:
The terms “above” and “below” are associated with the
CF
flag and refer to the relation between two unsigned integer values. The terms “greater” and “less” are associated with theSF
andOF
flags and refer to the relation between two signed integer values
通过使用 jb
和 ja
,您将比较结果视为无符号比较的结果,因此有符号数字最终出现在排序数组的末尾(例如 -22 解释为无符号 32 位值时为 4294967274).
您应该使用 jl
而不是 jb
,并使用 jg
而不是 ja
。