Skip to content

Switch Statement Implementation

October 26, 2013

The C language has a switch statement for choosing between alternatives based on a single variable’s value.  For example inside the printf() function there’s a switch statement that looks like:

switch(c) {
  case 'i':
    <handle integers>
    break;
  case 's':
    <handle strings>
    break;
  case 'x': case 'X':
    <handle hex printing>
    break;
  <whatever other cases>
 }

I had looked at the code once or twice and it was just a set of if-else’s like you’d expect. Then suddenly, when I added another case (‘f’) for printing floats, i got the kind of malfunction that means I’m missing something. Digging into the code I found it implementing a jump table: it checked for c being smaller than ‘c’ or bigger than ‘l’ and then for everything in between it used c as an index into a table of addresses and jumped to the address of the code it wanted. I’ll have to look into whether this is a good idea for the 1802 and whether i can change the behaviour if it’s not.  In this instance there are 8 cases, it gets 5 of them (i,d,s,c,l and f) with the jump table but has to handle x and X separately.

Cunning though.

803/ 13EB : (MACRO) jcI2I r11,99,lbnf,L106 ;LT=lbnf i.e. subtract immedB from A and jump if borrow

804/ 13FC : (MACRO) jnI2I r11,108,lbnf,L107; GT reverse the subtraction

805/ 140D : (MACRO) shl2I R11,1

806/ 1413 : (MACRO) ld2 R11,'O',R11,(L108-198);reg: INDIRP2(addr)

807/ 141F : (MACRO) jumpv r11; JUMPV(reg)

808/ 1429 : L108:

809/ 1429 : 14 E9 dw L92

810/ 142B : 14 AB dw L90

811/ 142D : 17 33 dw L87

812/ 142F : 16 E3 dw L101

813/ 1431 : 17 33 dw L87

814/ 1433 : 17 33 dw L87

815/ 1435 : 14 AB dw L90

816/ 1437 : 17 33 dw L87

817/ 1439 : 17 33 dw L87

818/ 143B : 15 79 dw L94

819/ 143D : L106:

820/ 143D : (MACRO) ld2 R11,'O',sp,(-5+16) ;reg:INDIRI2(addr)

821/ 1449 : (MACRO) jeqU2I r11,88,L93;EQI2(reg,con)

822/ 1455 : C0 17 33 lbr L87

823/ 1458 :
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: