# Given a number, this program compute the square of a given function
# For Example the 2*2 is 4
#This program showa how to call a function recursively
.section .data
#This program has no global data
.section .text
.globl _start
.globl square #this is unneeded unless we need to share this program
#among others
_start:
pushl $4 #The function takes one argument_ the number we want
# square of . So it get pushed of.
call square #run the square function
addl $4, %esp # Scrubs the paramter that was pushed on the stack
movl %eax, %ebx #factorial returns the answer in %eax, but we want it #in %ebx to send it as the exit status
movl $1, %eax #call the kernel's next function
int $0x80
#This is a function that test square of a function
# It takes one argument and then return the square
.type square, @square
square:
pushl %ebp #standard function stuff -we have to
#restore %ebp to its prior state before
#returning, so we have to push it.
movl %esp,%ebp #This is because we don't want to modify
#the stack pointer, so we use %ebp
movl 8(%ebp), %eax #This moves the first argument to %eax
#4(%ebp) holds the return address, and
#8(%ebp) holds the first parameter.
cmpl $1,%eax #If the number is 1, that is our base
#case, and we simply return(1 is
#already in %eax as the return value.)
je end_square
pushl %eax #Push it for call to square
call square #call square
movl 8(%ebp),%ebx #%eax has the return value, so we
#reload or parameter into %ebx
imull %ebx,%eax #multiply that by the result of the last call
#to square(in %eax) the answer is stored in %eax,
#which is good since that's where return values go.
end_square:
movl %ebp, %esp # standard function stuff-we have to restore %ebp and
popl %ebp # %esp to where they were were before the function started
ret # return from the function (this pops the # return value, too)
答案 0 :(得分:1)
尝试使用:.type square, @square
.type square, @function
(这没有意义)
供将来参考 - 不要只在无格式代码转储上添加标题。您应该花时间阅读网站about。