Reputation: 924
Recently I'm learning assembly and now i have some confusion. I learned it from Professional Assembly language.
My System's arch:
#uname -m
x86_64
This is my code:
.section .data
output:
.asciz "This is section %d\n"
.section .text
.globl _start
_start:
pushq $1
pushq $output
call printf
addq $8, %rsp
call overhere
pushq $3
pushq $output
call printf
addq $8, %rsp
pushq $0
call exit
overhere:
pushq %rbp
movq %rsp, %rbp
pushq $2
pushq $output
call printf
addq $8, %rsp
movq %rbp, %rsp
popq %rbp
ret
I assemble, link and run it like this, getting the error message shown:
#as -o calltest.o calltest.s
#ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o calltest calltest.o
#./calltest
Segmentation fault
How do I make it work?
Upvotes: 1
Views: 1007
Reputation: 14409
x86_64
has another kind of passing arguments, see: http://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI
This is how your example would work:
.section .data
output:
.asciz "This is section %d\n"
.section .text
.globl _start
_start:
movq $output, %rdi # 1st argument
movq $1, %rsi # 2nd argument
xorl %eax, %eax # no floating point arguments
call printf
call overhere
movq $output, %rdi # 1st argument
movq $3, %rsi # 2nd argument
xorl %eax, %eax # no floating point arguments
call printf
xor %edi, %edi
call exit
overhere:
pushq %rbp
movq %rsp, %rbp
movq $output, %rdi # 1st argument
movq $2, %rsi # 2nd argument
xorl %eax, %eax # no floating point arguments
call printf
movq %rbp, %rsp
popq %rbp
ret
Upvotes: 5