Assembly Language for Beginners

(nextflipdebug2) #1

1.15. SWITCH()/CASE/DEFAULT


exit:


jump_table dd case1
dd case2
dd case3
dd case4
dd case5


The jump to the address in the jump table may also be implemented using this instruction:
JMP jump_table[REG4]. OrJMP jump_table[REG8]in x64.


Ajumptableis just array of pointers, like the one described later:1.20.5 on page 287.


1.15.3 When there are severalcasestatements in one block.


Here is a very widespread construction: severalcasestatements for a single block:


#include <stdio.h>


void f(int a)
{
switch (a)
{
case 1:
case 2:
case 7:
case 10:
printf ("1, 2, 7, 10\n");
break;
case 3:
case 4:
case 5:
case 6:
printf ("3, 4, 5\n");
break;
case 8:
case 9:
case 20:
case 21:
printf ("8, 9, 21\n");
break;
case 22:
printf ("22\n");
break;
default:
printf ("default\n");
break;
};
};


int main()
{
f(4);
};


It’s too wasteful to generate a block for each possible case, so what is usually done is to generate each
block plus some kind of dispatcher.


MSVC

Free download pdf