0

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
icktoofay
  • 126,289
  • 21
  • 250
  • 231
safaiyeh
  • 1,655
  • 3
  • 16
  • 35

1 Answers1

4

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

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 4
    OS/X 64 bit uses [System V AMD64 ABI](http://x86-64.org/documentation/abi.pdf) conventions . The first parameter in a syscall is passed as `%rdi`. Exit takes one argument (the return code), thus it is placed in `%rdi` – Michael Petch Aug 29 '15 at 04:44