SampleMidtermCS3214F09

SampleMidtermCS3214F09 - CS 3214 Sample Midterm (Fall 2009)...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 3214 Sample Midterm (Fall 2009) 1/11 Sample Midterm (Fall 2009) Solutions are shown in this style. This exam was given in Fall 2009. 1. Executing Programs on IA32 (20 pts) The following questions relate to how programs are compiled for IA32. a) (8 pts) In lecture, we had discussed how each function obtains its own activation record, or stack frame, every time it is called. The stack frame is used for several purposes, including to hold the values of arguments passed to a function or to hold the values of local variables that cannot be kept in registers. Typically, accesses to these arguments and variables involve loads or stores that use relative addressing using the %ebp register as a base. Recent versions of gcc support an optimization option ‘-fomit-frame- pointer’ that organizes accesses to local variables differently. Instead of using the base/frame pointer register %ebp, the stack pointer register %esp is used to access local variables and arguments passed to a function. As a result, %ebp is available for other uses. i. (6 pts) Explain why and how this would work! Why is the base pointer, apparently, redundant? The base pointer is always at known offset from the stack pointer, so any accesses that use addressing relative to $ebp can be replaced with accesses that use addressing relative to $esp. ii. (2 pts) Consider the example of accessing the first argument, which is traditionally accessed using 8(%ebp) . How would code compiled with –fomit-frame-pointer access this argument? Let SFSIZE = |$ebp-$esp| be the current stack frame size, then any access to disp($ebp) can be replaced with disp+SFSIZE($esp). For example, 8($ebp) would become 8+SFSIZE($esp). For example: int sum(int x, int y) { char localarray[16]; return x + y; } When compiled with –fomit-frame-pointer (but without other optimizations), the code shows:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 3214 Sample Midterm (Fall 2009) 2/11 sum: subl $16, %esp movl 24(%esp), %eax addl 20(%esp), %eax addl $16, %esp ret b) (12 pts) Consider the following assembly code, which was produced by gcc for a function ‘g()’. The left column shows the result when compiling at the first level of optimization (-O1), the right column shows the result of compiling at the second optimization level. IA 32 Code,compiled with –O1 IA 32 Code, compiled with –O2 g: pushl %ebp movl %esp, %ebp subl $8, %esp movl 8(%ebp), %eax movl 12(%ebp), %edx cmpl %edx, %eax je .L2 cmpl $1, %eax je .L6 cmpl $1, %edx jne .L4 .L6: movl $1, %eax jmp .L2 .L4: cmpl %edx, %eax jge .L7 movl %eax, 4(%esp) subl %eax, %edx movl %edx, (%esp) call g jmp .L2 .L7: movl %edx, 4(%esp) subl %edx, %eax movl %eax, (%esp) call g .L2: leave ret g: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %ecx cmpl %ecx, %edx je .L3 .L15: cmpl $1, %edx je .L5 cmpl $1, %ecx je .L5 cmpl %ecx, %edx jge .L9 movl %ecx, %eax movl %edx, %ecx subl %edx, %eax movl %eax, %edx .L7: cmpl %edx, %ecx jne .L15 .L3: popl %ebp movl %ecx, %eax ret .L5: popl %ebp movl $1, %eax ret .L9:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

Page1 / 11

SampleMidtermCS3214F09 - CS 3214 Sample Midterm (Fall 2009)...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online