Topic : jump tables for switch statements

Forum : 8051

Original Post
Post Information Post
June 1, 2007 - 3:56pm
Guest

How can I force the RC51 compiler to produce jump tables for switch statements ?

thanks

M.Marolda

Replies
Post Information Post
+1
0
-1
June 4, 2007 - 5:49pm
Guest

A jump table will be generated when the "case" values are consecutive:
Typically:

switch ( c )
{
case 10:
fct10();
break;
case 11:
fct11();
break;
case 12:
fct12();
break;
case 13:
fct13();
break;

etc...

When the values are too different, table with couples "address+value" will be generated.

+1
0
-1
June 5, 2007 - 8:29am
Guest

I still can't see how to obtain a real jump table,
I tried to compile your example and this is the result:

Source code

void provaJT(unsigned char c) {
   switch ( c  )
      {
      case 10:
          P1_0=0;
          break;
      case 11:
          P1_1=0;
          break;
      case 12:
          P1_2=0;
          break;
      case 13:
          P1_3=0;
          break;      
      }
}

RC51 compiler result
_provaJT PROC
                        ; SOURCE LINE # 34 
                        ;$@$C_SOURCE_FILE(c:\progetti_8051\io\main_loop.c)
                        ;$@$C_SOURCE_LINE(34)
        MOV     A,R7
        ADD     A,#0F6H
        JZ      ?CASE4
        DEC     A
        JZ      ?CASE5
        DEC     A
        JZ      ?CASE6
        DEC     A
        JZ      ?CASE7
        RET     
?CASE4:
                        ; SOURCE LINE # 37 
                        ;$@$C_SOURCE_LINE(37)
        CLR     P1_0
                        ; SOURCE LINE # 38 
                        ;$@$C_SOURCE_LINE(38)
        RET     
?CASE5:
                        ; SOURCE LINE # 40 
                        ;$@$C_SOURCE_LINE(40)
        CLR     P1_1
                        ; SOURCE LINE # 41 
                        ;$@$C_SOURCE_LINE(41)
        RET     
?CASE6:
                        ; SOURCE LINE # 43 
                        ;$@$C_SOURCE_LINE(43)
        CLR     P1_2
                        ; SOURCE LINE # 44 
                        ;$@$C_SOURCE_LINE(44)
        RET     
?CASE7:
                        ; SOURCE LINE # 46 
                        ;$@$C_SOURCE_LINE(46)
        CLR     P1_3
                        ; SOURCE LINE # 49 
                        ;$@$C_SOURCE_LINE(49)
        RET     

        ENDP
+1
0
-1
June 6, 2007 - 11:37am
Guest

It depends on the number of tested values. If you add more cases, you will get the following listing:

0006         ?LAB5:
0006 502B           JNC    ?LAB4
0008 FE             MOV    R6,A
0009 23             RL     A
000A 2E             ADD    A,R6
000B 900000  R      MOV    DPTR,#?LAB6
000E 73             JMP    @A+DPTR
000F         ?LAB6:
000F 020000  R      LJMP   ?CASE12
0012 020000  R      LJMP   ?CASE13
0015 020000  R      LJMP   ?CASE14
0018 020000  R      LJMP   ?CASE15
001B 020000  R      LJMP   ?CASE16
001E 020000  R      LJMP   ?CASE17
0021 020000  R      LJMP   ?CASE18
0024 020000  R      LJMP   ?CASE19
0027 020000  R      LJMP   ?CASE20
002A 020000  R      LJMP   ?CASE21
002D 020000  R      LJMP   ?CASE22
0030 020000  R      LJMP   ?CASE23
0033         ?LAB4:
+1
0
-1
June 6, 2007 - 12:35pm
Guest

Perfect!!
Know I have a wonderfull and fast assembler code.

Thank you a lots

Massimo Marolda