| Home CBM ASCII-X BASIC Disk Commands Enter RUN mode Program Format Secret Variables Variable Format Expressions Keywords (divide) (equal) (less) (minus) (more) (multiply) (plus) (power) Abs And Append Asc Atn Auto Backup Bank Begin Bend Bload Boot Box Bsave Bump Catalog Char Chr Circle Close Clr Cmd Collect Collision Color Concat Cont Copy Cos Data Dclear Dclose Dec Def Delete Dim Directory Dispose Dload Do Dopen Draw Ds Ds string Dsave Dverify El Else End Envelope Er Err Exit Exp Fast Fetch Filter Fn For Fre Get Get num Getkey Go Gosub Goto Graphic Gshape Header Help Hex If Input Input num Instr Int Joy Key Left Len Let List Load Locate Log Loop Mid Monitor Movspr New Next Not Off On Open Or Paint Peek Pen Pi Play Pointer Poke Pos Pot Print num Pudef Quit Rclr Rdot Read Record Rem Rename Renumber Restore Resume Return Rgr Right Rlum Rnd Rreg Rspcolor Rsppos Rsprite Run Rwindow Save Scale Scnclr Scratch Sgn Sin Sleep Slow Sound Spc Sprcolor Sprdef Sprite Sprsav Sqr Sshape St Stash Step Stop Str Swap Sys Tab Tan Tempo Then Ti Ti string To Trap Troff Tron Until Using Usr Val Verify Vol Wait While Width Window Xor Syntax Tokens C128 D64plus Disk Escape Codes Hardware PCxface PETSCII Pet2asc Futurama IBM PC-AT Contact Games Glossary Hall of fame Hall of shame Miscellaneous Privacy policy Programming Twisty puzzles |
DISPOSE { FOR | GOSUB }
Stack management. Remove unwanted "tokens" from the BASIC stack.
DISPOSE is used to "clean" the BASIC stack of FOR and GOSUB entries ("tokens"), usually caused by the use of GOTO. If unused "tokens" are left on the stack repeatedly, an OUT OF MEMORY ERROR will eventually occur (a "memory leak"). DISPOSE must be followed by either the keyword FOR or else the keyword GOSUB. Otherwise a SYNTAX ERROR occurs. BASIC will then scan its stack for the most recent entry of the specified type (FOR or GOSUB). If not found, then UNABLE TO DISPOSE (without the word ERROR) occurs. Otherwise, all entries starting from the most recent back to (and including) the found entry will be removed from the BASIC stack. Unlike NEXT, which allows a variable name to be given to "dispose" of possibly multiple FOR entries, DISPOSE will not allow an a variable name. Thus you may need to use DISPOSE multiple times. Unfortunately this handy statement is only available in BASIC v4.7. I'm not sure why it isn't available in later versions; it may be because this is considered bad programming style, and because newer versions (not neccessarily a greater version #) provide DO/LOOP which can be cleanly escaped with EXIT. However, that doesn't solve the problem of FOR/NEXT. Maybe they want you to use DO/LOOP instead? But that can be slower and use more program memory. Also, it is often more effecient to DISPOSE of a GOSUB entry than to maintain a bunch of state flags within a program. This has a lot to do with the lack of exception-handling statements in CBM BASIC. Some ML utilities have been published for other machines to implement the behavior of DISPOSE, because it generally is faster than the alternatives. Example:
In that example, the "user abort" command (pressing any key while in the loop) escapes from the FOR/NEXT loop and cleans the stack. In this example, it would also be possible to clean the stack with X=10000:NEXT (because X=10000 is the terminating condition of FOR), however that would destroy the value of X which might be needed. Of course the value of X could be saved to another variable. But those alternatives are pretty kludgy compared to DISPOSE. Similar cases involving GOSUB are nearly imposible to solve without DISPOSE unless you maintain one or more state variables in your program.
© H2Obsession, 2014 |