[英]What is the difference between the instructions STI, RET and IRET in assembly
[英]What is the difference between calling “ret” vs calling the sys_exit number assembly gcc
在gcc程序集中,main函數可以返回或退出,兩者都可以工作。 這里我有兩個程序,其中一個以syscall int $0x80
退出,另一個只調用ret。 有什么區別?
.data
hello: .string "Hello, World!"
.globl main
main:
push %rbx
movq $hello, %rdi
call puts
pop %rbx
ret
和
.data
hello: .string "Hello, World!"
.globl main
main:
push %rbx
movq $hello, %rdi
call puts
pop %rbx
movq $1, %rax
movq $0, %rbx
int $0x80
我知道ret會將指令指針從堆棧中彈出,但在這種情況下,它真正做了什么?
調用main
的代碼如下所示:
int status = main(argc, argv, envp);
exit(status);
如果main
返回,則執行exit(status)
。 exit
是一個C庫函數,它刷新所有stdio流,調用atexit()
處理程序,最后調用_exit(status)
,它是SYS_exit
系統調用的C包裝器。 如果您使用C運行時(例如,通過讓程序從main
開始或使用任何libc函數),我強烈建議您不要直接調用SYS_exit
,以便C運行時有機會正確地取消初始化程序。 最好的想法通常是調用exit()
或從main
返回,除非你確切知道你在做什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.