5

初めて 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

これは準拠しましたが、配列は変更されませんでした...

4

1 に答える 1

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

BYTE 値 (文字) ではなく DWORD 値 (int) を並べ替えていれば、これははるかに簡単でした。

于 2010-04-15T17:53:22.807 に答える