【发布时间】:2010-10-27 04:01:54
【问题描述】:
我正在尝试使用汇编对字符串数组进行排序。我比较了第一个和第二个字母,然后按字母顺序重新排列它们。我几乎想通了,但是我的输出错误地重新排列了某些字符。例如,当打印 '8' 时,它只会打印 'eigh'。
.386
public _Sort
.model flat
.code
_Sort proc
push ebp
mov ebp, esp
push esi
push edi
mov ecx, 10
mov eax, 1
dec ecx
L1:
push ecx
mov esi, [ebp+8]
L2:
mov al, [esi]
cmp [esi + 20], al
jg L3
mov eax, [esi]
xchg eax, [esi + 20]
mov [esi], eax
L3:
add esi, 20
loop L2
pop ecx
loop L1
L4:
pop edi
pop esi
pop ebp
ret
_Sort endp
end
#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;
}
【问题讨论】:
-
如果它让你考虑更好的算法,那你很幸运,它不起作用。
标签: sorting assembly bubble-sort