[Coco] Mod10 Suggestions

Mark McDougall msmcdoug at iinet.net.au
Thu Feb 16 23:05:50 EST 2017


On 17/02/2017 12:26 PM, William Mikrut wrote:

Can't help myself! :(
Here's my take on it.
Assembles but ***NOT TESTED***!

On optimisation to the algorithm; check the result for >10 on each loop 
iteration - means you don't have to do an extra loop at then end!


         .list   (meb)     ; macro expansion binary
         .area   idaseg (ABS)

         .org    0x1200

ccd:    .ds     16        ; credit card number
result: .ds     1
status: .ds     1

start:  clrb
         stb     result    ; zero sum
         stb     status    ; flag=bad
         ldx     #result   ; ptr CCN
         ldb     #16       ; 16 digits

loop:   lda     ,x+       ; get digit (0-9)
         bita    #1        ; odd/even?
         bne     accum     ; odd, skip
         lsla              ; x2
         cmpa    #10       ; 2 digits?
         blo     accum     ; no, skip
         suba    #9        ; add digits

accum:  adda    result    ; add to result
         cmpa    #10       ; less than 10?
         blo     store     ; yes, skip
         suba    #10       ; discard tens
store:  sta     result    ; store result

         decb              ; done all digits?
         bne     loop      ; no, loop

         tsta              ; result=zero?
         bne     done      ; no, flagged bad already
         deca              ; $FF
         sta     status    ; update status

done:   rts

         .end    start

Regards,

-- 
Mark McDougall
<http://retroports.blogspot.com.au>


More information about the Coco mailing list