Two alternate programs for Example 2.17 of Huang

; Program to sum all odd numbers in a table
; Solution to Example 2.17 of Huang
; Bill Rison
; 1/30/97


         .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 
         ldab    #N             ;Initialize loop counter to number of table
	 stab    loop_ctr       ;  entries
         clra                   ;Hold sum in A-B -- clear to start
         clrb
l1:      brclr   0,x,#0x01,l2   ;If entry is even (LSB=0), don't sum
         addb    0,x            ;Entry was odd -- add to ACC B
         adca    #0             ;Add carry to ACC A to complete 16-bit result
l2:      inx                    ;REG X points to next entry in table1
         dec     loop_ctr       ;Decrement number left to process
         bne     l1             ;If not done, process next table entry
         std     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              ;Save sum here
loop_ctr: .ds    1              ;eight-bit counter of number left to process

; Program to sum all odd numbers in a table
; Solution to Example 2.17 of Huang
; Bill Rison
; 1/30/97


         .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 
         ldab    #N             ;ACC B holds number of entries left 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 -- need to get into B to add to Y
         aby                    ;Add to total
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