#include #include "db_utils.h" #include "registers.h" #include int fac(int); int i; /* Global */ int main(void) { char buffer[10]; while(1) { DBPRINTF("\nPlease input an integer (negative to quit):\n"); i = 0; /* buffer is not reinitialized */ while (i++ <= 9) buffer[i] = '\0'; DBGETS(buffer,10); i = atoi(buffer); if (i < 0) break; if (i > 12) {DBPRINTF("\nToo large!\n"); continue;} DBPRINTF("Factorial(%d) = %d\n", i, fac(i)); }; /* end while */ DBPUTS("\nProgram terminated. \n"); DBPUTS("Click HALT=F5 and then RESET=F6 to stop the microcontroller.\n"); return 0; } /* end main */ int fac(int n) /* Modified factorial.s from page 80 of PGA */ { asm(" 1: addiu $sp, $sp, -12 ;\ sw $ra, 0($sp) ;\ sw $a0, 4($sp) ;\ sw $s0, 8($sp) ;\ beqz $a0, 2f ;\ addiu $a0, $a0, -1 ;\ jal 1b ;\ lw $s0, 4($sp) ;\ mult $v0, $s0 ;\ mflo $v0 ;\ j 3f ;\ 2: addiu $v0, $0, 1 ;\ 3: lw $s0, 8($sp) ;\ lw $a0, 4($sp) ;\ lw $ra, 0($sp) ;\ addiu $sp, $sp, 12 ;\ jr $ra ;\ " ); }