#include "am_com.h"
#include "am_init.h"
#include "c8051F120.h"                          // Device-specific SFR Definitions
#include "c8051F120_io.h"                       // Device-specific SFR Definitions

#include <stdio.h>
#include <stdlib.h>

/* comment/uncomment __reentrant in the factorial function and run again */
unsigned long int factorial(unsigned long int n) // __reentrant 
  unsigned long int partial;                    // this function is written using excessive
  unsigned long int result = 1;                 // number of instructions in order to emulate
  if (n>1)                                      // a more complex real life function that
  {                                             // needs to use several paramanters and/or
    partial = factorial(n-1);                   // local variables so that the register-based
    result  = n * partial;                      // local variable space is exhausted

void main(void)
    xdata char    buffer[8];                    // SDCC sprintf requires buffer in xdata

	unsigned char number;
    long result;

    // Disable watchdog timer
    WDTCN = 0xde;
    WDTCN = 0xad;

    // Initialize the MCU
    UART1_Init ();

    UART1_puts("System Ready!\n");

    UART1_puts("Please enter a number to compute ");
	UART1_gets(buffer, 8);
	result = factorial(number);
    sprintf(buffer, "%ld", result);
    UART1_puts("The result is ");

    UART1_puts("System halted!\n");
    while(1) ;