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

9.1 Global and Local Variables                                       255


        In Figure 9.1c the ORG statement's operand is set to a RAM location, such as $800.
        char or unsigned char variables are allocated one byte by DS. B directives, and int
        or unsigned int variables are allocated two bytes by DS. W directives.
            Global variables, which in the 6812 are located in SRAM at $800 to $bff, generate
        16-bit direct addressed instructions. The global unsigned char variable guc can be
        written into by a STAB, MOVB, or CLR instruction. The statement guc = 0; is
        implemented in assembly-language code as

                      CLR $800     ; clear global variable guc

        Similarly, a char variable can be cleared, because signed and unsigned variables are
        coded as all zeros when intialized as zero. The global int variable gsi can be written
        into by a STD, MOVW, or a pair of CLR instructions. The statement gsi - 5; is
        implemented in assembly-language code as


                                   LDAB #5
                                   CLRA
                                   STD     $0801

            Local variables, which are declared within, and generally at the beginning of, a
        procedure, are generally allocated at run time by means of a LEAS statement. For
        instance, the local declaration char Isc, unsigned int lui; requires three bytes on
        the stack, so it is allocated by the instruction

             LEAS -3,SP            ; allocate local variables

        immediately upon entry into the procedure, and deallocated by the instruction

             LEAS 3, SP            ; deallocate local variables

        at the end of the procedure, just before RTS is executed. Local variables generate index
        addressed instructions. The local char variable Isc can be written into by a STAB,
        MOVB, or CLR instruction. The variable Isc is at 2, SP. Because the compiler knows
        that accumulator A must be clear, as a result of the previous operation, the statement
        Isc = 0; is implemented in assembly-language code as

             STAA 2, SP            ; clear local variable Isc

        The global unsigned int variable gui can be written into by a STD, MOVD, or a pair
        of CLR instructions. The variable lui is at 0, SP. Because the compiler knows that
        accumulator A must be clear, the statement lui = 7; is efficiently implemented in
        assembly-language code as

                        LDAB    #7
                         STD    0 , SP  ; write 7 into local variable lui
   273   274   275   276   277   278   279   280   281   282   283