排序通过x86汇编阵列(嵌入C ++)?可能? [英] Sort an array via x86 Assembly (embedded in C++)?? Possible?

查看:168
本文介绍了排序通过x86汇编阵列(嵌入C ++)?可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄x86汇编的第一次,我无法弄清楚如何排序的数组(通过插入排序)。我理解的算法,但组装困惑我,因为我主要是使用Java和放大器; C ++。所有继承人我至今

I am playing around with x86 assembly for the first time and I can't figure out how to sort an array (via insertion sort).. I understand the algorithm, but assembly is confusing me as I primarily use Java & C++. Heres all I have so far

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

Basically nothing :( Any ideas?? Thanks in advance.

Ok, this is just going to make me sound like a total idiot, but I can't even change any of the array's values in _asm

只是为了测试它,我把:

Just to test it out, I put:

mov temp, 'X'
mov al, temp
mov arrayOfLetters[0], temp

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

And this gave me an error C2415: improper operand type

所以我尝试:

mov temp, 'X'
mov al, temp
mov BYTE PTR arrayOfLetters[0], al

这遵守,但它并没有改变数组...

This complied, but it didn't change the array...

推荐答案

这code是在现在的方式进行测试。我写在记事本中,它并没有一个很好的调试器,把我的头顶部。这应该是一个很好的起点,但是:

This code is in now way tested. I wrote it in notepad, which doesn't have a very good debugger, off the top of my head. It should be a good starting place however:

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值(INT),而不是字节值(字符)。

This would have been MUCH easier if you were sorting DWORD values (int) instead of BYTE values (characters).

这篇关于排序通过x86汇编阵列(嵌入C ++)?可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆