Hello everyone. I haven't started to write the program yet but I have the guidelines to make it. They are :

Write a function in assembly language called “factorial” with the following prototype:

integer factorial(integer n)

Pre-Conditions:
“n” is an integer (positive or negative)
“n” has been passed in as a parameter via the stack.

Postconditions: Let the register EAX return the answer.

EAX will either be
a positive number (meaning a valid answer was found)
a -1 representing an error of “n” being negative
or a “-2” represent an overflow (ie an answer too large to be accommodated by 32 bits)

(NOTE: No input or output should happen inside of “factorial”, only the math operations and the returning of a answer via EAX)

Explanation of Factorial

Given an integer “n” the function should find the factorial of that integer. In other words the function should simulate the “n!” on your calculator. The factorial function (n!) means find the product of all numbers from 1 up to “n.”

For example:

3!=1 x 2 x 3 = 6 4! = 1 x 2 x 3 x 4 = 24

Important notes:

Factorials are only defined for positive numbers. So if your function is passed a negative number let your function return a “-1” representing an error code.

By definition the factorial of “0” is “1” (0! = 1)

Check for “overflow.” Factorial gets large very quickly.

For example:

8!= 1x2x3x4x5x6x7x8=40320

If “n” generates an overflow then let your function return a “-2” representing an “overflow” errorcode.

How to test your function:

Call your function from a main program. In the main program prompt the user to enter an integer (positive or negative). Then pass this integer (n) as a parameter (using the stack) to your factorial function. Your factorial function should then return its answer via EAX.

OUTPUT the following in MAIN

Based on the value of EAX, display either:

The factorial of “n” is #### (i.e. a EAX contain a valid answer)
The factorial of “-n” is undefined (i.e. EAX contains a “-1”)
Or
The factorial of “n” cannot be computed accurately because it’s answer has caused an overflow (i.e. EAX contains a “-2”).


Now i'm not too sure on how to do this so if somebody could give me some guidelines that'd be great. Thanks a lot!

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int x;

class factorial
{
    public:
        void fact();
};

void factorial::fact()
{
    clrscr();
    int f=1;
    int i;
    for(i=x;i>=1;i--)
    {
        f=f*i;
    }
    cout<<endl<<"Factorial:-- "<<f;
}

void main()
{
    clrscr();
    cout<<"Enter Number:-- ";
    cin>>x;

    factorial f1;   // Making an object of class factorial
    f1.fact();     //calling funtion fact() using object f1
    getch();
}

/* This Program is executed only for the value of x=1,2,3,4...7.  x>7 gives garbage value as x is an integer datatype, so the answer of factorial number is going out of range. For that you may take x as "long int" or "double". */

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
Wrong language, a YEAR too late, and NO FREAKING CODE TAGS!!!! :(

ALWAYS write your function in a highe level language first. Get the algorithms functioning properly.
THEN write it in assembly code.
Then run both functions and compare the results to validate the assembly!

So if using C/C++ write your function in pure C first (NOT C++), then take a pass at assembly. This isn't a slap it together type function so you need to have your logic verified and much quicker to do in C code first!

This article has been dead for over six months. Start a new discussion instead.