Machine-Level Control Flow, Lecture Slide - Computer Science, Slides of Introduction to Computers

Control Flow, Varieties of Loop , Switch Statements , Jump Table

Typology: Slides

2010/2011

Uploaded on 10/07/2011

rolla45
rolla45 🇺🇸

4

(6)

133 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Machine-Level Programming II
Control Flow
Sept. 10, 1998
Topics
Control Flow
Varieties of Loops
Switch Statements
class06.ppt
15-213
“The course that gives CMU its Zip!”
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Machine-Level Control Flow, Lecture Slide - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!

Machine-Level Programming II

Control Flow

Sept. 10, 1998

Topics

  • Control Flow
    • Varieties of Loops
    • Switch Statements

class06.ppt

“The course that gives CMU its Zip!”

Alpha Register Convention

General Purpose Registers

  • 32 total
  • Store integers and pointers
  • Fast access: 2 reads, 1 write in single cycle

Usage Conventions

  • Established as part of architecture
  • Used by all compilers, programs, and libraries
  • Assures object code compatibility - e.g., can mix Fortran and C

v t t t t t t t t s s s s s s s6,fp

Return value from integer functions

Temporaries (not preserved across procedure calls)

Callee saved

Frame pointer, or callee saved

C Code

long int fact_do (long int x) { long int result = 1; do { result *= x; x = x-1; } while (x > 1); return result;

Goto Version

long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; }

“Do-While” Loop Example

  • C allows “goto” as means of transferring control
    • Closer to machine-level programming style
  • Generally considered bad coding style

Goto Version

long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; }

“Do-While” Loop Compilation

Registers

$16 x $0 result

bis $31,1,$0 # result = 1 $37: # loop: mulq $0,$16,$0 # result *= x subq $16,1,$16 # x = x- cmple $16,1,$1 # if !(x<=1) beq $1,$37 # goto loop ret $31,($26),1 # return

Assembly

New Instructions

bis a, b, c c = a | b cmple a, b, c c = a <= b

C Code

long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;

First Goto Version

long int fact_while_goto (long int x) { long int result = 1; loop: if (!(x > 1)) goto done ; result *= x; x = x-1; goto loop ; done: return result; }

“While” Loop Example

  • Is this code equivalent to the do-while version?
  • Must jump out of loop if test fails

C Code

long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;

Second Goto Version

long int fact_while_goto (long int x) { long int result = 1; if (!(x > 1)) goto done ; loop: result *= x; x = x-1; if (x > 1) goto loop ; done: return result; }

Actual “While” Loop Translation

  • Uses same inner loop as do-while version
  • Guards loop entry with extra test

“While” Loop Example

Algorithm

  • Exploit property that p = p 0 + 2 p 1 + 4 p 2 + … 2 n –1 pn
  • Gives: xp^ = z 0 · z 1 2 · ( z 2 2 ) 2 · … · (…(( zn –1^2 ) 2 )…) 2 zi = 1 when pI = 0 zi = x when pI = 1
  • Complexity O(log p )

/* Compute x raised to nonnegative power p */ long int ipwr_while(long int x, long unsigned p) { long int result = 1; while (p) { if (p & 0x1) result = x; x = xx; p = p>>1; } return result; }

n times

“While” → “Do-While ” → “Goto ”

long int result = 1; while (p) { if (p & 0x1) result = x; x = xx; p = p>>1; }

long int result = 1; if (!p) goto done ; do { if (p & 0x1) result = x; x = xx; p = p>>1; } while (p); done :

long int result = 1; if (!p) goto done ; loop : if (!(p & 0x1)) goto skip ; result = x; skip : x = xx; p = p>>1; if (p) goto loop ; done :

  • Also converted conditional update into test and branch around update code

“For” Loop Example

for ( Init ; Test ; Update ) Body

long int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result = x; x = xx; }

General Form

Init

result = 1

Test

p != 0

Update

p = p >> 1

Body

if (p & 0x1) result = x; x = xx; }

“For” → “While”

for ( Init ; Test ; Update )

Body

Init ; while ( Test ) { Body Update ; }

Goto Version

Init ; if (! Test ) goto done ; loop: Body Update ; if ( Test ) goto loop ; done:

For Version While Version

Do-While Version

Init ; if (! Test ) goto done ; do { Body Update ; } while ( Test ) done :

Compiling Switch Statements

Implementation Options

  • Series of conditionals
    • Good if few cases
    • Slow if many
  • Jump Table
    • Lookup branch target
    • Avoids conditionals
    • Possible when cases are small integer constants
  • GCC
    • Picks one based on case structure
  • Bug in example code
    • No default given

typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type;

char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; } }

Jump Table Structure

Code Block 0

Targ0:

Code Block 1

Targ1:

Code Block 2

Targ2:

Code Block n

Targ n -1:

Targ Targ Targ

Targ n -

jtab:

target = JTab[op]; goto *target;

switch(op) { case 0: Block 0 case 1: Block 1

  • • • case n -1: Block n }

Switch Form

Approx. Translation

Jump Table Jump Targets

Assembly Setup Explanation

Instructions

zapnot a, b, c Use low order byte of b as mask m byte(c,i) = m[i]? byte(a,i) : 0 cmpule a, b, c c = ((unsigned long) a <= (unsigned long) b)

Symbolic Labels

  • Labels of form $ XX translated into addresses by assembler

Table Structure

  • Each target requires 4 bytes
  • Base address of jtab at $gp + $

Jump Table

Enumerated Values

ADD 0

MULT 1

MINUS 2

DIV 3

MOD 4

BAD 5

$74: .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $

Table Contents $68:

bis $31,43,$0 # return ‘+’ ret $31,($26), $69: bis $31,42,$0 # return ‘*’ ret $31,($26), $70: bis $31,45,$0 # return ‘-’ ret $31,($26), $71: bis $31,47,$0 # return ‘/’ ret $31,($26), $72: bis $31,37,$0 # return ‘%’ ret $31,($26), $73: bis $31,63,$0 # return ‘?’ $66: ret $31,($26),

Targets & Completion