2012-06-01 36 views
0

我想創建一個簡單的彙編程序,它允許用戶輸入一個值並打印出來。到目前爲止,我得到了以下幾點: 運行程序時,出現錯誤:分段錯誤。 有人可以幫我嗎?裝配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 

回答

2

如果當你在

formatstr: .asciz "%d" 

執行數據並沒有爆發,很可能會給出一個分段錯誤試圖使用堆棧有靠不住的堆棧指針:

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

pushl $input 

也許你的意思是

addl $8, %esp