Discussion Section – 11/3/2012

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 6 Machine Code: How the CPU is programmed.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Lecture 10 – Activation Records Eran Yahav 1 Reference: Dragon 7.1,7.2. MCD 6.3,
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Function Compiler Baojian Hua Function, or Procedure, or method, or … High-level abstraction of code logically-grouped Good for many.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Run-Time Storage Organization
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Semantics of Calls and Returns
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
CS2422 Assembly Language & System Programming November 7, 2006.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Activation Records (in Tiger) CS 471 October 24, 2007.
ELF binary # readelf -a foo.out ELF Header:
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Stack Usage with MS Visual Studio Without Stack Protection.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Recitation 3: Procedures and the Stack
CS 177 Computer Security Lecture 9
Reading Condition Codes (Cont.)
Run-Time Environments Chapter 7
Static and dynamic analysis of binaries
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
Review: Chapter 5: Syntax directed translation
143A: Principles of Operating Systems Lecture 4: Calling conventions
Exploiting & Defense Day 2 Recap
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Machine-Level Programming 4 Procedures
Activation Records and Function Calls
Stack Frames and Advanced Procedures
Stack Frame Linkage.
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The Runtime Environment
Topic 3-a Calling Convention 1/10/2019.
Machine-Level Programming: Introduction
Multi-modules programming
UNIT V Run Time Environments.
Miscellaneous Topics.
X86 Assembly Review.
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Computer Architecture and System Programming Laboratory
Presentation transcript:

Discussion Section – 11/3/2012 Midterm review

Topics to cover A bottom up parsing example A Type Unification Example Run Time Memory Subdivision A General Activation Record An illustration of implementing recursion using stacks Examples

Quick Bottom up parsing example Grammar: S-> CC C->cC C->d S and C are non-terminals c and d are terminals Show the stack trace for the grammar: cdccd Parsing Table (courtesy: Dragon Book)

Solve Type unification examples 1. Simple: def f(x): return -x y = f(1) 2. Parametric Polymorphism def f(z): return z x = f(0) y = f("hello") 3. Restrictions? def f(): def g(x): return x q = g([]) r = g(3)

Run Time memory subdivision Code Static Data Stack Free Memory Heap

A general Activation record Returned Value Actual Parameters Optional Control Link Optional Access Link Saved Machine Status Local Data Temporaries Temporaries: temporary values evaluated during expressions in a procedure are stored here Local Data: Variables local to the procedure are stored here Saved Machine Status: State of the machine just before the procedure is called. Values of the program counter, machine register that have to be restored when control returns to the procedure Optional Access Link: Nonlocal data held in other Activation Records. Optional Control Link: Activation Record of the caller Actual Parameters: Parameters passed by the caller Returned Value: value returned to the calling procedure is stored here

Recursion on stack An illustration of implementing recursion using stacks

Question 1 Objective Caml language: let binom n k = ... let test x y = let a = binom x y in let b = binom x (y+1) in a + b (* Return the sum *) If we compile this code and then disassemble the result we get the following (using Intel syntax): On calling test, sub sp,12 mov *sp+4, eax mov *sp, ebx Call binom Mov *sp+8, eax Mov ebx,*sp Add ebx, 1 Mov eax,*sp+4 Mov *sp+8,ebx Lea eax, [eax+ebx-1] Add *sp,12 return

Describe the calling conventions for binom: Where are parameters n and k stored? Where is the result located on return? Draw a diagram showing the layout of the stack and the register values right before the second call to binom. Your diagram should show where each argument and local variable is stored.

Describe the calling conventions for binom: Where are parameters n and k stored? Where is the result located on return? The argument n is passed in the eax register, and k in ebx. We can tell this because eax has the same value both times binom is called, but the second time ebx's value is incremented. The return value is passed in eax, which we can tell because we can trace the two values added for a + b back to the values of eax right after the two calls to binom.

Stack (growing downward) and Registers Draw a diagram showing the layout of the stack and the register le right before the second call to binom. Your diagram should show where each argument and local variable is stored. Stack (growing downward) and Registers RA a x y Register Value eax x eby y+1

What does this do? 080483b4 <test>: 80483b4: push ebp ; esp -= 4; *esp = ebp 80483b5: mov ebp,esp 80483b7: sub esp,0x18 80483ba: mov DWORD PTR [ebp-0x14],ecx 80483bd: mov DWORD PTR [ebp-0x18],edx 80483c0: mov eax,DWORD PTR [ebp-0x14] 80483c3: mov edx,DWORD PTR [eax] 80483c5: mov eax,DWORD PTR [ebp-0x18] 80483c8: mov eax,DWORD PTR [eax] 80483ca: imul edx,eax ; edx = edx * eax 80483cd: mov eax,DWORD PTR [ebp-0x14] 80483d0: mov ecx,DWORD PTR [eax+0x4] 80483d3: mov eax,DWORD PTR [ebp-0x18] 80483d6: mov eax,DWORD PTR [eax+0x4] 80483d9: imul eax,ecx 80483dc: lea eax,[edx+eax*1] ; eax = edx + eax 80483df: mov DWORD PTR [ebp-0x4],eax 80483e2: mov eax,DWORD PTR [ebp-0x4] 80483e5: leave ; esp = ebp; pop ebp 80483e6: ret This function takes two parameters, passed in registers ecx and edx respectively. Its result is returned in register eax. Decompile (translate) this assembly into equivalent C code. Hint: This code implements a well-known mathematical operation.

Solution : Dot Product