Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

another bug in UM/MOD #401

Closed
Picatout opened this issue Feb 8, 2021 · 2 comments
Closed

another bug in UM/MOD #401

Picatout opened this issue Feb 8, 2021 · 2 comments
Assignees
Labels

Comments

@Picatout
Copy link

Picatout commented Feb 8, 2021

I discovered another bug in UM/MOD and solved it. The udh part of the dividend must be smaller than the divisor to get the correct quotient but the original version was accepting udh==un to solve this bug I replaced JRULE for the overflow test by JRULT. the modified code is:

UMMOD:
        PUSHW   X               ; save stack pointer
        LDW     X,(X)           ; un
        LDW     YTEMP,X         ; save un
        LDW     Y,(1,SP)        ; X stack pointer
        LDW     Y,(4,Y)         ; Y=udl
        LDW     X,(1,SP)        ; X
        LDW     X,(2,X)         ; X=udh
        CPW     X,YTEMP
        JRULT   MMSM1           ; udh must be < un to avoid overflow
; division overflow  return q=0,r=-1 
        POPW    X
        INCW    X               ; pop off 1 level
        INCW    X               ; ADDW   X,#2
        LDW     Y,#0xFFFF
        LDW     (X),Y
        CLRW    Y
        LDW     (2,X),Y
        RET
MMSM1:
        LD      A,#17           ; loop count
MMSM3:
        CPW     X,YTEMP         ; compare udh to un
        JRULT   MMSM4           ; can't subtract
MMSMa:
        SUBW    X,YTEMP         ; can subtract
        RCF 
MMSM4:
        CCF                     ; quotient bit
        RLCW    Y               ; rotate into quotient
        RLCW    X               ; rotate into remainder
        DEC     A               ; repeat
        JREQ    MMSMb           ; if A == 0
        JRC     MMSMa           ; if carry out of rotate
        JRA     MMSM3           ;
MMSMb:
        RRCW    X
        LDW     YTEMP,X         ; done, save remainder
        POPW    X
        INCW    X               ; drop
        INCW    X               ; ADDW   X,#2
        LDW     (X),Y
        LDW     Y,YTEMP         ; save quotient
        LDW     (2,X),Y
        RET
MrMarkB added a commit to MrMarkB/stm8ef that referenced this issue Feb 8, 2021
Addresses UM/MOD issues including not accounting for carry out in shift operation for quotient and remainder (TG9541#400) and issue with compare on overflow checking (TG9541#401).
@TG9541 TG9541 self-assigned this Feb 8, 2021
@TG9541 TG9541 added the bug label Feb 8, 2021
@TG9541
Copy link
Owner

TG9541 commented Feb 8, 2021

@Picatout that's something! Not two but three bugs in one core routine - something that almost always works is much worse than a plain bug.

@MrMarkB merged your new solution into the two points identified in #400

Merci beaucoup and thanks a lot to both of you!

@TG9541
Copy link
Owner

TG9541 commented Feb 9, 2021

Fixed - closing this. Please refer to follow-up discussion in #400.

@TG9541 TG9541 closed this as completed Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants