Computer Organization: A Programmer's Perspectivegalk/teach/csapp/notes/04c... · 2021. 1. 16. ·...

82
Computer Organization: A Programmer's Perspective Machine-Level Programming (3: Procedures)

Transcript of Computer Organization: A Programmer's Perspectivegalk/teach/csapp/notes/04c... · 2021. 1. 16. ·...

  • Computer Organization:A Programmer's Perspective

    Machine-Level Programming(3: Procedures)

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 2

    Mechanisms in 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

    P(…) { • • y = Q(x); print(y) •}

    P(…) { • • y = Q(x); print(y) •}

    int Q(int i){ int t = 3*i; int v[10]; • • return v[t];}

    int Q(int i){ int t = 3*i; int v[10]; • • return v[t];}

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 3

    x86-64 Linux Memory Layout

    Stack Runtime stack (8MB limit) E. g., local variables

    Heap Dynamically allocated as needed When call malloc(), calloc(), new()

    Data Statically allocated data E.g., global vars, static vars, string constants

    Text / Shared Libraries Executable machine instructions Read-only

    Hex Address

    00007FFFFFFFFFFF

    000000

    Stack

    TextData

    Heap

    400000

    8MBnot drawn to scale

    SharedLibraries

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 4

    StackStack Region of memory Managed with stack discipline Grows toward lower addresses Register %rsp indicates lowest stack

    address address of top element

    StackPointer%rsp

    Stack GrowsDown

    IncreasingAddresses

    Stack “Top”

    Stack “Bottom”

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 5

    Stack PushingStack Pushing pushq Src Fetch operand at Src Decrement %rsp by 8 Write operand at address

    given by %rsp

    Stack GrowsDown

    IncreasingAddresses

    Stack “Top”

    Stack “Bottom”

    StackPointer%rsp -8

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 6

    Stack PoppingStack Popping popq Dest Read operand at address

    given by %rsp Increment %rsp by 8 Write to Dest (register!)

    StackPointer%rsp

    Stack GrowsDown

    IncreasingAddresses

    Stack “Top”

    Stack “Bottom”

    +8

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 7

    %rsp

    %eax%edx

    %rsp

    %eax%edx

    %rsp

    %eax%edx

    0x104

    5550x108

    0x1080x10c0x110

    0x104

    555213

    213123

    Stack Operation Examples(32 bits: pushl, popl)Stack Operation Examples(32 bits: pushl, popl)

    0x1080x10c0x110

    555213

    123

    0x108 0x104

    pushl %eax

    0x1080x10c0x110

    213

    123

    0x104213

    popl %edx

    0x108

    213

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 8

    Stack use in procedure calls

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 9

    Carnegie Mellon

    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

  • 10Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Code Example

    long mult2 (long a, long b){ long s = a * b; return s;}

    long mult2 (long a, long b){ long s = a * b; return s;}

    void multstore (long x, long y, long *dest) { long t = mult2(x, y); *dest = t;}

    void multstore (long x, long y, long *dest) { long t = mult2(x, y); *dest = t;}

    0000000000400550 : 400550: mov %rdi,%rax # a 400553: imul %rsi,%rax # a * b 400557: retq # Return

    0000000000400550 : 400550: mov %rdi,%rax # a 400553: imul %rsi,%rax # a * b 400557: retq # Return

    0000000000400540 : 400540: push %rbx # Save %rbx 400541: mov %rdx,%rbx # Save dest 400544: callq 400550 # mult2(x,y) 400549: mov %rax,(%rbx) # Save at dest 40054c: pop %rbx # Restore %rbx 40054d: retq # Return

    0000000000400540 : 400540: push %rbx # Save %rbx 400541: mov %rdx,%rbx # Save dest 400544: callq 400550 # mult2(x,y) 400549: mov %rax,(%rbx) # Save at dest 40054c: pop %rbx # Restore %rbx 40054d: retq # Return

  • 11Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Control Flow Example #1

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0x400544

    0x120

    •••

    %rsp

    0x1200x1280x130

    %rip

  • 12Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Control Flow Example #2

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0x400550

    0x118

    0x400549

    •••

    %rsp

    0x1200x1280x130

    0x118

    %rip

  • 13Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Control Flow Example #3

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0x400557

    0x118

    0x400549

    •••

    %rsp

    0x1200x1280x130

    0x118

    %rip

  • 14Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Control Flow Example #4

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400550 : 400550: mov %rdi,%rax • • 400557: retq

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0000000000400540 : • • 400544: callq 400550 400549: mov %rax,(%rbx) • •

    0x400549

    0x120

    •••

    %rsp

    0x1200x1280x130

    %rip

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 15

    … and in 32bit ISA:

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 16

    Procedure Control FlowProcedure Control FlowUse stack to support procedure call and return

    Procedure call:call label Push return address on stack; Jump to label

    Return address valueAddress of instruction beyond callExample from disassembly 804854e: e8 3d 06 00 00 call 8048b90 8048553: 50 pushl %eax

    Return address = 0x8048553Procedure return:ret Pop address from stack; Jump to address

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 17

    %esp

    %eip

    %esp

    %eip 0x804854e

    0x108

    0x1080x10c0x110

    0x104

    0x804854e

    0x8048553123

    Procedure Call ExampleProcedure Call Example

    0x1080x10c0x110

    123

    0x108

    call 8048b90

    804854e: e8 3d 06 00 00 call 8048b90 8048553: 50 pushl %eax

    0x8048b90

    0x104

    %eip is program counter

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 18

    %esp

    %eip

    0x104

    %esp

    %eip 0x80485910x8048591

    0x1040x104

    0x1080x10c0x110

    0x8048553123

    Procedure Return ExampleProcedure Return Example

    0x1080x10c0x110

    123

    ret

    8048591: c3 ret

    0x108

    %eip is program counter

    0x8048553

    0x8048553

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 19

    Linux Stack FrameLinux Stack Frame Current Stack Frame (“Top” to Bottom)

    Parameters for called function (if not in registers) “Argument build”

    Local variables If can’t keep in registers

    Saved register context Old frame pointer

    Caller Stack Frame Return address

    Pushed by call instruction Arguments for this call

    Stack Pointer(%rsp)

    Frame Pointer (optional)(%rbp)

    Return Addr

    SavedRegisters

    +Local

    Variables

    ArgumentBuild (opt.)

    Old %rbp

    Arguments

    CallerFrame

    Changes between operating systems, compilers, linkers, etc. See REQUIRED reading on web page.

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 20

    Carnegie Mellon

    Example: incr (no recursion yet)long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x;}

    long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x;}

    incr: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret

    incr: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret

    Register Use(s)

    %rdi Argument p%rsi Argument val, y%rax x, Return value

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 21

    Carnegie Mellon

    Example: Calling incr #1

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    %rsp

    Initial Stack Structure

    . . .

    Rtn address

    15213Unused %rsp

    Resulting Stack Structure

    . . .

    Rtn address

    %rsp+8

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 22

    Carnegie Mellon

    Example: Calling incr #2

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    15213Unused %rsp

    Stack Structure

    . . .

    Rtn address

    %rsp+8

    Register Use(s)

    %rdi &v1%rsi 3000

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 23

    Carnegie Mellon

    Example: Calling incr #3

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    18213Unused %rsp

    Stack Structure

    . . .

    Rtn address

    %rsp+8

    Register Use(s)

    %rdi &v1%rsi 3000

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 24

    Carnegie Mellon

    Example: Calling incr #4

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    18213Unused %rsp

    Stack Structure

    . . .

    Rtn address

    %rsp+8

    Register Use(s)

    %rax Return value

    %rsp

    Updated Stack Structure

    . . .

    Rtn address

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 25

    Carnegie Mellon

    Example: Calling incr #5

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2;}

    Register Use(s)

    %rax Return value

    %rsp

    Updated Stack Structure

    . . .

    Rtn address

    %rsp

    Final Stack Structure

    . . .

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 26

    Carnegie Mellon

    Register Saving Conventions When procedure yoo calls who:

    yoo is the caller who is the callee

    Can register be used for temporary storage?

    Contents of register %rdx overwritten by who This could be trouble something should be done!➙

    Need some coordination

    yoo:• • •

    movq $15213, %rdx call who addq %rdx, %rax

    • • • ret

    yoo:• • •

    movq $15213, %rdx call who addq %rdx, %rax

    • • • ret

    who:• • •subq $18213, %rdx• • •

    ret

    who:• • •subq $18213, %rdx• • •

    ret

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 27

    Carnegie Mellon

    x86-64 Linux Register Usage #1

    %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

    %rax

    %rdx%rcx

    Return value

    %r8%r9%r10%r11

    %rdi%rsi

    Arguments

    Caller-savedtemporaries

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 28

    Carnegie Mellon

    x86-64 Linux Register Usage #2

    %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

    %rbx

    %rsp

    Callee-savedTemporaries

    Special %rbp

    %r12%r13%r14

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 29

    Carnegie Mellon

    Callee-Saved Example #1

    call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret

    call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret

    long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2;}

    long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2;}

    %rsp

    Initial Stack Structure

    . . .

    Rtn address

    15213Unused %rsp

    Resulting Stack Structure

    . . .

    Rtn address

    %rsp+8Saved %rbx

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 30

    Carnegie Mellon

    Callee-Saved Example #2

    call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret

    call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret

    long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2;}

    long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2;}

    %rsp

    Pre-return Stack Structure

    . . .

    Rtn address

    15213Unused %rsp

    Resulting Stack Structure

    . . .

    Rtn address

    %rsp+8Saved %rbx

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 31

    Calling Conventions in IA32

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 32

    Revisiting swapRevisiting swap

    void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

    int zip1 = 15213;int zip2 = 91125;void call_swap(){ swap(&zip1, &zip2);}

    call_swap:• • •pushl $zip2 # Global Varpushl $zip1 # Global Varcall swap• • •

    &zip2&zip1Rtn adr %esp

    ResultingStack

    •••

    Calling swap from call_swap

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 33

    Revisiting swapRevisiting swap

    void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

    swap:pushl %ebpmovl %esp,%ebppushl %ebxmovl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

    Body

    SetUp

    Finish

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 34

    swap Setup #1swap Setup #1

    swap:pushl %ebpmovl %esp,%ebppushl %ebx

    ResultingStack

    &zip2&zip1Rtn adr %esp

    EnteringStack

    •••

    %ebp

    ypxp

    Rtn adr

    Old %ebp

    %ebp•••

    %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 35

    swap Setup #2swap Setup #2

    swap:pushl %ebpmovl %esp,%ebppushl %ebx

    ypxp

    Rtn adr

    Old %ebp %ebp

    ResultingStack

    •••

    &zip2&zip1Rtn adr %esp

    EnteringStack

    •••

    %ebp

    %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 36

    swap Setup #3swap Setup #3

    swap:pushl %ebpmovl %esp,%ebppushl %ebx

    ypxp

    Rtn adr

    Old %ebp %ebp

    ResultingStack

    •••

    &zip2&zip1Rtn adr %esp

    EnteringStack

    •••

    %ebp

    Old %ebx %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 37

    Effect of swap SetupEffect of swap Setup

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset(relative to %ebp)

    ResultingStack

    •••

    &zip2&zip1Rtn adr %esp

    EnteringStack

    •••

    %ebp

    Old %ebx %esp

    movl 12(%ebp),%ecx # get ypmovl 8(%ebp),%edx # get xp. . .

    Body

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 38

    swap Finish #1swap Finish #1

    movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset

    swap’sStack ••

    Old %ebx %esp-4

    ObservationSaved & restored register %ebx

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset

    •••

    Old %ebx %esp-4

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 39

    swap Finish #2swap Finish #2

    movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset

    swap’sStack ••

    Old %ebx %esp-4

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset

    swap’sStack ••

    %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 40

    swap Finish #3swap Finish #3

    movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

    ypxp

    Rtn adr

    %ebp

    4 8 12

    Offset

    swap’sStack ••

    ypxp

    Rtn adr

    Old %ebp %ebp 0 4 8 12

    Offset

    swap’sStack ••

    %esp

    %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 41

    swap Finish #4swap Finish #4

    movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

    &zip2&zip1 %esp

    ExitingStack

    •••

    %ebp

    ObservationSaved & restored register %ebxDidn’t do so for %eax, %ecx, or %edx

    ypxp

    Rtn adr

    %ebp

    4 8 12

    Offset

    swap’sStack ••

    %esp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 42

    Register Saving ConventionsRegister Saving ConventionsWhen procedure yoo calls who:

    yoo is the caller, who is the calleeCan Register be Used for Temporary Storage?

    Contents of register %edx overwritten by who

    yoo:• • •movl $15213, %edxcall whoaddl %edx, %eax• • •ret

    who:• • •movl 8(%ebp), %edxaddl $91125, %edx• • •ret

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 43

    Register Saving ConventionsRegister Saving Conventions When procedure yoo calls who:

    yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Conventions

    “Caller Save” Caller saves temporary in its frame before calling

    “Callee Save” Callee saves temporary in its frame before using

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 44

    IA32/Linux Integer Register UsageIA32/Linux Integer Register Usage Two have special uses: %rbp, %rsp

    Three managed as callee-save %ebx, %esi, %edi Old values saved on stack before using

    Three managed as caller-save %eax, %edx, %ecx

    Do what you please, but expect any callee to do so, as well

    Register %eax also stores returned value

    %eax%edx%ecx%ebx%esi%edi%rsp%rbp

    Caller-SaveTemporaries

    Callee-SaveTemporaries

    Special

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 45

    Recursion

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 46

    Recursion uses the stack!Recursion uses the stack! Code must be “Reentrant”

    Multiple simultaneous instantiations of single procedure Use stack to store state of each instantiation:

    Arguments Local variables, saved registers Return pointers

    Stack Discipline State for given procedure needed for limited time

    From when called to when return Callee returns before caller does

    Stack Allocated in Frames state for single procedure instantiation

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 47

    Call Chain ExampleCall Chain ExampleCode Structureyoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    } amI(…){••amI();••

    }

    yoo

    who

    amI

    amI

    amI

    Call Chain

    Procedure amI recursive

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 48

    StackPointer%rsp

    yoo

    who

    Procedurestate

    FramePointer%rbp

    Stack“Top”

    Stack FramesStack Frames Contents

    Local variables Return information Temporary space

    Management Space allocated when enter procedure

    “Set-up” code Deallocated when return

    “Finish” code

    Pointers Stack pointer %rsp indicates stack top Frame pointer %rbp indicates start of current

    frame

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 49

    StackPointer%rsp

    yoo

    •••Frame

    Pointer%rbp

    Stack OperationStack Operation

    yoo

    Call Chainyoo(…){

    ••who();••

    }

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 50

    yoo(…){

    ••who();••

    }Stack

    Pointer%rsp

    yoo

    who

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chainwho(…){

    • • •amI();• • •amI();• • •

    }

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 51

    StackPointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    amI

    Call Chainyoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    }

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 52

    StackPointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    amI

    Call Chain

    amIamI

    yoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    }

    amI(…){

    ••amI();••

    }

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 53

    StackPointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    amI

    Call Chain

    amIamI

    amIamI

    yoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    }

    amI(…){

    ••amI();••

    }

    amI(…){

    ••amI();••

    }

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 54

    StackPointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    amI

    Call Chain

    amIamI

    yoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    }

    amI(…){

    ••amI();••

    } amIamI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 55

    StackPointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    amI

    Call Chainyoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    } amI

    amIamI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 56

    yoo(…){

    ••who();••

    }Stack

    Pointer%rsp

    yoo

    who

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chainwho(…){

    • • •amI();• • •amI();• • •

    } amI

    amI

    amIamI

    amI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 57

    amIamIStack

    Pointer%rsp

    yoo

    who

    amI

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chainyoo(…){

    ••who();••

    }

    who(…){

    • • •amI();• • •amI();• • •

    }

    amI(…){

    ••amI();••

    } amI

    amI

    amI

    amI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 58

    yoo(…){

    ••who();••

    }Stack

    Pointer%rsp

    yoo

    who

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chainwho(…){

    • • •amI();• • •amI();• • •

    }

    amI

    amI

    amIamIamI

    amI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 59

    StackPointer%rsp

    yoo

    who

    •••

    FramePointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chainwho(…){

    • • •amI();• • •amI();• • •

    }amI

    amI

    amI

    amI

    amI

    amI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 60

    yoo(…){

    ••who();••

    }

    StackPointer%rsp

    yoo

    •••Frame

    Pointer%rbp

    Stack OperationStack Operation

    yoo

    who

    Call Chain

    amI

    amI

    amI

    amI

    who

    amI

    amI

    amI

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 61

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 62

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Terminal Casepcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rdi x Argument%rax Return value Return value

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 63

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Register Savepcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rdi x Argument

    %rsp

    . . .

    Rtn address

    Saved %rbx

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 64

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Call Setuppcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rdi x >> 1 Rec. argument%rbx x & 1 Callee-saved

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 65

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Callpcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rbx x & 1 Callee-saved%rax Recursive call

    return value

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 66

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Resultpcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rbx x & 1 Callee-saved%rax Return value

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 67

    Carnegie Mellon

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    /* Recursive popcount */long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1);}

    Recursive Function Completionpcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi # (by 1) call pcount_r addq %rbx, %rax popq %rbx.L6: rep; ret

    Register Use(s) Type

    %rax Return value Return value%rsp

    . . .

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 68

    Carnegie Mellon

    Observations About Recursion Handled Without Special Consideration

    Stack frames mean that each function call has private storage Saved registers & local variables Saved return pointer

    Register saving conventions prevent one function call from corrupting another’s data Unless the C code explicitly does so (e.g., buffer overflow bug/attack)

    Stack discipline follows call / return pattern If P calls Q, then Q returns before P Last-In, First-Out

    Also works for mutual recursion P calls Q; Q calls P

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 69

    Carnegie Mellon

    x86-64 Procedure Summary Important Points

    Stack is the right data structure for procedure call / return If P calls Q, then Q returns before P

    Recursion (& mutual recursion) handled by normal calling conventions Can safely store values in local stack frame and in callee-

    saved registers Put function arguments at top of stack Result return in %rax

    Pointers are addresses of values On stack or global

    Return Addr

    SavedRegisters

    +Local

    Variables

    ArgumentBuild

    Old %rbp

    Arguments7+

    CallerFrame

    %rbp(Optional)

    %rsp

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 70

    Optional Slides(for those interested in more examples)

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 71

    int rfact(int x){ int rval; if (x

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 72

    rfact:pushl %rbpmovl %rsp,%rbppushl %ebx

    rfact:pushl %rbpmovl %rsp,%rbppushl %ebx

    Rfact Stack SetupRfact Stack Setup

    Entering Stack

    xRtn adr 4

    8

    Caller

    %rbp 0 %rspOld %ebx-4

    Callee

    xRtn adr

    Caller

    %rsp

    %rbppre %rbppre %ebx

    pre %rbppre %ebx

    Old %rbp

    rfact:pushl %rbpmovl %rsp,%rbppushl %ebx

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 73

    Rfact BodyRfact Body

    Registers%ebx Stored value of x%eax

    Temporary value of x-1Returned value from rfact(x-1)

    Returned value from this call

    movl 8(%rbp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1jle .L78 # If

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 74

    Rfact RecursionRfact Recursion

    xRtn adr

    Old %rbp %rbpOld %ebx

    pushl %eax

    %rspx-1

    x-1%eaxx%ebx

    xRtn adr

    Old %rbp %rbpOld %ebx %rsp

    %eaxx%ebx

    x-1

    leal -1(%ebx),%eax

    xRtn adr

    Old %rbp %rbpOld %ebx

    x-1

    x-1%eaxx%ebx

    %rspRtn adr

    call rfact

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 75

    (x-1)!

    Rfact ResultRfact Result

    xRtn adr

    Old %rbp %rbpOld %ebx

    %rspx-1

    imull %ebx,%eax

    x!%eaxx%ebx

    xRtn adr

    Old %rbp %rbpOld %ebx

    %rspx-1

    (x-1)!%eaxx%ebx

    Return from Call

    (x-1)!

    Assume that rfact(x-1) returns (x-1)! in register %eax

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 76

    Rfact CompletionRfact Completion movl -4(%rbp),%ebxmovl %rbp,%rsppopl %rbpret

    xRtn adr

    Old %rbp %rbp 0 4 8

    Old %ebx%rsp

    -4

    x!%eaxx%ebx

    x-1-8

    pre %rbppre %ebx

    movl -4(%rbp),%ebxmovl %rbp,%rsppopl %rbpret

    xRtn adr

    Old %rbp %rbp 0 4 8

    %rsp

    x!%eaxOld %ebx%ebx

    pre %rbppre %ebx

    Old %ebx

    movl -4(%rbp),%ebxmovl %rbp,%rsppopl %rbpret

    xRtn adr

    %rbp

    %rsp

    x!%eaxOld %ebx%ebx

    pre %rbppre %ebx

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 77

    Pointer CodePointer Codevoid s_helper (int x, int *accum){ if (x

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 78

    Temp.Space

    %rsp

    Creating & Initializing PointerCreating & Initializing Pointer

    int sfact(int x){ int val = 1; s_helper(x, &val); return val;}

    _sfact:pushl %rbp # Save %rbpmovl %rsp,%rbp # Set %rbpsubl $16,%rsp # Add 16 bytes movl 8(%rbp),%edx # edx = xmovl $1,-4(%rbp) # val = 1

    Using Stack for Local VariableVariable val must be stored on stack

    Need to create pointer to itCompute pointer as -4(%rbp)Push on stack as second argument

    Initial part of sfactx

    Rtn adr

    Old %rbp %rbp 0 4 8

    -4 val = 1

    Unused-12 -8

    -16

    _sfact:pushl %rbp # Save %rbpmovl %rsp,%rbp # Set %rbpsubl $16,%rsp # Add 16 bytes movl 8(%rbp),%edx # edx = xmovl $1,-4(%rbp) # val = 1

    _sfact:pushl %rbp # Save %rbpmovl %rsp,%rbp # Set %rbpsubl $16,%rsp # Add 16 bytes movl 8(%rbp),%edx # edx = xmovl $1,-4(%rbp) # val = 1

    _sfact:pushl %rbp # Save %rbpmovl %rsp,%rbp # Set %rbpsubl $16,%rsp # Add 16 bytes movl 8(%rbp),%edx # edx = xmovl $1,-4(%rbp) # val = 1

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 79

    Passing PointerPassing Pointer

    int sfact(int x){ int val = 1; s_helper(x, &val); return val;}

    leal -4(%rbp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%rbp),%eax # Return val• • • # Finish

    Calling s_helper from sfactx

    Rtn adr

    Old %rbp %rbp 0 4 8

    val = 1 -4

    Unused-12 -8

    -16

    %rspx&val

    Stack at time of call

    leal -4(%rbp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%rbp),%eax # Return val• • • # Finish

    leal -4(%rbp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%rbp),%eax # Return val• • • # Finish val =x!

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 80

    Using PointerUsing Pointer

    • • •movl %ecx,%eax # z = ximull (%edx),%eax # z *= *accummovl %eax,(%edx) # *accum = z• • •

    void s_helper (int x, int *accum){ • • • int z = *accum * x; *accum = z; • • •}

    Register %ecx holds xRegister %edx holds pointer to accum

    Use access (%edx) to reference memory

    %edxaccum

    xx%eax

    %ecxaccum*x

    accum*x

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 81

    SummarySummaryThe Stack Makes Recursion Work

    Private storage for each instance of procedure callInstantiations don’t clobber each otherAddressing of locals+arguments relative to stack positions

    Can be managed by stack disciplineProcedures return in inverse order of calls

    IA32 Procedures Combination of Instructions + ConventionsCall / Ret instructionsRegister usage conventions

    Caller / Callee save %rbp and %rsp

    Stack frame organization conventions

  • Computer Organization: A Programmer's Perspective Based on class notes by Bryant and O'Hallaron 82

    ?שאלות