Code:
: # ( +d1 -- +d2)
base @ ud/mod rot dup 9 > 7 and + Ascii 0 + hold ;
If this is the
ONLY use you have for
UD/MOD in Pettil, it might be better to just code
# as a primitive, and embed a tailor-made (optimized just for
#) version of
UD/MOD. I think that's kind of what you were suggesting earlier, but it just now sank in. I'll whip up a primitive for
# and post it here when I get it at least half-baked.
[Update: Something like this (36 bytes, 11xx cycles):
Code:
; - - - - - - - - - - - - - - - - - - - - - - - - - - -
; # ( ud1 -- ud2 )
; Extract least significant digit of ud1, convert to
; ASCII and save that digit in HOLD area.
; ud2 is the quotient of ud1 / BASE
; Undefined behavior for a BASE outside [2..20]
;
sharp:
lda #0 ;partial remainder
ldy #32 ;loop counter
sharp2:
asl stackl,x ;Dividend in TOS:NOS (h:l)
rol stackh,x ; is gradually replaced
rol TOS ; with the quotient
rol TOS+1 ;a is gradually replaced
rol ; with the remainder
cmp BASE ;partial remainder < BASE?
bcc sharp3 ; no: update the partial
sbc BASE ; remainder and partial
inc stackl,x ; quotient
sharp3:
dey
bne sharp2 ;loop 32 times
cmp #10
sed
adc #'0' ;adjust remainder to ASCII
cld
jsr pushya
jmp hold ;HOLD ;
UNTESTED!]
Mike B.
[Edit: It seems kind of silly for UD/MOD to end with two ROTs, especially after I find that its only application in the standard word set is followed by a third ROT!]
[Edit: Improved comments, corrected pushay -> pushya.]