Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701 E-mail: karanth@ohio.edu Website: http://oucsace.cs.ohiou.edu/~avinashk/classes/ee461a/ee461a.htm Course Administration Homework 2 posted, please see class webpage for deadlines All assignment work to be done individually, NO COLLABORATION is allowed No classes on 9/19 and 9/21 next lecture posted. Read on your own. Review/Quiz on Mon 9/24 1
Converting Function Calls in Assembly Need to pass parameters to the called function Need to save return address of caller Need to save register values Need to get return values Restore register values Instructions for Accessing Procedures MIPS Procedure Call instruction jal Procedure Address # Jump and Link Saves PC+4 in register $ra to have a link to the next instruction for the procedure return Machine Format (J Format) op 26-bit address Then do a procedure return with a return $ra #return Instruction format (R Format) op rs funct 2
How to use Stack? PUSH: First decrement the stack pointer, and then store value POP: For reverse, first load the value, and then increment Assume $s0=000002 needs to be saved onto stack $sp PUSH: subi $sp, $sp, 4 sw $s0, 0($sp) $sp 0x02 0x103 0x102 0x101 0x100 0x23 0xFF 0xDB 0x2A 0x103 0x102 0x101 0x100 POP: lw $s0, 0($sp) addi $sp, $sp, 4 $sp 0x02 0x103 0x102 0x101 0x100 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 in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 3
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 Result Restore $s0 Return Call Stack MIPS conventions (and most other processors) allocate a region of memory called the call stack Parameters (that were not passed through registers) Local variables Temporary storage (run out of registers and need to save a value) Return address Sections of the memory on the call stack (stack frames) are allocated when you make a function call, and de-allocated when you return from a function 4
MIPS Stack Frame $ FP Incoming Parameters $ FP Stack Return Address $ SP Spilled Registers Heap Local Variables Static $ SP Outgoing Parameters Text Allocating Space to Local Variables Local variables (by default) are created when you enter a function and disappear when you leave How does the allocation take place? By incrementing and decrementing the stack pointer subi $sp, $sp, 12 \\ allocate space for 3 integer locals addi $sp, $sp, 12 \\ de-allocate space for locals 5
The stack grows as functions are called void time { int x, y[2]; bar(x); void bar(int x) { int a[3]; printf(); Inside time s stack frame time s stack frame time calls bar time s stack frame bar s stack frame bar calls printf time s stack frame bar s stack frame printf s stack frame The stack shrinks as function returns void time { int x, y[2]; bar(x); void bar(int x) { int a[3]; printf(); bar returns time s stack frame printf returns time s stack frame bar s stack frame 6
Stack Frame Content void time { int x, y[2]; bar(x); void bar(int x) { int a[3]; printf(); inside time time s stack frame return address to main x y[0] y[1] spilled registers in time x Return addr to time a[0] a[1] a[2] Spilled registers in bar return addr to bar printf local vars inside bar bar s stack frame Call Stacks with Recursion Each instance of the recursive function is given its own stack frame Local variables in function are private to a particular invocation Each has its own return address, spill space Global and static variables are shared there is 1 copy for all calls 7
Recursion Example main { bar(2); void bar(int a) { int x, y[2]; if( a > 0 ) bar(a 1 ); main calls bar return address to 2 return addr to main x, y[0], y[1] spilled registers in bar bar calls bar return address to 2 return addr to main x, y[0], y[1] spilled registers in bar 1 return addr to bar x, y[0], y[1] Spilled registers in bar Saving Registers during a call What happens to the values we have in registers when we have a function call? Options You can save registers before you make the function call and restore the registers when you return (caller-save register) You can save your registers after you make the function call and restore the registers before you return (callee-save register). Caller saved are $t0 - $t7 (temporary) and callee saved are $s0 - $s7 (saved) 8
Review of MIPS Operand Addressing Modes Register addressing operand is in a register op rs rt rd funct Register word operand Base (displacement) addressing operand is at the memory location whose address is the sum of a register and a 16-bit constant contained within the instruction op rs rt offset Memory base register Register relative (indirect) with Pseudo-direct with 0($a0) addr($zero) Immediate addressing operand is a 16-bit constant contained within the instruction op rs rt operand word or byte operand Review of MIPS Instruction Addressing Modes PC-relative addressing instruction address is the sum of the PC and a 16-bit constant contained within the instruction op rs rt offset Program Counter (PC) Memory branch destination instruction Pseudo-direct addressing instruction address is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC op jump address Memory jump destination instruction Program Counter (PC) 9
MIPS (RISC) Design Principles Simplicity favors regularity fixed size instruction 32 bits small number of instruction formats opcode always the first 6 bits Good design demands good compromises three instruction format Smaller is faster limited instruction set limited number of registers in register file limited number of addressing modes Make the common case fast arithmetic operands from the register file (load-store machine) allow instructions to contain immediate operands 10