Memory 理解调用约定和堆栈指针

Memory 理解调用约定和堆栈指针,memory,assembly,x86,calling-convention,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 cal

我想了解如何使用局部变量以及如何将参数传递给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
,否则您将遇到麻烦。您可能需要明确地写出它。

这只是一个草稿,此函数尚未完成。我想用‘lea-eax,ebp+8’让eax指向与ebp+8当前相同的字符,然后一个接一个地重写它们。为此,您需要
mov
。正如我所说,
lea
在需要值时获取地址。如果有帮助的话,这是你所做的C代码:
voidrewrite(char*p){writechar(&p);}
。您获取了参数的地址,它已经是一个指针,您确实需要该值。那么,将地址传递太多次是什么意思?writechar不会覆盖地址,它只是打印地址,不是吗?啊,这很有道理,谢谢。编辑:虽然有道理,但它仍然打印出垃圾值:(不确定
writechar
应该做什么,我假设它实际上是在写入
eax
所指向的字符串。如果它在
eax
中打印字符,那么您是在打印指针,因此是垃圾。在这种情况下,要打印字符串的第一个字母,您需要将其加载到eax中,例如:)e> mov eax[ebp+8];mov al[eax];调用writechar。这相当于C中的
writechar(*p)