0

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

    .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
4

1 回答 1

2

如果在执行数据时它没有爆炸

 formatstr: .asciz "%d"

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

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

pushl $input

也许你的意思是

addl $8, %esp
于 2012-06-01T17:35:54.903 回答