2012-11-24 8 views
1

例如輸入輸出兩次:彙編at&t從用戶處獲得2 diffrent int並將它們打印出來似乎無法工作。 insted的它打印第二號

input 

34 54 

expected output: 

54 
34 

output: 

54 54 

這與音符的代碼!

#This is a simple "matematical function" program 
.section .rodata #read only data section 
str: .string "Input :\n" 

input_format: .string "%d" 
output_format: .string "%d\n" 
.section .bss 
input1: .long 
input2: .long 
######## 
.section .text  #the beginnig of the code 
.globl main #the label "main" is used to state the initial point of this program 
.type main, @function # the label "main" representing the beginning of a function 
main: # the main function: 

    pushl %ebp #save the old frame pointer 
    movl %esp, %ebp #create the new frame pointer 
    #firs number input 
    pushl $input1 # push the ADDRESS of input to have the value stored in it 
    pushl $input_format # give scanf the ADDRESS of the format string 
    call scanf # call scanf to get number from the user 
    addl $8, %esp # clean up the stack 
    #second number input 
    pushl $input2 # push the ADDRESS of input to have the value stored in it 
    pushl $input_format # give scanf the ADDRESS of the format string 
    call scanf # call scanf to get number from the user 
    addl $8, %esp # clean up the stack 
    # Note the return value of scanf is passed through eax 
    #print the second number 
    pushl input2 # pass the number to printf BY VALUE 
    pushl $output_format # pass the ADDRESSS of the output format string to printf 
    call printf #call the printf function 

    #return from printf: 
    movl $0, %eax #return value is zero (just like in c - we tell the OS that this program finished seccessfully) 
    movl %ebp, %esp #restore the old stack pointer - release all used memory. 

    #print the firs number 
    pushl input1 # pass the number to printf BY VALUE 
    pushl $output_format # pass the ADDRESSS of the output format string to printf 
    call printf #call the printf function 
    #return from printf 
    movl $0, %eax #return value is zero (just like in c - we tell the OS that this program finished seccessfully) 
    movl %ebp, %esp #restore the old stack pointer - release all used memory 
    #end the program 
    popl %ebp #restore old frame pointer (the caller function frame) 
    ret #return to caller function (OS) 
+0

從printf的第一回後,不要做了'MOVL%EBP,%esp'(做第二個printf後...之前從主返回)。 –

回答

0

改變這樣的代碼:

#This is a simple "matematical function" program 
.section .rodata #read only data section 
str: .string "Input :\n" 

input_format: .string "%d" 
output_format: .string "%d\n" 
.section .bss 
input1: .long 24 # CHANGE HERE 
input2: .long 28 # CHANGE HERE 
######## 
.section .text  #the beginnig of the code 
.globl main #the label "main" is used to state the initial point of this program 
.type main, @function 
main: 

    pushl %ebp 
    movl %esp, %ebp 
    andl $-16, %esp # ADD THIS LINE 
... 
... 
相關問題