Download presentation
Presentation is loading. Please wait.
Published byLucas Chandler Modified over 8 years ago
1
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. 1
2
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. 13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir 2
3
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. 13/02/2009 3 CA&O Lecture 04 by Engr. Umbreen Sabir
4
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): 13/02/2009 4 CA&O Lecture 04 by Engr. Umbreen Sabir op rs funct op 26 bit address
5
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. 13/02/2009 5 CA&O Lecture 04 by Engr. Umbreen Sabir
6
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 13/02/2009 6 CA&O Lecture 04 by Engr. Umbreen Sabir
7
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 13/02/2009 7 CA&O Lecture 04 by Engr. Umbreen Sabir
8
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 13/02/2009 8 CA&O Lecture 04 by Engr. Umbreen Sabir
9
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. 13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir 9
10
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. 13/02/2009 10 CA&O Lecture 04 by Engr. Umbreen Sabir
11
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. 13/02/2009 11 CA&O Lecture 04 by Engr. Umbreen Sabir
12
Next Lecture and Reminders Next lecture – MIPS ISA Assignment Due – 19 Feb 2009 13/02/2009 12 CA&O Lecture 04 by Engr. Umbreen Sabir
13
END OF LECTURE 4 13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.