如何使它在x86-64汇编中工作?

时间:2014-08-20 09:19:49

标签: assembly x86-64

最近我正在学习装配,现在我有些困惑。我是从Professional Assembly language学到的。

我的系统的拱门:

#uname -m
x86_64

这是我的代码:

.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 

我像这样组装,链接和运行它,收到显示的错误消息:

#as -o calltest.o calltest.s
#ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o calltest calltest.o
#./calltest 
Segmentation fault

如何让它发挥作用?

1 个答案:

答案 0 :(得分:4)

x86_64有另一种传递参数,请参阅:http://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI

这就是你的例子如何运作:

.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