汇编语言-求X的阶乘

博客介绍了如何使用汇编语言求解输入整数的阶乘值,特别适合初学者。首先提出了题目要求,即输入不超过10的整数,然后通过循环结构实现阶乘计算。建议先用C++等高级语言理解算法,再转为汇编代码。文中给出了相应的C++和汇编代码示例,并提供了测试结果。

1. 题目:求X的阶乘值

2. 要求:输入一个整型数(不超过10),求出其阶乘值后输出,求阶乘的算法用子程序来实现。

3. 提示:可以用递归来实现,也可以用简单的循环来实现。

这里使用循环来实现:

对于汇编新手,最好通过高级语言的编程测试,然后再写汇编代码,这样效果会好一些、

求阶乘的C++代码如下:

 1 //The program is to find the factorial from 1 to 10
 2 //author:Karllen
 3 //Date:  05/21/2014
 4 
 5 #include <iostream>
 6 
 7 int factorial(int n);
 8 
 9 int main()
10 {
11     int n;
12     std::cin>>n;
13     std::cout<<factorial(n)<<std::endl;
14 
15     system("pause");
16     return 0;
17 }
18 
19 int factorial(int n)
20 {
21     int sum = 1;
22     while (n!=1)
23     {
24         sum*=n;
25         --n;
26     }
27     return sum;
28 }

汇编代码如下:

 1 ; Example assembly language program -- adds two numbers
 2 ; Author:  Karllen
 3 ; Date:    revised 05/2014
 4 
 5 .386
 6 .MODEL FLAT
 7 
 8 ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
 9 
10 INCLUDE io.h            ; header file for input/output
11 
12 cr      EQU     0dh     ; carriage return character
13 Lf      EQU     0ah     ; line feed
14 
15 .STACK  4096            ; reserve 4096-byte stack
16         
17 .DATA                   ; reserve storage for data
18         prompt   BYTE "The program is to find the factorial from 1 to 10",cr,Lf,0
19         numInput BYTE "Please enter a number from 1 to 10",cr,Lf,0
20         answer   BYTE "The number factorial is"
21         value    BYTE 11 DUP(?)
22                  BYTE cr,Lf,0
23 
24 PUBLIC _start         
25 .CODE 
26 _start:
27                                 ; start of main program code
28         output prompt 
29         
30         doInput:
31            output numInput
32            input  value,11
33            atod   value
34            cmp    eax,1
35            jl     doInput
36            cmp    eax,10
37            jg     doInput
38         push   eax
39         call   findFactorial
40         add    esp,4
41         
42         dtoa   value,eax
43         output answer 
44         
45         INVOKE  ExitProcess, 0  ; exit with return code 0
46                   ; make entry point public
47         
48  findFactorial  PROC NEAR32
49                   push ebp
50                   mov  ebp,esp
51                   
52                   mov  eax,[ebp+8]
53                   mov  ebx,eax
54                   cmp  eax,1
55                   je   endFindWhile     
56                   doFindWhile:
57                            dec ebx
58                            cmp ebx,1
59                            je  endFindWhile
60                            mul ebx
61                            jmp doFindWhile
62                   endFindWhile:          
63                   pop  ebp
64                   ret
65    findFactorial  ENDP
66 END                             ; end of source code

测试结果:

转载于:https://www.cnblogs.com/Forever-Kenlen-Ja/p/3744109.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值