Everything
A.7.4 Usage of a Table

If the processing in each case label of a switch statement is almost the same, consider the usage of a table.

[Example]

The character constant to be assigned to variable ch is changed by the value of variable i.

Source code before improvement

char func(int i)
{
     char ch;
     switch (i) {
          case 0:
               ch = ’a'; break;
          case 1:
               ch = ’x'; break;
          case 2:
               ch = ’b'; break;
     }
     return(ch);
}

 

Assembly-language expansion code before improvement

_func:
     CMP #00H,R1
     BEQ L17
L16:
     CMP #01H,R1
     BEQ L19
     CMP #02H,R1
     BEQ L20
     BRA L21
L12:
L17:
     MOV.L #00000061H,R1
     BRA L21
L13:
L19:
     MOV.L #00000078H,R1
     BRA L21
L14:
L20:
     MOV.L #00000062H,R1
L11:
L21:
     MOVU.B R1,R1
     RTS

 

Source code after improvement

char chbuf[] = {'a', 'x', 'b'};
 
char func(int i)
{
     return (chbuf[i]);
}

 

Assembly-language expansion code after improvement

_f
     MOV.L #_chbuf,R4
     MOVU.B [R1,R4],R1
     RTS