So I have been trying out nand2tetris, and got to the chapter about assembly language and how it can be used and implemented. I did the mult.asm successfully, and wanted to challenge myself to try to create a code that can find the factorial output of a number for any input by the user, so far this is all of my code
// ================================= Factorial ==================================
// This program calculates the Factorial F of a given number n
// At run time:
// - The user should enter the value of n into R0. R0.e. RAM[0]
// - The program then calculate the factorial of n R0.e. F(n)=n!
// F(n) = n*(n-1)*(n-2)*......*2*1
// - The result F(n) should be saved in RAM[1]
// -- You should also consider the F(0) case.
// ==============================================================================
// put your code here
//set tracker for current loop
@R1
M=0
@R2
M=1
@R3
M=0
@R4
M=0
@R5
M=0
@R6
M=0
(LOOP)
@R6
D=M //D=R6
@R1
M=M+D
@R2
D=M //D = R2
@R4
M=D //R4 = R2
@R0
D=D-M //D = R2 - R0
@END
D;JGE //If (R2-R0) > 0 goto END
@STEP
D;JLT //If (R2-R0) < 0 goto STEP
(STEP)
//Multiplication begins
// Gets R4 from R6.
@R6
D=M // wrting data of r6 into d register
// Add R1 to it.
@R1
D=D+M
// And write the result back to R6.
@R6
M=D
// Reduce R4 by 1.
@R4
D=M-1
M=D
// If R4 is still > 0 then loop.
@STEP
D;JGT
//Multiplication ends
@R2
M=M+1 //R2++
@LOOP
0;JMP //Got LOOP
(END)
@END
0;JMP //Infinite loop
The problem I have now is that the loop works, and the counter for my loop to execute the multiplication loop stops at the right time, but the output is somehow a few values higher than what is expected. I.E when I enter 3! it comes out with 8 instead of 6. Any advice would be great thanks!
I am trying to get a factorial output of any number that I put in, the counter is working correctly and stopping at the right time, but the final output is wrong
Some general comments: