mechanisms in procedures mechanisms in procedures
play

Mechanisms in Procedures Mechanisms in Procedures CS 105 Tour of - PowerPoint PPT Presentation

Mechanisms in Procedures Mechanisms in Procedures CS 105 Tour of the Black Holes of Computing P() { Passing control To beginning of procedure code y


  1. ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ Mechanisms in Procedures Mechanisms in Procedures CS 105 “Tour of the Black Holes of Computing” P(…) { Passing control • • To beginning of procedure code y = Q(x); Machine-Level Programming III: Machine-Level Programming III: print(y) Back to calling point • Procedures Procedures Passing data } Procedure arguments Return value int Q(int i) Memory management Topics { Allocate variables during procedure execution int t = 3*i; x86-64 stack discipline int v[10]; Deallocate upon return Register-saving conventions • Mechanisms all implemented with machine • Creating pointers to local variables return v[t]; instructions } x86-64 procedures use only what’s needed CS 105 – 2 – x86-64 Stack x86-64 Stack x86-64 Stack Pushing x86-64 Stack Pushing Stack “Bottom” Stack “Bottom” Pushing: pushq Src Region of memory managed with stack discipline Grows toward lower addresses Fetch operand at Src Register %rsp indicates numerically lowest stack address Increasing Decrement %rsp by 8 Increasing Addresses Addresses � Always holds address of “top” element Then write operand at address given by %rsp Stack Grows Stack Grows Stack Down Down Pointer Stack %rsp Pointer -8 %rsp Stack “Top” New Stack “Top” CS 105 CS 105 – 3 – – 4 –

  2. ✁ ✁ ✁ ✁ ✁ ✁ ✁ x86-64 Stack Popping x86-64 Stack Popping Stack Operation Examples Stack Operation Examples Stack “Bottom” Popping: popq Dest pushq %rax popq %rdx Read memory data at address given by %rsp Increment %rsp by 8 Increasing 0x118 0x118 0x118 Addresses Write to Dest 0x110 0x110 0x110 0x108 123 0x108 123 0x108 123 0x100 213 0x100 213 Stack Stack Grows Pointer Down %rax 213 %rax 213 %rax 213 %rsp +8 %rdx 555 %rdx 555 %rdx 213 555 %rsp 0x108 %rsp 0x100 0x108 %rsp 0x108 0x100 New Stack “Top” CS 105 CS 105 – 5 – – 6 – Procedure Control Flow Procedure Control Flow Control-Flow Example #1 Control-Flow Example #1 • • 0x130 Use stack to support procedure call and return 0000000000400540 <multstore>: • • 0x128 Procedure call: call or callq • 0x120 400544: callq 400550 <mult2> call label Push return address onto stack; jump to label 400549: mov %rax,(%rbx) • Return address value • %rsp 0x120 Address of instruction just beyond call Procedure return: ret or retq (or rep; ret ) %rip 0x400544 0000000000400550 <mult2>: Pop address (of instruction after corresponding call ) from stack 400550: mov %rdi,%rax Jump to that address • • 400557: retq CS 105 CS 105 – 7 – – 8 –

  3. Control-Flow Example #2 Control-Flow Example #2 Control-Flow Example #3 Control-Flow Example #3 • • • • 0x130 0x130 0000000000400540 <multstore>: 0000000000400540 <multstore>: • • • 0x128 • 0x128 • • 0x120 0x120 400544: callq 400550 <mult2> 400544: callq 400550 <mult2> 400549: mov %rax,(%rbx) 400549: mov %rax,(%rbx) 0x118 0x400549 0x118 0x400549 • • • • %rsp 0x118 %rsp 0x118 %rip 0x400550 %rip 0x400557 0000000000400550 <mult2>: 0000000000400550 <mult2>: 400550: mov %rdi,%rax 400550: mov %rdi,%rax • • • • 400557: retq 400557: retq CS 105 CS 105 – 9 – – 10 – Carnegie Mellon Control-Flow Example #4 Control-Flow Example #4 Procedure Data Flow Procedure Data Flow • • 0x130 0000000000400540 <multstore>: • • 0x128 • Registers Stack 0x120 400544: callq 400550 <mult2> First 6 arguments 400549: mov %rax,(%rbx) %rdi • • • • %rsi • %rsp 0x120 %rdx Arg n %rcx %rip 0x400549 %r8 • • • 0000000000400550 <mult2>: %r9 400550: mov %rdi,%rax Arg 8 • • Arg 7 400557: retq Only allocate stack space Return value %rax when needed CS 105 CS 105 – 11 – – 12 –

  4. ✁ ✁ ✁ ✁ ✁ ✁ Carnegie Mellon Data-Flow Example Data-Flow Example Diane’s Silk Dress Cost $89 Diane’s Silk Dress Cost $89 void multstore(long x, long y, long *dest) { long t = mult2(x, y); *dest = t; } Registers 0000000000400540 <multstore>: # x in %rdi, y in %rsi, dest in %rdx %rdi • • • 400541: mov %rdx,%rbx # Save dest %rsi 400544: callq 400550 <mult2> # mult2(x,y) %rdx # t in %rax 400549: mov %rax,(%rbx) # Save at dest %rcx • • • %r8 %r9 long mult2(long a, long b) 0000000000400550 <mult2>: { # a in %rdi, b in %rsi long s = a * b; 400550: mov %rdi,%rax # a return s; 400553: imul %rsi,%rax # a * b } # s in %rax 400557: retq # Return CS 105 CS 105 – 13 – – 14 – Stack-Based Languages Stack-Based Languages Call Chain Example Call Chain Example Call Chain Languages That Support Recursion Code Structure E.g., C, Pascal, Java, Python, Racket, Haskell, … yoo(…) yoo { Code must be “ reentrant ” • � Multiple simultaneous instantiations of single procedure • who � Need some place to store state of each instantiation who(…) who(); { � Arguments • • • • amI amI � Local variables • amI(); } � Return pointer • • • amI amI(); Stack Discipline amI(…) • • • { State for given procedure needed for limited time } amI • � From when called to when return • Procedure amI is recursive Callee returns before caller does amI(); • Stack Allocated in Frames • } State for single procedure instantiation CS 105 CS 105 – 15 – – 16 –

  5. ✁ ✁ ✁ ✁ ✁ Carnegie Mellon Carnegie Mellon Stack Frames Stack Frames Example Example ����� Contents yoo yoo(…) %rbp Return information { yoo yoo • Local storage (if needed) who %rsp • ��������� Temporary space (if needed) who(); ����� • amI amI Management • } Space allocated when procedure entered ��������������� %rbp amI � “Set-up” code ���������� � � Frame includes push done by call instruction ��������� proc amI Deallocated upon return � “Finish” code ��������������� %rsp � Includes pop done by ret instruction ����������� CS 105 CS 105 – 17 – – 18 – Carnegie Mellon Carnegie Mellon Example Example Example Example ����� ����� yoo(…) yoo yoo(…) yoo { who(…) { who(…) yoo yoo • { yoo • { yoo who amI(…) who • • • • • • • • %rbp { who(); amI(); who(); amI(); • • • • • • • • • who who amI amI amI amI • • amI(); • amI(); %rsp amI(); • • • • • • } } %rbp • } } amI amI amI • %rsp } amI amI CS 105 CS 105 – 19 – – 20 –

  6. Carnegie Mellon Carnegie Mellon Example Example Example Example ����� ����� yoo(…) yoo yoo(…) yoo { who(…) { who(…) yoo yoo • { yoo • { yoo amI(…) who amI(…) who • • • • • • • • { { who(); amI(); who(); amI(); amI(…) amI(…) • • • • • • • • • • who who amI amI amI amI { { • • • amI(); • amI(); amI(…) • • amI(); amI(); • • • • • • } } { • • • • } } amI amI • amI(); amI amI(); amI • • • • • } } amI(); • %rbp • amI • amI } } amI amI • %rsp } %rbp amI %rsp CS 105 CS 105 – 21 – – 22 – Carnegie Mellon Carnegie Mellon Example Example Example Example ����� ����� yoo(…) yoo yoo(…) yoo { who(…) { who(…) yoo yoo • { yoo • { yoo amI(…) who amI(…) who • • • • • • • • { { who(); amI(); who(); amI(); amI(…) • • • • • • • • • • who who amI amI amI amI { • • • amI(); • amI(); • amI(); amI(); • • • • • • } } %rbp • • • } } amI amI amI(); amI amI • • • %rsp } } • %rbp amI amI } amI %rsp CS 105 CS 105 – 23 – – 24 –

  7. Carnegie Mellon Carnegie Mellon Example Example Example Example ����� ����� yoo(…) yoo yoo(…) yoo { who(…) { who(…) yoo yoo • { yoo • { yoo who amI(…) who • • • • • • • • %rbp { who(); amI(); who(); amI(); • • • • • • • • • who who amI amI amI amI • • amI(); • amI(); %rsp amI(); • • • • • • } } %rbp • } } amI amI amI • %rsp } amI amI CS 105 CS 105 – 25 – – 26 – Carnegie Mellon Carnegie Mellon Example Example Example Example ����� ����� yoo(…) yoo yoo %rbp { who(…) yoo(…) yoo yoo • { yoo { yoo who who • • • • • %rsp %rbp who(); amI(); • • • • who(); • who amI amI amI amI • amI(); • %rsp • • • • } } } amI amI amI amI CS 105 CS 105 – 27 – – 28 –

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend