[英]Assembly language function template. - A simple lazy convention?
我正在用匯編語言開發一些函數,一開始,我的函數通過以下三種方式傳遞參數:
現在,我發現上述三種方式的混合使事情變得復雜。 而且我總是陷入這種情況,我必須撓頭以確保某些寄存器是否被污染。 所以我決定只通過stack傳遞參數。 並將以下功能模板用作懶惰的“一勞永逸”解決方案:
pushl %ebp
movl %esp, %ebp
pushal <--- save all the registers, this is kind of a lazy solution
subl xxx, %esp <--- allocate space for local variables
....
popal <--- restore all the registers
movl %ebp, %esp
popl %ebp
(addl yyy, %esp)<--- if it is __stdcall convention, the callee will clear the stack
ret
(xxx是局部變量的大小,yyy是調用者推送的參數的大小。)
調用方負責推入參數並清除堆棧(如C調用約定)。 當然,如果參數數量固定,我可以讓被調用方清除堆棧(如Windows上的__stdcall約定)。
我希望這個模板可以使我擺脫寄存器使用的混亂。 能做到嗎? 如果效率低下,是否有更好的方法? 我想聽聽您的評論。
非常感謝。
再次嗨,我的代碼有錯誤,所以我像這樣糾正了它:
pushl %ebp
movl %esp, %ebp
pushal <--- save all the registers, this is kind of a lazy solution
subl xxx, %esp <--- allocate space for local variables
....
addl xxx, %esp <--- reclaim the space for local variables
popal <--- restore all the registers
movl %ebp, %esp
popl %ebp
ret yyy <--- for __stdcall convention, the callee will clear the parameters pushed on stack by caller
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.