Title: EECSCS 370
1EECS/CS 370
- Instruction Set Architecture
- Lecture 5
2Lectures of Instruction Set Architecture (ISA)
Design
- Lecture 3 Storage types and addressing modes
- Lecture 4 MIPS architecture
- Lecture 5 Calling functions / passing arguments
- Lecture 6 Translation software
3Recap on branches
- beq 2, 3, offset // branch to
offsetPC4 if 2 3 - bge 2, 3, offset // psuedo-instruction
2 ? 3 - j target // jump to target (pseudo-direct
addressing mode) - // uses a 26-bit target address
concatenated to top 4 // bits of PC - jr 3 // jump to address in 3 when
is this useful? - jal target // put PC4 into register ra and
jump to target
4Converting high level semantics to assembly code
- C printf(hello world\n)
- Need to pass parameters
- Need to save return address
- Need to jump to printf
- Need to get return value (if used)
Execute instructions for printf()
5Task 1 Passing parameters
- Where should you put all of the parameters?
- Registers?
- Fast access but few in number and wrong size for
some objects - Memory?
- Good general solution but where?
- MIPS answer
- Registers and memory.
- Put the first few parameters in registers (if
they fit) - Put the rest in memory on the call stack
- add 4, 0, 1000 // put address of char array
hello world in 4
6Call stack
- MIPS conventions (and most other processors)
allocate a region of memory for the call stack - This memory is used to manage all the storage
requirements to simulate function call semantics - Parameters
- Local variables
- Temporary storage (when you run out of registers
and need somewhere to save a value) - Return address
- Etc.
- Sections of memory on the call stack are
allocated when you make a function call, and
deallocated when you return from a function.
7MIPS memory map
7fff ffff
stack
Stack starts at 0x7fff ffff and grows down to
lower addresses. Bottom of the stack resides
in the sp register
Heap starts at 0x1000 8000 and grows up to
higher addresses. Allocation done explicitly
with malloc(). Deallocation with free().
Runtime error if no free mem before running
into sp address.
heap
1000 8000
Static starts at 0x1000 0000. Holds all
global variables and those locals explicitly
declared as static.
Static data
1000 0000
Text (instructions)
Text starts at 0x0040 0000. Holds all
instructions in the program (except for
Dynamically linked library routines DLLs)
0040 0000
PH pg. 160
8Allocating space to local variables
- Local variables (by default) are created when you
enter a function, and disappear when you leave - Technical terminology local variables are place
in the automatic storage class (as opposed to the
static storage class used for globals). - Automatics are allocated on the call stack
- How?
- by incrementing (or decrementing) the pointer to
the top of the call stack. - sub sp, sp, 12 // allocate space for 3
integer locals - add sp, sp, 12 // de-allocate space for
locals
9Saving registers during a call
- What happens to the values we have in registers
when we make a function call? - a sqr (b) c ? d
- You can save your registers before you make the
function call. - Where?
- What if the function you are calling doesnt use
that register?
The call frame is used to store anything
required to support function calls
10Caller vs. Callee registers
- An alternative to the caller saving the register
is to have the callee save any that will be used
in the function. - This is achieved by saving any registers that are
going to be overwritten in this function to the
call frame at the start of the function. - These values are then loaded back into the
registers just prior to returning from the call.
11What is the best solution?
- Advantages of caller saved
- Advantages of callee saved
- Hybrid approach
- Have some registers that are caller saved, some
that are callee saved. - If you need a register, but not at the site of a
call, allocate a caller saved. - If you need a register at a call site, allocate a
callee saved - The principle is to always pick the easiest
register for your current needs.
12MIPS register usage
- 0 constant 0
- 1 reserved for assembler
- 2,3 temporary and return value
- 4 - 7 parameters
- 8 - 15, 24, 25 caller saved
- 16 - 23 callee saved
- 26, 27 reserved for operating system
- 28 global pointer
- 29 stack pointer
- 30 frame pointer
- 31 return address
PH pg. A-23
13Putting it all together (Using activation
records)
- add 4, 0, 1000 // param hello world\n
- sw 12, 24(sp) // caller save 12
- jal _printf // call printf()
- // 2 holds return value (ignored)
- ldw 12, 24(sp) // restore 12 value
- sub sp, sp, 16 // allocate space for 2
locals - sw 16, 8(sp) // callee save 16
- sw 31, 12(sp) // save return address
- // function body
- add 2, 0, 0 // return value 0
- lw 16, 8(sp) // restore 16
- lw 31, 12(sp) // restore return address
- add sp, sp, 16
- jr 31
14Next time
- Finishing the program translations
- Assembly language to machine language
- Functions of the Linker
- Dynamically linked Libraries
- MIPS object code format
- X86 object code format