Rutgers University David Menendez 86 C Code int factwhileint x int result 1

Rutgers university david menendez 86 c code int

This preview shows page 86 - 95 out of 160 pages.

Rutgers University David Menendez 86 C Code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } Goto Version int fact_while_goto2 (int x) { 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
Image of page 86
C Code while ( Test ) Body Do-While Version if (! Test ) goto done ; do Body while( Test ); done : General “While” Translation Goto Version if (! Test ) goto done ; loop : Body if ( Test ) goto loop ; done :
Image of page 87
Rutgers University David Menendez 88 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 '?'; } }
Image of page 88
Rutgers University David Menendez 89 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 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 '?'; } }
Image of page 89
Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n –1 Targ n -1: Targ0 Targ1 Targ2 Targ n -1 jtab: target = JTab[op]; goto *target; switch(op) { case val_0: Block 0 case val_1: Block 1 • • • case val_ n -1: Block n –1 } Switch Form Approx. Translation Jump Table Jump Targets
Image of page 90
Rutgers University David Menendez 91 Switch Statement Example Branching Possibilities Setup: unparse_symbol: pushl %ebp # Setup movl %esp,%ebp # Setup movl 8(%ebp),%eax # eax = op cmpl $5,%eax # Compare op : 5 ja .L49 # If > goto done jmp *.L57(,%eax,4) # goto Table[op] Enumerated Values ADD 0 MULT 1 MINUS 2 DIV 3 MOD 4 BAD 5 typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { • • • } }
Image of page 91
Rutgers University David Menendez 92 Assembly Setup Explanation Table Structure Each target requires 4 bytes Base address at .L57 Jumping jmp .L49 Jump target is denoted by label .L49 jmp *.L57(,%eax,4) Start of jump table denoted by label . L57 Register %eax holds op Must scale by factor of 4 to get offset into table Fetch target from effective Address . L57 + op*4
Image of page 92
Rutgers University David Menendez 93 Jump Table Enumerated Values ADD 0 MULT 1 MINUS 2 DIV 3 MOD 4 BAD 5 .section .rodata .align 4 .L57: .long .L51 #Op = 0 .long .L52 #Op = 1 .long .L53 #Op = 2 .long .L54 #Op = 3 .long .L55 #Op = 4 .long .L56 #Op = 5 Table Contents .L51: movl $43,%eax # ’+’ jmp .L49 .L52: movl $42,%eax # ’*’ jmp .L49 .L53: movl $45,%eax # ’-’ jmp .L49 .L54: movl $47,%eax # ’/’ jmp .L49 .L55: movl $37,%eax # ’%’ jmp .L49 .L56: movl $63,%eax # ’?’ # Fall Through to .L49 Targets & Completion
Image of page 93
Rutgers University David Menendez 94
Image of page 94
Image of page 95

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture