zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    MYSQL优化
    linux 基础知识 之基础系统管理2
    mysql数据库读写分离+高可用
    Varnish代理缓存服务器
    tomcat
    Memcached, Redis, MongoDB区别、特点、使用场景
    session共享
    基于docker的zabbix服务搭建
    php-fpm 启动后没有监听端口9000
    学习网站
  • 原文地址:https://www.cnblogs.com/bittorrent/p/2706482.html
Copyright © 2011-2022 走看看