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

4,! Introductory Example and Assembler Printout                      89

        program into machine code, and you should understand how the assembler works.
        Although you may not be able to understand how to write an assembler, you will be
        prepared from now on to use an assembler as a tool to help you write your programs,



        4.1 Introductory Example and Assembler Printout

        We now consider a simple example to introduce you to assembly-language programs.
        Consider a program that obtains the maximum of a sequence of numbers. We will
        assume that this sequence consists of 16-bit unsigned numbers stored consecutively in
        memory, high byte first for each number. This data structure is called a vector or (one-
        dimensional) array. The name of the vector will be the location of the first byte of the
        vector, so that the high byte of the ith number in the vector (i = 0, 1, 2, . . .) can be
        found by adding 2*i to the vector name. Suppose then that Z is a vector of four 16-bit
        two's-complement numbers beginning in location $86a with N stored in location $868.
        The ith number will be denoted Z(i) for i = 0 through N - 1. We want a program that
        finds the maximum of these numbers, putting it in locations $868 and $869.
            One possible program for this, following the style of previous examples, is shown
        in Figure 4.1. We have arbitrarily started the program at address $89C.
            Looking at the preceding program, we certainly would like to use just the
        mnemonics column with the variable addresses and the labels for the branches and let the
        assembler generate the other two columns, that is, do what we have been doing by hand.
        We would also like to be able to use labels, also called symbolic addresses (or just
        symbols) for the memory locations that hold the values of variables. The meaning of
        symbolic addresses is explored in greater detail in the next chapter. We use them in this
        section to get the main idea (they are used before dissecting them carefully). The use of
        symbolic addresses allows program segment (2) to be replaced by program segment (3),

                                       LDX #$86A                            (2)
                                       STD $868

                                       LDX #Z                               (3)
                                       STD RESULT


















                                 Figure 4.1. Program MAX
   107   108   109   110   111   112   113   114   115   116   117