calculates factorial n! by recursive (gnu assembly)

學習軟體︰ GNU Assembly for Intel硬體平台︰ Intel作業平台︰ Linux、FreeBSD 和 Windows 的 MinGW 說明︰  1.市面上的組合語言書籍大多以 MASM 為主,缺乏介紹 GNU Assembly 的書。          2.GNU Assembly 除了用在 Intel CPU上,也有其它知名 CPU 的版本,如 MIPS、SPARC等等。          GNU Assembly 更可用在「嵌入式系統」的 ARM 或 Intel CPU上。          3.GNU Assembly 和 GNU C++ 連結方式在 Windows 和 Linux 上的做法都是一致的。          4.GNU 整套開發系列是跨平台而且免費的。
1 #Program Description:
 2 #Author:Jeremy
 3 #Creation Date:2012/9/25 10:21
 4 #Revisions:
 5 #Date: #Modified by:
 6 
 7 # calculates factorial
 8 # int fact(int n)
 9 # { if(n<=1) return (1); #終止條件,令一個標籤
10 # else return n*fact(n-1);
11 # }
12 
13 
14 .section .data 
15 msg: .asciz "計算n!,請輸入n:"
16 n: .int 0
17 ofmt:.asciz "%d"
18 .section .text# Using the stack
19 #
20 #用 global variable 
21 _main:
22 
23 .globl _main
24 movl $0, %ebx
25 pushl $msg
26 call _printf # printf("計算n!,請輸入n:")
27 addl $4, %esp
28 
29 pushl $n
30 pushl $ofmt
31 call _scanf # scanf("%d",&n)
32 addl $8, %esp
33 
34 movl n, %eax # n = %eax
35 pushl %eax
36 call fact 
37 addl $4, %esp
38 
39 pushl %ebx #printf("%d",&n)
40 pushl $ofmt 
41 call _printf
42 addl $8, %esp
43 
44 pushl $0     #exit(0)
45 call _exit
46 fact:
47 pushl %ebp 
48 movl %esp, %ebp
49 
50 movl 8(%ebp), %eax #read agrv
51 cmp $1, %eax # test for n<=1
52 jbe L1 # n <= 1 ,then go to fact_end, return n*fact(n-1)
53 subl $1, %eax
54 pushl %eax #fact(n-1)
55 call fact
56 movl 8(%ebp), %eax
57 imull %eax, %ebx    
58 jmp L2
59 L1:
60 addl $1, %ebx #if (n<=1) return 1 
61 L2:    
62 leave
63 ret
64 
65

ref:http://203.64.187.42/99-A/Asm-99A/Asm-99A-Notes/
原文链接: https://www.cnblogs.com/bittorrent/archive/2012/09/28/2706482.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/64022

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午11:15
下一篇 2023年2月9日 上午11:15

相关推荐