ASM调用printf [英] ASM call Printf

查看:617
本文介绍了ASM调用printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

movl %ebx, %esi
movl $.LC1, %edi
movl $0, %eax
call printf

我用下面的ASM code打印是什么的 EBX 的注册。当我使用

movl $1,%eax
int 0x80

回声$?我得到正确的答案,但在第一种情况下分段错误。我使用的GNU汇编器和AT& T公司的语法。我怎样才能解决这个问题?

and the echo $? I get the correct answer but segmentation fault in the first case. I am using the GNU Assembler and AT&T syntax. How can I fix this problem?

推荐答案

由code来看,你很可能在64位模式(请确认)在这种情况下指针在大小64位。您应该替换 MOVL $ .LC1,EDI% leaq .LC1,%RDI (或 leaq .LC1(%RIP),%RDI ),它应该工作。

Judging by the code, you are probably in 64 bit mode (please confirm) in which case pointers are 64 bit in size. You should replace the movl $.LC1, %edi by leaq .LC1, %rdi (or leaq .LC1(%rip), %rdi) and it should work.

此外,请确保:


  • 您是 RBX 在函数$​​ P $ pserving值

  • 堆栈指针对准的要求

  • you are preserving value of rbx in your function
  • stack pointer is aligned as required

这code对我的作品在64位:

This code works for me in 64 bit:

.globl main
main:
    push %rbx
    movl $42, %ebx
    movl %ebx, %esi
    leaq .LC1, %rdi
    movl $0, %eax
    call printf
    xor  %eax, %eax
    pop  %rbx
    ret

.data
    .LC1: .string "%d\n"

这篇关于ASM调用printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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