1 / 14

Computer Architecture & Operations I

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

aviv
Download Presentation

Computer Architecture & Operations I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Instructor: Yaohang Li Computer Architecture & Operations I

  2. Review • Last Class • Procedure Call • Leaf Procedure • This Class • Quiz • Non-leaf Procedure • Next Class • Characters • Starting a Program • Linking

  3. 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

  4. 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

  5. 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

  6. What is preserved and what is not? • Data and registers preserved and not preserved across a procedure call

  7. 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

  8. 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

  9. 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

  10. 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

  11. Memory Layout

  12. Register Summary • Register 1: $at • reserved for the assembler • Register 26-27: $k0-$k1 • reserved for the OS

  13. Summary • Procedure Call • Registers used • Stack • jal and jr • leaf and no-leaf procedure • Allocating space for new data on the heap

  14. What I want you to do • Review Chapter 2 • Work on your assignment 5

More Related