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

102                               Chapter 4 Assembly Language Programming


              SRCH:   CLRB              initialize result in case first branch is taken
                      CMPA #' L'        compare to a character of string, move pointer
                      BEQ EXIT          if it is 'L', exit the loop
                      INCB              increase result to 1 in case next branch is taken
                      CMPA #' A'        compare to a character of string, move pointer
                      BEQ EXIT          if it is 'A', exit the loop
                      INCB              increase result to 2 in case next branch is taken
                      CMPA #' S'        compare to a character of string, move pointer
                      BEQ EXIT          if it is 'S', exit the loop. Otherwise it is S
                      INCB              increase result to 3 since all other cases tested
              EXIT:   SWI               return to the debugger
                                a) Using immediate operands
                      ORG $800
              Cmprd: DC . b "DSAL"      ; comparand string (downloaded)

              CMPR: LDX #Cmprd         ; get address of comparand string
                      LDAB #3          ; loop counter, and also the return value
              NEXT:   CMPA 1, X+       ; compare to a character of string, move pointer
                      BEQ EXIT         ; if it is a matching character, exit the loop
                      DBNE B, NEXT     ; decrement B, count out four loop executions
              EXIT: SWI                ; return to the debugger
                        b) Using a character string of comparison values
                           Figure 4.11. Character Search in a String




                      ORG $800
              Diet: Ds. b 32           ; storage for the dictionary
              Ptr:    DC . w Diet      ; address beyond the end of the dictionary
              Cntr: DC . b 0           ; size of the dictionary

              INSRT: LDX Ptr           ; get the pointer
                      STD 2, X+        ; store letter that is in A and value that is in B
                      STX Ptr          ; save the pointer
                      INC Cntr         ; increment the count of the dictionary size
                                         a) Build

              CMPR: LDX #Dict          ; get address of beginning of the dictionary
                      LDAB Cntr        ; loop counter, and also the return value
              NEXT:   CMPA 2, X+       ; compare to a character of string, move pointer by 2
                      BEQ EXIT         ; if it is NULL, exit the loop
                      DBNE B,NEXT      ; decrement B, count out four loop executions
              EXIT:   LDAB -1, X       ; get the numerical value of the letter into B
                                        b) Search
                          Figure 4.12. Dictionary Program Segments
   120   121   122   123   124   125   126   127   128   129   130