Upload
doublefelix921
View
228
Download
0
Embed Size (px)
Citation preview
7/25/2019 Lecture04 Machine Programming 3 Procedures
1/40
Machine Level
Programming: ProceduresComputer Systems Organization (Spring 2016)
CSCI-UA 201, Section 2
Instructor: Joanna Klukowska
Slides adapted from
Randal E. Bryant and David R. OHallaron (CMU)
Mohamed Zahran (NYU)
7/25/2019 Lecture04 Machine Programming 3 Procedures
2/40
Procedures
Passing control To beginning of procedure code
Back to return point
Passing data Procedure arguments
Return value
Memory management Allocate during procedure execution
Deallocate upon return
Mechanisms all implemented with
machine instructions
x86-64 implementation of aprocedure uses only those
mechanisms required
2
7/25/2019 Lecture04 Machine Programming 3 Procedures
3/40
Stack Structure
3
7/25/2019 Lecture04 Machine Programming 3 Procedures
4/40
x86-64 Stack
Region of memory managed with
stack discipline
Grows toward lower addresses
Register %rspcontains lowest stack
address (i.e., the address of top
element)
4
7/25/2019 Lecture04 Machine Programming 3 Procedures
5/40
x86-64:push
5
pushq Src
Fetch operand at Src
Decrement %rspby 8
Write operand at address given by %
rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
6/40
x86-64:pop
6
popq Dst
Read value at address given by %rsp
Increment %rspby 8
Fetch operand at Dst (must be
register)
7/25/2019 Lecture04 Machine Programming 3 Procedures
7/40
Passing Control
7
7/25/2019 Lecture04 Machine Programming 3 Procedures
8/40
Procedure Control Flow - Code example
8
7/25/2019 Lecture04 Machine Programming 3 Procedures
9/40
Procedure Control Flow
Use stack to support procedure call and return
Procedure call: call label
Push return address on stack
Jump to label
Return address:
Address of the next instruction right after call
Example from disassembly
Procedure return: ret
Pop address from stack
Jump to address
9
7/25/2019 Lecture04 Machine Programming 3 Procedures
10/40
Control Flow Example
10
7/25/2019 Lecture04 Machine Programming 3 Procedures
11/40
Control Flow Example
11
7/25/2019 Lecture04 Machine Programming 3 Procedures
12/40
Control Flow Example
12
7/25/2019 Lecture04 Machine Programming 3 Procedures
13/40
Control Flow Example
13
7/25/2019 Lecture04 Machine Programming 3 Procedures
14/40
Passing Data
14
7/25/2019 Lecture04 Machine Programming 3 Procedures
15/40
Passing arguments and returning values
Procedure arguments:
Registers
First six integer/pointer arguments are placed
in registers: %rdi, %rsi, %rdx%, %rcx,
%r8, %r9
Note: you have to remember the order
because that's how the arguments are
mapped
Stack
7+ arguments (integer and pointer) saved on
the stack (in IA-32 all arguments were saved on the
stack - accessing stack is slower than
accessing the registers)
Return value:
Register %rax is used to transfer a15
Registers Stack
7/25/2019 Lecture04 Machine Programming 3 Procedures
16/40
Example: Passing Data
16
7/25/2019 Lecture04 Machine Programming 3 Procedures
17/40
Local Data
17
7/25/2019 Lecture04 Machine Programming 3 Procedures
18/40
Stack-Based Languages
Languages that support recursion e.g., C, Pascal, Java
Code must be Reentrant
Multiple simultaneous instantiations of single procedure
Need some place to store state of each instantiation
Arguments
Local variables
Return pointer
Stack discipline State for given procedure needed for limited time
From when called to when return
Callee returns before caller does
Stack allocated inFrames state for single procedure instantiation
18
7/25/2019 Lecture04 Machine Programming 3 Procedures
19/40
Example: Function Call Chain
19
7/25/2019 Lecture04 Machine Programming 3 Procedures
20/40
Stack Frames
Contents Return information
Local storage (if needed)
Temporary space (if needed)
Management Space allocated when enter procedure
Set-up code
Includes push by call instruction
Deallocated when return
Finish code Includes pop by ret instruction
20
7/25/2019 Lecture04 Machine Programming 3 Procedures
21/40
21
Stack%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
22/40
22
Stack
yoo
who%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
23/40
23
Stack
yoo
who
amI%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
24/40
24
Stack
yoo
who
amI
amI
amI%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
25/40
25
Stack
yoo
who
amI%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
26/40
26
Stack
yoo
who%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
27/40
27
Stack
yoo
who
amI%rbp%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
28/40
28
Stack%rbp
%rsp
7/25/2019 Lecture04 Machine Programming 3 Procedures
29/40
X86-64 Stack Frame
Current Stack Frame (Top to Bottom) Argument build:
Parameters for function about to call
Local variables
If cant keep in registers
Saved register context
Old frame pointer (optional)
Caller Stack Frame Return address
Pushed by call instruction Arguments for this call
29
7/25/2019 Lecture04 Machine Programming 3 Procedures
30/40
Examples
30
7/25/2019 Lecture04 Machine Programming 3 Procedures
31/40
incr function
31
7/25/2019 Lecture04 Machine Programming 3 Procedures
32/40
Calling incrfunction
32
7/25/2019 Lecture04 Machine Programming 3 Procedures
33/40
Calling incrfunction
33
7/25/2019 Lecture04 Machine Programming 3 Procedures
34/40
Calling incrfunction
34
7/25/2019 Lecture04 Machine Programming 3 Procedures
35/40
Calling incrfunction
35
7/25/2019 Lecture04 Machine Programming 3 Procedures
36/40
Calling incrfunction
36
7/25/2019 Lecture04 Machine Programming 3 Procedures
37/40
Register Saving Conentions
When procedure yoocalls who: yoois the caller
who is the callee
Can register be used for temporary storage?
Conventions Caller Saved - Caller saves temporary values in its frame before the call
Callee Saved - Callee saves temporary values in its frame before using (Callee restores
them before returning to caller)
37
7/25/2019 Lecture04 Machine Programming 3 Procedures
38/40
Register Saving Convention %rax
Return value
Also caller-saved Can be modified by procedure
%rdi, ..., %r9
Arguments
Also caller-saved
Can be modified by procedure
%r10, %r11
Caller-saved
Can be modified by procedure
%rbx, %r12, %r13, %r14
Callee-saved
Callee must save & restore
%rbp
Callee-saved Callee must save & restore
May be used as frame pointer
Can mix & match
%rsp
Special form of callee save
Restored to original value upon exit from
procedure\38
7/25/2019 Lecture04 Machine Programming 3 Procedures
39/40
39
7/25/2019 Lecture04 Machine Programming 3 Procedures
40/40
40