Download - Discussion Section – 11/3/2012
![Page 1: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/1.jpg)
M I D T E R M R E V I E W
DISCUSSION SECTION – 11/3/2012
![Page 2: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/2.jpg)
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
![Page 3: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/3.jpg)
QUICK BOTTOM UP PARSING EXAMPLE
• Grammar: S-> CCC->cCC->d
S and C are non-terminalsc and d are terminals
Parsing Table (courtesy: Dragon Book)
Show the stack tracefor the grammar:
cdccd
![Page 4: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/4.jpg)
SOLVE TYPE UNIFICATION EXAMPLES
1. Simple: def f(x):
return -xy = f(1)
2. Parametric Polymorphismdef f(z): return zx = f(0)y = f("hello")
3. Restrictions?def f(): def g(x): return x q = g([]) r = g(3)
![Page 5: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/5.jpg)
RUN TIME MEMORY SUBDIVISION
Code
Static Data
Stack
Free Memory
Heap
![Page 6: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/6.jpg)
A GENERAL ACTIVATION RECORD
Returned Value
Actual Parameters
Optional Control Link
Optional Access Link
Saved Machine Status
Local Data
Temporaries
![Page 7: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/7.jpg)
RECURSION ON STACK
• An illustration of implementing recursion using stacks
![Page 8: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/8.jpg)
QUESTION 1
Objective Caml language:let binom n k = ...
let test x y =let a = binom x y inlet 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,12mov *sp+4, eaxmov *sp, ebx
Call binomMov *sp+8, eax Mov ebx,*spAdd ebx, 1Mov eax,*sp+4
Call binomMov *sp+8,ebx
Lea eax, [eax+ebx-1]Add *sp,12return
![Page 9: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/9.jpg)
• 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.
![Page 10: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/10.jpg)
• 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.
![Page 11: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/11.jpg)
• 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
![Page 12: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/12.jpg)
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.
![Page 13: Discussion Section – 11/3/2012](https://reader036.vdocument.in/reader036/viewer/2022082612/56813b22550346895da3dc7e/html5/thumbnails/13.jpg)
• Solution : Dot Product