0

What is the difference between leave-ret and system call exit? So I am using GNU assembler with gcc compiler in the Ubuntu 20.04 and when I turn a c program to an assembly program with this command:

gcc -S foo.c

the foo.s file always ends with this code:

leave
ret

Should I use system call exit instead of leave and ret? Like this:

movl $1, %eax
movl $0, %ebx
int $0x80
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
The Mayhem
  • 23
  • 3
  • Do you know the difference between `return 0;` and `exit(0);` in C? – Joseph Sible-Reinstate Monica Jun 12 '21 at 15:14
  • 1
    Use `ret` (or `call exit`, not `_exit`) if you call any functions like printf that might leave buffered I/O to be flushed at exit. If you're writing your own `_start` , `ret` isn't an option. I think this is a duplicate of [On x64 Linux, what is the difference between syscall, int 0x80 and ret to exit a program?](https://stackoverflow.com/q/67676658), where my answer covers this territory in detail. – Peter Cordes Jun 12 '21 at 15:16
  • 1
    [No](https://man7.org/linux/man-pages/man3/atexit.3.html) – Hans Passant Jun 12 '21 at 15:18
  • 1
    @JosephSible-ReinstateMonica: Maybe you mean `_exit(0)`, because the difference in C between having `main` `return 0` vs. `exit(0)` is negligible, unless main is recursive or called by other functions. (The difference in asm is that you don't have to clean up the stack, or save any call-preserved registers.) Oh, I guess you're considering functions other than `main`, since the question didn't specify that! Yeah, big difference, IIRC there's another duplicate for that C / function concept. – Peter Cordes Jun 12 '21 at 15:18
  • @PeterCordes Yep, I did mean `_exit` instead of `exit`. Too late to edit now though. – Joseph Sible-Reinstate Monica Jun 12 '21 at 15:53

0 Answers0