所以我有一个赋值,我必须对动态分配的数组进行排序,并将冒泡排序写为内联汇编函数。麻烦的是我的汇编函数不适用于动态分配的数组。
int *array;
array=new int[N]; //N=number of elements
for(int i=0;i<N;i++)
{
//generate random numbers
}
N--;
__asm {
outer_loop:
xor edx, edx
lea esi, array
mov ecx, N
inner_loop:
mov eax, [esi]
mov ebx, [esi+4]
cmp eax, ebx
jae next_pair
mov [esi], ebx
mov [esi+4], eax
add edx, 1
next_pair:
add esi,4
dec ecx
jnz inner_loop
test edx, edx
jnz outer_loop
}
for(int t=0;t<5;t++)
{
cout<<array[t]<<" "; // it get's stuck here "Unhandled exception"
}
我不知道我做错了什么,因为我是NOOB而且我有点选择,所以是的。 不管怎样,谢谢
答案 0 :(得分:0)
我在masm32中测试了你的代码,因为你应该使用使用masm语法的Visual Studio,而我的编译器是GCC,你的冒泡排序不起作用,因为lea esi,数组替代了mov esi,数组也许它可以工作,因为我没有在VS中测试过,代码片段是英特尔语法,而AT&amp; T语法就在这里
void BubbleSort(int * array,int n){
n--;
__asm__ __volatile__(
"outter_loop:\n"
".intel_syntax noprefix\n" // use intel syntax
"xor edx, edx\n"
".att_syntax prefix\n" // back to at&t to get parameters
"movl %[p], %%esi\n" // mov esi, array
"movl %[n], %%ecx\n" // mov ecx, n
".intel_syntax noprefix\n"
"inner_loop:\n"
"mov eax, [esi]\n"
"mov ebx, [esi+4]\n"
"cmp eax, ebx\n"
"jae next_pair\n"
"mov [esi], ebx\n"
"mov [esi+4], eax\n"
"mov edx, 1\n"
"next_pair:\n"
"add esi, 4\n"
"dec ecx\n"
"jnz inner_loop\n"
"test edx, edx\n"
"jnz outter_loop\n"
".att_syntax prefix\n" // back to at&t again
:
:[p]"m"(array), [n]"m"(n)
: "%eax", "%ebx", "%ecx", "%edx", "%esi"); // clobbered registers
}
这个代码片段完美地工作,因为你想从最高到最低顺序制作。