0 évaluation0% ont trouvé ce document utile (0 vote)
22 vues17 pages
ISA-II: procedure calls & Program Assembly. Review ISA and understand instruction encodings. Review memory organization memory (data movement) instructions control flow instructions. Procedure Calls Basic functionality! Transfer of parameters and control to procedure. Transfer of results and control back to the calling program. Support for nested procedures! the stack, $fp, $sp and $ra.
ISA-II: procedure calls & Program Assembly. Review ISA and understand instruction encodings. Review memory organization memory (data movement) instructions control flow instructions. Procedure Calls Basic functionality! Transfer of parameters and control to procedure. Transfer of results and control back to the calling program. Support for nested procedures! the stack, $fp, $sp and $ra.
ISA-II: procedure calls & Program Assembly. Review ISA and understand instruction encodings. Review memory organization memory (data movement) instructions control flow instructions. Procedure Calls Basic functionality! Transfer of parameters and control to procedure. Transfer of results and control back to the calling program. Support for nested procedures! the stack, $fp, $sp and $ra.
& Program Assembly (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding 2 (3) Reading Reading 2.8, 2.12 Appendix B: B1 - B.6 (4) Procedure Calls Basic functionality ! Transfer of parameters & control to procedure ! Transfer of results & control back to the calling program ! Support for nested procedures What is so hard about this? ! Consider independently compiled code modules o Where are the inputs? o Where should I place the outputs? o Recall: What do you need to know when you write procedures in C? 3 (5) Specifics Where do we pass data ! Preferably registers " make the common case fast ! Memory as an overflow area Nested procedures ! The stack, $fp, $sp and $ra ! Saving and restoring machine state Set of rules that developers/compilers abide by ! Which registers can am I permitted to use with no consequence? ! Caller and callee save conventions for MIPS (6) Basic Parameter Passing Register usage What about nested calls? What about excess arguments? .data arg1: .word 22, 20, 16, 4 arg2: .word 33,34,45,8
$31 $31 PC PC + 4 4 (7) Leaf Procedure Example C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } ! Arguments g, , j are passed in $a0, , $a3 ! f in $s0 (we need to save $s0 on stack we will see why later) ! Results are returned in $v0, $v1 $a0 $a1 $a2 $a3 $v0 $v1 argument registers result registers procedure (8) Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel ! Address of following instruction put in $ra ! Jumps to target address Procedure return: jump register jr $ra ! Copies $ra to program counter ! Can also be used for computed jumps o e.g., for case/switch statements Example: 5 (9) Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Restore $s0 Result Return (10) Procedure Call Mechanics Old Stack Frame arg registers return address Saved registers local variables New Stack Frame $fp $fp $sp $sp Low Address High Address compiler ISA HW compiler addressing $gp PC $sp stack dynamic data static data text reserved System Wide Memory Map 6 (11) Example of the Stack Frame Call Sequence 1. place excess arguments 2. save caller save registers ($a0-$a3, $t0-$t9) 3. jal 4. allocate stack frame 5. save callee save registers ($s0-$s9, $fp, $ra) 6 set frame pointer Return 1. place function argument in $v0 2. restore callee save registers 3. restore $fp 4. pop frame 5. jr $31 arg 1 arg 2 .. callee saved registers
caller saved registers
local variables .. $fp $ra $s0-$s9 $a0-$a3 $t0-$t9 $fp $sp (12) Policy of Use Conventions Name Register number Usage $zero 0 the constant value 0 $v0-$v1 2-3 values for results and expression evaluation $a0-$a3 4-7 arguments $t0-$t7 8-15 temporaries $s0-$s7 16-23 saved $t8-$t9 24-25 more temporaries $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address 7 (13) Summary: Register Usage $a0 $a3: arguments (regs 4 7) $v0, $v1: result values (regs 2 and 3) $t0 $t9: temporaries ! Can be overwritten by callee $s0 $s7: saved ! Must be saved/restored by callee $gp: global pointer for static data (reg 28) $sp: stack pointer (reg 29) $fp: frame pointer (reg 30) $ra: return address (reg 31) (14) 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 8 (15) Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } ! Argument n in $a0 ! Result in $v0 (16) Template for a Procedure 1. Allocate stack frame (decrement stack pointer) 2. Save any registers (callee save registers) 3. Procedure body (remember some arguments may be on the stack!) 4. Restore registers (callee save registers) 5. Pop stack frame (increment stack pointer) 6. Return (jr $ra) 9 (17) Non-Leaf Procedure Example int fact (int n) { callee save if (n < 1) return f; else return n * fact(n - 1); restore } (18) 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 return L1: addi $a0, $a0, -1 # else decrement n 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 Callee save Termination Check Leaf Node Recursive call Intermediate Node 10 (19) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding (20) The Complete Picture C program compiler Assembly assembler Object module executable linker loader memory Object libarary Reading: 2.12, B2, B3, B4, B5 11 (21) The Assembler Create a binary encoding of all native instructions ! Translation of all pseudo-instructions ! Computation of all branch offsets and jump addresses ! Symbol table for unresolved (library) references Create an object file with all pertinent information Header (information) Text segment Data segment Relocation information Symbol table Example: (22) One pass vs. two pass assembly Effect of fixed vs. variable length instructions Time, space and one pass assembly Local labels, global labels, external labels and the symbol table ! What does mean when a symbol is unresolved? Absolute addresses and re-location Assembly Process 12 (23) Example .data L1: .word 0x44,22,33,55 # array
Assembly Program Native Instructions Assembled Binary What changes when you relocate code? (24) Linker & Loader Linker ! Links independently compiled modules ! Determines real addresses ! Updates the executables with real addresses Loader ! As the name implies ! Specifics are operating system dependent 13 (25) Linking Why do we need independent compilation? What are the issues with respect to independent compilation? references across files (can be to data or code!) absolute addresses and relocation Program A Program B Assembly A Assembly B cross reference labels header text static data reloc symbol table debug Study: Example on pg. 143 (26) Example: # separate file
14 (27) Loading a Program Load from image file on disk into memory 1. Read header to determine segment sizes 2. Create virtual address space 3. Copy text and initialized data into memory o Or set page table entries so they can be faulted in 4. Set up arguments on stack 5. Initialize registers (including $sp, $fp, $gp) 6. Jump to startup routine o Copies arguments to $a0, and calls main o When main returns, do exit syscall (28) Dynamic Linking Static Linking ! All labels are resolved at link time ! Link all procedures that may be called by the program ! Size of executables? Dynamic Linking: Only link/load library procedure when it is called ! Requires procedure code to be relocatable ! Avoids image bloat caused by static linking of all (transitively) referenced libraries ! Automatically picks up new library versions 15 (29) Lazy Linkage Indirection table Stub: Loads routine ID, Jump to linker/loader Linker/loader code Dynamically mapped code (30) The Computing Model Revisited 0xFFFFFFFF Arithmetic Logic Unit (ALU) 0x00 0x01 0x02 0x03 0x1F Processor Internal Buses Memory Interface Register File (Programmer Visible State) stack Data segment (static) Text Segment Dynamic Data Reserved Program Counter Programmer Invisible State Kernel registers Program Execution and the von Neumann model Memory Map Instruction register 16 (31) Instruction complexity is only one variable ! lower instruction count vs. higher CPI / lower clock rate Design Principles: ! simplicity favors regularity ! smaller is faster ! good design demands compromise ! make the common case fast Instruction set architecture ! a very important abstraction indeed! Summary (32) Study Guide Compute number of bytes to encode a SPIM program What does it mean for a code segment to be relocatable? Identify addresses that need to be modified when a program is relocated. ! Given the new start address modify the necessary addresses Given the assembly of an independently compiled procedure, ensure that it follows the MIPS calling conventions, modifying it if necessary 17 (33) Study Guide (cont.) Given a SPIM program with nested procedures, ensure that you know what registers are stored in the stack as a consequence of a call Encode/disassemble jal and jr instructions Computation of jal encodings for independently compiled modules How can I make procedure calls faster? ! Hint: What about a call is it that takes time? How are independently compiled modules linked into a single executable? (assuming one calls a procedure located in another) (34) Glossary Argument registers Caller save registers Callee save registers Disassembly Frame pointer Independent compilation Labels: local, global, external Linker/loader Linking: static vs. dynamic vs. lazy Native instructions Nested procedures Object file One/two pass assembly Procedure invocation Pseudo instructions Relocatable code Stack frame Stack pointer Symbol table Unresolved symbol