自动调用MASM保存寄存器?
当我在masm中使用Invoke指令时,是否可以自动保存CPU寄存器?
Is it possible to save cpu registers automatically when I use the Invoke directive in masm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看我在那里发布的示例:汇编语言中的选择排序
目标 PROC 中的 USES 指令就是您正在寻找的内容。
使用 EAX ESI EDI 将在 PROC 进入时自动保存这些寄存器,并在退出时恢复它们(即使您有多个 RET 点,即使不建议有多个 ret 点)。 IOW,它将在 PROC 输入时生成 PUSH,并在每次 RET 之前生成一致匹配(逆序)的 POP。这个想法是,由于这是汇编,因此您对您修改和想要保留的寄存器拥有完全的控制权和责任。
与其他地方的建议相反,声明 stdcall 不会在 MASM 中自动为您保留任何内容。它只是确定调用者(为 INVOKE 生成的代码)还是被调用者(在 PROC 中生成的代码)是否 POP 参数。
Look at the example I posted there: selection sort in assembly language
The USES directive in the target PROC is what you are looking for.
USES EAX ESI EDI will automatically save those registers upon PROC entry and restore them upon exit (even if you have multiple RET points, and even if having multiple ret points is not recommended). IOW, it will generate PUSHes upon PROC entry and consistently matched (reverse order) POPs before each and every RET. The idea is that since this is assembly, you have full control and responsability upon those registers you modify and want to preserve.
And contrary to what was suggested somewhere else, declaring stdcall doesn't preserve anything automatically for you in MASM. It just determines whether the caller (code generated for INVOKE) or the callee (code generated in the PROC) POPs the parms.
不确定你的意思,因为你的标签是 masm32 我假设 Windows x86。
完全可能的是推送 API 调用所需的所有参数,然后您只需调用所需的函数。我的意思是,当您在 Windows 程序集中进行编程时,您不需要使用寄存器来“调用”API,您必须推送参数,然后调用(或调用)API。
例如,这个:
完全等于这个(实际上只是参数的值不同):
这是你的问题的意思吗?
Not sure exactly what you meant, and since your tag is masm32 I'll assume Windows x86.
What is perfectly possible is to push all the arguments that an API call requires and then you just call the desired function. What I mean is that when you're promming in assembly for Windows you don't need to use registers to "invoke" the APIs, you have to push the arguments and then call (or invoke) the API.
For example, this:
is exactly equal to this (in fact just the values of the parameters are different):
Is that what you meant with your question?
“stdcall”调用约定保证该函数不会破坏除 eax、edx、ecx 之外的任何寄存器。如果你想保存 edx 和 ecx - 编写一个宏。
"stdcall" calling convention guarantees that function won't spoil any registers but eax, edx, ecx. If you want to save edx and ecx - write a macro.