Discussion Section – 11/3/2012

1 / 13

# Discussion Section – 11/3/2012 - PowerPoint PPT Presentation

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:

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Discussion Section – 11/3/2012' - alima

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
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

Sand C are non-terminals

cand d are terminals

Show the stack trace

for the grammar:

cdccd

Parsing Table (courtesy: Dragon Book)

Solve Type unification examples

1. Simple:

deff(x):

return -x

y = f(1)

2. Parametric Polymorphism

deff(z): return z

x = f(0)

y = f("hello")

3. Restrictions?

deff():

defg(x): return x

q = g([])

r = g(3)

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

Movebx,*sp

Moveax,*sp+4

Call binom

Mov *sp+8,ebx

Lea eax, [eax+ebx-1]

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

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
What does this do?
• 080483b4 <test>:
• 80483b4: push ebp ; esp -= 4; *esp = ebp
• 80483b5: movebp,esp
• 80483b7: sub esp,0x18
• 80483ba: movDWORD PTR [ebp-0x14],ecx
• 80483bd: movDWORD PTR [ebp-0x18],edx
• 80483c0: moveax,DWORD PTR [ebp-0x14]
• 80483c3: movedx,DWORD PTR [eax]
• 80483c5: moveax,DWORD PTR [ebp-0x18]
• 80483c8: moveax,DWORD PTR [eax]
• 80483ca: imuledx,eax ; edx = edx * eax
• 80483cd: moveax,DWORD PTR [ebp-0x14]
• 80483d0: movecx,DWORD PTR [eax+0x4]
• 80483d3: moveax,DWORD PTR [ebp-0x18]
• 80483d6: moveax,DWORD PTR [eax+0x4]
• 80483d9: imuleax,ecx
• 80483dc: lea eax,[edx+eax*1] ; eax = edx + eax
• 80483df: movDWORD PTR [ebp-0x4],eax
• 80483e2: moveax,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.