汇编(AT&T)实现n的阶乘的两种方式

发布于 2019-11-16  51 次阅读


本程序没有将结果pt在标准输出中,需要使用GDB查看

使用跳转指令实现

#as filename.s -o filename.o
#ld filename.o -o filename 
# gdb filename
 
.section .data
.section .text
.globl _start
_start:
	movl $1, %eax #result = 1
	movl $6, %ecx # n = 5
	cmpl $1, %ecx
	jle .loop_exit
 
.start_loop:
	imull %ecx, %eax
	subl  $1, %ecx
	cmpl $1, %ecx
	jge .start_loop
	movl %eax, %ebx
	movl $1, %eax
	int $0x80 
.loop_exit:
	movl $1, %eax
	int $0x80

函数递归

.section .data
.section .text
.globl _start
_start:
	pushl $5
	call .JC
 
	movl %eax, %ebx
	movl $1, %eax
	int $0x80
 
.JC:
	pushl %ebp
	movl %esp, %ebp
	pushl %ebx
	subl $4, %esp
	movl 8(%ebp), %ebx #get n = 5;
	
	movl $1, %eax      #result = 1;
	cmpl $1, %ebx	
	jle .L1					
	leal -1(%ebx), %eax		
	movl %eax, (%esp)		
	call .JC 
	imull %ebx, %eax
.L1:
	addl $4, %esp
	popl %ebx
	popl %ebp
	ret 
 

 

 

 

 

喜欢这篇文章吗,不妨分享给朋友们吧!

科学是第一生产力