简体   繁体   English

在嵌入式x86程序集中使用数组?

[英]Using an array in embedded x86 assembly?

I have a method (C++) that returns a character and takes an array of characters as its parameters. 我有一个方法(C ++)返回一个字符并将一个字符数组作为其参数。

I'm messing with assembly for the first time and just trying to return the first character of the array in the dl register. 我第一次搞乱程序集,只是试图在dl寄存器中返回数组的第一个字符。 Here's what I have so far: 这是我到目前为止所拥有的:

char returnFirstChar(char arrayOfLetters[])
{
 char max;

 __asm
 {
  push eax
      push ebx
       push ecx
     push edx
  mov dl, 0

  mov eax, arrayOfLetters[0]
  xor edx, edx
  mov dl, al

  mov max, dl       
  pop edx
  pop ecx
  pop ebx
  pop eax

 }

 return max;
}

For some reason this method returns a ♀ 由于某种原因,此方法返回♀

Any idea whats going on? 有什么想法吗? Thanks 谢谢

The line of assembly: 装配线:

mov eax, arrayOfLetters[0]

is moving a pointer to the array of characters into eax (note, that's not what arrayOfLetters[0] would do in C, but assembly isn't C). 正在将指向字符数组的指针移动到eax (注意,这不是arrayOfLetters[0]在C中所做的,但是程序集不是C)。

You'll need to add the following right after it to make your little bit of assembly work: 您需要在它之后添加以下内容以使您的一些装配工作:

mov al, [eax]

Well here is how I'd write that function: 那么这就是我写这个函数的方式:

char returnFirstChar( const char arrayOfLetters[] )
{
    char max;
    __asm
    {
         mov eax, arrayOfLetters ; Move the pointer value of arrayOfLetters into eax.
         mov dl, byte ptr [eax]  ; De-reference the pointer and move the byte into eax.
         mov max, dl             ; Move the value in dl into max.
    }
    return max;
}

That seems to work perfectly. 这似乎完美无缺。

Notes: 笔记:

1) As I said in my comment you don't need to push the registers on the stack, let MSVC handle that. 1)正如我在评论中所说,你不需要在堆栈上推送寄存器,让MSVC处理它。
2) Don't bother clearing edx by X'oring it against it self or don't set dl to 0. Both will achieve the same thing. 2)不要通过对它自己进行X的扫描来清除edx,或者不要将dl设置为0.两者都会达到同样的效果。 All in you don't even need to do that as you can just overwrite the value stored in dl with your value. 你甚至都不需要这样做,因为你可以用你的值覆盖存储在dl中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM