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

9.6 Examples from Character String Procedures                        283


            3: char *strchr(char *str, int chr){
        0000088A 3B                PSHD
            4:   while (*str) {
         QOG0088B 200B             BRA    *+13     ;abs = 0898
            5:       if(*str == chr) return (str);
        0000088D B715              SEX    B,X
         0000088F AE80             CPX 0,SP
         00000891 2711             BEQ    *+19     ;abs = 08A4
             6:      -M-str;
         00000893 EE84             LDX    4,SP
         00000895 08               INX
         00000896 6E84             STX    4,SP
            4: while (*str) {
         00000898 EE84             LDX    4,SP
        0000089A E600              LDAB 0,X
         0000089C 26EF             BNE    *-15     ?abs = 088D
             8:  if(*str == chr) return str;
         0000089E B715             SEX    B,X
         000008AO AE80             CPX    0,SP
         000008A2 2603             BNE    *+5     ;abs = 08A7
         000008A4 EC84             LDD    4,SP
         000008A6 8FC787           CPS    #51079
            10: }
         000008A9 30               PULX
         000008AA 3D               RTS
                              Figure 9.18. The Strchr Procedure

            The procedure str chr searches for a character in a null-terminated string. See
        Figure 9.18. The first argument specifies the string. The second argument is a character.
        The procedure searches the string for a matching character; if it finds the character, it
        returns the address of the character in the string, otherwise it retuns a null (0).
            We now show strncpy which is used to copy characters from and to a null-
        terminated string. See Figure 9.19. We show the calling routine for this example to
        illustrate the passing of more than three arguments. The main procedure calls the
        strncpy procedure with three arguments. Notice how arguments are pushed in order or
        their appearance from left to right, so the leftmost string, pushed first, is at 8,SP inside
        strncpy. You should step through the while loop to see how each C statement is
        compiled into assembly language. Note, however, that the pointers keep getting reloaded
        into X and Y registers from their local variable storage locations. You can do a lot better
        by writing the program in assembler language. But you can use this code, produced by
        the Hiware C++ compiler, as a starting point for a tightly coded assembler language
        program.
            The procedure strncmp compares characters in two null-terminated strings,
        specified by the first two arguments, up to a number of characters specified in the third
        argument. See Figure 9.20. Observe the condition used to execute the while loop. If
        any of the three conditions are false, the subroutine terminates.
   301   302   303   304   305   306   307   308   309   310   311