26S-CS107-Midterm-Key

26S-CS107-Midterm-Key - CS107 Spring 2007 Handout 26S May...

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

View Full Document Right Arrow Icon
CS107 Handout 26S Spring 2007 May 16, 2007 CS107 Midterm Solution Problem 1: Color Theory (15 points: ) Consider the following struct definitions: typedef struct palette { short *lavender; char indigo[4]; struct palette *peach[2]; } palette; static palette **swatch(palette *ecru, int mint); static int colorwheel(palette ruby, int seafoam) { seafoam = ruby.lavender[ruby.indigo[3]]; ruby.peach[0] += ((palette *)ruby.indigo)->lavender[2]; return swatch(&ruby, 8) - ruby.peach; } Generate code for the entire colorwheel function. Be clear about what assembly code corresponds to what line. You have this and the next two pages for your work. // line 1 R1 = M[SP + 4]; // load ruby.lavender R2 =.1 M[SP + 11]; // load ruby.indigo[3] R3 = 2 * R2; // scale by sizeof(short) R4 = R1 + R3; // full address of rhs short R5 =.2 M[R4]; // pull in rhs short M[SP + 20] = R5; // write rhs val to seafoam // line 2 R1 = M[SP + 12]; // load old lhs R2 = M[SP + 8]; R3 =.2 M[R2 + 4]; // load third short in short array starting at R2 R4 = R3 * 16; // scale offset in R3 by sizeof(palette) R5 = R1 + R4; // compute new value for lhs M[SP + 12] = R5; // flush to lhs space // line 3 R1 = SP + 4; SP = SP – 8; // make space to two parameters M[SP] = R1; // press ecru M[SP +4] = 8; // press mint CALL <swatch> // jump away, expecting RV to be populated by a palette ** SP = SP + 8; // deallocate params R2 = SP + 12; R3 = RV – R2; // compute difference in raw bytes RV = R3 / 4; // divide by sizeof(palette *) and populate RV RET; line 1 line 2 line 3 saved PC seafoam ruby.peach[1] ruby.peach[0] ruby.indigo[0. ..3] ruby.lavender SP
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 Criteria for Problem 1 (written for the staff, visible to the students for transparency) Criteria for Line 1: 5 points Properly pulling the lavender field: 1 point Properly pulling the third character in indigo : 1 point Properly scaling the offset and computing the location of the relevant short : 1 point Properly loading the short : 1 point Properly writing the four byte version of that short to seafoam : 1 point 0.5 point deductions are fine, and we double ding for the same error, but we don’t triple ding, so make sure between all three lines you cap similar deductions re .1/.2, and illegal assembly code instructions at 1 point total. Line 2 is 5 points Properly grabbing lavender field of pretend struct using the proper number of dereferences: 2 points (this is 2 or 0, because it’s difficult to get this right, and it’s clear from the sample midterm and all the examples that this is challenging.) Properly grabbing third short : 1 point Properly scaling it by sizeof(palette) : 1 point Properly identifying, loading, and eventually updating the correct lhs l-value: 1 point Line 3 is 5 points Allocating and deallocating the right amount of space for the params: 1 point Populates param space within correct values, in correct order: 1 point
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 / 8

26S-CS107-Midterm-Key - CS107 Spring 2007 Handout 26S May...

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