23S-CS107-Practice-Solution

23S-CS107-Practice-S - CS107 Spring 2007 Handout 23S May 4 2007 CS107 Practice Midterm Solution Problem 1 American Idol Who Will Win You are to

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

View Full Document Right Arrow Icon
CS107 Handout 23S Spring 2007 May 4, 2007 CS107 Practice Midterm Solution Problem 1: American Idol: Who Will Win? You are to generate assembly for the following nonsense code. Don't be concerned about optimizing your instructions or conserving registers. Do not assume that the contents of registers are preserved between lines. a) (15 points) Consider the following struct definitions: typedef struct { char *simon[2]; char paula[4]; char *randy; } judge; static short **absolutelyputrid(diva *jennifer, short *diana); static void diabolicallybad(diva george) { diva *matt = &george; **absolutelyputrid(matt + 1, george.jasmine) = 98; } First generate code for the entire diabolicallybad function. SP = SP – 4; // make space for matt; R1 = SP + 8; M[SP] = R1; // initialize matt R1 = M[SP]; // load matt R1 = R1 + 24; // compute matt + 1 (sizeof(diva) -> 24) R2 = SP + 28; // load &george.jasmine[0]; SP = SP – 8; // make space for parameters M[SP] = R1; // initialize first parameter M[SP + 4] = R2; // initialize second parameter CALL <absolutelyputrid> SP = SP + 8; // clean up params R1 = M[RV]; // load short * addressed by RV M[R1] =.2 98 // store 98 as a short at R1 SP = SP + 4; // deallocate matt RET; // jump back typedef struct { judge fantasia; judge *latoya; short jasmine[2]; } diva; line 1 line 2 saved pc SP before setup and after cleanup SP after setup and before cleanup george.fantasia george.latoya george.jasmine[0…1] diana jennifer matt line 1 line 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 b) (15 points) Now generate code for the absolutelyputrid function. You have two pages for your work. typedef struct { char *simon[2]; char paula[4]; char *randy; } judge; static short **absolutelyputrid(diva *jennifer, short *diana) { diana += *(char *)diana; ((diva *)(jennifer->latoya))->fantasia.randy = ((diva *)(jennifer->fantasia.paula))->fantasia.paula; return &(&diana)[100]; } R1 = M[SP + 8]; // load diana R2 =.1 M[R1]; // load char addressed by diana R2 = R2 * 2; // scale offset by sizeof(short) R1 = R1 + R2; // compute new value of diana M[SP + 8] = R1; // flush back R1 = M[SP + 4]; // load jennifer R2 = M[R1 + 16]; // load jennifer->latoya (and poof, it’s a diva *)
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 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 6

23S-CS107-Practice-S - CS107 Spring 2007 Handout 23S May 4 2007 CS107 Practice Midterm Solution Problem 1 American Idol Who Will Win You are to

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