Path: utzoo!attcan!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: F-PC Forth Tutorial Message-ID: <647.UUL1.3#5129@willett.UUCP> Date: 12 Mar 90 04:01:28 GMT Organization: Latest link in the ForthNet chain. (Pgh, PA) Lines: 69 Date: 03-10-90 (16:57) Number: 414 (Echo) To: JACK BROWN Refer#: NONE From: KENNETH O'HESKIN Read: NO Subj: BYTE ARRAY PUZZEL Status: PUBLIC MESSAGE ( BYTE_AVG ( addr-byte array, n-size -- average ) ( : -ROT ROT ROT ; ) ( : TUCK ) ( DUP 2 -ROLL ; ) ( High level definition ) ( : BYTE_AVG ( addr n -- average ) ( TUCK ) ( OVER + SWAP ) ( 0 -ROT ) ( DO I C@ + LOOP ) ( SWAP / ; ) ( Use: DATA N BYTE_AVG . ) HEX ( 70 CONSTANT RMW ( Reserve a variable in direct page memory ) ( Is it cheating to declare an external variable? ) ( If there's any danger of colliding with other data we can increment ) ( the stack by a word and use it as a workspace; I think d p memory ) ( should only be used for local variables and the stack anyway. ) ( Code definition ) CODE BYTE_AVG ( addr n -- ) 2 ,Y LDD, ( load byte array addr d ) 0 ,Y ADDD, ( add size to d ) 70 DIR STD, ( store loop limit in RMW ) 2 ,Y LDX, ( load index x with base adr ) CLRA, ( init d to hold current byte ) CLRB, BEGIN, 0 ,X ADDB, ( accumulate total ) 0 #, ADCA, INX, ( increment addr ) 70 DIR CPX, ( 5 cycles, gulp! ) U>=, ( continue until x=RMW ) UNTIL, 0 ,Y LDX, IDIV, ( yowl! ) INY, ( dec stack by one word ) INY, 0 ,Y STX, ( leave result on tos ) NEXT END-CODE DECIMAL ( Approximate 11 times faster than hi-level definition for averaging ) ( an 8k byte array, looped 10 times . ) --- ~ EZ 1.26 ~ NET/Mail : British Columbia Forth Board - Burnaby BC - (604)434-5886 ----- This message came from GEnie via willett through a semi-automated process. Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'