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

发布于 2024-08-28 11:24:29 字数 443 浏览 5 评论 0原文

我有一个方法(C++),它返回一个字符并采用一个字符数组作为其参数。

我第一次搞乱汇编,只是试图返回 dl 寄存器中数组的第一个字符。这是我到目前为止所得到的:

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;
}

由于某种原因,这个方法返回一个 ♀

知道发生了什么吗?谢谢

I have a method (C++) that returns a character and takes an array of characters as its parameters.

I'm messing with assembly for the first time and just trying to return the first character of the array in the dl register. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-09-04 11:24:29

汇编行:

mov eax, arrayOfLetters[0]

将指向字符数组的指针移动到 eax 中(注意,这不是 arrayOfLetters[0] 在 C 中所做的事情,但汇编不是C)。

您需要在其后添加以下内容以使您的一点组装工作正常进行:

mov al, [eax]

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).

You'll need to add the following right after it to make your little bit of assembly work:

mov al, [eax]
無處可尋 2024-09-04 11:24:29

这就是我编写该函数的方式:

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;
}

这似乎工作得很好。

注意:

1)正如我在评论中所说,您不需要将寄存器压入堆栈,让 MSVC 处理它。
2)不要费心通过与自身进行异或来清除 edx 或不要将 dl 设置为 0。两者都会实现相同的效果。总之,您甚至不需要这样做,因为您只需用您的值覆盖 dl 中存储的值即可。

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.
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. All in you don't even need to do that as you can just overwrite the value stored in dl with your value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文