Factorial 8

Finds the factorial of the number 8 using recursion. The number 8 was chosen as its factorial is the largest number that will fit in 2 bytes - for easy reading in the memory panel.

Processor: ATtiny 2313

This example was compiled for the 2313 but will run on all processors.*
The calculation takes 1300 cycles and uses the following algorithm:

long factorial(int n);
long result;

int main(void){
  long fact = factorial(8);
  result = fact;
  
  return 0;
}

long factorial(int n){
  if(n != 1)
    return(n * factorial(n-1));
  else
    return 1;
}

The result of the calculation is stored in ram at location 0x60. To best see the result configure the memory panel to show words ( 2 bytes ) rather than bytes and switch to decimal representation.

*If run on the ATmega238 this location is in the I/O memory rather than sram.