Programming: Procedures
Computer Systems Organization (Spring 2016)
CSCI-UA 201, Section 2
Procedures
Passing control
Passing data
Procedure arguments
Return value
Memory management
Stack Structure
x86-64 Stack
x86-64: push
pushq Src
x86-64: pop
popq Dst
Passing Control
Jump to label
Return address:
Jump to address
10
11
12
13
Passing Data
14
Registers
Registers
Stack
Stack
Return value:
15
16
Local Data
17
Stack-Based Languages
Stack discipline
18
19
Stack Frames
Contents
Return information
Local storage (if needed)
Temporary space (if needed)
Management
20
Stack
%rbp
%rsp
21
Stack
yoo
%rbp
who
%rsp
22
Stack
yoo
who
%rbp
amI
%rsp
23
Stack
yoo
who
amI
amI
%rbp
amI
%rsp
24
Stack
yoo
who
%rbp
amI
%rsp
25
Stack
yoo
%rbp
who
%rsp
26
Stack
yoo
who
%rbp
amI
%rsp
27
Stack
%rbp
%rsp
28
Argument build:
Parameters for function about to call
Local variables
If cant keep in registers
Saved register context
Old frame pointer (optional)
Return address
Pushed by call instruction
Arguments for this call
29
Examples
30
incr function
31
32
33
34
35
36
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
%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
39
40