ARM Technical Support Knowledge Articles

SWITCH/CASE STATEMENTS

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

When using a switch / case statements, I have noticed that code generated for the last case in the switch block takes longer to begin execution than the first case code. It seems this is because the compiler is using the ?C?CCASE function call.

Is there a way to get the compiler to generate a jump table for the switch statement?

ANSWER

Yes, there is. The switch-case statement is implemented using several different methods. Among them are:

The method used is determined by the efficiency of the code that is generated. The internal specification that is used to implement switch-case is very complex and relies on numerous conditions. If you restructure your switch statements to use sequential numbers, the compiler will generate a jump table. For example, the following program:

void func (unsigned int aaa)
{
volatile unsigned char car;

switch (aaa)
  {
  case 0: car = 'A'; break;
  case 1: car = 'S'; break;
  case 2: car = 'D'; break;
  case 3: car = 'F'; break;
  case 4: car = 'G'; break;
  case 5: car = 'H'; break;
  case 6: car = 'J'; break;
  }
}

when compiled with C51 Version 7 generates the following code:

0000 EE            MOV     A,R6
0001 703D          JNZ     ?C0009
0003 EF            MOV     A,R7
0004 B40700        CJNE    A,#07H,?C0010
0007         ?C0010:
0007 5037          JNC     ?C0009
0009 900000  R     MOV     DPTR,#?C0011
000C F8            MOV     R0,A
000D 28            ADD     A,R0
000E 28            ADD     A,R0
000F 73            JMP     @A+DPTR
0010         ?C0011:
0010 020000  R     LJMP    ?C0002
0013 020000  R     LJMP    ?C0003
0016 020000  R     LJMP    ?C0004
0019 020000  R     LJMP    ?C0005
001C 020000  R     LJMP    ?C0006
001F 020000  R     LJMP    ?C0007
0022 020000  R     LJMP    ?C0008
                                           ; SOURCE LINE # 6
                                           ; SOURCE LINE # 7
0025         ?C0002:
0025 750041  R     MOV     car,#041H
0028 22            RET
                                           ; SOURCE LINE # 8
0029         ?C0003:
0029 750053  R     MOV     car,#053H
002C 22            RET
                                           ; SOURCE LINE # 9
002D         ?C0004:
002D 750044  R     MOV     car,#044H
0030 22            RET
                                           ; SOURCE LINE # 10
0031         ?C0005:
0031 750046  R     MOV     car,#046H
0034 22            RET
                                           ; SOURCE LINE # 11
0035         ?C0006:
0035 750047  R     MOV     car,#047H
0038 22            RET
                                           ; SOURCE LINE # 12
0039         ?C0007:
0039 750048  R     MOV     car,#048H
003C 22            RET
                                           ; SOURCE LINE # 13
003D         ?C0008:
003D 75004A  R     MOV     car,#04AH
                                           ; SOURCE LINE # 14
                                           ; SOURCE LINE # 15
0040         ?C0009:
0040 22            RET

The jump table is created and used here with no timing impact due to the position of case items within the switch.

Notes The following Compiler directives influence the generation of switch/case:

Article last edited on: 2004-10-21 08:08:17

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential