943,511 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 7966
  • Assembly RSS
May 2nd, 2008
0

Factorial

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 2008
Jun 30th, 2009
-1

Re: Factorial

#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". */
Reputation Points: -10
Solved Threads: 1
Light Poster
nirav99 is offline Offline
28 posts
since Jun 2009
Jun 30th, 2009
0

Re: Factorial

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: MIPS: Print array in columns (SPIM)
Next Thread in Assembly Forum Timeline: Help Please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC