当前位置:编程学习 > 网站相关 >>

Linux下汇编学习-4

 

#PURPOSE:This program will compute the value of

# 2^3 + 5^2

.section .data

.section .text

.globl _start

_start:

pushl $3

pushl $2

call power

addl $8, %esp

pushl %eax

pushl $2

pushl $5

call power

addl $8, %esp

popl %ebx

addl %eax, %ebx

movl $1, %eax

int $0x80

.type power, @function

power:

pushl %ebp

movl %esp, %ebp

subl $4, %esp

movl 8(%ebp), %ebx

movl 12(%ebp), %ecx

movl %ebx, -4(%ebp)

power_loop_start:

cmpl $1, %ecx

je end_power

movl -4(%ebp), %eax

imull %ebx, %eax

movl %eax, -4(%ebp)

decl %ecx

jmp power_loop_start

end_power:

movl -4(%ebp), %eax

movl %ebp, %esp

popl %ebp

ret

使用.type funname @function去定义一个函数

使用call指令去调用一个子函数,在调用子函数我们要明确,函数参数放在什么地方,返回返回值放在什么地方,函数返回地址在什么地方以及函数内的局部变量放在什么地方。我们知道,调用call指令时会做如下工作,将当前pc指针压入栈中,跳转到标号处执行程序。那么在子函数中,我们要使用ret指令,使得子函数执行完后能够跳到主函数处继续执行。

 



摘自 mcgrady_tracy的专栏
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,