main 的 ret 指令在哪里 [英] where goes the ret instruction of the main

查看:84
本文介绍了main 的 ret 指令在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从头开始编程"一书中了解了汇编 (x86) 的全局工作原理.在本书中,每个程序都以退出的中断调用结束.

I learned how assembly (x86) globally works in the book : "Programming from ground up". In this book, every program ends with an interruption call to exit.

然而,在 C 编译的程序中,我发现程序以 ret 结尾.这假设有一个地址要弹出,这将导致程序结束.

However, in C compiled programs, I found out that programs end with a ret. This supposes that there is an address to be popped and that would lead to the end of the program.

所以我的问题是:这个地址是什么?(那里的代码是什么?)

So my question is : What is this address? (And what is the code there?)

推荐答案

您可以通过要求 OS 将控制权传递给 start_start 来启动您的程序 通过跳转到代码中的那个标签来运行你的程序.在 C 程序中,start 函数来自 C 库,并且(正如其他人之前已经说过的)执行一些特定于平台的环境初始化.然后 start 函数调用您的 ma​​in 并且控制是您的.从 ma​​in 返回后,它会将控制权传回 C 库,该库正确终止程序并执行特定于平台的系统调用,以将控制权返回给 操作系统.

You start your program by asking the OS to pass control to the start or _start function of your program by jumping to that label in your code. In a C program the start function comes from the C library and (as others already said before) does some platform specific environment initialization. Then the start function calls your main and the control is yours. After you return from the main, it passes control back to the C library that terminates the program properly and does the platform specific system call to return control back to the OS.

所以地址 main pops 是来自 C 库的标签.如果你想检查它,它应该在 stdlib.h (cstdlib) 中,你会看到它调用 exit 进行清理.

So the address main pops is a label coming from the C library. If you want to check it, it should be in stdlib.h (cstdlib) and you will see it calling exit that does the cleanup.

它的作用是在程序终止或线程终止(C++11)时销毁静态对象(当然是C++).在 C 的情况下,它只是关闭流,刷新它们的缓冲区,调用 atexit 函数并执行系统调用.

Its function is to destroy the static objects (C++ of course) at program termination or thread termination (C++11). In the C case it just closes the streams, flushes their buffers, calls atexit functions and does the system call.

我希望这是您想要的答案.

I hope this is the answer you seek.

这篇关于main 的 ret 指令在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆