CS232: Lecture 4 (Functions in MIPS)

int main() {                                int fact(int n) {                
  ...                                         int i, f = 1;                        
  t1 = fact(8);                               for (i = n; i > 1; i--)        
  t2 = fact(3);                                 f = f * i;                        
  t3 = t1 + t2;                               return f;                        
  ...                                       }                                
  return 0;
}

int main() {                 main:
  ...



  t1 = fact(8);






  t2 = fact(3);



  t3 = t1 + t2;                     add        $t3, $t1, $t2



  ...              
  return 0;
}


int fact(int n) {            fact:                                                
  int i, f = 1;                     li     $t0, 1          # f = 1                

                                    move   $t1, ___        # i = n                
  for (i = n; i > 1; i--)    loop:  ble    $t1, 1, ret     # i > 1                                                        
    f = f * i;                      mul    $t0, $t0, $t1   # f = f _ i        

                                    sub    $t1, $t1, 1     # i--                
                                    j      loop                                

  return f;                  ret:                                                

}