【发布时间】:2018-07-15 23:36:49
【问题描述】:
在 gcc 汇编中,主函数可以返回或退出,两者都可以工作。这里我有两个程序,一个以系统调用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 将指令指针从堆栈中弹出,但在这种情况下,它到底做了什么?
【问题讨论】:
-
如果您与 C 运行时链接,它将执行
call main。当您执行ret时,它会返回到最终终止进程的 C 运行时。 sys_exit 系统调用避免返回调用 main 并请求操作系统停止进程的 C 运行时。 -
@MichaelPetch 如果我不与 c 运行时链接怎么办(当然,假设我更改了 puts 函数)
-
@MichaelPetch 另外,这两个选项有什么区别
-
如果你不链接运行时,而且你在linux上,你根本不能使用
ret,因为没有地方可以返回。 -
@Jester Cool,谢谢
标签: assembly