Search code examples
assemblyx86-64exitsystem-callsdarwin

Assembly: Exit Code Wrong


I am learning Assembly and I created a simple exit program.

.section __DATA, __data
.section __TEXT, __text
.globl _main

_main:
  movl $0x2000001, %eax   #System call exit, offset by 0x00000
  movl $1, %ebx           #Exit Return code
  syscall                 #Wakes up kernal to run the systen call

How ever after running the file, echo $? returns 0

Assemble and Link command:

as exit.asm -o exit.o
ld exit.o -e _main -o exit
./exit

Solution

  • The exit code goes in %edi, not %ebx. (I’m not sure of a canonical reference for this; I just used trial and error.)