[prev] 24 [next]

Passing structs to Functions

C can pass whole structures to functions, e.g.

# Student stu; ...
# // set values in stu struct
# showStudent(stu);

   .data
stu: .space 56
   .text
   ...
   la   $t0, stu
   addi $sp, $sp, -56    # push Student object onto stack
   lw   $t1, 0($t0)      # allocate space and copy all
   sw   $t1, 0($sp)      # values in Student object
   lw   $t1, 4($t0)      # onto stack
   sw   $t1, 4($sp)
   ...
   lw   $t1, 52($t0)     # and once whole object copied
   sw   $t1, 52($sp)
   jal  showStudent      # invoke showStudent()
   ...