procedures overview - lecture 33 chapter 6 - intel manual...
TRANSCRIPT
Procedures OverviewLecture 33
Chapter 6 - Intel Manual, v. 1
Robb T. Koether
Hampden-Sydney College
Mon, Apr 13, 2015
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 1 / 43
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 2 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 3 / 43
Functions and Function Calls
A function definition includesA return type.A function name.A list of formal parameters.A block of local variables.Statements, including optional return statements.
Each formal parameter and each local variable is introduced as adeclaration.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 4 / 43
Functions and Function Calls
Example (Function Definition)double average(int a, int b){
double sum;double avg;sum = a + b;avg = sum/2.0;return avg;
}
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 5 / 43
Functions and Function Calls
This is summarized in the productions
func → fbeg stmts }fbeg → fname fargs { dcls
fname → type id | idfargs → ( args ) | ( )args → args , arg | argarg → type id
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 6 / 43
Functions and Function Calls
func
stmtsfbeg }
fargsfname dcls{
type id ( args )
,arg ,... arg
type id type id
The function definition parse tree
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 7 / 43
Functions and Function Calls
A function call includesA function name.A list of actual parameters.
Each actual parameter is an expression (of the appropriate type).
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 8 / 43
Functions and Function Calls
Example (Function Call)int a;int b;double c;int main(){
a = 8;b = 16;c = average(-a, b + 10);print c;return 0;
}
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 9 / 43
Functions and Function Calls
This is summarized in the productions
expr → id ( ) | id ( exprs )exprs → exprs , expr | expr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 10 / 43
Functions and Function Calls
expr
id ( exprs )
,expr ,... expr
The function call parse tree
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 11 / 43
Functions and Function Calls
We need to distinguish the physical end of the function from thelogical end(s).The physical end is defined by the right brace }.The logical ends are defined by the return statements.If there is no return statement at the physical end, then weinsert one.
Rather than try to figure out whether there is one, we will insertone in all cases.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 12 / 43
Functions and Function Calls
We need to distinguish the physical end of the function from thelogical end(s).The physical end is defined by the right brace }.The logical ends are defined by the return statements.If there is no return statement at the physical end, then weinsert one.Rather than try to figure out whether there is one, we will insertone in all cases.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 12 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 13 / 43
Parameter Passing
The actual parameters are pushed onto the stack, where they canbe accessed by the called procedure.It is the responsibility of the calling procedure to push theparameters onto the stack before the CALL statement.Why?
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 14 / 43
Parameter Passing
esp
Before pushing the parameters.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 15 / 43
Parameter Passing
esp
Param 3
Param 2
Param 1
Actualparameters
After pushing the parameters.The leftmost parameter is at the top.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 15 / 43
The Procedure Call
When the procedure is called,The instruction pointer eip is pushed onto the stack.The address of the procedure is loaded into the instruction pointer.
This is handled automatically when the CALL instruction isexecuted.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 16 / 43
The Procedure Call
esp
Param 3
Param 2
Param 1
After pushing the actual parameters, but before the procedure call
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 17 / 43
The Procedure Call
esp
Param 3
Param 2
Param 1
Return address
After the procedure call
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 17 / 43
Creating the Stack Frame
To create a new stack frame (or activation record) for theprocedure,
Push ebp onto the stack to preserve the “old” base pointer.Move esp to ebp to preserve the “old” stack pointer and establishthe base pointer of the stack frame.Subtract from esp the size of the block of local variables.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 18 / 43
Creating the Stack Frame
Create a Stack Framepush %ebp # Save old base ptrmov %esp,%ebp # Save old stack ptrsub $n,%esp # Create local block
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 19 / 43
The Procedure Call
ebp
Param 3
Param 2
Param 1
Return addressesp
Just after executing CALL
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 20 / 43
The Procedure Call
ebp
Param 3
Param 2
Param 1
Return address
Old ebpesp
push %ebp # Save old base ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 20 / 43
The Procedure Call
Param 3
Param 2
Param 1
Return address
Old ebpebp, esp
mov %esp,%ebp # Save old stack ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 20 / 43
The Procedure Call
esp
Param 3
Param 2
Param 1
Return address
Old ebp
Local variables
ebp
sub $n,%esp # Create local block
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 20 / 43
The Procedure Call
esp
Param 3
Param 2
Param 1
Return address
Old ebp
Local variables
Stackframe
ebp
sub $n,%esp # Create local block
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 20 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 21 / 43
Accessing the Parameters
With each parameter, there is associated an offset.The offset refers to the byte of the value that has the lowestaddress.The offset of a parameter is positive since the parameter is“below” the base pointer in the stack.For example, to access the parameter with offset 12, we use theindexed addressing mode.
12(%ebp)
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 22 / 43
Accessing the Parameters
esp
Param 3
Param 2
Param 1
Return address
Old ebp
Local variables
ebp
ebp + 12 eax
lea 12(%ebp),%eax
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 23 / 43
Accessing Local Variables
With each local variable, there is associated an offset.The offset of a local variable is negative since the local variable is“above” the base pointer in the stack.For example, to access the parameter with offset -4, we use theindexed addressing mode.
-4(%ebp)
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 24 / 43
Accessing Local Variables
esp
Param 3
Param 2
Param 1
Return address
Old ebp
Local variables
ebp
ebp - 4 eax
lea -4(%ebp),%eax
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 25 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 26 / 43
The Return Value
When we return from a procedure, we first must pass the returnvalue to the calling procedure.
If the return value is an int, then we move it to eax.If the return value is a double, then we move it to st(0) in theFPU.
The calling procedure will go to eax or st(0) to retrieve thereturn value.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 27 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 28 / 43
Returning from a Procedure
When execution returns to the calling procedure, we mustClear the block of local variables from the stack.Restore the old stack pointer.Restore the old base pointer.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 29 / 43
Returning from a Procedure
Clear the Stack Framemov %ebp,%esp # Restore old stack ptrpop %ebp # Restore old base ptrret # Restore old instr ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 30 / 43
Returning from a Procedure
esp
Param 3
Param 2
Param 1
Return address
Old ebp
Local variables
Stackframe
ebp
Just before returning
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 31 / 43
Returning from a Procedure
Param 3
Param 2
Param 1
Return address
Old ebpebp, esp
mov %ebp,%esp # Restore old stack ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 31 / 43
Returning from a Procedure
ebp
Param 3
Param 2
Param 1
Return address
Old ebpesp
pop %ebp # Restore old base ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 31 / 43
Returning from a Procedure
ebp
Param 3
Param 2
Param 1
Return addressesp
ret # Restore old instr ptr
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 31 / 43
Returning from a Procedure
After execution returns to the calling function, the calling functionmust remove the parameters from the stack.This is done by simply adjusting the stack pointer.We must add to the stack pointer the number of bytes in theparameter block.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 32 / 43
Returning from a Procedure
esp
Param 3
Param 2
Param 1
Before removing the parameters
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 33 / 43
Returning from a Procedure
esp
After removing the parameters
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 33 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 34 / 43
Computing the Parameter Offsets
When parsing the argument list of a function definition, we mustkeep count of the number of bytes used by the arguments.The SymbolTable class variable f_arg_size is used for this.When we begin parsing the argument list, we must initializef_arg_size to 8.Why 8?
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 35 / 43
Computing the Parameter Offsets
For each argument, we mustStore the value of f_arg_size as the offset of that argument in itsIdEntry object.Add the argument size to f_arg_size.
Because the production
args → args , arg
is left-recursive, the arguments are parsed from left to right.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 36 / 43
Computing the Parameter Offsets
The syntax tree for the actual parameter list is built from thebottom up and later traversed from the top down.Therefore, the parameters will be pushed onto the stack in reverseorder, from right to left.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 37 / 43
Computing the Parameter Offsets
ebp
double b
int a
Return address
Old ebp
ebp + 4
esp + 12
For example, if the parameter list is (int a, double b)then the offset of a is 8 and the offset of b is 12.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 38 / 43
Computing the Local Variable Offsets
When parsing the local variable declarations in the functiondefinition, we must keep count of the number of bytes used by thelocal variables.The SymbolTable class variable dcl_size is used for this.When we begin parsing the declarations, we must initializedcl_size to 0.Why 0?
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 39 / 43
Computing the Parameter Offsets
For each local variable, we mustAdd the variable’s size to dcl_size.Store the value of -dcl_size as the offset of that variable in itsIdEntry object.
Because the production
dcls → dcls dcl
is left-recursive, the variables are parsed from left to right.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 40 / 43
Computing the Parameter Offsets
ebp
double b
int a
Return address
Old ebp
ebp - 4
ebp - 12
For example, if the local variables areint a; double b;
then the offset of a is -4 and the offset of b is -12.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 41 / 43
Outline
1 Function Calls
2 Parameter Passing
3 Accessing the Parameters
4 The Return Value
5 Returning from the Function
6 Managing the Offsets
7 Assignment
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 42 / 43
Assignment
AssignmentRead Sections 6.1 - 6.3 of the Intel Manual, Vol. 1.
Robb T. Koether (Hampden-Sydney College) Procedures Overview Mon, Apr 13, 2015 43 / 43