寫一個x86彙編函數返回一個寄存器值到C中的調用函數的正確語法是什麼?是否存在一些堆棧操作,如果是這樣,那麼這個清理過程怎麼樣?我會感謝一個簡單的例子。先謝謝你。如何在x86中返回寄存器值
0
A
回答
1
作爲一項規則,以下頁眉和頁腳是用於32位程序:
MyProc:
; prologue
push ebp
mov ebp, esp
sub esp, sizeof_local_variables ; this is optional, if your procedure
; needs some local variables.
; here write your code.
; The arguments are accessible on addresses [ebp+8+some_const]
; The local variables are on addresses [ebp-some_const]
; where some_const must be less than sizeof_local_variables of course.
; Return the result in EAX register if you want to return it to C/C++ or other HLL.
; epilogue
mov esp, ebp ; restores the stack pointer as it was on the entry
retn n ; returns to the caller. If you are using CCALL convention
; (usualy in C code) you have to omit "n" constant.
; n is the count of bytes of the pushed arguments.
; in 32bit environment it must be multiply of 4
序列 「推EBP/MOV EBP,尤指」 可以通過instrunction 「進入」 和「MOV ESP提供, ebp「by」leave「,但我更願意讓它更易於理解,並且在大多數平臺上使用明確的指令會更快。
+0
事後清理很有意思。謝謝。 – stian 2013-02-12 12:52:16
0
在Linux中,如果你返回一個int,返回eax,一個long(僅在64位模式下)以rax返回。
1
使用gcc,您可以使用內聯彙編,而不用擔心堆棧太多。
unsigned int get_eax(void) {
unsigned int eax;
asm("" : "=a" (eax));
return eax;
}
它使用與它沒有任何指令的組件部分,與說EAX的值應放在一個名爲eax
變量的輸出限制。
我使用unsigned int
作爲寄存器,它不是非常便攜,但內聯彙編不是很便攜。變量需要是寄存器的大小。
相關問題
- 1. 如何使用臨時寄存器 - 返回後保存值?
- 2. 如何在x86處理器中讀取標誌寄存器
- 3. 將兩個x86 32位寄存器存儲到128位xmm寄存器中
- 4. x86彙編寄存器地址
- 5. 通過LLVM IR控制x86寄存器?
- 6. 啓動x86機器時的默認寄存器和段值
- 7. ptrace寄存器中返回的異常值
- 8. 我怎樣在寄存器存儲在x86
- 9. 如何直接讀寫x86標誌寄存器?
- 10. X86操作碼將xmm寄存器移動到通用寄存器
- 11. 寄存器值8086
- 12. 返回值寄存器和析構函數調用順序
- 13. ElasticSearch - 匹配(電子郵件值)返回錯誤的寄存器
- 14. x86寄存器名稱,尋址模式,位移和存儲器
- 15. 彙編器x86中遞增寄存器的值不會高於1
- 16. 如何查看寄存器上的值?
- 17. x86程序集MOV指令 - 寄存器到寄存器和存儲器到存儲器
- 18. 如何在kprobe中使用寄存器?
- 19. 如何在LLVM中保留寄存器?
- 20. 我在哪裏可以找到x86寄存器名稱?
- 21. 在x86-64溢出的gcc參數寄存器
- 22. x86:使用寄存器作爲中間件,爲什麼?
- 23. Intel x86彙編語法寄存器中的括號
- 24. 如何使用gdb獲取存儲在寄存器中的值? 「p/x $ ebx」因「沒有寄存器」而失敗。
- 25. 如何在OSX上的用戶空間編寫x86 Debug寄存器?
- 26. 如何在Verilog中將值分配給輸出寄存器?
- 27. 如何在GDB中打印寄存器值?
- 28. 獲得值寄存器
- 29. JB和值寄存器
- 30. 使用單獨的寄存器來存儲返回地址?
研究編譯器的調用約定。 – 2013-02-12 12:31:47
這取決於您的代碼使用的其他調用約定;請參閱http://en.wikipedia.org/wiki/X86_calling_conventions。 – 2013-02-12 12:34:03
@alexey。嗯。。好。我正在使用gcc。 – stian 2013-02-12 12:35:28