第8部分-Linux x86 64位汇编 AT&T汇编示例三
示例——求指数
这里几个例子还是使用的32位的寄存器,后面会渐渐过渡到64位的寄存器。
在求指数中,我们定义了函数,通过函数来实现指数求解(这里实现的23+52=33,将参数写入了代码中)。
.section .data
.section .text
.code32
.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 #第二个值在%eax中,第一个值在栈中
addl %eax, %ebx #将他们相加在%ebx
movl $1, %eax #退出
int $0x80
#INPUT: 第一个参数-基数 %ebx
# 第二个参数-指数 %ecx
# -4(%ebp) 保存当前结果
# %eax 用于临时存储
#OUTPUT:
# 结果返回
.type power, @function
power:
pushl %ebp #保存旧的基指针
movl %esp, %ebp #将栈指针移动给基指针
subl $4, %esp #为本地变量腾出空间
movl 8(%ebp),%ebx #第一个参数放入到%eax
movl 12(%ebp),%ecx #第二个参数放入到%ecx
movl %ebx, -4(%ebp) #存储当前结果
power_loop_start:
cmpl $1, %ecx #如果结果 是1,则结束
je end_power
movl -4(%ebp), %eax #移动当前结果到%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
#as -o power.o power.s --32
# ld -o power power.o -melf_i386
#./power
通过echo $?可以得到33.