在嵌入式 x86 程序集中使用数组?
我有一个方法(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
汇编行:
将指向字符数组的指针移动到 eax 中(注意,这不是
arrayOfLetters[0]
在 C 中所做的事情,但汇编不是C)。您需要在其后添加以下内容以使您的一点组装工作正常进行:
The line of assembly:
is moving a pointer to the array of characters into
eax
(note, that's not whatarrayOfLetters[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:
这就是我编写该函数的方式:
这似乎工作得很好。
注意:
1)正如我在评论中所说,您不需要将寄存器压入堆栈,让 MSVC 处理它。
2)不要费心通过与自身进行异或来清除 edx 或不要将 dl 设置为 0。两者都会实现相同的效果。总之,您甚至不需要这样做,因为您只需用您的值覆盖 dl 中存储的值即可。
Well here is how I'd write that function:
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.