Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion Section – 11/3/2012

Similar presentations


Presentation on theme: "Discussion Section – 11/3/2012"— Presentation transcript:

1 Discussion Section – 11/3/2012
Midterm review

2 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

3 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)

4 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)

5 Run Time memory subdivision
Code Static Data Stack Free Memory Heap

6 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

7 Recursion on stack An illustration of implementing recursion using stacks

8 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

9 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.

10 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.

11 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

12 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.

13 Solution : Dot Product


Download ppt "Discussion Section – 11/3/2012"

Similar presentations


Ads by Google