A solution and a flow chart for Example 2.17 of Huang -- add the odd numbers in an array.



Program

Keeps sum in Y register, checks to see when X register passes end of table.

; Program to sum all odd numbers in a table
; Alternate Solution to Example 2.17 of Huang
; Bill Rison
; 1/28/98


         .title  SUM ODD NUMBERS

EVBRAM   =  0x0000              ;0x0000 is start of user ram on 68HC11EVBU
DATA     =  EVBRAM
PROG     =  EVBRAM+0x100        ;start program above BUFFALO
N        =  20                  ;number of entries in table
SUMADDR  =  0x20                ;location to put result

         .area   CODE  (ABS)

         .org    PROG           ;set program counter to 0x0100
start:   ldx     #table         ;Reg X points to entry to process 
         ldy     #0             ;Hold sum in Y -- clear to start
l1:      brclr   0,x,#0x01,l2   ;If entry is even (LSB=0), don't sum
         ldab    0,x            ;Entry was odd -- get into ACC B
         aby                    ;Add to total in Y
l2:      inx                    ;REG X points to next entry in table1
         cpx     #(table+N)     ;Has pointer gone past end of table?
         blo     l1             ;If not done, process next table entry
         sty     sum            ;Save result
         swi                    ;Done -- Exit

         .area   DATA  (ABS)

         .org    DATA
table:   .db     0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19

         .org    SUMADDR
sum:     .ds     2