示例输入输出:
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)