Benutzer-Werkzeuge

Webseiten-Werkzeuge


projects:sample2.blk

EXAMPLES FOR LECTURE #2

Screen 0 not modified     
 0 \ EXAMPLES FOR LECTURE #2                      17:42JWB09/21/85 
 1 \ Last change:   Screen  019                   09:57JWB01/17/86 
 2                                                                 
 3                                                                 
 4                                                                 
 5                                                                 
 6                                                                 
 7                                                                 
 8                                                                 
 9                                                                 
10                                                                 
11                                                                 
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 1 not modified     
 0 \ REVIEW - 1                                   17:56JWB09/21/85 
 1     FORTH83 B:<name>.BLK     ( Start F83  with VEDIT .)         
 2     F83      <name>.BLK     ( Start original F83    )           
 3     BYE                     ( Exit FORTH, return to DOS.)       
 4     WORDS                   ( List all words in dictionary.)    
 5     FORGET  <name>          ( Remove <name> and all following ) 
 6                             ( words from the dictionary.)       
 7     EMPTY                   ( Remove all new words added since )
 8                             ( this session was started.)        
 9     : <name> .... ;         ( Create new word  <name> .)        
10     ."   <string> "         ( Compile the  <string> into the )  
11                             ( dictionary. <string> will be   )  
12                             ( printed when word is executed. )  
13                             ( Do not use outside a colon def.)  
14     .(   <string> )         ( Print string to terminal. Use  )  
15                             ( outside a colon definition.    )  


Screen 2 not modified     
 0 \ REVIEW - 2                                   18:15JWB09/21/85 
 1   last first   DO  ...  LOOP    ( Use within a colon def to )   
 2                                 ( loop from first to last.  )   
 3   last first  ?DO  ...  LOOP    ( Same as above except loop )   
 4                                 ( 0 times if   first=last   )   
 5     BEGIN  ....  AGAIN          ( Use within a colon def to )   
 6                                 ( create an infinite loop.  )   
 7     SPACE       ( --   -- )     ( Send a space to the display.) 
 8     SPACES      ( n    -- )     ( Send n spaces to display. )   
 9     CR          ( --   -- )     ( Start a new line on display.) 
10     EMIT        ( n    -- )     ( Display ASCII character n .)  
11     .           ( n    -- )     ( Send top of stack to display.)
12     +         ( m n   sum )     ( Leave sum:  sum = m + n      )
13     -         ( m n   dif )     ( Leave difference: dif = m - n)
14     *         ( m n   prod )    ( Leave product: prod = m * n  )
15     .S          ( --   -- )     ( Non destructive stack print. )


Screen 3 not modified     
 0 \ REVIEW - 3                                   18:34JWB09/21/85 
 1     MAX         ( m n   max )   ( Leave larger  of m and n )    
 2     MIN         ( m n   min )   ( Leave smaller of m and n )    
 3     DEPTH       ( --    n   )   ( Leave count of stack items.)  
 4     \                           ( Comment to end of line   )    
 5     (  this is a comment  )                                     
 6     A:          ( --   -- )     ( Make the current drive A )    
 7     B:          ( --   -- )     ( Make the current drive B )    
 8     DIR         ( --   -- )     ( List current drive directory.)
 9     LIST        ( n    -- )     ( List screen or block n .)     
10     LOAD        ( n    -- )     ( Compile screen n .)           
11     INDEX       ( m n  -- )     ( Index screens m through n .)  
12     L           ( --   -- )     ( List the current screen. )    
13     N L         ( --   -- )     ( List the next  screen. )      
14     B L         ( --   -- )     ( List the previous screen.)    
15                                                                 


Screen 4 not modified     
 0 \ REVIEW - 4                                   18:49JWB09/21/85 
 1     FILE?       ( --   -- )     ( Display current screen file.) 
 2     DEFAULT     ( --   -- )     ( Revert to default screen file)
 3                                 ( used when FORTH was started.) 
 4     OPEN  <name>.BLK            ( Open an existing screen file.)
 5 \  Create a new screen file  <name>.BLK  with n blank screens.) 
 6     CREATE-FILE <name>.BLK  ( n   -- )                          
 7     MORE        ( n    -- )    ( Add n blank screens to the    )
 8                                ( end of the current screen file)
 9     QUERY       ( --   -- )    ( Fetch a line of text from user)
10     INTERPRET   ( --   -- )    ( Interpret the text line word by
11                                ( word.)                         
12     VEDIT       ( n    -- )    ( Begin editing screen n )       
13     VED         ( --   -- )    ( Begin editing current screen ) 
14     CAPACITY    ( --   n  )    ( Leave number of screens in the 
15                                ( current screen file.)          


Screen 5 not modified     
 0 \ EXAMPLES -1                                  09:55JWB01/17/86 
 1 \ Clear the data (or parameter stack).                          
 2 : CLEAR  ( ??   empty )                                         
 3         DEPTH 0 ?DO  DROP  LOOP ;                               
 4                                                                 
 5 \ Count  from 0 to n .                                          
 6 : COUNT_UP  ( n   -- )                                          
 7         0 ?DO CR         \ Advance to new line.                 
 8               I          \ Fetch current value of loop counter. 
 9               .          \ Display it.                          
10            LOOP  ;   EXIT                                       
11                                                                 
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 6 not modified     
 0 \ DEBUGGER                                     09:54JWB01/17/86 
 1 The debugger is designed to let the user single step through    
 2 the execution sequence of a high level definition. This process 
 3 is also called tracing.  To activate the debugger type:         
 4                                                                 
 5         DEBUG   <name>                                          
 6                                                                 
 7 where <name> is the word to be debugged or traced.  When the    
 8 word <name> is next executed you will get a single step trace   
 9 showing the next word to be executed and the contents of the    
10 data stack. Press any key except C F or Q for the next step.    
11         Q   -  Quit debugging process.                          
12         C   -  Continue without pausing between steps.          
13         F   -  Return to FORTH to execute other commands.       
14             -  You must type  RESUME to continue debugging.     
15      UNBUG  -  Disconect the debugger or you may crash and burn.


Screen 7 not modified     
 0 \ NEW  WORDS                                   09:55JWB01/17/86 
 1 \ You add the stack comments and descriptions.                  
 2                                                                 
 3         DROP                                                    
 4         SWAP                                                    
 5         DUP                                                     
 6         OVER                                                    
 7         ROT                                                     
 8        -ROT                                                     
 9                                                                 
10         2DROP                                                   
11         2SWAP                                                   
12         2DUP                                                    
13         2OVER                                                   
14                                                                 
15                                                                 


Screen 8 not modified     
 0 \ EXAMPLE - 2  Volume of a tank.               09:56JWB01/17/86 
 1                                                                 
 2 : TANK_VOLUME   ( l w h   -- )                                  
 3         CR ROT DUP  ." Length " .  ." feet"                     
 4         CR ROT DUP  ." Width  " .  ." feet"                     
 5         CR ROT DUP  ." Height " .  ." feet"                     
 6         CR *   *    ." Volume " .  ." cubic feet." ;            
 7                                                                 
 8 \  Explain  FACTORING .                                         
 9 \  Show the effect of an incomplete definition.                 
10 \  More New Words Below.                                        
11 \  HIDE   ( --   -- )  Mark last word so it cannot be found.    
12 \  REVEAL ( --   -- )  Mark last word so it can be found.       
13 \  [                   Stop compiling and resume interpretation.
14 \  ]                   Stop interpreting and resume compilation.
15                                                                 


Screen 9 not modified     
 0 \ EXAMPLE - 3  Volume of a tank factored.      22:32jwb09/21/85 
 1                                                                 
 2 : .ECHO   ( a b c   b c a )                                     
 3         ROT DUP . ;                                             
 4                                                                 
 5 : .FT   ( --   -- )                                             
 6         ."  feet." CR ;                                         
 7                                                                 
 8 : TANK_VOLUME   ( l w h   -- )                                  
 9         CR  ." Length " .ECHO .FT                               
10             ." Width  " .ECHO .FT                               
11             ." Height " .ECHO .FT                               
12    *   *    ." Volume " .  ." cubic" .FT   ;                    
13                                                                 
14                                                                 
15                                                                 


Screen 10 not modified     
 0 \ EXAMPLE - 4  Surface area of closed tank     09:56JWB01/17/86 
 1 \ New Words                           I made up the ones below  
 2 \       PICK                          all by myself!!           
 3 \       ROLL                                                    
 4 \       NIP                           SPIN  ( a b c  c b a )    
 5 \       TUCK                          HATCH ( a b c  a b b c )  
 6 \       3DUP                          SHOVE ( a b c  c a b c )  
 7                                                                 
 8 : TANK_AREA  ( l w h   -- )  \ add stack comments for each line 
 9         3DUP            (                   )                   
10         5 ROLL *  2 *   (                   )                   
11         -ROT   *  2 *   (                   )                   
12         +               (                   )                   
13         -ROT *  2 *     (                   )                   
14         +               (                   )                   
15     CR  ." Surface area of tank is " .  ." square" .FT ;        


Screen 11 not modified     
 0 \ PROBLEM - 1   Rectangle area & perimeter     09:56JWB01/17/86 
 1                                                                 
 2 a) Write a word called  .LW  that takes two stack items, a      
 3    length and a width and echos the values on separate lines    
 4    with identifiers.  Note .LW should leave stack empty.**      
 5 b) Write a word called AREA that takes two stack items, a       
 6    length and a width, and computes and displays the area of a  
 7    rectangle.  Note  AREA  should leave the stack empty.**      
 8 c) Write a word call PERIMETER that takes two stack items, a    
 9    length and a width, and computes and displays the perimeter  
10    of a rectangle.  Note  PERIMETER should leave stack empty.** 
11 d) Write a word called RECTANGLE that takes two stack items, a  
12    length and a width, and performs the functions of a b & c .  
13    Did you use the   .LW , AREA , and PERIMETER in your         
14    definition of RECTANGLE?                                     
15 **  It would be better to say: it consumes its stack parameters.


Screen 12 not modified     
 0 \ PROBLEW-2 Tank volume and surface area       09:56JWB01/17/86 
 1                                                                 
 2   Write words  .LWH   VOLUME   and  AREA  along the lines       
 3   of the previous problem except that they take 3 stack items,  
 4   length, width, and height.  VOLUME computes the volume of the 
 5   tank. AREA computes the surface area of the tank.  Combine    
 6   these into a single word   TANK  which performs the function  
 7   of all three as in PROBLEM - 1.                               
 8                                                                 
 9                                                                 
10                                                                 
11                                                                 
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 13 not modified     
 0 \ PROBLEM - 3   Rectangle  again.              09:56JWB01/17/86 
 1   The area and perimeter of a rectangle       -----------(X2,Y2)
 2   can be determined from the coordinates      |         |       
 3   of either pair of opposite corners.         |         |       
 4   A = ( X2 - X1 )*( Y2 - Y1)                  |         |       
 5   P = 2*(( X2 - X1 )+( Y2 - Y1))              -----------       
 6                                            (X1,Y1)              
 7   Write words to find the length, width, area, and perimeter    
 8   and then combine them into one one word as in problem 1.      
 9   Can any of the work from problem 1 be used here?              
10   Do your words work if you feed them upper left and lower      
11   right coordinates?                                            
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 14 not modified     
 0 \  PROBLEM - 4  Falling objects.               09:56JWB01/17/86 
 1   The height of a falling object above the ground at time t     
 2   seconds is given by                                           
 3            height =  h0 - v0*t - 16*t*t                         
 4   where h0 is the initial height in feet                        
 5   and v0 is the initial velocity in feet per second.            
 6   Write a word called   HEIGHT that takes initial height        
 7   initial velocity and a time from the stack and produces the   
 8   following output.                                             
 9                                                                 
10   You type:     1000 100 3  HEIGHT     ( return )               
11   And see :     Initial height is 1000 feet.                    
12                 Initial velocity is 100 feet per second.        
13                 Height after  3  seconds is 412  feet.          
14   Does your solution consist of one long definition?            
15   ( the 412 above may be wong!! )                               


Screen 15 not modified     
 0 \ DIVISION   A real can of worms.              09:56JWB01/17/86 
 1      /                                                          
 2      MOD                                                        
 3      /MOD                                                       
 4                                                                 
 5                                                                 
 6                                                                 
 7                                                                 
 8                                                                 
 9  : TRIANGLE.AREA  ( b h   -- )                                  
10         * 2 / ;                                                 
11                                                                 
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 16 not modified     
 0 \ EASY WORDS                                   09:56JWB01/17/86 
 1                                                                 
 2         1+                                                      
 3         2+                                                      
 4         1-                                                      
 5         2-                                                      
 6         2*                                                      
 7         2/                                                      
 8                                                                 
 9         ABS                                                     
10                                                                 
11         NEGATE                                                  
12                                                                 
13                                                                 
14                                                                 
15                                                                 


Screen 17 not modified     
 0 \  DISPLAYING A TABLE    contains error!!!     09:57JWB01/17/86 
 1 \   New word:    .R   ( n w   -- )                              
 2 \   Print  n  right justified in a field  w  wide.              
 3 : .ONE  ( n   -- )                                              
 4         12 SPACES               \ Indent table entry.           
 5         DUP 8 .R                \ Print number.                 
 6         DUP DUP *  8 .R         \ Print square of number.       
 7     DUP DUP DUP  * *  8 .R ;    \ Print cube of number.         
 8 : .HEADING  ( --   -- )                                         
 9        CR CR CR 14 SPACES                                       
10        ." NUMBER  SQUARE    CUBE" ;                             
11 : .TABLE  ( first last   -- )                                   
12         .HEADING             \  ?ENOUGH   explain it.           
13         1+ SWAP                                                 
14         ?DO CR I .ONE LOOP                                      
15         CR CR ;                                                 


Screen 18 not modified     
 0 \ PROBLEM - 5   Tables for Tanks.              09:57JWB01/17/86 
 1 a)Modify the previous example so that it gives a table of       
 2   volumes and surface areas of cubical tanks.                   
 3   The new heading should have the form:                         
 4         SIDE    SURFACE AREA     VOLUME                         
 5         feet    square feet    cubic feet                       
 6         ====    ============   ==========                       
 7   Entries should be neatly centered under the table headings!!  
 8                                                                 
 9 b)Repeat the above problem for spherical tanks.  Heading        
10   should read:  RADIUS    SURFACE AREA     VOLUME   etc.        
11   For a sphere   V = 4 pi R*R*R/3                               
12                  S = 4 pi R*R                                   
13                 pi = 3.1416                                     
14   Hint:  to multiply by pi see Brodie page 122 or use -         
15 : *PI  ( n   n*pi )   355 113  */  ;                            


Screen 19 not modified     
 0 \  ASCII TABLE                                 09:57JWB01/17/86 
 1 : .ONE  ( n   -- )                                              
 2         12 SPACES DUP EMIT DUP 4 .R                             
 3         HEX 4 .R DECIMAL ;                                      
 4 : .HEADING  ( --   -- )                                         
 5        CR CR CR 9 SPACES ." CHAR DEC HEX"  ;                    
 6 : ATABLE  ( first last   -- )                                   
 7         .HEADING                                                
 8         1+ 256 MIN                                              
 9         SWAP  0 MAX                                             
10         ?DO CR I .ONE LOOP ;                                    
11 : .TABLE  ( --   -- )                                           
12         16 0 DO  I 16 * DUP 15 +                                
13                  ATABLE  KEY  DROP                              
14              LOOP ;                                             
15                                                                 


Screen 20 not modified     
 0 \ Notes on Starting Forth. 1                   17:12JWB09/22/85 
 1 \ P 50    Replace 'S with SP@ or define  : 'S   SP@ ;           
 2 \         Replace S0 with SP0 or define  : S0   SP0 ;           
 3 \  Because FORTH83 loops work differently you should use:       
 4         : .S CR  SP@ SP0 @ 2DUP <>                              
 5              IF   2- DO I  @ . -2 +LOOP                         
 6              ELSE 2DROP ." EMPTY" THEN   ;                      
 7 \ Or  just use .S  that is already part of your system.         
 8                                                                 
 9 \ P 66-72  Use:    n  VEDIT     to edit screen n .              
10 \ The original F83.COM fill supports the STARTING FORTH style   
11 \ editor.   Go back to DOS and type  F83  <name>.BLK  to try it.
12 \ P 83  See notes for P 50 above.  Also VIEW  .S  to see how    
13 \        the F83 version is implemented.                        
14                                                                 
15                                                                 


Screen 21 not modified     
 0 \ Notes on Starting Forth. 2                   17:12JWB09/22/85 
 1 \ P 110 In F83 I J and K are used only within loops to fetch    
 2 \       the current values of the loop counters.  Use  R@       
 3 \       instead of I to fetch a copy of the top of the return   
 4 \       stack to the parameter stack without changing the       
 5 \       return stack.                                           
 6 \       Use the sequence:  R> R@ SWAP >R  within a definition   
 7 \       instead of I' to fetch  a copy of the second item on    
 8 \       the return stack.  Or use the definition:               
 9 : 2R@   R> R> R@ -ROT >R >R ;                                   
10 \       Use 2R@  for I' .   I' is a poor name!!                 
11 \       WARNING!  It is dangerous to mess with the return stack.
12 \ P112  Use  R@  instead of I  ,  also a dot  .  was ommited    
13 \       from the definition of QUADRATIC.   See below.          
14 : QUADRATIC     ( a b c x   n )                                 
15         >R SWAP ROT R@ * + R> * + .  ;                          


Screen 22 not modified     
 0 \ Notes on Starting Forth. 3                   17:13JWB09/22/85 
 1 \ P 119 Error in footnote!                                      
 2 : R%  50 */ 1+ 2/ ;                                             
 3 \ P 123 I I' and J  see comments for page 110.                  
 4 \ P 131 For COMPOUND to display as in text  insert a   CR       
 5 \       before  SWAP .  ie  : COMPOUND  CR SWAP 21 0 DO  etc    
 6 \ P 135 There is an error in TEST -  should be 10 10 DO  but    
 7 \       even so . .   the FORTH83  DO .. LOOP is not like       
 8 \       the one in Starting FORTH !!!!   Try the following      
 9 : -TEST  DO I . ?KEY ?LEAVE ( any key exit ) -1 +LOOP ;         
10 : +TEST  DO I . ?KEY ?LEAVE ( any key exit )  1 +LOOP ;         
11 \  9 10 +TEST   loops 65534 times.  9 10 -TEST gives   10 9     
12 \ 10 10 +TEST   loops 65535 times. 10 10 -TEST gives   10       
13 \ 11 10 +TEST  gives  10           11 10 -TEST loops 65535 times
14 \ If you try any of the longer loops above press any key to     
15 \ abort!!                                                       


Screen 23 not modified     
 0 \ Notes on Starting Forth. 4                   17:13JWB09/22/85 
 1 \ P 137 XX will clear the stack as indicated but our definition 
 2 \       : CLEAR  DEPTH 0 ?DO DROP LOOP ;  will do it without    
 3 \       the error message.  An alternate definition of CLEAR is:
 4 : CLEAR  SP0 @ SP! ;                                            
 5 \ P 140 In FORTH83  LEAVE causes an immediate exit from the     
 6 \       DO ... LOOP  !!  Thus any words between LEAVE and LOOP  
 7 \       will not be executed if the condition for leaving is    
 8 \       is satisfied.  Try the following and note that  HELLO   
 9 \       is not printed the last time.                           
10 : HELLOS  10 1 DO CR I . I 5 = IF LEAVE THEN ." HELLO" LOOP ;   
11 \       Note: ?LEAVE can replace the phrase  IF LEAVE THEN      
12 \ P 142-143  Use CLEAR-SCREEN definition below instead of PAGE  
13 : CLEAR-SCREEN  0 0 0 2 [ EDITOR ] VIDEO-IO 2DROP 2DROP ; FORTH 
14                                                                 
15                                                                 


Screen 24 not modified     
 0 \ Notes on Starting Forth. 5                   17:13JWB09/22/85 
 1 \ P 161 In FORTH83  UM*     is the name for U*                  
 2 \       and         UM/MOD  is the name for U/MOD               
 3 \       In FORTH83  all loops are relative, use +LOOP for /LOOP 
 4 \ P 164 Double number punctuation is  .  only!  not  ,          
 5 \ P 166 Use the   .   for the  ,  in the double numbers!!!      
 6 \ P 169 FORTH83   SIGN  uses the first number on the stack!!    
 7 \       Whenever Brodie uses  SIGN  replace it with  ROT SIGN   
 8 \ P 170 Replace   SIGN   with   ROT  SIGN                       
 9 \       VIEW  D.  to see how F83  formats numbers.              
10 \ P 172 Replace   SIGN   with   ROT  SIGN                       
11 \ P 174 Use the definitions below:                              
12 : M+    S>D D+ ;     : M/    M/MOD  NIP ;                       
13 : M*/   2DUP XOR SWAP ABS >R SWAP ABS >R OVER XOR -ROT          
14         DABS SWAP R@ UM* ROT R> UM* ROT 0 D+ R@ UM/MOD          
15         -ROT R> UM/MOD NIP SWAP ROT ?DNEGATE ;                  


Screen 25 not modified     
 0 \ Notes on Starting Forth. 6                   17:28JWB09/22/85 
 1 \ P 177 In FORTH83  use   UM*  for  U* and UM/MOD for U/MOD     
 2 \       In FORTH83  use  +LOOP for /LOOP                        
 3 \ P 178 Use  ROT SIGN where ever Brodie uses SIGN  !!           
 4 \ P 202 Use CLEAR-SCREEN definition given earlier for  PAGE     
 5 \ P 204 Use +LOOP  for /LOOP                                    
 6 \ P 215 Type  VIEW  INTERPRET  for definition of our INTERPRET  
 7 \ P 217 In F83  DEFER   is used for execution vectors.          
 8 \ P 233 In F83  the variable H is called  DP                    
 9 \ P 235-237  : 'S  SP@ ;                                        
10 \ P 240 Use DP  instead of H  ;  No OFFSET in this system.      
11 \ P 242 F83 Uses the ONLY vocabulary structure. See the FORTH-83
12 \       standard or wait till we discuss it in class.           
13 \ P 247 Use DP  instead of H  .                                 
14                                                                 
15                                                                 


Screen 26 not modified     
 0 \ BYTE                                         21:51JWB09/22/85 
 1 - the smallest piece of data that can be accessed by most modern
 2   micro-computers.                                              
 3 - consists of a cell of 8 binary bits:  01110101 <= see the byte
 4 - the low order bit is number  0.       76543210 <= bit number  
 5 - the high order bit is number 7.                               
 6 - the bit pattern in a byte can represent an unsigned integer:  
 7          0 -      255 decimal                                   
 8   00000000 - 11111111 binary                                    
 9         00 -       FF hexadecimal                               
10 - the data structure often represented by bytes is the ASCII    
11   character.                                                    
12     0   20  control characters.                                 
13    21  127  common symbols, digits, letters.                    
14   128  255  in the IBM-PC  the extended character set.          
15                                                                 


Screen 27 not modified     
 0 \ WORD                                         22:14JWB09/22/85 
 1 - a word consists of two bytes or 16 binary bits.               
 2 - most easily represented by four hexadecimal digits.           
 3 - examples:  A123   FEED  DEAD                                  
 4 - the bit pattern in a word can represent an unsigned integer:  
 5         0 - 65535  decimal    0000 - FFFF  hexadecimal          
 6 - micro computers can access 65,535 memory cells (bytes) using  
 7   the word as an address pointer.                               
 8 - the bit pattern in a word can also represent signed integers: 
 9      -32768 - 32767  decimal   -8000 - 7FFF  hexadecimal        
10 - In FORTH each data stack number is represented by a word.     
11 - In FORTH address are represented by words or unsigned integers
12   thus FORTH can address or access 65535 bytes of memory.       
13                                                                 
14                                                                 
15                                                                 


Screen 28 not modified     
 0                                                                 
 1                                                                 
 2 ONLY FORTH ALSO ALSO DEFINITIONS                                
 3                                                                 
 4 CODE  SPLIT                                                     
 5         AX  POP         \ Get number from stack                 
 6      BH BH  SUB         \ Clear high bits of BX                 
 7      AL BL  MOV         \                                       
 8         BX  PUSH                                                
 9      AH BL  MOV                                                 
10         BX  PUSH                                                
11             NEXT                                                
12         END-CODE                                                
13                                                                 
14                                                                 
15                                                                 
projects/sample2.blk.txt · Zuletzt geändert: 2013-06-06 21:27 von 127.0.0.1