Factorial Program in AssemblyMy Comsats

Factorial Program in Assembly

Factorials are used all the time in math. Have you ever wanted to make a program that could calculate the factorial of a number you want. Well here is an assembly code that will calculate the factorial of 5. If you want to change the number of which you want the factorial just make some minor adjusts in the code.

Factorial Program in Assembly CODE and Explanation

We will write the header files in the start. The header files here are of PCSPIM. If you want to make this code in AVR you can just change the header files to your requirement.

.data.text.globl mainmain:li $t0,5 //you can change the number to any number you want the factorial to be.

We subtract the number by one and save it to a new register so it can be multiplied for the final result. If you change the number you will also have to subtract it that many number of times.

sub $t1,$t0,1    
sub $t2,$t1,1   
sub $t3,$t2,1
sub $t4,$t3,1

we multiply the two registers and store it in a register. After that the result containing register will be multiplied with the rest and the results rewritten on it.

        mul $t5,$t0,$t1  
        mul $t5,$t5,$t2
        mul $t5,$t5,$t3
        mul $t5,$t5,$t4
        move $a0,$t5  
        li $v0,1      
        li $v0,10      

Let's Discuss

  • http://www.buycabledeals.com/internet-ca/high-speed-internet-california/ High Speed Internet California

    Thank you very much for sharing your knowledge on this subject. I agree with a great deal of your material. This is very good information.