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

112                              Chapter 4 Assembly Language Programming



        19. Write an assembly-language program that adds N 2-byte signed numbers stored in
        the vector Z, N < 10. Your program should sign-extend each number as it is being added
        into the current sum so that the result SUM is a 3-byte signed number. Your program
        should have the assembler directives
                                       ORG       $800
                              N        DS.B      1
                              Z        DS.W      10
                              SUM      DS        3
        Can overflow occur in your program?

        2 0. Write an assembly-language program to find the smallest nonzero positive number
        NUM in the array Z of N 2-byte signed numbers. If there are no nonzero positive
        numbers, the result should be put equal to zero. Your program should have the following
        assembler directives:
                                       ORG
                              N        DS        1
                              Z        DS.W       100
                              NUM      DS        2
        21. Write an assembly-language program that finds the sum SUM of two N-byte
        numbers NUM1 and NUM2 and, when the SWI is encountered, has the condition code bits
        Z, N, V and C set properly. Your program should have the directives

                              N        DS        1
                              NUM1     DS        20
                              NUM2     DS        20
                              SUM      DS        20



                  ORG      $800           Begin program
                  LEAX     N,VECTOR       Point X to VECTOR
                  LDAB    N               Length of VECTOR into B
        LDDP      LD      A f X+          A vector element into A
                  BLO      LI             if negative.
                  COM     A               replace with two's-complement
        Ll        ANDA    #~1             Make contents of A even
                  STAA -1, X              Put number back
                  DECB                    Counter is in accumulator B
        *         BCS      \0
                  SWI                     End of program
        VECTOR    DS      N
        N         EQU      5              Number of elements in VECTOR

                              Figure 4.24. Program with Errors
   130   131   132   133   134   135   136   137   138   139   140