130 likes | 296 Views
Computer Architecture & Organization. Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. Supporting Procedures in Computer Hardware.
E N D
Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. CA&O Lecture 04 by Engr. Umbreen Sabir
Supporting Procedures in Computer Hardware • Procedure: A stored subroutine that performs a specific task based on the parameters with which it is provided. • Procedure execution step • Place Parameters. • Transfer control to the procedure. • Acquire storage resources • Perform desired task. • Place result value. • Return control back. CA&O Lecture 04 by Engr. Umbreen Sabir
Supporting Procedures in Computer Hardware cont. • MIPS register conventions for procedures: • $a0 - $a3: argument registers • $v0 - $v1: two registers to return values. • $ra – return address register • MIPS instruction for procedures is jump and link instruction (jal). • It jumps to an address and simultaneously saves the address of following instruction in return register $ra. CA&O Lecture 04 by Engr. Umbreen Sabir
Instructions for Accessing Procedures • MIPS procedure call instruction:jal ProcedureAddress #jump n link • Machine format (J format): • Then can do procedure return with a jr $ra #return • Instruction format (R format): op 26 bit address op rs funct CA&O Lecture 04 by Engr. Umbreen Sabir
Procedure Register Requirements • Compiler need more registers for a procedure. • Spill registers to memory. • Structure for spilling registers is stack: • Stack pointer is needed. • Push n Pop are required. • $sp is used to save the registers needed by the callee. • Stacks grows from higher addresses to lower addresses. • Push by subtracting from stack. • Pop by adding to stack pointer. CA&O Lecture 04 by Engr. Umbreen Sabir
Compiling a C Procedure • C procedure is: int leaf_example( int g, int h, iny I, int j) { int f; f = ( g + h) – (i + j); return f; } • The parameters g, h, I, j and f are in $a0, $a1, $a2, $a3 and $s0. • Start with label leaf_example CA&O Lecture 04 by Engr. Umbreen Sabir
MIPS code is: • Save three registers • Addi $sp, $sp, -12 • Sw $t1, 8( $sp) • Sw $t0 4( $sp) • Sw $s0, 0( $sp) • Procedure body • Add $t0, $a0, $a1 • Add $t1, $a2, $a3 • Sub $s0, $t0, $t1 CA&O Lecture 04 by Engr. Umbreen Sabir
Compiling if-then-else into Conditional Branches: • Return value • Add $v0, $s0, $zero • Restore registers • Lw $s0, 0( $sp) • Lw $t0, 4($sp) • Lw $t1, 8($sp) • Return • Jr $ra CA&O Lecture 04 by Engr. Umbreen Sabir
Nested Procedures • Procedure that do not call others are called leaf procedures. • Caller pushes ($a0- $a3) and ($t0 - $t7). • Callee pushes $ra and any saved registers ($s0 - $s7) used by callee. CA&O Lecture 04 by Engr. Umbreen Sabir
Allocating Space for New Data on the Stack • Stack is also used to store variables that are local to procedure and that do not fit in the registers e.g. local arrays or structures. • The segment of stack containing a procedures saved registers and local variables is called a procedure frame or activation record. • MIPS frame pointer $fp points to the first word of the frame of a procedure. • $sp may change during procedure execution but frame pointer is a stable base register within a procedure for local memory references. CA&O Lecture 04 by Engr. Umbreen Sabir
Allocating Space for New Data on the Heap • Space is needed for static variables and for dynamic data structures. • Stack starts in the high end and grows down. • First part of the low end of memory is reserved. • Next is the portion for MIPS machine code called the text segment. • Above it is static data segment, which is the place for constants and other static variables. • Linked lists tend to grow and shrink their lifetimes, segment for such data is called heap and is placed next in memory. CA&O Lecture 04 by Engr. Umbreen Sabir
Next Lecture and Reminders • Next lecture • MIPS ISA • Assignment Due – 19 Feb 2009 CA&O Lecture 04 by Engr. Umbreen Sabir
END OF LECTURE 4 CA&O Lecture 04 by Engr. Umbreen Sabir