PPT - University of Wisconsin

advertisement
Apps
O/S
Arch
mArch
Execution of Machine Code
Logic
Digital
Machine State and Operations
Analog
Devices
Physics
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Longer Assembly Example
start
done
five
neg1
stAddr
lw
lw
add
beq
beq
noop
halt
.fill
.fill
.fill
1 0 five
213
112
012
0 0 start
load reg1 with 5 (uses symbolic address)
load reg2 with -1 (uses numeric address)
decrement reg1
goto end of program when reg1 equals 0
go back to the beginning of the loop
end of program
5
-1
start
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
will contain the address of start (2)
Dan Ernst
State of the Machine
• Exposed State
– “Architected State”
• Behavioral Simulation
– Doesn’t simulate the inner workings of the processor
– Only includes as much detail as is necessary to get correct
execution
– Role in verification
• This exact idea is used in industry
• “Golden Model”
– Role in software/OS development
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Functions and JALR
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Jump and Link Register
• jalr – J-type – opcode 101
• Usage:
jalr RegA RegB
• First store PC+1 into regA, where PC is the address of
the jalr instruction. Then branch to the address
contained in regB.
– Note: This explicit ordering implies that if regA is the same as
regB (i.e. jalr 3 3), the processor will first store PC+1 into that
register, then end up branching to PC+1.
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
JALR Example
…
func
…
faddr
add
lw
jalr
add
3
6
7
4
2 1
0 faddr
6
1 1
nand
jalr
1 3 4
6 7
.fill
func
#Where to store this?
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Converting 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
Execute instructions for printf()
Need to get return value (if used)
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Task 1: Passing parameters
Q: Where should you put all of the parameters?
Registers?
Fast access but few in number and wrong size for some objects
Memory?
Slower, but good general solution – where in memory?
A: Registers and memory.
Put the first few parameters in registers (if they fit)
Put the rest in memory on the call stack
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Call stack
• There are conventions on most processors which allocate a
specific region of memory for the call stack
– This memory is used to manage all the storage requirements
to simulate function call semantics, such as…:
• Parameters / Return values
• Local variables
• Temporary storage (when you run out of registers and
need somewhere to save a value) (“spillage”)
• 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.
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
The MIPS Stack Frame
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Saving registers during a call
• What happens to the values we have in registers when we make a
function call?
– You can save your registers before you make the function call.
• Where? The call frame is used to store anything
required to support function calls
– What if the function you are calling doesn’t use that register?
We just wasted our time… (and space)
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Caller vs. Callee registers
• An alternative to the caller saving the register is to have the
callee save any registers that will be used in the function.
– Save any registers that are going to be overwritten in this function
to the call frame at the start of the function.
• “You break it, you buy it”
– These values are then loaded back into the registers just prior to
returning from the call.
Is this really any better?
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
What is the best solution?
• Advantages to caller saved
• Advantages to 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 programmer (or compiler) should pick the easiest register
for your current needs.
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
The MIPS Stack Frame - Updated
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Caller and Callee Save Example
•
Assume that
– r1 is caller-save
– r2 is callee-save
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Class Problem
• You have:
– 2 caller-save regs
• (1 and 2)
– 2 callee-save regs
• (3 and 4)
• What is the best choice of regs for
a, b, c, d?
CS 352 : Computer Organization and Design
University of Wisconsin-Eau Claire
Dan Ernst
Download