Misp Procedure Calls
Misp Procedure Calls
Module Outline
Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions
(2)
Reading
Reading 2.8, 2.12 Appendix B: B1 - B.6
(3)
Procedure Calls
Basic functionality
Transfer of parameters & control to procedure Transfer of results & control back to the calling program Support for nested procedures
(4)
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
Register usage What about nested calls? What about excess arguments?
loop:
PC + $31 4
PC $31
func: exit:
(6)
(7)
jal ProcedureLabel
Address of following instruction put in $ra Jumps to target address
jr $ra
Copies $ra to program counter Can also be used for computed jumps
o e.g., for case/switch statements
Example:
(8)
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
Procedure body
(9)
$sp $sp
stack
$fp
arg registers return address dynamic data
$gp
Saved registers
PC
$sp
local variables
compiler
compiler
ISA
Low Address
HW
addressing
(10)
..
callee $s0-$s9 saved registers caller $a0-$a3 saved registers $t0-$t9 local variables
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
(11)
..
$fp $sp $ra
(12)
$gp: global pointer for static data (reg 28) $sp: stack pointer (reg 29) $fp: frame pointer (reg 30) $ra: return address (reg 31)
(13)
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
(14)
(15)
(16)
(17)
Module Outline
Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions
(19)
linker executable
loader memory
(20)
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
Example:
Assembly Process
One pass vs. two pass assembly Effect of fixed vs. variable length instructions
(22)
Example
.data
L1: .word 0x44,22,33,55 # array .text .globl main main: la $t0, L1 li $t1, 4 add $t2, $t2, $zero lw $t3, 0($t0) add $t2, $t2, $t3 addi $t0, $t0, 4 addi $t1, $t1, -1 bne $t1, $zero, loop bgt $t2, $0, then move $s0, $t2 j exit move $s1, $t2 li $v0, 10 syscall
loop:
lui $8, 4097 [L1] ori $9, $0, 4 add $10, $10, $0 lw $11, 0($8) add $10, $10, $11 addi $8, $8, 4 addi $9, $9, -1 bne $9, $0, -16 [loop-0x0040001c] slt $1, $0, $10 bne $1, $0, 12 [then-0x00400024] addu $16, $0, $10 j 0x00400034 [exit] addu $17, $0, $10 ori $2, $0, 10 syscall
Assembled Binary
Native Instructions
then: exit:
(23)
Loader
As the name implies Specifics are operating system dependent
(24)
Linking
Program A Program B
header text
Assembly A
static data
Assembly B
What are the issues with respect to independent compilation? references across files (can be to data or code!)
absolute addresses and relocation
(25)
Example:
# separate file .text addi $4, $0, 4 addi $5, $0, 5 jal func_add done 0x20040004 0x20050005 000011 0x0340200a 0x0000000c
0x00400000
0x00400004 0x00400008 0x0040000c 0x00400010 0x00400014 0x00400018 Ans: 0c100005
0x20040004
0x20050005 ? 0x3402000a 0x0000000c 0x008551020 0x03e00008
# separate file .text .globl func_add func_add: add $2, $4, $5 0x00851020 jr $31 0x03e00008
(26)
Loading a Program
Load from image file on disk into memory
1. Read header to determine segment sizes
(27)
Dynamic Linking
Static Linking
All labels are resolved at link time Link all procedures that may be called by the program Size of executables?
(28)
Lazy Linkage
Indirection table
Stub: Loads routine ID, Jump to linker/loader Linker/loader code
(29)
0x1F
Data segment (static) Text Segment Programmer Invisible State Program Counter Instruction register Kernel registers Arithmetic Logic Unit (ALU)
0xFFFFFFFF
Reserved
Memory Map
Summary
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
(31)
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
(32)
How are independently compiled modules linked into a single executable? (assuming one calls a procedure located in another)
(33)
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
(34)