Keeps sum in Y register, checks to see when X register passes end of table.
; Program to sum all odd numbers in a table ; Solution to Example 2.17 of Huang ; Bill Rison ; 1/30/97 ; ; Write a program to compute the sum of the odd numbers in an array with ; twenty 8-bit elements. The starting address of the array is 0x00. ; Save the sum at 0x20 and 0x21. .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
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 ldaa #N ;Init loop counter (ACC A) to number in table 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 ;Put odd entry into ACC B aby ;Add to Y l2: inx ;REG X points to next entry in table1 deca ;Decrement number left to process bne 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 ;Save sum here loop_ctr: .ds 1 ;eight-bit counter of number left to process