Assembly language-finding the factorial of X

Source: Internet
Author: User

1. title: Finding the factorial value of X

2. requirements: input An integer number (not more than 10), to find its factorial value after the output, the factorial algorithm is implemented with Subroutines.

3. Hint: can be implemented by recursive return, can also be implemented with a simple loop.

This is accomplished by using loops:

For the novice assembly, it is best to test the programming in a high-level language and then write the assembly code so that the effect is better,

The C + + code to find factorial is as follows:

1//the program was to find the factorial from 1 to 2//author:karllen 3//date:  05/21/2014 4  5 #include <ios Tream> 6  7 int factorial (int n); 8  9 int main () {one     int n;12     std::cin>>n;13     std::cout <<factorial (n) <<std::endl;14     system ("pause"),     return 0;17}18 int factorial (int n) 20 {21     int sum = 1;22 while     (n!=1) @     {         sum*=n;25         --n;26     }27     return sum;28}

The assembly code is as Follows:

 1; Example assembly language program--adds, numbers 2; Author:karllen 3; date:revised 05/2014 4 5.386 6. MODEL FLAT 7 8 exitprocess PROTO NEAR32 stdcall, dwexitcode:dword 9 INCLUDE io.h; Header file for INPUT/OUTPUT11-CR EQU 0dh; Carriage return character13 Lf EQU 0ah; Line FEED14 15. STACK 4096; Reserve 4096-byte Stack16 17. DATA; Reserve storage for Data18 prompt BYTE "the program was to find the factorial from 1 to ten", cr,lf,019 num    Input byte "please Enter a number from 1 to ten", cr,lf,020 answer byte "the number factorial is" BYTE-by-one DUP (?) BYTE cr,lf,023 public _start 25. CODE _start:27;            Start of main program code28 output prompt doinput:31 output numInput32   Input value,1133 atod value34 CMP eax,135 JL doInput36 CMP eax,1037 JG doInput38 push eax39 C         All findFactorial40 add esp,441 dtoa value,eax43 output Answer 44 45 INVOKE exitprocess, 0; Exit with return code 046; Make entry point public47 findfactorial PROC NEAR3249 push EBP50 mov ebp , ESP51 mov eax,[ebp+8]53 mov ebx,eax54 cmp E                            ax,155 JE endfindwhile dofindwhile:57 Dec ebx58                            CMP ebx,159 JE endFindWhile60 mul ebx61                   jmp doFindWhile62 endfindwhile:63 Pop Ebp64                       Ret65 findfactorial ENDP66 END      ; End of source code

Test Results:

Assembly language-finding the factorial of X

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.