Skip to content

Commit

Permalink
Merge pull request #406 from JeeeK/master
Browse files Browse the repository at this point in the history
UM/MOD: Slightly improvements for entry and exit, more efficient loop structure.
Tested with @MrMarkB 's test script (see #400) - no evidence of any problem after ~20,000 random parameter combinations (using a bare STM8S103F3P6).
  • Loading branch information
TG9541 authored Feb 21, 2021
2 parents c3d755f + 15de653 commit 0c3dcaf
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions forth.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1496,47 +1496,44 @@ WITHI:

HEADER UMMOD "UM/MOD"
UMMOD:
PUSHW X ; save stack pointer
LDW Y,X ; stack pointer to Y
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
LDW X,Y
INCW X ; drop un
INCW X
PUSHW X ; save stack pointer
LDW X,(X) ; X=udh
LDW Y,(4,Y) ; Y=udl (offset before drop)
CPW X,YTEMP
JRULT MMSM1 ; X is still on the R-stack
POPW X
INCW X ; pop off 1 level
INCW X ; ADDW X,#2
LDW Y,#0xFFFF
LDW (X),Y
POPW X ; restore stack pointer
LDW Y,#0xFFFF ; overflow result:
LDW (X),Y ; quotient max. 16 bit value
CLRW Y
LDW (2,X),Y
LDW (2,X),Y ; remainder 0
RET
MMSM1:
LD A,#17 ; loop count
LD A,#16 ; loop count
SLLW Y ; udl shift udl into udh
MMSM3:
RLCW X ; rotate udl bit into uhdh (= remainder)
JRC MMSMa ; if carry out of rotate
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
RLCW Y ; rotate into quotient, rotate out udl
DEC A ; repeat
JREQ MMSMb ; if A == 0
JRC MMSMa ; if carry out of rotate
JRA MMSM3 ;
JRNE MMSM3 ; if A == 0
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
POPW X ; restore stack pointer
LDW (X),Y ; save quotient
LDW Y,YTEMP ; remainder onto stack
LDW (2,X),Y
RET

Expand Down

0 comments on commit 0c3dcaf

Please sign in to comment.