Why Does MIPS Show a Negative Result for Large Factorials?

Click For Summary
SUMMARY

The MIPS assembly program for calculating factorials fails to produce correct results for large inputs, specifically when computing 20!. The output of -2102132736 indicates an overflow issue due to the use of a 32-bit signed integer, which cannot accommodate the value of 20!. The factorial of 20 requires more than 62 bits, leading to truncation and incorrect negative output. The problem arises from calculating the factorial using 64-bit arithmetic but displaying it as a 32-bit integer.

PREREQUISITES
  • Understanding of MIPS assembly language
  • Knowledge of integer overflow and data types
  • Familiarity with factorial computation algorithms
  • Experience with debugging assembly code
NEXT STEPS
  • Research MIPS data types and their limitations
  • Learn about handling large integers in MIPS assembly
  • Explore techniques for preventing integer overflow in programming
  • Investigate alternative methods for computing factorials in MIPS
USEFUL FOR

Students and developers working with MIPS assembly language, programmers dealing with integer overflow issues, and anyone interested in optimizing mathematical computations in low-level programming.

rictor
Messages
7
Reaction score
0
when I try to compile the MIPS instruction in order to compute the factorial of n number? i get the following error in the console as:
Enter the factorial number: 20
factorial of given number: -2102132736
The output which I am getting as negative integer of a given factorial instead of positive integer.
The program for the given MIPS instructions is seen in the link below:
https://gist.github.com/dcalacci/3747521
 

Attachments

  • upload_2016-11-10_1-7-44.png
    upload_2016-11-10_1-7-44.png
    1.5 KB · Views: 496
Engineering news on Phys.org
Looks like a size or a formatting problem:
2102132736 is 31 bits and -2102132736 is 32 bits interpreted as a 4 byte integer (first bit a sign bit)

For 20! you need more than 62 bits
 
  • Like
Likes rbelli1 and rictor
20! truncated to 32 bits is -2102132736. It looks like you are calculating to 64 bits then displaying 32 bits.

BoB
 
  • Like
Likes rictor

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K