了解调用约定和堆栈指针
Understanding calling convention and stack pointer
我想了解我应该如何使用局部变量以及如何将参数传递给 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
您将指针作为参数传递给 rewrite
,然后将其地址传递给 writechar
。那就是你把地址拿了两次。太多了:)
你想要 mov eax, [ebp+8]
而不是 lea eax, [ebp+8]
此外,您需要自己清理堆栈,但您没有这样做。此外,确保你的汇编器自动为 ENDP
指令发出一个 RET
,否则你会有麻烦。你可能想明确地写出来。
我想了解我应该如何使用局部变量以及如何将参数传递给 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
您将指针作为参数传递给 rewrite
,然后将其地址传递给 writechar
。那就是你把地址拿了两次。太多了:)
你想要 mov eax, [ebp+8]
而不是 lea eax, [ebp+8]
此外,您需要自己清理堆栈,但您没有这样做。此外,确保你的汇编器自动为 ENDP
指令发出一个 RET
,否则你会有麻烦。你可能想明确地写出来。