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

300                                   Chapter 10 Elementary Data Structures

         and are displayed differently on different terminals. These are usually input from the
        terminal keyboard by holding down a "control" key and pressing one of the other keys.
        These characters will not be needed in this discussion.) Implicit in the subroutine DUTCH
         is a segment of code that will wait until the previous character is displayed on the
        terminal before c is displayed. In assembly language, we might use two subroutines
        equivalent to these procedures; to input a character, execute subroutine INCH , which
        leaves the character in accumulator A, and to output a character, put it in accumulator A,
        and execute subroutine OUTCH. The exact code used in INCH and OUTCH are not important
         at this point.
            In C, if ptr points to the beginning of a null-terminated string s, a statement
        while (*ptr) outch( *ptr++); outputs the string s, if ptr points to the beginning
        of a buffer b, a statement do *ptr++ = c = inch(); while (c != '\r' ) inputs
        characters to the buffer b, until a carriage return is received (the carriage return is
         written at the end of the string that is in b). In assembly language, we can input or
        output strings of ASCII characters almost as easily. For example, we could display the
         constant string (9) on the terminal beginning at the current cursor position with the
        segment
                       LDAB        #16       ; Number of characters in STRING  (10)
                       LDX         #STRING
        LOOP:          LDAA         1,X+     ; Next character of STRING; into A
                       JSR         OUTCH
                       DBNE        B,LOOP

        Program segment (10) assumes that the programmer will count the number of characters
        in the string. This can be avoided by adding
        LENGTH:        EQU         *-STRING

        after the definition of STRING and replacing LDAB #16 with LDAB #LENGTH in the
        sequence (10). We can also input a string of characters from the terminal, terminated by
        an ASCII carriage return ($OD), with the program segment below. The string is stored in
        a buffer labeled BUFFER established with the directive
         BUFFER:       DS           100
        The string is entered with the program segment (11)

                       LDX         #BUFFER     ; X -> buffer to hold the string  (11)
        AGAIN:         JSR         INCH
                       STAA        1, X+       ; Place character in buffer
                       CMPA        #$ OD       ; is the character input a carriage return?
                       BNE         AGAIN
        OUT:           RTS

            Another type of sequential data structure is the deque, which we now discuss. A
        special case of the deque is the stack, which we studied extensively in Chapters 3 and 8.
        Our stacks have also been indexable, because the S register can be used as an index
        register as well as a stack pointer. Nevertheless, when these stack pointers are used only
        with push and pull instructions, they become true sequential structures.
            A deque is a generalization of a stack. It is a data structure that contains elements
        of the same precision. There is a top element and a bottom element, and only the top and
   318   319   320   321   322   323   324   325   326   327   328