程序集scanf检索值

时间:2012-06-01 07:32:26

标签: assembly printf scanf

我正在尝试创建一个简单的汇编程序,它允许用户输入一个值并将其打印出来。到目前为止,我有以下内容: 运行程序时,我收到错误:分段错误。 请有人帮帮我吗?

    .text
    STRING: .asciz "test\n"

    input: .long

    .global main
    inout:
    pushl %ebp # Prolog: push the base pointer.
    movl %esp, %ebp # and copy stack pointer to EBP.

    formatstr: .asciz "%d"

    subl $4, %esp # Reserve stack space for variable
    leal -4(%ebp), %eax # Load address of stack var in eax
    pushl $input # Push second argument of scanf
    pushl $formatstr # Push first argument of scanf
    call scanf # Call scanf

    movl $8, %esp # Clear local variables from stack.

    pushl $input
    call printf

    movl $0, %eax
    movl %ebp, %esp
    popl %ebp # Restore caller's base pointer.
    ret # Return

    main:
    pushl $STRING #push the format string printing
    call printf # print the number
    call inout

    add $4, %esp
    pushl $0
    call exit

1 个答案:

答案 0 :(得分:2)

如果在

执行数据时没有爆炸
 formatstr: .asciz "%d"

它很可能会在尝试使用带有不稳定堆栈指针的堆栈时产生分段错误:

movl $8, %esp # Clear local variables from stack.

pushl $input

也许你的意思是

addl $8, %esp