The assembly language referenced by the chapter is as follows. I've changed it slightly so that it uses the symbol "_start", since I am on Linux instead of OS X.
write "exit.asm" segment .text
global _start
_start:
mov eax,60 ; 60 is the exit syscall number on Linux
mov edi,5 ; the status value to return
syscall ; execute the system call
end
Building that program using the Yasm assembler on Linux can be done with the following command, which is explained in the book.
build exit.o exit.asm yasm -f elf64 -g dwarf2 exit.asm
Then, once we have the object file, we can turn it into an executable using the linker ld.
build exit exit.o ld -o exit exit.o
When we execute exit on the command line and check the return code for the process with $?, we find that 5 was indeed returned from the program. If we want to return 0, we just change the value that we put in the edi register.
write "s_exit.asm" segment .text
global _start
_start:
mov eax,60 ; 60 is the exit syscall number on Linux
mov edi,0 ; the status value to return
syscall ; execute the system call
end
We can assemble and link to produce an executable just as we did with exit.asm.
build s_exit s_exit.asm yasm -f elf64 -g dwarf2 s_exit.asm && ld -o s_exit s_exit.o
We can verify that the process produced by the executable s_exit returns 0 after it exits.