[prev] 23 [next]

Structs in MIPS (cont)

Structs that are local to functions are allocated space on the stack

fun:                         int fun(int x) 
   # prologue                {
   addi $sp, $sp, -4
   sw   $fp, ($sp)
   move $fp, $sp
   addi $sp, $sp, -4
   sw   $ra, ($sp)              // push onto stack
   addi $sp, $sp, -56           Student st;
   move $t0, $sp                Student *t0 = &st;
   # function body
   ... compute ...              // compute using t0
   # epilogue                   // to access struct
   addi $sp, $sp, 56            // pop st off stack
   lw   $ra, ($sp)
   addi $sp, $sp, 4
   lw   $fp, ($sp)
   addi $sp, $sp, 4
   jr   $ra                  }