定义C内的变量++内联汇编 [英] Defining a variable inside c++ inline assembly
问题描述
让我们说我们有以下的C ++ code:
Let's say we have the following c++ code:
int var1;
__asm {
mov var1, 2;
}
现在,我想知道是,如果我不想定义VAR1的__asm指令之外,那么我有什么做的,把它放在里面。它甚至有可能?
Now, what I'd like to know is if I didn't want to define var1 outside the __asm directive, what would I have to do to put it inside it. Is it even possible?
感谢
推荐答案
要做到这一点,你需要创建一个裸方法,_declspec(裸体),并自己写序言以及通常创建的收尾由编译器
To do that, you'll need to create a "naked" method with _declspec(naked) and to write yourself the prolog and the epilog that are normally created by the compiler.
一个序言的宗旨是:
- 设置了EBP和ESP
- 在堆栈中为局部变量预留空间
- 保存应在函数体 进行修改寄存器
- set up EBP and ESP
- reserve space on stack for local variables
- save registers that should be modified in the body of the function
这是收尾有:
- 恢复保存的寄存器值
- 清理为局部变量保留空间
下面是一个标准的序言
push ebp ; Save ebp
mov ebp, esp ; Set stack frame pointer
sub esp, localbytes ; Allocate space for locals
push <registers> ; Save registers
和一个标准的结语:
pop <registers> ; Restore registers
mov esp, ebp ; Restore stack pointer
pop ebp ; Restore ebp
ret ; Return from function
您的局部变量将随后开始在(EBP - 4)
和向下转到(EBP - 4 - localbytes)
。该函数的参数将开始(EBP + 8)
和去向上。
Your local variables will then begin at (ebp - 4)
and go downward to (ebp - 4 - localbytes)
. The function parameters will start at (ebp + 8)
and go upward.
这篇关于定义C内的变量++内联汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!