通过x86汇编排序数组(嵌入在C++中)?? 可能?

VV.*_*VV. 5 c++ x86 assembly insertion-sort

我第一次玩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
 }
}
Run Code Online (Sandbox Code Playgroud)

基本上没什么:(任何想法?提前谢谢.

好吧,这只会让我听起来像一个完全白痴,但我甚至无法改变_asm中任何数组的值

为了测试它,我把:

mov temp, 'X'
mov al, temp
mov arrayOfLetters[0], temp
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误C2415:不正确的操作数类型

所以我试过了:

mov temp, 'X'
mov al, temp
mov BYTE PTR arrayOfLetters[0], al
Run Code Online (Sandbox Code Playgroud)

这符合,但它没有改变阵列......

Spa*_*ile 2

该代码现已经过测试。我是用记事本写的,记事本没有很好的调试器,这是我凭空想象出来的。然而,这应该是一个很好的起点:

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
Run Code Online (Sandbox Code Playgroud)

如果您对 DWORD 值(int)而不是 BYTE 值(字符)进行排序,这会容易得多。