汇编代码:
frac:
pushl %ebp //压栈
movl %esp, %ebp //栈帧
subl $24, %esp //esp-24,开辟空间
cmpl $1, 8(%ebp) //比较1和a的大小,a>1则跳到L2
jg .L2
movl 8(%ebp), %eax //eax=a
imull 8(%ebp), %eax //eax*=eax,eax=a*a
jmp .L3 //无条件跳转到L3
.L2:
movl 8(%ebp), %eax //eax=a
subl $2, %eax //eax-=2 eax=a-2
movl %eax, (%esp) //esp的位置=eax=a-2
call frac //调用frac,此时由于esp的位置是a-2,因此相当于frac(a-2)
addl 8(%ebp), %eax //eax+=a,即eax=frac(a-2)+a
.L3:
leave //退出
ret
sum:
pushl %ebp //压栈
movl %esp, %ebp //栈帧
subl $40, %esp //esp-40开辟空间
movl 12(%ebp), %eax //eax=b
movl 8(%ebp), %edx //edx=a
addl %edx, %eax //eax+=edx,eax=b+a
movl %eax, (%esp) //eax给到esp
call frac //调用frac函数,同上面的那个frac,相当于frac(a+b)
movl %eax, -12(%ebp) //c=frac(a+b)
movl -12(%ebp), %eax //eax=c
addl %eax, %eax //eax+=eax,eax=2*c
addl 12(%ebp), %eax //eax+=b,eax=2*c+b
leave
ret
main:
pushl %ebp //压栈
movl %esp, %ebp //栈帧
andl $-16, %esp //esp和-16与运算
subl $32, %esp //esp-32开辟空间
movl $10, 20(%esp) //i=10
movl $6, 24(%esp) //j=6
movl 20(%esp), %eax //eax=10=i
movl 24(%esp), %edx //edx=6=j
subl %eax, %edx //edx=-4
movl 20(%esp), %eax //eax=10
addl $1, 20(%esp) //i=11
movl %edx, 4(%esp) //esp大于4处=-4
movl %eax, (%esp) //eax给到esp=10
call sum //调用sum,sum函数需要两个参数,这两个参数在上面,也就是sum(j-i,i++)
movl %eax, 28(%esp) //esp大于28处=10,即k=eax=sum(j-i,i++)
movl $0, %eax //eax=0
leave //退出
ret