I was asked to create a small program in assembly while using C functions. While doing so, I was wondering about something specific.
在使用C函数时,我被要求在程序集中创建一个小程序。在这样做的时候,我在想一些具体的事情。
I know that when working with assembly, wherever I want to call a C function, I must push it's arguments to the stack, and after the function returned I must pop those arguments (or add 4×x to esp
, where x represents the number of pushed arguments).
我知道在处理装配时,无论我想调用一个C函数,我必须将它的参数压入堆栈,函数返回之后,我必须流行这些参数(esp或添加4×x,其中x代表推参数)的数量。
My question is this: When calling the C function exit
in particular, I must first push an argument for the status. Let's say I want to push 0 to indicate that my program worked without errors.
我的问题是:在调用C函数退出时,我必须首先对状态进行一个论证。假设我想推0来表示我的程序没有错误。
Knowing that the exit
function does not return and that I must use it and can't just call the exit system interrupt myself, how can I, in this case, pop that 0 from the stack? Does the exit
function perform that for me?
知道退出函数不返回并且我必须使用它并且不能仅仅调用退出系统中断我自己,在这种情况下,我如何从堆栈中弹出0 ?退出函数为我执行这个操作吗?
1 个解决方案
#1
1
You don't have to. As exit()
does not return and the program is terminated, the system will deallocate all memory you used, including the stack.
你不需要。当exit()不返回并且程序终止时,系统将释放您使用的所有内存,包括堆栈。
Note though that the compiler will generate an add esp, 4
to cleanup the stack because the compiler doesn't know that exit
will never return.
请注意,编译器将生成一个add esp, 4来清理堆栈,因为编译器不知道退出将永远不会返回。
#1
1
You don't have to. As exit()
does not return and the program is terminated, the system will deallocate all memory you used, including the stack.
你不需要。当exit()不返回并且程序终止时,系统将释放您使用的所有内存,包括堆栈。
Note though that the compiler will generate an add esp, 4
to cleanup the stack because the compiler doesn't know that exit
will never return.
请注意,编译器将生成一个add esp, 4来清理堆栈,因为编译器不知道退出将永远不会返回。