{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

H09 Practice Soln

# H09 Practice Soln - CS107 J Zelenski Handout#9 December 3...

This preview shows pages 1–2. Sign up to view the full content.

CS107 Handout #9 J Zelenski December 3, 2010 Solutions to final practice Final Exam: Thursday, Dec 9th 8:30-11:30am Bishop Auditorium Problem 1: IA32 a) void *Winky(int param1, int *param2) { int local; for (local = 4; local > 1; local *= 4) { param1 += 16; if (*param2 == 4) break; param2 -= 2; local++; } return &param2; } b) • The param1 += 16 is completely elided since param1 result isn't used after the loop; this saves instructions. • The values for local and param2 are cached in registers %edx and %eax respectively; this avoids load/store instructions to access values on stack. • No locals or temporaries conserves stack space and removes instructions to adjust stack pointer. • The increment of local and subsequent multiply by 4 are combined into one lea instruction instead of separate instructions. The loop is entered the first time without evaluating the test as the compiler can see that it will always be true, avoids a branch, saves instructions. Problem 2: Runtime stack char *GetArgv0(int mainSize) { void *mainAddr = (void *)main; // get address of main's code void **ebp = (void **)&mainSize - 2; // orient to current stack frame do { void *ra = *(ebp + 1); // if RA within main, preceding frame is one we want if (ra > mainAddr && ra < (char *)mainAddr + mainSize) { void **ebp_of_main = (void **)*ebp; // deref one last time char **argv = *(ebp_of_main + 3); // get to second param return argv[0]; } } while ((ebp = (void **)*ebp) != 0); }

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}