Lecture 6

# Some code break case 2 some code case 3 some code

This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: (p & 0x1) result *= x; x = x*x; } return result; } before iteraMon 1 2 3 4 5 result x=3 p=10 1 1 9 9 59049 3 9 81 6561 43046721 10=10102 5= 1012 2= 102 1= 12 02 x86 University of Washington “For” Loop Example int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } General Form for (Init; Test; Update) Body Init result = 1 Test p != 0 Update p = p >> 1 Body { if (p & 0x1) result *= x; x = x*x; } x86 University of Washington “For”→ “While” For Version for (Init; Test; Update ) Body Goto Version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done: While Version Init; while (Test ) { Body Update ; } x86 University of Washington For ­Loop: CompilaMon For Version for (Init; Test; Update ) Body for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } Goto Version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done: result = 1; goto middle; loop: if (p & 0x1) result *= x; x = x*x; p = p >> 1; middle: if (p != 0) goto loop; done: x86 University of Washington long switch_eg (unsigned long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; } Switch Statement Example   MulMple case labels   Here: 5, 6   Fall through cases   Here: 2   Missing cases   Here: 4   x86 Lots to manage, we need a jump table University of Washington Jump Table Structure switch(x) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1 } Jump Targets Jump Table Switch Form JTab: Targ0 Targ0: Code Block 0 Targ1: Code Block 1 Targ2: Code Block 2 Targ1 Targ2 • • • Targn-1 • • • Approximate TranslaMon target = JTab[x]; goto *target; Targn-1: x86 Code Block n–1 University o...
View Full Document

## This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online