nasm/yasm 参数,链接到 C++

发布于 2024-08-25 10:32:32 字数 843 浏览 10 评论 0原文

我有一个关于 nasm 及其与 C++ 的链接的问题。我将一个小测试函数声明为

extern "C" void __cdecl myTest( byte i1, byte i2, int stride, int *width );

,我这样称呼它:

byte i1 = 1, i2 = 2;
int stride = 3, width = 4;
myTest( i1, i2, stride, &width );

该方法仅用于调试程序集并看看如何使用堆栈指针来获取参数。除此之外,指针参数值应设置为 7,以了解其工作原理。这是这样实现的:

    global _myTest

_myTest:
    mov     eax, [esp+4]     ; 1
    mov     ebx, [esp+8]     ; 2
    mov     ecx, dword [esp+16]    ; width
    mov     edx, dword [esp+12]    ; stride

    mov eax, dword [esp+16]
    mov dword [eax], 7

    ret

并通过 编译

yasm -f win32 -g cv8 -m x86 -o "$(IntDir)\$(InputName).obj" "$(InputPath)"

,然后链接到 c++ 应用程序。在调试模式下,一切正常。该函数被调用几次并按预期工作,而在发布模式下该函数只能工作一次,但后续的编程操作会失败。在我看来,堆栈/帧指针、近/远有问题,但我对这个主题很陌生,需要一些帮助。提前致谢! 一个。

I've got a question concerning nasm and its linkage to C++. I declare a litte test function as

extern "C" void __cdecl myTest( byte i1, byte i2, int stride, int *width );

and I call it like this:

byte i1 = 1, i2 = 2;
int stride = 3, width = 4;
myTest( i1, i2, stride, &width );

the method only serves to debug assembly and have a look at how the stack pointer is used to get the arguments. beyond that, the pointer arguments value shall be set to 7, to figure out how that works. This is implemented like this:

    global _myTest

_myTest:
    mov     eax, [esp+4]     ; 1
    mov     ebx, [esp+8]     ; 2
    mov     ecx, dword [esp+16]    ; width
    mov     edx, dword [esp+12]    ; stride

    mov eax, dword [esp+16]
    mov dword [eax], 7

    ret

and compiled via

yasm -f win32 -g cv8 -m x86 -o "$(IntDir)\$(InputName).obj" "$(InputPath)"

, then linked to the c++ app. In debug mode, everything works fine. the function is called a couple of times and works as expected, whereas in release mode the function works once, but subsequent programm operations fail. It seems to me that something's wrong with stack/frame pointers, near/far, but I'm quite new to this subject and need a little help. thanks in advance!
a.

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

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

发布评论

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

评论(2

不必了 2024-09-01 10:32:32

好吧,看来我必须通过push/pop 保存ebx

Well, it seems that I have to preserve ebx via push/pop.

ら栖息 2024-09-01 10:32:32

可能这会有所帮助: FLAC 在汇编器中使用了一些打算由 nasm 编译的源代码。

May be this helps: FLAC uses some sources in assembler which intended to be compiled by nasm.

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