












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Control Flow, Varieties of Loop , Switch Statements , Jump Table
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













class06.ppt
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
long int fact_do (long int x) { long int result = 1; do { result *= x; x = x-1; } while (x > 1); return result;
long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; }
long int fact_goto (long int x) { long int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; }
$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
bis a, b, c c = a | b cmple a, b, c c = a <= b
long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;
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; }
long int fact_while (long int x) { long int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;
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; }
/* 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
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 :
for ( Init ; Test ; Update ) Body
long int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result = x; x = xx; }
result = 1
p != 0
p = p >> 1
if (p & 0x1) result = x; x = xx; }
for ( Init ; Test ; Update )
Body
Init ; while ( Test ) { Body Update ; }
Init ; if (! Test ) goto done ; loop: Body Update ; if ( Test ) goto loop ; done:
Init ; if (! Test ) goto done ; do { Body Update ; } while ( Test ) done :
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 '?'; } }
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
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)
$74: .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $ .gprel32 $
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),