如何使它在x86-64的装配工作? [英] How to make it work in x86-64 assembly?

查看:279
本文介绍了如何使它在x86-64的装配工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在学习组装,现在我有一些困惑。我从专业汇编语言学会了。

Recently I'm learning assembly and now i have some confusion. I learned it from Professional Assembly language.

我的系统的拱门:

#uname -m
x86_64

这是我的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

我如何工作的?

推荐答案

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

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

这篇关于如何使它在x86-64的装配工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆