Jump to content
Willsy

The 7's Problem

Recommended Posts

46 minutes ago, unhuman said:

If I remember correctly, I used HCHAR b/c printing with ; puts an extra space in on the TI, unlike other basics.

Thanks for weighing in. Nice to hear from the author.

When I ran it, the output seemed exactly the same as the original. Try it if you have an emulator and let me know if I missed something.

It also takes a fair bit less code. Always a win in TI BASIC.

  • Like 1

Share this post


Link to post
Share on other sites
On 11/18/2019 at 10:17 PM, TheBF said:

I have been using Lee's version of this program to evaluate my screen I/O routines.  I found a small improvement.

We don't have to make a counted string since TYPE uses a stack string (addr,len) and we have LENGTH.

It takes a few milliseconds off. :)

 

: TYPE-A1 ( -- )
\   LENGTH PAD C!   \ store string length
   PAD               \ copy of PAD to start string storage loop
   A1 1- DUP LENGTH + DO  \ DO A1+length-1 to A1
      I [email protected]           \ get next digit
      48 +           \ convert to ASCII
      OVER C!        \ store ASCII digit in PAD
      1+             \ next PAD location
   -1 +LOOP
   DROP              \ clean up stack
   CR PAD LENGTH TYPE CR ;  \ type number

 

And after glancing at it again I realized we don't have put the chars into PAD at all! 

Lee has nicely made the array printable as is, we just have to type from back to front.

: TYPE-A1
    A1 LENGTH 1- +
    0 LENGTH
    DO
       DUP [email protected] 48 + EMIT
       1-
    -1 +LOOP
    DROP
 ;

And if I make use of my new words to use VDP hardware auto-increment it becomes this and it runs in 1:03

: TYPE-A1
    VPOS VDPWA!
    A1 LENGTH 1- +
    0 LENGTH
    DO
       DUP [email protected] 48 + VEMIT ?CR
       1-
    -1 +LOOP
    DROP
 ;

 

 

 

Share this post


Link to post
Share on other sites
3 hours ago, unhuman said:

If I remember correctly, I used HCHAR b/c printing with ; puts an extra space in on the TI, unlike other basics.

 

TI Basic “PRINT ;” does output a space or ‘-’ before and a space after a number. It does not, however, output any spaces for characters or strings.

 

...lee

Share this post


Link to post
Share on other sites
9 minutes ago, Lee Stewart said:

 

TI Basic “PRINT ;” does output a space or ‘-’ before and a space after a number. It does not, however, output any spaces for characters or strings.

 

...lee

Wow...  I never knew that.  I was always annoyed at that extra space.  Prevented some things that other basics allowed when I was a kid... 

Edited by unhuman

Share this post


Link to post
Share on other sites
3 hours ago, TheBF said:

Thanks for weighing in. Nice to hear from the author.

When I ran it, the output seemed exactly the same as the original. Try it if you have an emulator and let me know if I missed something.

It also takes a fair bit less code. Always a win in TI BASIC.

Now you gotta compile it! 

Share this post


Link to post
Share on other sites
9 hours ago, unhuman said:

Now you gotta compile it! 

I will have to learn how first... or write a compiler :)

 

Share this post


Link to post
Share on other sites

This topic is rather long winded but what the heck.  

 

I have taken to using Lee's version of the Sevens Problem, with a few "Camel Forth friendly changes, as a general test of my system builds.

 

Lately I built a "poor-man's just-in-time compiler addition that requires some changes to the source code.

You use the INLINE[   ]   directive on Forth primitives, constants, variables and user-variables and they are compiled as inline machine code. Kind of manual but it works.

 

So I wondered what it would do.

With the inlining, doing the version that only prints the final results,  I finally got down near the times that Lucien gets with his MLC compiler. 9.3 seconds versus 16.1 secs without inlining any code.

Nice to know the tool can make a difference.

 

 

Spoiler
\ Lee Stewart's mod of Lucien2's code for the sevens problem

\ Speedup mods for CAMEL99 Forth
\ 1. Used VALUES
\ 2. VTYPE for all short strings
\ 3. Used UM/MOD , native division
\ 4. >DIGIT for digit conversion
\ 5. Redefined PAD as static memory
\ 6. Defined 7*
\ 7. Used inline for as many primitives as possible


NEEDS ELAPSE FROM DSK1.ELAPSE
NEEDS VALUE  FROM DSK1.VALUES
NEEDS MALLOC FROM DSK1.MALLOC
NEEDS VTYPE  FROM DSK1.VTYPE
NEEDS INLINE[ FROM DSK1.INLINE

MARKER /SEVENS

DECIMAL
\ ------------------------------------
180 CONSTANT SIZE
SIZE MALLOC CONSTANT A1  \ A1 digit array
SIZE MALLOC CONSTANT PAD

0 VALUE LENGTH    \ current number of digits in result

HEX
CODE 7*   C044 , 0A34 , 6101 , NEXT, ENDCODE

DECIMAL
: A1*7->A1 ( -- ) \ perform 7 * last result
   0              \ initialize carried digit on stack
   1 +TO LENGTH   \ assume we will increase length by 1 digit
   A1 LENGTH BOUNDS
   DO
      INLINE[ I [email protected]  7* +  0 10 UM/MOD ] \ make result ud..unsigned divide by 10
      INLINE[ SWAP I C!  ]   \ store rem as cur digit..carry on stack
   LOOP
   DROP            \ clean up stack
 \ eliminate leading 0
   A1 LENGTH INLINE[ 1- + [email protected] 0=  ]   \ highest digit = 0?
   IF
      -1 +TO LENGTH  \ correct digit count
   THEN  ;

: A1$ ( -- addr len)
   PAD DUP              \ PAD & COPY for string storage
   A1 1- DUP LENGTH +
   DO
      INLINE[ I [email protected] >DIGIT   OVER C!  1+ ]
   -1 +LOOP
    DROP             \ clean up stack
   ( PAD) LENGTH ;

: A1$.TYPE ( --)
   [ A1 1- ] LITERAL LENGTH OVER +
   DO
     INLINE[ I [email protected] >DIGIT  VPUT  VCOL [email protected] C/[email protected] ]
     >= IF  CR  THEN
   -1 +LOOP ;

: 7COUNTER ( -- ? )  \ Brian Fox's technique
   0                 \ initialize counter
   A1 LENGTH BOUNDS  \ DO A1 to A1 + length
   DO
    INLINE[ 1+ I [email protected] 7 = AND  DUP 6 = ]
      IF             \ more than '77777'?
         LEAVE       \ yup..we're done
      THEN
   LOOP
;

DECIMAL
: .POWER ( n -- ) S" SEVEN TO THE POWER OF " VTYPE DECIMAL .  S" IS" VTYPE ;

: RUN      \ V2.58 1:26 ,
           \ v2.59 with 8 line scrolling, 1:02
           \ v2.62 with inline v4  54 sec.
   PAGE
   A1 SIZE 0 FILL
   7 A1 C!
   1 TO LENGTH
   2                 \ starting power
   BEGIN
      7COUNTER 5 <
   WHILE
      A1*7->A1
      DUP            \ dup power for display
      CR .POWER
      1+             \ increment power
      CR A1$.TYPE
      CR
   REPEAT
   DROP ;

DECIMAL
: NOSCROLL
   PAGE
   A1 SIZE 0 FILL
   7 A1 C!
   1 TO LENGTH
   2                 \ starting power
   BEGIN
      7COUNTER 5 <
   WHILE
      A1*7->A1
      DUP            \ dup power for display
      0 0 AT-XY .POWER
      1+             \ increment power
\     CR CR A1$ TYPE    \ 39:08
     CR CR A1$ VTYPE    \ 24:50, W/INLINE4 00:15.35
   REPEAT
   0 7 AT-XY ;

DECIMAL
: FASTRUN         ( 16.1 seconds) ( W/INLINE 9.3 seconds)
   PAGE ." Working..."
   A1  SIZE 0 FILL
   PAD SIZE 0 FILL
   7 A1 C!
   1 TO LENGTH
   2                 \ starting power
   BEGIN
     7COUNTER 5 <
   WHILE
      A1*7->A1
      1+             \ increment power
   REPEAT
   1- CR .POWER
   CR A1$ VTYPE
   0 7 AT-XY
;

DECIMAL

CR .( TYPE: ELAPSE RUN, ELAPSE NOSCROLL or ELAPSE FASTRUN ) CR

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...