Why is there always a useless function argument on the stack?

I am learning assembly language programming on FreeBSD. I am using the FreeBSD 9.0 i386 release and the nasm assembler.

When I wrote a simple syscall function, I found that I needed to push a useless value onto the stack so that the code worked correctly.

For instance:

; File:test.asm
section .text
  global _start
_start:
  xor  eax,eax
  ; Argument of exit()
  push 0x0
  ; Syscall of exit()
  mov  al,1
  int  0x80

I used the following command to build and reference the code above:

%nasm -f elf test.asm -o test.o
%ld test.o -o test.bin

I used ktrace to test the program and found:

%ktrace ./test.bin
%kdump -d -f ./ktrace.out 
2059 ktrace   RET   ktrace 0
2059 ktrace   CALL  execve(-1077940941,-1077941260,-1077941252)
2059 ktrace   NAMI  "./test.bin"
2059 test.bin RET   execve 0
2059 test.bin CALL  exit(1)

Thus, the code did not start correctly because I provided 0 as the only argument to exit (), but the program actually runs exit (1).

Then I changed my code.

; File:test.asm
section .text
  global _start
_start:
  xor  eax,eax
  push 0x0
  ; Whatever digits,0x1,0x2...0xFFFFFFFF, ect.
  push 0xFFFFFFFF
  mov  al,1
  int  0x80

Then the code was executed correctly.

, - - "pad padding" " ", , . , 16- . . , :

; File:test.asm
section .text
  global _start
_start:
  xor  eax,eax
  push 0x0
  ; Actual argument of exit()
  push 0x3
  push 0xFFFFFFFF
  ; Syscall of exit()
  mov  al,1
  int  0x80

(3). , . gdb, , :

0xFFFFFFFF  -> esp
0x00000003
0x00000000

, : ?

+5

All Articles