Why Does MIPS Show a Negative Result for Large Factorials?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
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: 533
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   Reactions: 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   Reactions: rictor