我是第一次玩 x86 汇编,我不知道如何对数组进行排序(通过插入排序)。我理解算法,但汇编让我感到困惑,因为我主要使用 Java 和 C++ .这是我目前所拥有的一切
int ascending_sort( char arrayOfLetters[], int arraySize )
{
char temp;
__asm{
push eax
push ebx
push ecx
push edx
push esi
push edi
//// ???
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
}
}
基本上没有 :( 有什么想法吗?提前致谢。
好吧,这只会让我听起来像个彻头彻尾的白痴,但我什至无法更改 _asm 中数组的任何值
为了测试它,我把:
mov temp, 'X'
mov al, temp
mov arrayOfLetters[0], temp
这给了我一个错误 C2415:不正确的操作数类型
所以我尝试了:
mov temp, 'X'
mov al, temp
mov BYTE PTR arrayOfLetters[0], al
这符合要求,但它并没有改变数组......
最佳答案
此代码现在已经过测试。我用记事本写的,它没有一个很好的调试器,超出了我的头脑。然而,它应该是一个很好的起点:
mov edx, 1 // outer loop counter
outer_loop: // start of outer loop
cmp edx, length // compare edx to the length of the array
jge end_outer // exit the loop if edx >= length of array
movzx eax, BYTE PTR arrayOfLetters[edx] // get the next byte in the array
mov ecx, edx // inner loop counter
sub ecx, 1
inner_loop: // start of inner loop
cmp eax, BYTE PTR arrayOfLetters[ecx] // compare the current byte to the next one
jg end_inner // if it's greater, no need to sort
add ecx, 1 // If it's not greater, swap this byte
movzx ebx, BYTE PTR arrayOfLetters[ecx] // with the next one in the array
sub ecx, 1
mov BYTE PTR arrayOfLetters[ecx], bl
sub ecx, 1 // loop backwards in the array
jnz inner_loop // while the counter is not zero
end_inner: // end of the inner loop
add ecx, 1 // store the current value
mov BYTE PTR arrayOfLetters[ecx], al // in the sorted position in the array
add edx, 1 // advance to the next byte in the array
jmp outer_loop // loop
end_outer: // end of outer loop
如果您对 DWORD 值(整数)而不是 BYTE 值(字符)进行排序,这会容易得多。
关于c++ - 通过 x86 程序集(嵌入在 C++ 中)对数组进行排序?可能的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2643341/