Page 296 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 296

9.3 Conditionai Statements                                          273

            The case statement is a useful alternative to the conditional statement. Consider an
        expression like switch(n){ case 1: i=l; break; case 3: i=2; break; case 6:
        i=3;break;}. This is compiled into assembly language by calling a subroutine
        switch to evaluate the case and providing the cases as addresses below its call, as
        shown in Figure 9.9a. This technique is used in Hi Ware's compiler when the cases are,
        or are nearly, consecutive numbers. Another technique used in some compilers is to
        implement the test using a sequence of CMP A and BNE instructions. Assembly language
        in Figure 9.9b implements the same switch statement with branch instructions. This
        technique is used in HiWare's compiler when the cases are not consecutive numbers.

        * switch(n){case 0:1=1; break; case l:i=2; break; case 2:i=3; break;}
              00000867 EC81             LDD 1,SP
              00000869 072D             BSR switch
              0000086B 03        L:     DC.B LO-L
              0000086C 06               DC.B Ll-L
              0000086D 09               DC.B L2-L
            5:                     case 1: i=l; break;
              0000086E C601 LO:         LDAB #1
              00000870 8FC602           CPS     #50690 ; is SKIP2 L1:LDAB #2
            6:                     case 3: i=2; break;
              00000873 8FC603           CPS     #50691 ; is SKIP2 L2:LDAB #3
              00000876 6B80             STAB 0,SP
                                   * .. .
              00000898 30      switch: PULX
              00000899 E6E6             LDAB D,X
              0000089B 05E5             JMP B,X
                           a) Using a Subroutine and Argument List

        *     switch(n){case l:i=l; break;case 3:i=2;break;case 6: i=3;break;}
                   LDAA $ 0 8 0 0 ; get switch operand alpha
                   CMPA #6        ; check for last case
                   BHI    *+2 7   ; branch over the rest of the cases
                   CMPA #1        ; check for first case
                   BEQ *+12       ; if found, go to LDAB instruction
                   CMPA #3        ; check second case
                   BEQ    * +11   ; if so go to middle of first CPS instruction
                   CMPA   #6      ; check last case
                   BEQ    * + 10  ; if so go to middle of second CPS instruction
                   BRA    * -f 13  ; if not matched, skip over cases
                   LDAB #1        ; this is for the case one
                   CPS    #50690 ; skip, or LDAB #2
                   CPS    #50691 ; skip, or LDAB #3
                   STAB $ 0 8 01 ; store result in beta
                b) Using a Sequence of CMP and conditional branch instruction Pairs

                 Figure 9.9. Alternative Assembly Language for a Case Statement
   291   292   293   294   295   296   297   298   299   300   301