Factorial

Reply

Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Factorial

 
0
  #1
May 2nd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 26
Reputation: nirav99 has a little shameless behaviour in the past 
Solved Threads: 1
nirav99 nirav99 is offline Offline
Light Poster

Re: Factorial

 
-1
  #2
Jun 30th, 2009
#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". */
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Factorial

 
0
  #3
Jun 30th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC