When I run my program I have segmentation fault error. I don't understand why.
.data
str1: .string "hello"
str2: .string "world"
.text
.globl _start
_start:
pushl $str1
call puts
call strcall
call finish
strcall:
pushl $str2
call puts
ret
finish:
movl $1, %eax
movl $0, %ebx
int $0x80
Any idea why is it happening like that?
The normal cdecl
calling convention mandates that the caller remove the arguments it has placed on the stack. Since you don't do that in strcall
, the pushl $str2
is still on the stack and ret
will try to use that as the return address. Solution: insert addl $4, %esp
before the ret
.
Next time use a debugger to see where the problem is for yourself.
Also, if you intend to use C library functions, you should really use main
as entry point and compile with gcc
so you get the C library properly initialized. Similarly, you should not use the exit
system call, you should just return from main
or if abolsutely necessary, call exit
from the C library.