papierkorb:sample3.blk
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| papierkorb:sample3.blk [2025-08-16 17:50] – ↷ Seite von projects:sample3.blk nach papierkorb:sample3.blk verschoben mka | papierkorb:sample3.blk [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | === EXAMPLES FOR LECTURE #3 === | ||
| - | < | ||
| - | |||
| - | Screen 0 not modified | ||
| - | 0 \ EXAMPLES FOR LECTURE #3 11: | ||
| - | 1 \ Last change: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 1 not modified | ||
| - | 0 \ Load screen for help system. | ||
| - | | ||
| - | 2 \ The word FROM temporarily redirects the input to the | ||
| - | 3 \ indicated block file for ONE screen load only. | ||
| - | 4 \ However.... that one screen could specify that others be | ||
| - | 5 \ loaded. | ||
| - | 6 \ Go back and check it yourself. | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 2 not modified | ||
| - | 0 \ REVIEW - 1 DEBUGGER | ||
| - | 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. | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 where < | ||
| - | 8 word < | ||
| - | 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 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 3 not modified | ||
| - | 0 \ REVIEW - 2 STACK OPERATORS | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 2DROP ( dn -- ) Drop double number from top. | ||
| - | 13 2SWAP ( dn dm dm dn) Swap top two double numbers. | ||
| - | 14 | ||
| - | 15 2OVER ( dn dm dn dm dn) Copy second double number to top. | ||
| - | |||
| - | |||
| - | Screen 4 not modified | ||
| - | 0 \ REVIEW - 3 | ||
| - | 1 \ Floored symmetric division. | ||
| - | 2 \ the equations: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 Quiz: | ||
| - | | ||
| - | | ||
| - | 10 -2 5 5 * | ||
| - | 11 | ||
| - | 12 13 | ||
| - | 13 | ||
| - | 14 -2 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 5 not modified | ||
| - | 0 \ REVIEW - 4 Easy Words | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 \ These may help recover from wierd LOADing errors. | ||
| - | 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 6 not modified | ||
| - | 0 \ Number displaying words. | ||
| - | 1 \ Single signed 16bit numbers. | ||
| - | | ||
| - | | ||
| - | | ||
| - | 5 \ Single unsigned 16bit numbers. | ||
| - | | ||
| - | | ||
| - | | ||
| - | 9 \ Double signed 32bit numbers | ||
| - | 10 D. ( d -- ) Display signed 32bit # followed by space. | ||
| - | 11 D.R ( d w -- ) Display # right justified in w wide field. | ||
| - | 12 | ||
| - | 13 \ Double unsigned 32bit numbers. | ||
| - | 14 UD. ( ud -- ) Display unsigned 32bit # followed by space | ||
| - | 15 UD.R ( ud w -- ) Display # right justified in w wide field. | ||
| - | |||
| - | |||
| - | Screen 7 not modified | ||
| - | 0 \ Logicals and conditionals. | ||
| - | 1 \ tf = true flag = -1 ff = false flag = 0 | ||
| - | 2 \ flag = true flag or false flag. | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 8 not modified | ||
| - | 0 \ Ex 1 (IN) Prob 1 & Conditional Structur | ||
| - | 1 \ (IN) leaves a true flag if a < x < b | ||
| - | 2 : (IN) ( x a b flag ) | ||
| - | | ||
| - | 4 \ Problem 1: Write words related to (IN) which do the following. | ||
| - | 5 \ [IN] leaves a true flag if a <= x <= b , otherwise false. | ||
| - | 6 \ (IN] leaves a true flag if a < x <= b , otherwise false. | ||
| - | 7 \ [IN) leaves a true flag if a <= x < b , otherwise false. | ||
| - | | ||
| - | 9 \ CONDITIONAL STRUCTURES ... USE ONLY WITHIN A COLON DEFINITION. | ||
| - | 10 \ | ||
| - | 11 \ THEN continue | ||
| - | 12 | ||
| - | 13 \ | ||
| - | 14 \ ELSE do this part only if false | ||
| - | 15 \ THEN continue | ||
| - | |||
| - | |||
| - | Screen 9 not modified | ||
| - | 0 \ Example 2 , Problem 2 & 3 | ||
| - | 1 : TEST ( n -- ) \ Determine if number is even or odd. | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 \ Problem 2 | ||
| - | 8 \ Write word similar to TEST , whose output is a sentence | ||
| - | 9 \ stating whether the top number on the stack is positive , | ||
| - | 10 \ zero or negative. | ||
| - | 11 | ||
| - | 12 \ Problem 3 | ||
| - | 13 \ Write a word called | ||
| - | 14 \ input n and leaves a true flag if n is even and a false flag | ||
| - | 15 \ if n is odd. | ||
| - | |||
| - | |||
| - | Screen 10 not modified | ||
| - | 0 \ Terminating an infinite loop. | ||
| - | 1 \ New Word: KEY Wait for user to press key on keyboard and | ||
| - | 2 \ KEY ( -- n ) return the keycode n. | ||
| - | 3 \ Old Word: EXIT Stops screen compilation when not in a : def | ||
| - | 4 \ EXIT ( -- -- ) When compiled in a word, EXIT , will cause | ||
| - | 5 \ | ||
| - | 6 : KEY_TEST | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 DUP . EMIT \ Otherwise show key pressed. | ||
| - | 11 AGAIN ; | ||
| - | 12 \ Return | ||
| - | 13 : PCKEY ( -- | ||
| - | 14 KEY DUP IF TRUE ELSE KEY SWAP THEN ; | ||
| - | 15 \ Problem 4 Put this word in a loop and document function keys. | ||
| - | |||
| - | |||
| - | Screen 11 not modified | ||
| - | 0 \ Example - 3 Super simple numeric input. | ||
| - | 1 : #IN QUERY INTERPRET ; | ||
| - | | ||
| - | 3 : GETL ( -- l ) CR ." Enter tank length " #IN ; | ||
| - | 4 : GETW ( -- w ) CR ." Enter tank width " #IN ; | ||
| - | 5 : GETH ( -- h ) CR ." Enter tank height " #IN ; | ||
| - | | ||
| - | 7 : .VOLUME ( l w h -- ) | ||
| - | | ||
| - | 9 : .AREA ( l w h -- ) | ||
| - | 10 3DUP 5 ROLL * 2* -ROT * 2* + -ROT * 2* + | ||
| - | 11 CR ." Surface area " . ." square feet." ; | ||
| - | 12 | ||
| - | 13 : TANK ( -- -- ) | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 12 not modified | ||
| - | 0 \ Support words for better #IN | ||
| - | | ||
| - | 2 : DIGIT? | ||
| - | | ||
| - | | ||
| - | 5 : RUBOUT | ||
| - | | ||
| - | | ||
| - | 8 : -DIGIT | ||
| - | | ||
| - | 10 | ||
| - | 11 : +DIGIT | ||
| - | 12 48 - SWAP 10 * + ; | ||
| - | 13 | ||
| - | 14 --> | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 13 not modified | ||
| - | 0 \ Better, but not so simple # input. | ||
| - | 1 : #IN ( -- num ) | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 THEN | ||
| - | 11 AGAIN ; | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 14 not modified | ||
| - | 0 \ Support words for best #IN | ||
| - | 1 : DIGIT? | ||
| - | | ||
| - | 3 : RUBOUT | ||
| - | | ||
| - | 5 \ Note: -DIGIT & +DIGIT are changed from screen 11 !! | ||
| - | 6 \ Remove digit from screen and number then dec digit count. | ||
| - | 7 : -DIGIT | ||
| - | | ||
| - | 9 \ Increment digit count and add in digit. | ||
| - | 10 : +DIGIT | ||
| - | 11 SWAP 10 UM* 2 PICK 48 - 0 D+ 32767. 2OVER DU< | ||
| - | 12 | ||
| - | 13 ELSE DROP SWAP EMIT SWAP 1+ SWAP THEN ; | ||
| - | 14 : RESET ( flg cnt n ff cnt n ) | ||
| - | 15 ROT DROP FALSE -ROT ; --> | ||
| - | |||
| - | |||
| - | Screen 15 not modified | ||
| - | 0 \ Support words for the best # input. | ||
| - | 1 \ Correct an error input. | ||
| - | 2 : CORRECT.IT ( flg cnt num key flg cnt num ) | ||
| - | | ||
| - | | ||
| - | | ||
| - | 6 \ Process all other keystrokes. | ||
| - | 7 : PROCESS.IT ( flg cnt num key flg cnt num ) | ||
| - | | ||
| - | | ||
| - | 10 ELSE DROP BEEP THEN ; \ Invalid key or overflow. | ||
| - | 11 \ Apply sign to number. | ||
| - | 12 : APPLY_SIGN | ||
| - | 13 DROP NIP SWAP \ Drop key, nip cnt, get flg. | ||
| - | 14 IF NEGATE THEN ; --> | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 16 not modified | ||
| - | 0 \ Best #IN - protected field, signed input | ||
| - | 1 : #IN ( -- num ) \ flg=sign flag | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 DUP CONTROL H = \ Correct error input? | ||
| - | 11 IF | ||
| - | 12 ELSE PROCESS.IT | ||
| - | 13 THEN | ||
| - | 14 THEN AGAIN ; | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 17 not modified | ||
| - | 0 \ REVIEW - 3 | ||
| - | 1 \ Floored symmetric division. | ||
| - | 2 \ the equations: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 Quiz: | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 -2 5 3 -1 5 *-1 | ||
| - | 12 13 | ||
| - | 13 | ||
| - | 14 -2 | ||
| - | 15 Note: Remainder takes sign of divisor!! | ||
| - | |||
| - | |||
| - | Screen 18 not modified | ||
| - | 0 \ Problem 4 23: | ||
| - | 1 \ Program the following number guessing game. | ||
| - | 2 \ The computer picks a secret number between 1 and 100. You try | ||
| - | 3 \ to guess the number. | ||
| - | 4 \ " | ||
| - | 5 \ " | ||
| - | 6 \ " | ||
| - | 7 \ "YOU GOT IT" if the guess is correct. | ||
| - | 8 \ Hints: | ||
| - | 9 \ Use #IN | ||
| - | 10 \ Use the random number generator below. | ||
| - | 11 | ||
| - | 12 : (RND) SEED @ 259 * 3 + 32767 AND DUP SEED ! ; | ||
| - | 13 : RND ( n r ) \ r is a random number | ||
| - | 14 (RND) 32767 */ ; | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 19 not modified | ||
| - | 0 \ Problem 4 Solution. | ||
| - | 1 : WINNER? 2 PICK OVER = ; | ||
| - | 2 : HOT? 2 PICK OVER - ABS 3 < ; | ||
| - | 3 : WARMER? 2 PICK OVER - ABS | ||
| - | | ||
| - | | ||
| - | 6 : GAME | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 THEN NIP | ||
| - | 13 AGAIN ; | ||
| - | 14 \ Problem: | ||
| - | 15 \ number of guesses required and reports this at the game end. | ||
| - | |||
| - | |||
| - | Screen 20 not modified | ||
| - | 0 \ Example 4 Nasty Game. 10: | ||
| - | 1 \ A nasty game for the IBM-PC . | ||
| - | 2 : WHITE 177 EMIT ; | ||
| - | 3 : GAME CR | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 10 0 DO BEEP LOOP | ||
| - | 11 DROP ." You just busted your space bar!" | ||
| - | 12 EXIT THEN THEN | ||
| - | 13 DROP AGAIN ; | ||
| - | 14 \ Problem: | ||
| - | 15 \ | ||
| - | |||
| - | |||
| - | Screen 21 not modified | ||
| - | 0 \ Return Stack Example 5 Average | ||
| - | 1 \ New Words: | ||
| - | 2 \ These words are very dangerous!! Do NOT test or execute them | ||
| - | 3 \ interactively. They can only be used within colon definitions. | ||
| - | 4 \ >R ( n -- ) Transfer top data stack item to return stack. | ||
| - | 5 \ R> ( -- n ) Transfer top return stack item to data stack. | ||
| - | 6 \ R@ ( -- n ) Copy top return stack item to data stack. | ||
| - | 7 \ RULES: | ||
| - | 8 \ 1. Each use of >R must be balanced with a corresponding R> | ||
| - | 9 \ 2. Do not use >R R> and R@ within DO ... LOOPs. | ||
| - | 10 \ info is kept on the return stack and could be destroyed. | ||
| - | 11 : AVERAGE | ||
| - | 12 DEPTH >R R@ 1- 0 | ||
| - | 13 ?DO + LOOP | ||
| - | 14 CR ." The average of the " R@ . ." numbers is " | ||
| - | 15 R> / . CR ; | ||
| - | |||
| - | |||
| - | Screen 22 not modified | ||
| - | 0 \ Example 6 Histogram, Problems 5 & 6 11: | ||
| - | 1 \ Problem 5: | ||
| - | 2 \ Rewrite AVERAGE | ||
| - | 3 \ and frequency fi . ie average = [ sum xi*fi ]/n n = sum fi | ||
| - | 4 \ AVERAGE ( x1 f1 x2 f2 ... xk fk -- ) | ||
| - | | ||
| - | 6 : WHITE 177 EMIT ; | ||
| - | | ||
| - | 8 \ Given n frequencies construct histogram or bar chart. | ||
| - | 9 : HISTOGRAM ( f1 f2 ... fn -- ) | ||
| - | 10 CR DEPTH 0 | ||
| - | 11 ? | ||
| - | 12 \ Problem 6: | ||
| - | 13 \ Modify HISTOGRAM so that the bars come out in the proper order | ||
| - | 14 \ ( f1 first). Hint: " ROLL " | ||
| - | 15 \ the stack when finished printing bars. | ||
| - | |||
| - | |||
| - | Screen 23 not modified | ||
| - | 0 \ Example - 7 Square Root 11: | ||
| - | 1 \ Square root by Newton' | ||
| - | 2 \ Theory: | ||
| - | 3 \ function is the square root of n. | ||
| - | 4 \ Newton' | ||
| - | 5 \ according to: xn = xo - f(xo)/ | ||
| - | 6 \ It can be shown that: xn = ( xo + n/xo )/2 | ||
| - | | ||
| - | 8 : XNEW ( n xold n xnew ) | ||
| - | | ||
| - | 10 : SQRT ( n root ) | ||
| - | 11 DUP 0< IF ABORT" Illegal argument" | ||
| - | 12 DUP 1 > | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 \ Note: This is not the best or fastest square root algorithm. | ||
| - | |||
| - | |||
| - | Screen 24 not modified | ||
| - | 0 \ Example 8 Hypotenuse, Problem 7 Area | ||
| - | 1 \ Hypotenuse of a right triangle. | ||
| - | 2 : HYPO ( a b c ) | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 : TEST 15 1 DO 15 1 DO | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 \ Problem 7: Write a word that calculates the area of a triangle | ||
| - | 12 \ using HERO's formula. | ||
| - | 13 \ where s is the semi perimeter. | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 25 not modified | ||
| - | 0 \ Problem 8 Identify. | ||
| - | 1 \ Write the word IDENTIFY | ||
| - | 2 \ the data stack and prints one of the following descriptive | ||
| - | 3 \ phrases identifying the key code. | ||
| - | 4 \ Control character , Punctuation character , Lower case letter | ||
| - | 5 \ Upper case letter , Numeric Digit , Extended character. | ||
| - | 6 \ Hint: | ||
| - | 7 : IDENTIFY ( n -- ) | ||
| - | | ||
| - | | ||
| - | 10 DUP DIGIT? | ||
| - | 11 ... | ||
| - | 12 | ||
| - | 13 : DIGIT? | ||
| - | 14 ASCII 0 ASCII 9 [IN] ; | ||
| - | 15 \ Modify IDENTIFY to respond intelligently for n <0 and n>255 . | ||
| - | |||
| - | |||
| - | Screen 26 not modified | ||
| - | 0 \ Hard copy screen documentation. | ||
| - | | ||
| - | 2 \ Print three screens starting with n on the printer. | ||
| - | 3 : HTRIAD | ||
| - | | ||
| - | | ||
| - | | ||
| - | 7 \ Send a top of page command to printer. | ||
| - | 8 : FFEED | ||
| - | | ||
| - | 10 | ||
| - | 11 \ Print screens | ||
| - | 12 : DOC ( first last -- ) | ||
| - | 13 1+ SWAP DO I HTRIAD FFEED 3 +LOOP ; | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 27 not modified | ||
| - | 0 \ Solution to problem 5 19: | ||
| - | 1 : AVERAGE | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 \ | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 28 not modified | ||
| - | 0 \ Binary, decimal and hexadecimal number display. | ||
| - | 1 \ The radix of the FORTH system is the number base with which | ||
| - | 2 \ all arithmetic is performed. | ||
| - | | ||
| - | | ||
| - | 5 : BINARY 2 BASE ! ; \ Set system radix to base 2 | ||
| - | | ||
| - | 7 : .B BINARY | ||
| - | | ||
| - | 9 : .H HEX 4 U.R SPACE | ||
| - | 10 : .D DECIMAL 6 U.R SPACE ; | ||
| - | 11 | ||
| - | 12 : TABLE ( n -- ) | ||
| - | 13 CR ." DEC HEX | ||
| - | 14 1+ 0 ?DO CR I 4 .R I .H I .B LOOP ; | ||
| - | 15 | ||
| - | </ | ||
papierkorb/sample3.blk.1755359401.txt.gz · Zuletzt geändert: 2025-08-16 17:50 von mka