我试图了解堆栈到底是如何工作的,所以我将在这里重新创建一个带有一些问题的小示例。
假设我在 ASM 中有一段小代码,它执行以下操作:
(这一切都是x86、intel语法、Linux)
push ebp
mov ebp, esp
sub esp, 16
mov eax, 0xdeadbeef <-- let's call this 'local variable a'
mov [ebp - 16], eax
mov eax, 0xabcdabcd <-- let's call this 'local variable b'
mov [ebp - 12], eax
mov eax, 0xcacafafa <-- let's call this 'local variable c'
mov [ebp - 8], eax
mov eax, 0xdada1111 <-- let's call this 'local variable d'
mov [ebp - 4], eax
call 0x10101010 <------- pretend that is the real address of function_B
mov esp, ebp
pop ebp
ret
现在假设我有一个名为的 C 函数function_B
从 asm 代码中调用 get ,它看起来像这样:
asmlinkage void function_B(void){
//some code here...
}
我如何访问a
, b
, c
and d
from function_B
使用内联 ASM 代码?
这行得通吗?我应该采取不同的做法吗?
uint32_t val_a;
__asm__ __volatile__(
".intel_syntax noprefix;"
"mov %0, dword [ebp + 4 + 4 + 0];"
".att_syntax;"
: "=r" (val_a)
);
uint32_t val_b;
__asm__ __volatile__(
".intel_syntax noprefix;"
"mov %0, dword [ebp + 4 + 4 + 4];"
".att_syntax;"
: "=r" (val_b)
);
另外,我将如何访问a
, b
, c
and d
如果我的函数如下所示:
asmlinkage void function_B(unsigned int val){
//some code here...
}