理解调用约定和堆栈指针

时间:2015-01-13 23:12:05

标签: memory assembly x86 calling-convention

我想了解如何使用局部变量以及如何在x86中将参数传递给函数。我读了很多指南,他们都写道,第一个参数应该是[ebp + 8],但它不是在这里:/我错过了什么?我没有正确理解的是什么?

number byte "724.5289",0

.code
main PROC

mov ebx,offset number ;making so that [ebp] = '7' atm
push ebx ;I push it on stack so I can access it inside the function
call rewrite

main ENDP

rewrite PROC
push ebp     ; push ebp so we can retrieve later
mov ebp, esp ; use esp memory to retrieve parameters and 
sub esp, 8   ; allocate data for local variable 

lea ebx, [ebp-8]
lea eax, [ebp+8]  ; i think here ebp+8 should point to the same now to which ebx did 
                  ;before function, but it does not, writechar prints some garbage ascii character
call writechar
call crlf     

rewrite ENDP

END main

1 个答案:

答案 0 :(得分:2)

将指针作为参数传递给rewrite,然后将其地址传递给writechar。那就是你拿两次地址。这太多了:)

您希望mov eax, [ebp+8]代替lea eax, [ebp+8]

另外,你需要自己清理堆栈,这是你不能做的。此外,请确保您的汇编程序自动为RET指令发出ENDP,否则您将遇到麻烦。您可能希望明确地将其写出来。