MIPS Programming - Lecture Slides | CS 365, Study notes of Computer Architecture and Organization

Material Type: Notes; Class: Computer Systems Architecture; Subject: Computer Science; University: George Mason University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/12/2009

koofers-user-mrj
koofers-user-mrj 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 365 1
MIPS Programming
CS 365 Lecture 3
Prof. Yih Huang
CS 365 2
Exercise 1
Create a routine at address 1000 to add up 1 to 10.
Store the result in 1FF0
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download MIPS Programming - Lecture Slides | CS 365 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

CS 365 1

MIPS Programming

CS 365 Lecture 3

Prof. Yih Huang

CS 365 2

Exercise 1

 Create a routine at address 1000 to add up 1 to 10.

Store the result in 1FF

CS 365 3

Procedure Call Conventions

 Passing arguments/parameters

  • 4 parameters are passed in registers $4 - $7 ($a0 - $a3)
  • Additional ones in memory

 Returning results

  • Results are returned in register $v

 Those are conventions.

CS 365 4

Procedure Frames

 A procedure frame contains all data pertaining to “one instance” of a procedure (one clone, one procedure frame).

  • Local variables (in registers or memory)
  • Parameters (in registers or memory)
  • Return address  Procedure frames are stored in a stack.

 Register $29 ($sp, for stack pointer) points to the top location of the stack

CS 365 7

Procedure Frames on MIPS

Argument 6 Argument 5

Saved registers

Local variables

Higher memory addresses

Lower memory addresses

Stack grows

$fp

$sp

CS 365 8

Duties of a MIPS Caller

  1. Save on the stack registers $a0-$a3,

$t0-$t9 (they are called caller-saved registers)

  1. Pass arguments. First 4 are in $a0-$a3.

Remaining are pushed on to stack and

appear at the beginning of called procedure’s stack frame

  1. Execute jal

CS 365 9

Duties of a MIPS Callee

 Before it starts running

  1. Allocate memory for stack frame
  2. Save $s0-$s7, $fp, $ra in the stack frame (they are called callee-saved)
  3. Establish frame pointer  Before returning
  4. Place return value in $v
  5. Restore all callee-saved registers
  6. Pop the stack frame
  7. Return by jumping to $ra

CS 365 10

saved fp

Example:

The Frame of G() on

the MIPS Platform

 G() has

  • 1 parameter ppp
  • 3 local variables xx uses $t yy uses $t zz uses $s
  • A return value

$ra $s xx

-4($fp) -8($fp) -12($fp) -16($fp)

High Memory

Low Memory

yy -20($fp) zz $sp

$fp

CS 365 13

0($fp) saved fp $ra $s xx

-4($fp) -8($fp) -12($fp) -16($fp) yy -20($fp) zz Default $sp in G (^) $t $t

p p p $sp when G calls F

Caller saved registers of G()

My $fp

$a

When G()

Calls F()

Arguments to F()

CS 365 14

The Code for G() to Call F()

sw $t1, 0($sp) // backup my $t

sw $t2, -4($sp) // backup my $t

sw $a0, -8($sp) // backup my $a0 (ppp)

// copy p1 to p4 to $a0 to $a

// Store p7, p6, p5 in -12($sp), -16($sp), -20($sp)

jal f // Label-X stored in $ra

addi $sp, $sp, -24 // Delay slot of jal

addi $sp, $sp, 24 // restore the normal $sp

lw $t1, 0($sp) // restore my $t

lw $t2, -4($sp) // restore my $t

lw $a0, -8($sp) // restore my $a

// G() continues

Label-X :

CS 365 15

y

$s3 of G’s caller $ra of G’s caller $fp of G’s caller

zz yy xx

$s3 of G $ra of G $fp of G P5 of F P6 of F P7 of F $a0 of G $t2 of G $t1 of G x

calling Fwhen $sp of G $sp of G $fp of G

$sp of F $fp of F

Grows Stack

CS 365 16

Exercise: Give the Entrance Code of F()