140 likes | 242 Views
Instructor: Yaohang Li. Computer Architecture & Operations I. Review. Last Class Procedure Call Leaf Procedure This Class Quiz Non-leaf Procedure Next Class Characters Starting a Program Linking. Non-Leaf Procedures. Procedures that call other procedures
E N D
Instructor: Yaohang Li Computer Architecture & Operations I
Review • Last Class • Procedure Call • Leaf Procedure • This Class • Quiz • Non-leaf Procedure • Next Class • Characters • Starting a Program • Linking
Non-Leaf Procedures • Procedures that call other procedures • For nested call, caller needs to save on the stack: • Its return address • Any arguments and temporaries needed after the call • Restore from the stack after the call
Non-Leaf Procedure Example • C code: int fact (int n){ if (n < 1) return 1; else return n * fact(n - 1);} • Argument n in $a0 • Result in $v0
Non-Leaf Procedure Example • MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and returnL1: addi $a0, $a0, -1 # else decrement n = n - 1 jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return
What is preserved and what is not? • Data and registers preserved and not preserved across a procedure call
Global Pointer • Two kinds of C/C++ variables • automatic • Local to a procedure • Discarded when the procedure exits • static • Global to a procedure • Still exist after procedure exits • Can be revisited • Global Pointer • $gp • Point to static area
Procedure Frame • Revisiting Stack • Stack not only stores the saved registers • but also local variables that do not fit in registers • local arrays or structures • Procedure Frame (activation record) • Segment of the stack containing a procedure’s saved registers and local variables • Frame pointer • Point to the location of the saved registers and local variables for a given procedure
Local Data on the Stack • Local data allocated by callee • e.g., C automatic variables • Procedure frame (activation record) • Used by some compilers to manage stack storage
MIPS Memory Layout • 32-bit address space • 0x80000000 ~ 0xFFFFFFFF • Not available for user program • For OS and ROM • 0x00000000~0x003FFFFF • Reserved • 0x00400000~0x0FFFFFFF • Text: Machine language of the user program • 0x10000000~0x7FFFFFFF • Data • Static • Static variables • Constants • Dynamic • Malloc() in C, New in java • Stack
Register Summary • Register 1: $at • reserved for the assembler • Register 26-27: $k0-$k1 • reserved for the OS
Summary • Procedure Call • Registers used • Stack • jal and jr • leaf and no-leaf procedure • Allocating space for new data on the heap
What I want you to do • Review Chapter 2 • Work on your assignment 5