[cc65] 16x16 multiply routine overflow detection

From: Shawn Jefferson <sjefferson1shaw.ca>
Date: 2010-01-19 02:20:39
Hi,

 

I'm trying to a slightly modified version of the tosumulax routine from the
runtime in one of my projects, and what I'd like to be able to do is detect
when the result may overflow the 2 bytes, and in that case just return
65535.  The problem I have is that I don't really understand the routine
very well, and can't figure out where/how to do an overflow check?  I was
hoping someone here might be able to help, or at the very least say it isn't
possible with this algorithm.

 

Here's the routine from the lib runtime (I doing the full 16x16):

 

tosumulax:

        sta            ptr4

        txa                     ; High byte zero

            stx        ptr4+1               ; Save right operand

            jsr         popsreg             ; Get left operand

 

; Do ptr4:ptr4+1 * sreg:sreg+1 --> AX

 

            lda        #0

            ldx        sreg+1              ; Get high byte into register for
speed

            sta        tmp1

            ldy        #16                   ; Number of bits

 

        lsr     ptr4+1

        ror     ptr4            ; Get first bit into carry

@L0:    bcc     @L1

 

            clc

            adc       sreg

            pha

            txa                                ; hi byte of left op

            adc       tmp1

            sta        tmp1

            pla

 

@L1:    ror     tmp1

            ror        a

            ror        ptr4+1

            ror        ptr4

        dey

        bne     @L0

 

            lda        ptr4                   ; Load the result

            ldx        ptr4+1

            rts                                ; Done

 

 



----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Tue Jan 19 02:20:46 2010

This archive was generated by hypermail 2.1.8 : 2010-01-19 02:20:49 CET