MIPS calling convention

  • The Saved Registers Section of the stack frame contains space to save the values of any of the saved registers ($s0 to $s7) that the current subroutine wants to use.
//leaf procedure
leaf:
        addiu   $sp,$sp,-8 // $16 = $s0, $fp for frame pointer
        sw      $fp,4($sp)
        sw      $16,0($sp)
        move    $fp,$sp
        move    $8,$4
        move    $3,$5
        move    $4,$6
        move    $2,$7
        addu    $3,$8,$3
        addu    $2,$4,$2
        subu    $16,$3,$2
        move    $2,$16
        move    $sp,$fp
        lw      $fp,4($sp)
        lw      $16,0($sp)
        addiu   $sp,$sp,8
        jr      $31
        nop

Non-leaf procedure calling

  • caller need to save = return address
    • any arguments
    • temporaries needed after the call
int fact(int n) {
  if (n < 1) {
    return 1;
  }
  return n * fact(n - 1);
}

stack frame

  • $fp
  • saved argument register
  • stored return address
  • saved save registers
  • local arrays and structure
  • $sp

Memory layout

  • text (program code)
  • static data: global variable
    • by $gp register ()
  • dynamic data (heap)

Byte/halfword operation

" only last byte is filled (stored)

  • lb/lh rt, offset(rs): upper bytes are sign extened (sign extension) -
  • lbu/lhu rt, offset(rs): unsigned extend to 32bits (zero extended) -> used in character manipulation
  • sb/sh rt, offset(rs): zero extend to 32 bits

32-bit immediate values

  • dealing immediate values are I instruction: only 16bits for operand lui rt, constant
  • copies 16 bit constant to upper 16 bits of the rt register
  • load lower 16bits by or-ing ori rt, rt, immediate

Branch addressing

branch is relative addressing (branchaddr < 0 is possible) jump is absolute addressing (only accepts)

  • opcode, two register, target address

  • word size alignment : branch to 4 + pc + 4 * branchaddr

  • jump addressing

j instruction (op(6) + address(26)) target address = pc[31..28] : (address * 4)

80000 loop: s11 $t1, $s3, 2
80004       add t1, t1, s6
80008       lw t0, 0(t1)
80012       bne t0, s5,  exit // pc relative address: 2 bytes jump
80016       addi s3, s3, 1
80020       j loop
80024 exit:
  • branch maximum 16 bit address difference -> used jump instead