我想弄清楚使用程序集對字符串數組進行排序。我比較第一個和第二個字母,然後按字母順序重新排列。我已經知道了,但是我的輸出卻錯誤地重新排列了一些字符。例如,打印'八'時,它會打印'唉'。氣泡排序裝配
.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;
}
如果它讓你考慮一個更好的算法,你是幸運的,這是行不通的。 – 2010-10-27 04:18:07