我试图用汇编语言编写函数排序。它将对二维数组进行排序,使得行现在将按照字母顺序包含数据。 我尝试了很多东西,但它实际上超出了我目前的知识范围。 这里是我试过到目前为止...对程序集中的字符串进行排序
.386
public _Sort
.model flat
.code
_Sort proc
push ebp
mov ebp, esp
push esi
push edi
mov edi, [esp + 4] ; address of destination array
mov esi, [esp + 8] ; address of source array
mov ecx, [esp + 16] ; # of elements to mov
cld
rep movsd
L1:
mov eax, [esi]
cmp [esi + 8], eax
jg L2
xchg eax, [esi + 8]
mov [esi], eax
L2:
pop edi
pop esi
pop ebp
ret
_Sort endp
end
这里的C++代码...
#include <iostream>
using namespace std;
extern "C" int Sort (char [] [20], int, int);
void main()
{
char Strings [10] [20]
= { "One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten" };
int i;
cout << "Unsorted Strings are" << endl;
for (i = 0; i < 10; i++)
cout << '\t' << Strings [i] << endl;
Sort (Strings, 10, 20);
cout << "Sorted Strings are" << endl;
for (i = 0; i < 10; i++)
cout << '\t' << Strings [i] << endl;
}
我不知道我的组装没有任何意义,我们对此深感抱歉。
'lodsb'已经增加了'esi'。使用'movzx eax,[esi]'来提高效率('lodsb'在Intel CPU上是3 uops,与'inc' /'movzx'不相称)。还可以使用'cmp al,[edi]'代替sub,所以它可以在更多的CPU上与'jz'进行宏观融合。 – 2017-11-26 10:57:45
此外,如果它们等于终止'0'>,它将超过字符串的末尾。< – 2017-11-26 10:58:34
显然,为了提高效率,您应该比较双字或qword(或更好地使用SSE2)。使用SSE2,可以很容易地并行检查每个字节是否为'0',但对于普通整数代码,您可以使用https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord。 (如果两个隐含长度的字符串相互错位,那么更大的负载会变得非常棘手。[这并不像使用'strlen'进行对齐加载那样简单](https://stackoverflow.com/questions/37800739/它是安全可读的 - 过去 - 在同一页上的一个缓冲区在x86和x64)。) – 2017-11-26 11:03:43