Professional Documents
Culture Documents
LeoBrodie'sinabilitytoexpresseventhemostcomplextechnical conceptswithoutaddingatwistofhumorcomesfromanearlylove ofcomedy.HespecializedinplaywritingatUCLAandhashad severalcomediesproducedthereandinlocaltheater.Hehasalso writtenfreelancemagazinearticlesandhasworkedasacopywriter foranaddagency.Whenacompanyhewasworkingforinstalleda computer,hebecameinspiredtotrydesigningamicroprocessor basedtoy.Althoughhenevergotthetoyrunning,helearnedalot aboutcomputersandprogramming.HenowworksatForth,Inc.asatechnicalandmarketingwriter, wherehecanplayonthecomputersasthemusedetermineswithouthavingtobeafanaticalcomputer jockey,andisallowedtowritebookssuchasthis. Leo'sotherinterestsincludesinging,drivingclassicVolvos,anddancingto50'smusic.
Foreword
TheForthcommunitycancelebrateasignificanteventwith thepublicationofStartingForth.Agreatereffort,talent, andcommitmenthavegoneintothisbookthanintoany previousintroductorymanual.I,particularly,ampleasedat thisevidenceofthegrowingpopularityofForth,the language. IdevelopedForthovertheperiodofsomeyearsasan interfacebetweenmeandthecomputersIprogrammed.The traditionallanguageswerenotprovidingthepower,ease,or flexibilitythatIwanted.Idisregardedmuchconventional wisdominordertoincludeexactlythecapabilitiesneeded byaproductiveprogrammer.Themostimportantoftheseis theabilitytoaddwhatevercapabilitieslaterbecome necessary. ThefirsttimeIcombinedtheideasIhadbeendeveloping intoasingleentity,IwasworkingonanIBM1130,a"third generation"computer.TheresultseemedsopowerfulthatI consideredita"fourthgenerationcomputerlanguage."IwouldhavecalleditFOURTH,exceptthatthe 1130permittedonlyfivecharacteridentifiers.SoFOURTHbecameFORTH,anicerplayonwords
anyway. OneprinciplethatguidedtheevolutionofForth,andcontinuestoguideitsapplication,isbluntly:KeepIt Simple.Asimplesolutionhaselegance.Itistheresultofexactingefforttounderstandtherealproblem andisrecognizedbyitscompellingsenseofrightness.Istressthispointbecauseitcontradictsthe conventionalviewthatpowerincreaseswithcomplexity.Simplicityprovidesconfidence,reliability, compactness,andspeed. StartingForthwaswrittenandillustratedbyLeoBrodie,aremarkablycapablepersonwhoseinsightand imaginationwillbecomeapparent.Thisbookisanoriginalanddetailedprescriptionforlearning.It deftlyguidesthenoviceoverthethresholdsofunderstandingthatallForthprogrammersmustcross. AlthoughIamtheonlypersonwhohasneverhadtolearnForth,Idoknowthatitsstudyisaformidable one.Aswithahumanlanguage,theusageofmanywordsmustbememorized.Forbeginners,Leo'sdroll commentsandsuperblycastcharactersappeartomakethisstudyeasyandenjoyable.Forthoselike myselfwhoalreadyknowForth,aquickreadingprovidesadelightfultripandfreshviewsoffamiliar terrain.ButIhopethisbookisnotsoeasyandenjoyablethatitseemstrivial.Bewarnedthatthereis heavycontenthereandthatyoucanlearnmuchaboutcomputersandcompilersaswellasabout programming. Forthprovidesanaturalmeansofcommunicationbetweenmanandthesmartmachinesheissurrounding himselfwith.Thisrequiresthatitsharecharacteristicsofhumanlanguages,includingcompactness, versatility,andextensibility.Icannotimagineabetterlanguageforwritingprograms,expressing algorithms,orunderstandingcomputers.Asyoureadthisbook,Ihopethatyoumaycometoagree. CharlesH.Moore InventorofForth
AboutThisBook
WelcometoStartingForth,yourintroductiontoanexcitingandpowerfulcomputerlanguagecalled Forth. Ifyou'reabeginnerwhowantstolearnmoreaboutcomputers,Forthisagreatwaytolearn.Forthismore funtowriteprogramswiththananylanguagethatIknowof.(Seethe"IntroductionforBeginners",and checkWikipedia) IfyouareaseasonedprofessionalwhowantstolearnForth,thisbookisjustwhatyouneed.Forthisa verydifferentapproachtocomputers,sodifferentthateveryone,fromnewcomerstooldhands,learns Forthbestfromthegroundup.Ifyou'readeptatothercomputerlanguages,putthemoutofyourmindfor now,andrememberonlywhatyouknowaboutcomputers.(Seethe"IntroductionforProfessionals.") SincemanypeoplewithdifferentbackgroundsareinterestedinForth,I'vearrangedthisbooksothat you'llonlyhavetoreadwhatyouneedtoknow,withfootnotesaddressedtodifferentkindsofreaders. ThefirsthalfofChap.7providesabackgroundtocomputerarithmeticforbeginnersonly. ThisbookexplainshowtowritesimpleapplicationsinForth.ItincludesallstandardForthwordsthat youneedtowriteahighlevelsingletaskapplication.Thiswordsetisanextremelypowerfulone, includingeverythingfromsimplemathoperatorstocompilercontrollingwords.(ANSForthstandard online)
Excludedfromthisbookareallcommandsthatarerelatedtotheassembler,targetcompilerandother specializedutilities.ThesecommandsareavailableonsomeversionsofForthsuchaseForthandmost commercialimplementations.(Forthvendors) I'vechosenexamplesthatwillactuallyworkonaForthsystemwithaterminalandadisk.Don'tinfer fromthisthatForthislimitedtobatchorstringhandlingtasks,sincethereisreallynolimittoForth's usefulness. Herearesomefeaturesofthisbookthatwillmakeiteasytouse: Allcommandsarelistedtwice:first,inthesectioninwhichthewordisintroduced,andsecond,inthe summaryattheendofthatchapter. Eachchapteralsohasareviewoftermsandasetofexerciseproblems,withanswers. Several"HandyHints"havebeenincludedtorevealproceduraltipsoroptionalroutinesthatareusefulfor learnersbutthatdon'tmeritanexplanationastohoworwhytheywork. Apersonalnote:Forthisaveryunusuallanguage.Itviolatesmanycardinalrulesofprogramming.My firstreactiontoForthwasextremelysceptical,butasItriedtodevelopcomplicatedapplicationsIbegan toseeitsbeautyandpower.Youoweittoyourselftokeepanopenmindwhilereadingaboutsomeofits peculiarities.I'llwarnyounow:fewprogrammerswholearnForthevergobacktootherlanguages. Goodluck,andenjoylearning! LeoBrodie FORTH,Inc. Copyrightnotice
Acknowledgements
TheoriginalprinteditionofStarting FORTHiscopyrightedbyFORTH, Inc.andallrightsarereserved. PermissionhasbeengrantedtoMarcel ForconsultationonForthtechniqueandstyle:Dean Hendrixtomakethisonlineversion Sanderson,MichaelLaManna,JamesDewey, oftheworkavailableandtomake EdwardK.Conklin,andElizabethD.Rather,allof certainmodificationstotheoriginal FORTHInc.;forprovidinginsightsintotheartof printedwork.Furtherreproduction teachingForthandforwritingseveralofthe Copyright (exceptfortheprintingofsinglecopies problemsinthisbook:KimHarrisoftheForth forpersonaluse),modification, InterestGroup;forproofreading,editorial distribution,postingonotherweb suggestions,andenormousamountsofwork sites,orunauthorizeduseofeitherthe formattingthepages:CarolynA.Rosenberg;for printedoronlineversionofStarting helpwithtypingandothernecessities:SueLinstrot, FORTHbyanypartyotherthan CarolynLubisich,KevinWeaver,KrisCramer,and MarcelHendrixisexpresslyforbidden StephanieBrownBrodie;forhelpwiththegraphics: withoutpriorwrittenpermissionfrom: WinnieShows,NatashaElbert,BarbaraRoberts,and FORTH,Inc. JohnDotsonofSunrisePrintery(RedondoBeach, 5155W.RosecransBlvd.#1018 CA);fortechnicalassistance:BillPattersonand Hawthorne,CA90250 GaryFriendlander;forconstructivecriticism,much More patienceandlove:StephanieBrownBrodie;andfor information USA +1(310)9789454[fax] +1(310)4913356[phone] I'dliketothankthefollowingpeoplewhohelpedto makethisbookpossible:
inventingForth:CharlesH.Moore.
StartingForth,FirstEditionisfrom1981.Thesewebpagesweredesignedin2003,whenitbecame apparentthatSFwouldneverbereissuedbythecopyrightholder.Asmallsupplyofabout500books wasallthatwasleft. Whenyoucangetholdoftheoriginal,doso. InthistranscriptForthcodehasbeenANSified.Thesamplesshouldrunon,atleast,iForthand SwiftForth.Wherenecessary,statementsthatwerevalidin1981havebeenexchangedwithstatements moreappropriatefor2003(whenthistributewaswritten). StartingForthisfullofverydifficulttoreproducegraphics.Theseenormouslyenhancethetext's mnemonicvalue,andareinvaluableforafirsttimeForthuser.Ihavethereforeadded"substitute"graphic elements,roughlyatthesamespotwheretheyareintheoriginal.Theoriginalgraphicsare,ofcourse, muchbetter. InthistranscriptIhaveassumeda32bit,byteaddressingForth,with8bitcharacters.Theaddress returnedbyWORDisassumedtobeHERE.ThisallowsthecommontrickofALLOTinglengthofstr CHARSafterusingWORDinordertocompilestringstrtomemory.Multitaskingissuesareignored (e.g.no>TYPE,justTYPE).Divisionissymmetric,notfloored,andtwo'scomplementisassumed throughout.MostForthsshouldnothaveproblemswillthis.Chapter7exploitsextendedusesofnumber conversion.SomeForthsarebrokeninthisrespect,butiForthandSwiftForthdosupporttheseneat features.
Introductions
IntroductionforBeginners:WhatisaComputerLanguage?
Atfirstwhenbeginnersheartheterm"computerlanguage,"theywonder,"Whatkindof languagecouldacomputerpossiblyspeak?Itmustbeawfullyhardforpeopletounderstand. Itprobablylookslike:
976#!@NX714&+
Ontheonehand,itmustbepreciseinitsmeaningtothecomputer,conveyingalltheinformationthatthe computerneedstoknowtoperformtheoperation.Ontheotherhand,itmustbesimpleandeasytouse bytheprogrammer. Manylanguageshavebeendevelopedsincethebirthofcomputers:Fortranistheelderstatesmanofthe field;COBOLisstillthestandardlanguagefordataprocessing;BASICwasdesignedasabeginner's languagealongtheroadtowardlanguageslikeFortranandCOBOL;CandJavaarethegeneralpurpose applicationlanguagesofthe90's.Thisbookisaboutaverydifferentkindoflanguage:Forth.Forth's popularityhaskeptconstantoverthepastseveralyears,anditspopularityissharedamongprogrammers inallfields. Allthelanguagesmentionedabove,includingForth,arecalled"highlevel"languages.It'simportantfor beginnerstorecognizethedifferencebetweenahighlevellanguageandthecomputeritrunson.Ahigh levellanguagelooksthesametoaprogrammerregardlessofwhichmakeormodelofcomputerit's runningon.Buteachmakeormodelhasitsowninternallanguage,or"machinelanguage."Toexplain whatamachinelanguageis,let'sreturntothemarionette. Imaginethatthereisnowoodencontrolandthatthepuppeteerhastodealdirectlywiththestrings.Each stringcorrespondstoexactlyonepartofthemarionette'sbody.Theharmoniouscombinationsof movementsoftheindividualstringscouldbecalledthemarionette's"machinelanguage." Nowtiethestringstoacontrol.Thecontrolislikeahighlevellanguage.Withasimpleturnofthewrist, thepuppeteercanmovemanystringssimultaneously. Soitiswithahighlevelcomputerlanguage,where thesimpleandfamiliarsymbol"+"causesmany internalfunctionstobeperformedintheprocessof addition. Here'sacleverthingaboutacomputer:itcanbe programmedtotranslatehighlevelsymbols(suchas "+")intothecomputer'sownmachinelanguage.Then itcanproceedtocarryoutthemachineinstructions. Ahighlevellanguageisacomputerprogramthat translateshumanlyunderstandablewordsand symbolsintothemachinelanguageoftheparticularmakeandmodelofcomputer. What'sthedifferencebetweenForthandotherhighlevellanguages?Toputitverybriefly:ithastodo withthecompromisebetweenmanandcomputer.Alanguageshouldbedesignedfortheconvenienceof itshumanusers,butatthesametimeforcompatibilitywiththeoperationofthecomputer. Forthisuniqueamonglanguagesbecauseitssolutiontothisproblemisunique.Thisbookwillexplain how.
IntroductionforProfessionals:ForthintheRealWorld
Forthenjoyedarisingtideofpopularityuptoaround1994,(ANSandISOForthstandards),perhaps mostvisiblyamongenthusiastsandhobbyists.After1996orsoForth'spopularityhasstayedrelatively constant.ButthisdevelopmentisonlyanewwrinkleinthehistoryofForth.Forthhasbeeninusefrom 1972on,incriticalscientificandindustrialapplications.Infact,ifyouuseaminiormicrocomputer professionally,chancesarethatForthcanrunyourapplicationmoreefficientlythanthelanguageyou're
Asalanguage,Forthbeginswithapowerfulsetofstandardcommands,thenprovidesthemechanicsby whichyoucandefineyourowncommands.Thestructuralprocessofbuildingdefinitionsuponprevious definitionsisForth'sequivalentofhighlevelcoding.Alternatively,wordsmaybedefineddirectlyin assemblermnemonics,usingForth'sassembler.Allcommandsareinterpretedbythesameinterpreterand compiledbythesamecompiler,givingthelanguageextremeflexibility. ThehighestlevelofyourcodewillresembleanEnglishlanguagedescriptionofyourapplication.Forth hasbeencalleda"metaapplicationlanguage"alanguagethatyoucanusetocreateproblemoriented languages. Asanoperatingsystem,Forthdoeseverythingthattraditionaloperatingsystemsdo,including interpretation,compilation,assembling,virtualmemoryhandling,I/O,textediting,etc. ButbecausetheForthoperatingsystemismuchsimplerthanitstraditionalcounterpartsduetoForth's design,itrunsmuchmorequickly,muchmoreconveniently,andinmuchlessmemory. WhatisForthgoodfor?Forthoffersasimplemeanstomaximizeaprocessor'sefficiency.Forexample: Forthisfast.HighlevelForthexecutesasfastasotherhighlevellanguagesandbetween20to75% slowerthanequivalentassemblylanguageprograms,whiletimecriticalcodemaybewritteninassembler torunatfullprocessorspeed.Withoutatraditionaloperatingsystem,Fortheliminatesredundancyand needlessruntimeerrorchecking. Forthcompiledcodeiscompact.Forthapplicationsrequirelessmemorythantheirequivalentassembly languageprogramsandconsumelesspower(importantforhandheldsandportablegadgets!)Writtenin Forth,theentireoperatingsystemanditsstandardwordsetresideinlessthan8Kbytes.Supportfora targetapplicationmayrequirelessthan1Kbytes. Forthistransportable.Ithasbeenimplementedonjustabouteveryminiandmicrocomputerknownto theindustry.MostmicrocontrollersandDSPs,eventinyones,alsohaveaForthimplementation. Forthhasbeenknowntocutprogramdevelopmenttimebyafactoroftenforequivalentassembly languageprogrammingandbyafactoroftwoforequivalenthighlevelprogramminginCorJava. ProductivityincreasesbecauseForthepitomizes"structuredprogramming"andbecauseitisinteractive andmodular.
HereareafewsamplesofForthintherealworld(FORTH,Inc.,MPE):
AVCO/TextronSystems,buildingautomationandauxiliaryservicesforKingKhaledInternational Airport(SaudiArabia).SystemcontainsninePDP11/44s,3788086basedcomputers,and320 8085basedsecurityprocessors,collectivelymonitoringandcontrollingover36,000points. EastmanKodakCompany,qualitycontrolsystemmonitoringphotographicfilmdensity.Includes filmmotioncontrol,automaticrecognitionoffilm densitysteps,andcustomIEEE488businterface. FederalExpress,handheldSuperT racker,carriedby everyFedExdeliveryagent.Containsbarcodereader, keyboard,2x20linedisplay.Performsextensive packageentryandtrackingfunctions,includingcross indexfromairportcodetoall10,000USzipcodes. Includessmartpoweroffsequencingtoextendbattery life. NASAGoddardSpaceFlightCenter: 1. Controlof50footlong,sixjointarmforSpaceShuttlesimulator.Extensivemathroutines converttwothreeaxisjoystickcommandsintorequiredjointvelocitiesinsixdifferentco ordinatesystems. Multitaskingoperatingsystem,Forthlanguagecompiler, andlibrariesforUT69R000radiationhardened microprocessorusedinSpaceShuttleinstrumentation. DevelopmentoftheForthbasedSmallPayload AccomodationsInterfaceModule(SPAIM),which interfacestheShuttleSolarBackscatterUltraviolet (SSBUV)instrumenttotheSpaceShuttle'savionic systems.TheSSBUVinstrumentisusedtocalibrate ozonemeasuringinstrumentsaboardNOAAsatellites. OwensCorningFiberglas,OwensCorninghasused Forthformanyyearsasthebasicfirmwareinits distributedindustrialcontrollers.Thesecontrollers performawidevarietyoffunctions,managingwinders, weighingdevices,etc.,usedinthemanufactureof variousfiberglassproducts.PlantsinKoreaandMexico alsouseFORTH,Inc.'sEXPRESStoprovide supervisorycontrolandreportingfunctions. SaturnCorp,distributedHVACsystemforentireSaturnautomobileassemblyplant,controlling overtwohundred40hp.heatingcoolinghumidifyingunits(withZ80s)overatwotier networkusingPCsastextandgraphicalsystemmonitors.Outsideairsensorsprovideinputsfor intelligentsetpointcontrolandeconomicuseofgasheatingandchilledwatercoolingsystems. SacramentoMunicipalUtilitiesDistrict(California): photovoltaicarraysinthestatecapitolfeatureEXPRESSto provideuserconfigurablelivetrending,historicaltrending, alarm/exceptionreporting,ruletracking,I/Osystemexerciser, classbasedrealtimedatabase,graphicalprocessdisplays, simultaneousmultiplevendorI/Osystemscanning,I/Oand processsimulationfordevelopment,andmultipleremote
terminalaccesswithfullgraphics.CustomdriversfortheDigitronicsSixnet(TM)I/Osystemwere providedinoneweek;EXPRESSalreadysupportsModicon,AllenBradle y,OPTO22 OPTOMUXandPAMUX,plusothers. UniversityofMinnesota,PCbasedsystemfortelescopecontrolanddatataking(overIEEE488 bus),dataanalysisandgraphicsdisplay.Includesprovisionforremoteobserving,usingacustom protocoltomultiplexpacketsfromthreeindependentdatastreamsoverasingletelephoneline. VertexRSI(Div.ofTripointGlobal),softwareforcustomsatellitetrackingreceivers.Includes frequencysynthesizercontrol,remoteRS232commandport,vacuum fluorescentgraphicsdisplay. Amobilephonemanufacturerisintroducinganewgamesenginederived fromtheSENDITproject.ThisusesaForthbasedvirtualmachineto reducethesizeofgamesinthephone,andtopermitmorefunctionalityto beprovidedinthephonewithoutincreasingmemorysize. ArecentconsultancyprojectbasedonMPE8051andARMhardware, willintroduceanewrangeofvendingmachinestothemarket. ConstructionComputerSoftware(CCS)inCapeTownproducethe MARSandCANDYapplicationswhichareastandardallovertheworld. TheCCSsoftwareisanexampleofalargescaleWindowsapplication writteninProForthforWindows,andtheVFXForthversionalready consistsofover850,000linesofcode.CCSsoftwarewasusedtoplanthe newChaiTakairportinHongKong.TheCCSwebsiteishere. BarefootAuditorisusedbyMicrosoftforcollectinginformationabout theirownPCs,andwaswrittenusingoneofMPE'sForthsystems. BarefootAuditorhasbeenavailableonseveralmagazinecoverdisks recently,andmoreinformationisavailablefromPathfinder.
1FundamentalForth
Inthischapterwe'llacquaintyouwithsomeoftheuniquepropertiesoftheForthlanguage.Afterafew introductorypageswe'llhaveyousittingataForthterminal.
ALivingLanguage
Imaginethatyou'reanofficemanagerandyou'vejusthiredanew,eagerassistant.Onthefirstday,you teachtheassistanttheproperformatfortypingcorrespondence.(Theassistantalreadyknowshowto type.)Bytheendoftheday,allyouhavetodoissay"Pleasetypethis." Onthesecondday,youexplainthefilingsystem.Ittakesallmorningtoexplainwhereeverythinggoes, butbytheafternoonallyouhavetosayis"Pleasefilethis." Bytheendoftheweek,youcancommunicateinakindofshorthand,where"Pleasesendthisletter" means"Typeit,getmetosignit,photocopyit,filethecopy,andmailtheoriginal."Bothyouandyour assistantarefreetocarryoutyourbusinessmorepleasantlyandefficiently. Goodorganizationandeffectivecommunicationrequirethatyou 1. defineusefultasksandgiveeachtaskaname,then 2. grouprelatedtaskstogetherinlargertasksandgiveeachoftheseaname,andsoon. Forthletsyouorganizeyourownproceduresandcommunicatethemtoacomputerinjustthisway (exceptyoudon'thavetosay"please"). Asanexample,imagineamicroprocessorcontrolledwashingmachineprogrammedinForth.The ultimatecommandinyourexampleisnamedWASHER.HereisthedefinitionofWASHER,aswrittenin Forth:
:WASHERWASHSPINRINSESPIN;
AllThisand...Interactive!
OneofForth'smanyuniquefeaturesisthatitletsyou"execute" awordbysimplynamingthe word.Ifyou'reworkingataterminalkeyboard,thiscanbeassimpleastypinginthewordandpressing theRETURNkey. Ofcourse,youcanalsousethesamewordinthedefinitionofanyotherword,simplybyputtingitsname inthedefinition. Forthiscalledan"interactive"languagebecauseitcarriesoutyourcommandstheinstantthatyouenter them. We'regoingtogiveanexamplethatyoucantryyourself,showingtheprocessofcombiningsimple commandsintomorepowerfulcommands.We'llusesomesimpleForthwordsthatcontrolyourterminal screen.Butfirst,let'sgetacquaintedwiththemechanicsof"talking"toForththroughyourterminal's keyboard. TakeaseatatyourrealorimaginaryForthterminal.We'll assumethatsomeonehasbeenkindenoughtoset everythingupforyou,orthatyouhavefollowedalltheinstructions givenforloadingForthonyourparticularcomputer. Nowpressthekeylabeled:
RETURN
Thecomputerwillrespondbysaying
ok
overandover,let'sdefineitasawordcalled"STAR." Enterthis:
:STAR42EMIT; ok
HereSTARisthename;"42EMIT"isthedefinition.Noticethatwesetoffthecolonandsemicolon
Asyoucansee,Forthexecutedacarriagereturn,thenprintedok(onthenextline). Nowtrythis:
CRSTARCRSTARCRSTAR * * *ok
Let'sputaCRinadefinition,likethis:
:MARGINCR30SPACES; ok
Nowwecanenter
MARGINSTARMARGINSTARMARGINSTAR
andgetthreestarslinedupvertically,thirtyspacesinfromtheleft. OurMARGINSTARcombinationwillbeusefulforwhatweintendtodo,solet'sdefine
:BLIPMARGINSTAR; ok
Wewillalsoneedtoprintaahorizontalrowofstars.Solet'senterthefollowingdefinition(we'llexplain howitworksinalaterchapter):
:STARS0DOSTARLOOP; ok
Nowwecansay
5STARS *****ok
or
35STARS ***********************************ok
oranynumberofstarsimaginable. WewillneedawordwhichperformsMARGIN,thenprintsfivestars.Let'sdefineitlikethis:
:BARMARGIN5STARS; ok
Nowwecanenter
BARBLIPBARBLIPBLIPCR
andgetaletter"F"(forForth)madeupofstars.Itshouldlooklikethis:
***** * ***** * *
Thefinalstepistomakethisnewprocedureaword.Let'scalltheword"F":
:FBARBLIPBARBLIPBLIPCR; ok
TheDictionary
EachwordanditsdefinitionareenteredintoForth's"dictionary."Thedictionaryalreadycontainedmany wordswhenyoustarted,butyourownwordsarenowinthedictionaryaswell. Whenyoudefineanewword,Forthtranslatesyourdefinitionintodictionaryformandwrites
theentryinthedictionary.Thisprocessiscalled"compiling." Forexample,whenyouentertheline
:STAR[CHAR]*EMIT; ok
ThiswillactivateawordcalledINTERPRET,alsoknownasthe"textinterpreter."Thetextinterpreter scanstheinputstream,lookingforstringsofcharactersseparatedbyspaces.Whenastringis found,itislookedupinthedictionary.Ifthewordisinthedictionary,itispointedouttoa wordcalledEXECUTE.EXECUTEexecutesthedefinition(inthiscaseanasteriskisprinted).Finally, theinterpretersayseverything's"ok." Iftheinterpretercannotfindthestringinthedictionary,hecallsthenumberrunner(called NUMBER).NUMBERknowsanumberwhenheseesone.IfNUMBERfindsanumber,he runsitofftoatemporarystoragelocationfornumbers. Whathappenswhenyoutrytoexecuteawordthatisnotinthedictionary?Enterthisandseewhat happens:
XLERB XLERB?
SayWhat?
InForth,awordisacharacterorgroupofcharactersthathaveadefinition.Almostanycharactercanbe usedinnamingaword.Thereasonsthatsomeofthecontrolcharacterscannotbeusedare: becausethecomputerthinksyou'vefinishedentering. becausethecomputerthinksyouaretryingtocorrecta backspace typingerror. space becausethecomputerthinksit'stheendoftheword. HereisaForthwordwhosenameconsistsoftwopunctuationmarks.Thewordis."anditispronounced dotquote.Youcanuse."insideadefinitiontotypeastringoftextatyourterminal.Here'sanexample:
:GREET."Hello,IspeakForth"; ok
return
TheStack:Forth'sWorksiteforArithmetic
Acomputerwouldnotbemuchgoodifitcouldn'tdoarithmetic.Ifyouneverstudiedcomputersbefore,it mayseemprettyamazingthatacomputer(orevenapocketcalculator)candoarithmeticatall.Wecan't citeallthemechanicsinthisbook,butbelieveus,it'snotamiracle. Ingeneral,computersperformtheiroperationsbybreakingeverythingtheydointoridiculouslytiny piecesofinformationandridiculouslyeasythingstodo.Toyouandme,"3+4"isjust"7,"withouteven thinking.Toacomputer,"3+4"isactuallyaverylonglistofthingstodoandremember. Withoutgettingtoospecific,let'ssayyouhaveapocketcalculatorwhichexpectsitsbuttonstobepushed inthisorder:
inordertoperformtheadditionanddisplaytheresult.Here'sageneralizedpictureofwhatmightoccur: Whenyoupress
thenumber3goesintooneplace(calledBoxA).
theintendedoperation(addition)isrememberedsomehow.
thenumber4isstoredintoasecondplace(calledBoxB).
Thenextword,.,isalsofoundinthedictionary.Ithasbeenpreviouslydefinedtotakethenumberoffthe stackandprintitattheterminal.
PostfixPower
Nowwait,yousay.WhydoesForthwantyoutotype
34+
insteadof
3+4
Wecoulddefineitthisway:
:FOURMORE4+;
andtestitthisway:
3FOURMORE. 7ok
andagain:
10FOURMORE. 6ok
KeepTrackofYourStack
We'vejustbeguntodemonstratethephilosophybehindthestackandpostfixnotation.Beforewe continue,however,let'slookmorecloselyatthestackinactionandgetaccustomedtoitspeculiarities. Forth'sstackisdescribedasa"lastin,firstout"(LIFO).Youcanseefromtheearlierillustrationwhythis isso.Thethreewaspushedontothestackfirst,thenthefourpushedontopofit.Latertheadding machinetookthefourofffirstbecauseitwasontop.Hence"lastin,firstout." Ingeneral,theonlyaccessiblevalueatanygiventimeisthetopvalue.Let'suseanotheroperation,the.to furtherdemonstrate.Rememberthateach.removesonenumberfromthestackandprintsit.Fourdots, therefore,removefournumbersandprintthem.
2468.... 8642ok
Thesystemreadsinputfromlefttorightandexecuteseachwordinturn.
Forinput,therightmostvalueonthescreenwillenduponthetopofthestack. Foroutput,therightmostvalueonthescreencamefromthebottomofthestack.
Let'sseewhatkindoftroublewecangetourselvesinto.Type:
102030....
(that'sfourdots)thenRETURN.Whatyougetis:
102030.... 3020100Stackempty
Eachdotremovesonevalue.Thefourthdotfoundthattherewasnovalueleftonthestacktosendtothe terminal,andittoldyouso. Thiserroriscalled"stackunderflow."(Noticethatastackunderflowisnot"ok.") Theoppositecondition,whenthestackcompletelyfillsup,iscalled"stackoverflow."The stackissodeep,however,thatthisconditionshouldneveroccurexceptwhenyou'vedonesomething terriblywrong. It'simportanttokeeptrackofnewwords'"stackeffects";thatis,thesortofnumbersawordneedstohave onthestackbeforeyouexecuteit,andthesortofnumbersitwillleaveonthestackafterwards. Ifyoumaintainalistofyournewlycreatedwordswiththeirmeaningsasyougo,youoranyoneelsecan easilyunderstandtheword'soperations.InForth,suchalistiscalleda"glossary." Tocommunicatestackeffectsinavisualway,Forthprogrammersconventionallyuseaspecialstack notationintheirglossariesortablesofwords.We'reintroducingthestacknotationnowsothatyou'llhave itunderyourbeltwhenyoubeginthenextchapter. Hereisthebasicform:
(beforeafter)
Thedashseparatesthethingsthatshouldbeonthestack(beforeyouexecutetheword)fromthethings thatwillbeleftthereafterwards.Forexample,here'sthestacknotationfortheword.:
.(n)
wefeelit'sneededforclarity.Youcanusuallytellwheretopress"return"becausethecomputer's responseisalwaysunderlined. Here'salistoftheForthwordsyou'velearnedsofar,includingtheirstacknotations("n"standsfor number;"c"standsforcharacter): :xxxxyyy; () CR SPACES () (n) Createsanewdefinitionwiththenamexxx,consistingofwordor wordsyyy. Performsacarriagereturnandlinefeedatyourterminal. Printsthegivennumberofblankspacesatyourterminal.
Printsoneblankspaceatyourterminal. Transmitsacharactertotheoutputdevice. Printsthecharacterstringxxxatyourterminal.The"character ."xxx" () terminatesthestring. + (n1n2sum) Adds. . (n) Printsanumber,followedbyonespace. Inthenextchapterwe'lltalkaboutgettingthecomputertoperformsomefancierarithmetic. SPACE EMIT ReviewofTerms togenerateadictionaryentryincomputermemoryfromsourcetext(thewrittenoutform ofadefinition).Distinctfrom"execute." inForth,alistofwordsanddefinitionsincludingboth"system"definitions(predefined) Dictionary and"user"definitions(whichyouinvent).Adictionaryresidesincomputermemoryin compiledform. toperform.Specifically,toexecuteawordistoperformtheoperationsspecifiedinthe Execute compileddefinitionoftheword. acharacteristicofacomputerlanguagewhichallowsaprogrammertoaddnewfeatures Extensibility ormodifyexistingones. alistofwordsdefinedinForth,showingtheirstackeffectsandanexplanationofwhat Glossary theydo,whichservesasareferenceforprogrammers. Infixnotation themethodofwritingoperatorsbetweentheoperandstheyaffect,asin"2+5." thetexttobereadbythetextinterpreter.Thismaybetextthatyouhavejusttypedinat Inputstream yourterminal,oritmaybetextthatisstoredondisk. (whenreferringtoForth'stextinterpreter)toreadtheinputstream,thentofindeachword Interpret inthedictionaryor,failingthat,toconvertittoanumber. (lastin,firstout)thetypeofstackwhichForthuses.AcanoftennisballsisaLIFO LIFO structure;thelastballyoudropinistheoneyoumustremovefirst. Postfix themethodofwritingoperatorsaftertheoperandstheyaffect,asin"25+"for"2+5." notation AlsoknownasReversePolishNotation. inForth,aregionofmemorywhichiscontrolledinsuchawaythatdatacanbestoredor Stack removedinalastin,firstout(LIFO)fashion. theerrorconditionthatoccurswhentheentireareaofmemoryallowedforthestackis Stackoverflow completelyfilledwithdata. Stack theerrorconditionthatoccurswhenanoperationexpectsavalueonthestack,butthere underflow isnovaliddataonthestack. Word inForth,thenameofadefinition. Compile
() (c)
ProblemsChapter1
Note:beforeyouworktheseproblems,rememberthesesimplerules:
Every:needsa;.
and
Every."needsa".
1. DefineawordcalledGIFTwhich,whenexecuted,willtypeoutthenameofsomegift.For example,youmighttry:
:GIFT."Bookends";
2HowToGetResults
Inthischapter,we'lldiverightintosomespecificsthatyouneedtoknowbeforewegoon.Specifically, we'llintroducesomeofthearithmeticinstructionsbesides+andsomespecialoperatorsforrearranging theorderofnumbersonthestack,sothatyou'llbeabletowritemathematicalequationsinForth.
ForthArithmeticCalculatorStyle
HerearethefoursimplestintegerarithmeticoperatorsinForth: + (n1n2sum) Adds. (n1n2diff) Subtracts(n1n2). * (n1n2prod) Multiplies. / (n1n2quot) Divides(n1/n2). Unlikecalculators,computerterminalsdon'thavespecialkeysformultiplicationordivision.Insteadwe
use*and/.
Inthefirstchapter,welearnedthatwecanaddtwonumbersbyputtingthembothonthestack,then executingtheword+,thenfinallyexecutingtheword.(dot)togettheresultprintedatourterminal.
175+.22ok
WecanusethismethodwithallofForth'sarithmeticoperators.Inotherwords,wecanuseForthlikea calculatortogetanswers,evenwithoutwritinga"program."Tryamultiplicationproblem:
78*.56ok
Bynowwe'veseenthattheoperatorcomesafterthenumbers.Inthecaseofsubtractionanddivision, though,wemustalsoconsidertheorderofnumbers("74"isnotthesameas"47"). Justrememberthisrule: Toconverttopostfix,simplymovetheoperatortotheendoftheexpression: Infix Postfix 3+4 34+ 500300 500300 6x5 65* 20/4 204/ Sotodothesubtractionproblem:
74=
simplytypein
74.3ok
italsomeansthatyoucanonlygetintegerresults,asin
214/.5okinsteadof5.25ok
Anotherthingisthatifyoutrytomultiply:
100000001000*.
Theorderofnumbersstaysthesame.Let'stryadivisionproblem:
204/.5ok
Theword/isdefinedtodividethesecondnumberonthestackbythetopnumber. Whatdoyoudoifyouhavemorethanoneoperatorinanexpression,like:
4+(17*12)
youask?Let'stakeitstepbystep:theparenthesestellyoutofirstmultiplyseventeenbytwelve,thenadd four.SoinForthyouwouldwrite:
1712*4+.208ok
Nowhere'saninterestingproblem:
(3+9)*(4+6)
Tosolveitwehavetoaddthreetoninefirst,thenaddfourtosix,thenfinallymultiplythetwosums.In Forth,wecanwrite
39+46+*.120ok
Thepictureattherightisshowingwhathappens.
PostfixPracticeProblems(Quizzie2a)
Convertthefollowinginfixequationstopostfix"calculatorstyle."Forexample,
ab+c
wouldbecome
ab*c+
1. 2. 3. 4. 5.
ForthArithmeticDefinitionStyle
InChap.1wesawthatwecoulddefinenewwordsintermsofnumbersandotherpredefined words.Let'sexploresomefurtherpossibilities,usingsomeofournewlylearnedmath operators. Let'ssaywewanttoconvertvariousmeasurementstoinches.Weknowthat
1yard=36inches
and
1foot=12inches
sowecandefinethesetwowords:
:YARDS>IN36*;ok :FT>IN12*;ok
wherethenamessymbolize"yardstoinches"and"feettoinches."Here'swhattheydo:
10YARDS>IN.360ok
2FT>IN.24ok
Ifwealwayswantourresulttobeininches,wecandefine:
:YARDS36*;ok :FEET12*;ok :INCHES;ok
Sothatwecanusethephrase
10YARDS2FEET+9INCHES+.393ok
NoticethatthewordINCHESdoesn'tdoanythingexceptremindthehumanuserwhatthenineisfor.If wereallywanttogetfancy,wecanaddthesethreedefinitions:
:YARDYARDS;ok :FOOTFEET;ok :INCH;ok
sothattheusercanenterthesingularformofanyofthenounsandstillgetthesameresult:
1YARD2FEET+1INCH+.61ok 2YARDS1FOOT+.84ok
Sofarwehaveonlydefinedwordswhosedefinitions ForSemanticFreaks containasinglemathoperator.Butit'sperfectly possibletoputmanyoperatorsinsideadefinition,if Inmathematics,theword"argument"refersto anindependentvariableofafunction.Computer that'swhatyouneedtodo. linguistshaveborrowedthistermtoreferto Let'ssaywewantawordthatcomputesthesumof numbersbeingoperatedonbyoperators.They fivenumbersonthestack.Afewpagesbackwe havealsoborrowedtheword"parameters"to summedfivenumberslikethis: describeprettymuchthesamething.
1720+132+3+9+.181ok
Butwecanalsoenter
172013239++++.181ok
Wegetthesameanswer,eventhoughwe'veclusteredallthenumbersintoonegroupandalltheoperators intoanothergroup.Wecanwriteourdefinitionlikethis:
:5#SUM++++;ok
andexecuteitlikethis:
1720132395#SUM.181ok
Ifweweregoingtokeep5#SUMforfutureuse,wecouldenteritintoourevergrowingglossary,along withanotethatit"expectsfivearguments"onthestack,whichitwilladdtogether.
Hereisanotherequationtowriteadefinition for:
(a+b)*c
ForBeginnersWhoLikeWordProblems
Ifajet plane AswesawinQuizzie2a,thisexpressioncan fliesat an bewritteninpostfixas average cab+* air speed Thuswecouldwriteourdefinition of600 :SOLUTION+*;ok mph andifitflieswithatailwindof25mph,howfarwillit aslongaswemakesurethatweenterthe travelinfivehours? argumentsintheproperorder; Ifwedefine
cabSOLUTION :FLIGHTDISTANCE+*;
DefinitionStylePractice Problems(Quizzie2b)
wecouldenter
560025FLIGHTDISTANCE.3125ok
Convertthefollowinginfixexpressionsinto Tryitwithdifferentvalues,includingheadwinds Forthdefinitionsandshowthestackorder (negativevalues). requiredbyyourdefinitions.Sincethisis Quizzie2b,youcannameyourdefinitions2B1,2B2,etc. 1. ab+cwouldbecome:2B1*+; whichexpectsthisstackorder:(cbaresult) 2. (a4b)/6+c 3. a/(8b) 4. 0.5ab/100 5. a(2a+3) 6. (ab)/c [answer]
TheDivisionOperators
Theword/isForth'ssimplestdivisionoperator.Slashsuppliesonlythequotient;anyremainderislost.If youtype:
224/.5ok
Sothatyoucantype:
22QUARTERS
withthisresult:
22QUARTERS5onesand2quartersok
StackManeuvers
IfyouworkedProb.6inthelastset,youdiscoveredthattheinfixequation
(ab)/c
cannotbesolvedwithadefinitionunlessthereissomewaytorearrangevaluesonthestack. Well,thereisaway:byusinga"stackmanipulationoperator"calledSWAP.
SWAP
ThewordSWAPisdefinedtoswitchtheorderofthetoptwostackitems.
thenagain,thistimewithSWAP:
12SWAP..12ok
ThusProb.6canbesolvedwiththisphrase:
SWAP/
with(cab)onthestack. Let'sgivea,b,andcthesetestvalues:
a=10b=4c=2
thenputthemonthestackandexecutethephrase,likeso:
2104SWAP/.3ok
Hereisalistofseveralstackmanipulationoperators,includingSWAP SWAP (n1n2n2n1) DUP (nnn) OVER (n1n2n1n2n1) ROT (n1n2n3n2n3n1) DROP (n) Reversesthetoptwostackitems. Duplicatesthetopstackitem. Makesacopyoftheseconditemandpushesitontop. Rotatesthethirditemtothetop. Discardsthetopstackitem.
DUP
Thenextstackmanipulationoperatoronthelist,DUP,simplymakesasecondcopy (duplicate)ofthetopstackitem. Forexample,ifwehave"a"onthestack,wecancompute:
a2
asfollows:
DUP*
DUP *
a aa a2
OVER
Nowsomebodytellsyoutoevaluatetheexpression:
a*(a+b)
giventhefollowingstackorder:
(ab)
Nowtheexpression
a*(a+b)
caneasilybewritten
OVER+*
WhenwritingequationsinForth,it'sbestto"factorthemout"first.Forexample,ifsomebodyasksyouto evaluate:
a2+ab
inForth,you'llfinditquitecomplicated(andmaybeevenimpossible)usingthewordswe'veintroduced sofar...unlessyoufactorouttheexpressiontoread:
a*(a+b)
whichistheexpressionwejustevaluatedsoeasily.
ROT
ThefourthstackmanipulatoronthelistisROT(pronouncedrote),whichisshortfor"rotate."ROT transformsthetopthreestackvaluesfrom(abc)to(bca). Forexample,ifweneedtoevaluatetheexpression:
abbc
weshouldfirstfactoroutthe"b"s:
b*(ac)
Nowifourstartingstackorderisthis:
(cba)
wecanuse:
ROT*
DROP
ThefinalstackmanipulationoperatoronthelistisDROP.Allitdoesisdiscardthetopstackvalue. Prettysimple,huh?We'llseesomegoodusesforDROPlateron. AHandyHint ANondestructiveStackPrint Beginnerswhoarejustlearningtomanipulatenumbersonthestackinusefulwaysveryoftenfind themselvestypingaseriesofdotstoseewhat'sonthestackaftertheirmanipulations.Theproblem withdots,though,isthattheydon'tleavethenumbersonthestackforfuturemanipulation. TheForthword.Sprintsoutallthevaluesthathappentobeonthestack"nondestructively";thatis, withoutremovingthem.Let'stestit,firstwithnothingonthestack:
.S<0>ok
Asyoucansee,inthisversionof.S,weseeatleastonenumber.Thisisthenumberofitemsactually
onthestack. Nowlet'strywithnumbersonthestack:
123.S<3>123ok ROT.S<3>231ok
StackManipulationandMathDefinitions(Quizzie2c)
1. Writeaphrasewhichflipsthreeitemsonthestack,leavingthemiddlenumberinthemiddle;that is,
abcbecomescba
PlayingDoubles
Thenextfourstackmanipulationoperatorsshouldlookvaguelyfamiliar: 2SWAP (d1d2d2d1) 2DUP (ddd) Reversesthetoptwopairsofnumbers. Duplicatesthetoppairofnumbers.
Onemorething:therearestillsomestackmanipulatorswehaven'ttalkedaboutyet,sodon'tgocrazyby tryingtoomuchfancyfootworkonthestack. Here'salistoftheForthwordswe'vecoveredinthischapter: + * / /MOD MOD SWAP DUP OVER ROT DROP 2SWAP 2DUP 2OVER 2DROP (n1n2sum) (n1n2diff) (n1n2prod) (n1n2quot) (n1n2remquot) (n1n2rem) (n1n2n2n1) (nnn) (n1n2n1n2n1) (n1n2n3n2n3n1) (n) (d1d2d2d1) (ddd) (d1d2d1d2d1) (d) Adds. Subtracts(n1n2). Multiplies. Divides(n1/n2). Divides.Returnstheremainderandquotient. Returnstheremainderfromdivision. Reversesthetoptwostackitems. Duplicatesthetopstackitem. Makesacopyoftheseconditemandpushesitontop. Rotatesthethirditemtothetop. Discardsthetopstackitem. Reversesthetoptwopairsofnumbers. Duplicatesthetoppairofnumbers. Makesacopyofthesecondpairofnumbersandpushesitontop. Discardsthetoppairofnumbers. ReviewofTerms
ProblemsChapter2
1. What'sthedifferencebetweenDUPDUPand2DUP?[answer] 2. Writeaphrasewhichwillreversetheorderofthetopfouritemsonthestack;thatis,
(12344321)
[answer]
oranyseriesofcrimebeginningwiththewordCONVICTEDOFandendingwithWILL SERVE.Usethesesentences
HOMICIDE20years ARSON10years BOOKMAKING2years TAXEVASION5years
3TheEditor(AndStaff)
Uptillnowyou'vebeencompilingnewdefinitionsintothedictionarybytypingthematyourterminal. Thischapterintroducesanalternatemethod,usingdiskstorage. Let'sbeginwithsomeobservationsthatspecificallyconcernthedictionary.
AnotherLookattheDictionary
Ifyou'vebeenexperimentingwitharealcomputer,youmayhavediscoveredsomethingswehaven't mentionedyet.Inanycase,it'stimetomentionthem. DiscoveryOne:Youcandefinethesamewordmorethanonceindifferent waysonlythemostrecentdefinitionwillbeexecuted. Forexample,ifyouhaveentered:
:GREET."Hello,IspeakForth.";ok
thenyoushouldgetthisresult:
GREETHello,IspeakForth.ok
Andifyouredefine:
:GREET."Hithere!";ok
yougetthemostrecentdefinition:
GREETHithere!ok
and
GREETHello,IspeakForth.ok
(theoldGREETagain!)
HowForthUsestheDisk
AllForthsystemsusediskmemory.EventhoughdiskmemoryisnotabsolutelynecessaryforaForth system,it'sdifficulttoimagineForthwithoutit. Tounderstandwhatdiskmemorydoes,compareitwithcomputermemory(RAM).Thedifferenceis analogoustothedifferencebetweenafilingcabinetandarollingcardindex. Sofaryou'vebeenusingcomputermemory,whichislikethecardindex.Thecomputercanaccessthis memoryalmostinstantaneously,soprogramsthatarestoredinRAMcanrunveryfast.Unfortunately,this kindofmemoryissometimesverylimited(e.g.inembeddedcontrollers)andrelativelyexpensive. Ontheotherhand,thediskiscalleda"bulkmemory"device,because,likeafilingcabinet,itcanstorea lotofinformationatamuchcheaperpriceperunitofinformationthanthememoryinsidethecomputer. Bothkindsofmemorycanbewrittentoandreadfrom. Thecompilercompilesalldictionaryentriesintocomputermemorysothatthedefinitionswillbequickly accessible.Theperfectplacetostoresourcetext,however,isonthedisk,whichiswhatForthdoes.You caneithersendsourcetextdirectlyfromthekeyboardtotheinterpreter(asyouhavebeendoing),oryou cansaveyoursourcetextonthediskandthenlaterreaditoffthediskandsendittothetextinterpreter. Diskmemoryisdividedinto unitscalled"blocks."Eachblock holds1,024charactersofsource textorbinarydata,traditionally organizedas16linesof64 characters.TheANSForth standarddoesnotspecifyhow manyblocksthereare.The documentationofyourForth systemshouldtellyouthis. WithcurrentForths,disk memoryresidesinOSfiles. Therearewaystoattachspecific OSfilestothe"Forthdisk."Due tothespecial16by64formatof Forthblocks,OSutilities considerthemasbinarydataand cannotgenerallyprint,list,filteroreditthem.Forthsystemshavestandardizedfacilitiestohandlesome ofthesetasksbythemselves. AssumingyouareusingiForth,thenthefollowingshouldinstructdiskmemorytocomefromsomefile:
USEblocks.fbok
Tolistablock,simplytypetheblocknumberandthewordLIST,asin:
1LIST 0 1(LargeletterFMHX21:2907/01/89) 2 3:STAR[CHAR]*EMIT; 4:STARS0DOSTARLOOP; 5:MARGINCR30SPACES; 6:BLIPMARGINSTAR; 7:BARMARGIN5STARS; 8:FBARBLIPBARBLIPBLIPCR; 9 10 11 12 13 14 15 ok
youwouldsendblock1totheinputstreamandthenontothetextinterpreter.Thetextinterpreterdoes notcarewherehistextcomesfrom.Recognizingthecolons,hewillhaveallthedefinitionscompiled,and thenwillexecutethenewwordF. (interpret...) Nowfortheunfinishedbusiness:line1.Thewordsinsidetheparentheses areforhumansonly;theyareneithercompilednorexecuted.Theword((leftparenthesis)tellsthetext interpretertoskipallthefollowingtextuptotheterminatingrightparenthesis.Because(isaword,it mustbesetoffwithaspace.Theclosingparenthesisisnotaword,itissimplyacharacterthatislooked forby(,calledadelimiter.(Recallthatthedelimiterfor."istheclosingquotemark.) Tosummarize,thethreeANSForthcommandswe'velearnedsofarthatconcerndiskblocksare: LIST LOAD (n) Listsadiskblock. (n) Loadsadiskblock(compilesorexecutes). Causesthestringxxxtobeignoredbythetext interpreter.Thecharacter)isthedelimiter.
(xxx) ()
BlockbufferBasics
Wehavediscussedblocksmainlybecauseofhistoricalreasons.Blocksarehardlyeverusedforsource textstorageanymore.Thepreferredwaytohandlesourceisinstandardtextfiles,usingtheword INCLUDEtoloadthem:
INCLUDEblocks.forthok
thesystemwillcopyblock1ofthecurrentlyopenfileintooneofthesystembuffers.BLOCKalsoleaves onthestacktheaddressofthebeginningofthebuffer(1024bytes,remember)thatitused.Thecontents ofthisbufferareguaranteedtostayvaliduntilyouexecuteawordfromthesetofprocedureswith "multitaskingimpact,"likeEMITorTYPE.Ifyouatanytimemodifythebuffercontentsandthen executethewordUPDATE,Forthwillremembertofirstwritetheblockbacktodiskwhenitneedsto reusethebuffer.If,forsomereason,youexecuteUPDATEandthendecidethatyoudon'twanttohave theblocksrewrittenafterall,useEMPTYBUFFERStoinvalidatethem.ThisworksbecauseForthdoes notimmediatelywritethediskafteryouuseUPDATE.Toforcewritingoutthebuffersrightnow,usethe wordFLUSH. Here'salistoftheForthwordswe'vecoveredinthischapter: DesignateOStextfilexxxasthe"Forthdisk." Listsadiskblock. Loadsadiskblock(compilesorexecutes). Causesthestringxxxtobeignoredbythetextinterpreter.The (xxx) () character)isthedelimiter. Marksthemostrecentlyreferencedblockasmodified.Theblockwill UPDATE () laterbeautomaticallytransferredtomassstorageifitsbufferis neededtostoreadifferentblockorifFLUSHisexecuted. Marksallblockbuffersasemptywithoutnecessarilyaffectingtheir EMPTYBUFFERS () actualcontents.Updatedblocksarenotwrittentomassstorage. Leavestheaddressofthefirstbyteinblocku.Iftheblockisnot alreadyinmemory,itistransferredfrommassstorageintowhichever BLOCK (uaddr) memorybufferhasbeenleastrecentlyaccessed.Iftheblock occupyingthatbufferhasbeenupdated(i.e.,modified),itisrewritten ontomassstoragebeforeblockuisreadintothebuffer. INCLUDExxx () Loadthetextfilexxx(compilesorexecutes). FORGETxxx () Forgetsalldefinitionsbacktoandincludingxxx. Createsawordxxxwhich,whenexecuted,restoresthedictionaryto MARKERxxx () thestateithadjustpriortothedefinitionofxxx.Inparticular,remove xxxandallsubsequentworddefinitions. USExxx LIST LOAD () (n) (n)
4Decisions,Decisions,...
Inthischapterwe'lllearnhowtoprogramthecomputertomake"decisions."Thisisthemomentwhen youturnyourcomputerintosomethingmorethananordinarycalculator.
TheConditionalPhrase
Let'sseehowtowriteasimpledecisionmakingstatementinForth.Imagineweareprogramminga mechanicaleggcartonpacker.Somesortofmechanicaldevicehascountedtheeggsontheconveyorbelt, andnowwehavethenumberofeggsonthestack.TheForthphrase:
12=IFFILLCARTONTHEN
testswhetherthenumberonthestackisequalto12,andifitis,thewordFILLCARTONisexecuted.If it'snot,executionmovesrightalongtothewordsthatfollowTHEN.
Theword=takestwovaluesofthestackandcomparesthemtoseeiftheyareequal.
Iftheconditionistrue,IFallowstheflowofexecutiontocontinuewiththenextwordinthedefinition.
Ifthetemperatureonthestackisgreaterthan220,thedangermessagewillbeprintedattheterminal. Youcanexecutethisoneyourself,byenteringthedefinition,thentypinginavaluejustbeforetheword.
290?TOOHOTDangerreduceheatok 130?TOOHOTok
willexecutethewordsafterIF,ifthetwonumbersonthestackarenotequal.
TheAlternativePhrase
ForthallowsyoutoprovideanalternativephraseinanIFstatement,withthewordELSE. Thefollowingexampleisadefinitionwhichtestswhetheragivennumberisavaliddayofthemonth:
:?DAY32<IF."Looksgood"ELSE."noway"THEN;
NoticethatwefirsthavetoDUPthedenominatorbecausethephrase
0=IF
NestedIF...THENStatements
It'spossibletoputanIF...THEN(orIF...ELSE...THEN)statementinsideanotherIF...THENstatement. Infact,youcangetascomplicatedasyoulike,solongaseveryIFhasoneTHEN. Considerthefollowingdefinition,whichdeterminesthesizeofcommercialeggs(extralarge,large,etc.) giventheirweightinouncesperdozen:
OnceEGGSIZEhasbeenentered,herearesomeresultsyou'dget:
23EGGSIZEmediumok 29EGGSIZEextralargeok 40EGGSIZEerrorok
ACloserLookatIF
Howdoesthecomparisonoperator(=,<,>,orwhichever)letIFknowwhetherthecondition istrueorfalse?BysimplyleavingTRUEorFALSEonthestack.ATRUE(allbitshigh) meansthattheconditionistrue;aFALSE(allbitslow)meansthattheconditionisfalse. Incomputerjargon,whenonepieceofprogramleavesavalueasasignalforanotherpieceofprogram, thatvalueiscalleda"flag." Tryenteringthefollowingphrasesattheterminal,letting.showyouwhat'sonthestackasaflag.
54>.1ok 54<.0ok
Nowwe'llletyouinonalittlesecret:IFwilltakeanynonzerovaluetomeantrue. Toproveit,tryenteringthistest:
:TESTIF."non"THEN."zero";
Eventhoughthereisnocomparisonoperatorintheabovedefinition,you'llstillget
0TESTzerook 1TESTnonzerook 400TESTnonzerook
dividesbytenandreturnstheremainderonly.Anevenmultipleoftenwouldproduceazeroremainder, sothephrase
10MOD0=
givestheappropriate"true"or"false"flag. Stillanotherinterestingresultisthatyoucanuse(minus)asacomparisonoperatorwhichtestswhether twovaluesare"notequal."Whenyousubtracttwoequalnumbers,yougetzero(false);whenyousubtract twounequalnumbers,yougetanonzerovalue.However,nowwemusttalkabitabout"wellformed flags." Ifyouthinkaboutit,both0=andINVERTdoalmostthesamething.However,0=changesthenumber0 tothenumber1andanynonzeronumberto0,whileINVERTchangesallzerobitsinanumbertoone bitsandtheonebitsinthatnumbertozerobits.Onlywhenthenumberisa"wellformedflag",i.e., either0or1,theresultof0=andINVERTisthesame.Allcomparisonoperatorsreturnwellformed flags,fitforeither0=orINVERT.However,whenyouusetocomparetwonumbers,aswedidabove, theflagwillnotbewellformedwhenthetwonumbersdifferinvalue,andonly0=canbeusedtosafely reversethemeaningofthecomparison. Afinalresultisdescribedinthenextsection.
ALittleLogic
It'spossibletotakeseveralflagsfromvarioustestsandcombinethemintoasingleflagforoneIF statement.Youmightcombinethemasan"either/or"decision,inwhichyoumaketwocomparisontests. Ifeitherorbothofthetestsaretrue,thenthecomputerwillexecutesomething.Ifneitheristrue,itwon't. Here'sarathersimplemindedexample,justtoshowyouwhatwemean.Sayyouwanttoprintthename "ARTICHOKE"ifaninputnumberiseithernegativeoramultipleoften. HowdoyoudothisinForth?Considerthephrase:
DUP0<SWAP10MOD0=+
Here'swhathappenswhentheinputnumberissay,30: Operator Contentsofstack DUP 0< SWAP 10MOD0= 30 3030 300 030 01
Operation
+ 1 Addtheflags. Addstheflags?Whathappenswhenyouaddflags?Herearefourpossibilities:
algebra.Thustheterm"aBooleanflag"(orevenjust"aBoolean")simplyreferstoaflagthatwillbeusedina logicalstatement.
Howcanwedothis"anddecision"inForth?ByusingthehandywordAND.Here'swhatANDwoulddo withthefourpossiblecombinationsofflagswesawearlier:
Inotherwords,onlythecombination"11AND"producesaresultof"true."Let'ssaywe'relookingfor acardboardboxthat'sbigenoughtofitadiskdrivewhichmeasures:
height6" width19" length22"
Theheight,width,andlengthrequirementsallmustbesatisfiedfortheboxtobebigenough.Ifwehave thedimensionsonthestack,thenwecandefine:
:BOXTEST(lengthwidthheight) 6>ROT22>ROT19>ANDAND IF."Bigenough"THEN;
whichchecksthatthereisavailablelightandthatthereisfilminthecamerabeforetakingthepicture. Anotherexample,whichmightbeusedinacomputerdatingapplication,is:
:MATCH HUMOROUSSENSITIVEAND ART.LOVINGMUSIC.LOVINGORAND SMOKING0=AND IF."Ihavesomeoneyoushouldmeet"THEN;
wherewordslikeHUMOROUSandSENSITIVEhavebeendefinedtocheckarecordinadiskfilethat containsinformationonotherapplicantsoftheappropriatesex.
TwoWordswithBuiltinIF
?DUP Theword?DUPduplicatesthetopstackvalueonlyifitisnonzero.Thiscaneliminateafewsurplus words.Forexample,thedefinition:
:/CHECKDUPIF/ELSEDROPTHEN;
canbeshortenedto
:/CHECK?DUPIF/THEN;
Inthisversion,ifthedenominatoriszero,anynumbersthathappentobeonthestackwillbedropped andtheterminalwillshow:
80/CHECK Error2 zerodenominator?
Justasanexperiment,tryputting/CHECKinsideanotherdefinition:
:ENVELOPE/CHECK."Theansweris".;
andtry
84ENVELOPETheansweris2ok 80ENVELOPE Error2 zerodenominator?
maketheirowntradeoffsbetweenconvenienceandexecutionspeed.
Here'salistoftheForthwordswe'vecoveredinthischapter: IFxxx ELSEyyy THENzzz = < > 0= 0< 0> AND OR ?DUP IF:(f) (n1n2f) (n1n2ndiff) (n1n2f) (n1n2f) (nf) (nf) (nf) (n1n2and) (n1n2or) (nnn)or (00) Iffistrue(nonzero)executesxxx;otherwiseexecutesyyy;continues executionwithzzzregardless.ThephraseELSEyyyisoptional. Returnstrueifn1andn2areequal. Returnstrue(i.e.,thenonzerodifference)ifn1andn2arenotequal. Returnstrueifn1islessthann2. Returnstrueifn1isgreaterthann2. Returnstrueifniszero(i.e.,reversethetruthvalue). Returnstrueifnisnegative. Returnstrueifnispositive. ReturnsthelogicalAND. ReturnsthelogicalOR. Duplicatesonlyifnisnonzero. Iftheflagistrue,typesoutanerrormessage,followedbythetext. Alsoclearsthestacksandreturnscontroltotheterminal.Iffalse, takesnoaction. Returnstrueifastackunderflowconditionhasoccurred. ReviewofTerms
asageneralcomputerterm,toabruptlyceaseexecutionifaconditionoccurswhichthe programisnotdesignedtohandle,inordertoavoidproducingnonsenseorpossibly doingdamage. "And"decisiontwoconditionsthatarecombinedsuchthatifbothofthemaretrue,theresultistrue. breakingthenormallystraightforwardflowofexecution,dependingoncond itionsin Branching effectatthetimeofexecution.Branchingallowsthecomputertoresponddifferentlyto differentconditions. ingeneral,acommandthatcomparesonevaluewithanother(forexample,determines Comparison whetheroneisgreaterthantheother),andsetsaflagaccordingly,whichnormallywill operator becheckedbyaconditionaloperator.InForth,acomparisonoperatorleavestheflagon thestack. asageneralcomputerterm,avaluestoredinmemorywhichservesasasignalasto Flag whethersomeknownconditionistrueorfalse.Oncethe"flagisset,"anynumberof routinesinvariouspartsofaprogrammaycheck(orreset)theflag,asnecessary. incomputerterminology,thesystemofrepresentingconditionsintheformof"logical variables,"whichcanbeeithertrueorfalse,andcombiningthesevariablesusingsuch Logic "logicaloperators"as"and,""or,"and"not,"toformstatementswhichmaybetrueor false. Nesting placingabranchingstructurewithinanouterbranchingstructure. Abort
"Or"decision twoconditionsthatarecombinedsuchthatifeitheroneofthemistrue,theresultistrue.
ProblemsChapter4
1. Whatwillthephrase
0=0=
UsingthewordSTARS,defineanewversionofSTARSthatcorrectsthisproblem.[answer] 6. WritethedefinitionforthewordWITHINwhichexpectsthreearguments:
(nlolimithilimit)
andleavesa"true"flagonlyif"n"iswithintherange
lowlimit<=n<hilimit
itbyexecutingthewordPAGE,whichclearstheterminalscreen).Thenyouaskanotherplayerto enteraguessfollowedbythewordGUESS,asin
100GUESS
andsoon,untiltheplayerguessestheanswer:
391391TRAPYOUGOTIT!ok
Hint:youmayhavetomodifytheargumentstoWITHINsothatTRAPdoesnotsay"BETWEEN" whenonlyoneoftheargumentsisequaltothehiddenvalue.[answer]
5ThePhilosophyofFixedPoint
Inthischapterwe'llintroduceanewbatchofarithmeticoperators.Alongthewaywe'lltackletheproblem ofhandlingdecimalpointsusingonlywholenumberarithmetic.
QuickieOperators
Let'sstartwiththerealeasystuff.Youshouldhavenotroublefiguringoutwhatthewordsinthe followingtabledo. 1+ (nn+1) Addsone.
MiscellaneousMathOperators
Here'satableoffourmiscellaneousmathoperators.Likethequickieoperators,these functionsshouldbeobviousfromtheirnames. ABS NEGATE MIN MAX (n|n|) (nn) (n1n2nmin) (n1n2nmax) Returnstheabsolutevalue. Changesthesign. Returnstheminimum. Returnsthemaximum. AuntMin and UncleMax
Thisgivesthesameresultwhetherweenter
5237DIFFERENCE.15ok 3752DIFFERENCE.15ok
Threedifferentvalueswouldproducetheseresults:
600COMMISSION.50ok
450COMMISSION.45ok 50COMMISSION.5ok
TheReturnStack
Wementionedbeforethattherewerestillsomestackmanipulationoperatorswehadn'tdiscussedyet. Nowit'stime. Uptillnowwe'vebeentalkingabout"thestack"asiftherewereonlyone.Butinfacttherearetwo: the"parameterstack"andthe"returnstack."Theparameterstackisusedmoreoftenby Forthprogrammers,soit'ssimplycalled"thestack"unlessthereiscausefordoubt. Asyou'veseen,theparameterstackholdsparameters(or"arguments")thatarebeingpassedfromwordto word.Thereturnstack,however,holdsanynumberof"pointers"whichtheForthsystemusestomakeits merrywaythroughthemazeofwordsthatareexecutingotherwords.We'llelaboratelateron. You,theuser,canemploythereturnstackasaskindof"extrahand"toholdvaluestemporarilywhileyou performoperationsontheparameterstack. Thereturnstackisalastinfirstoutstructure,justliketheparameterstack,soitcanholdmanyvalues. Buthere'sthecatch:whateveryouputonthereturnstackyoumustremoveagainbeforeyougettothe endofthedefinition(thesemicolon),becauseatthatpointtheForthsystemwillexpecttofindapointer there.Youcannotusethereturnstacktopassparametersfromonewordtoanother. Thefollowingtableliststhewordsassociatedwiththereturnstack.Remember,thestacknotationrefers totheparameterstack. Takesavalueofftheparameterstackandpushesitonto >R (n) thereturnstack. Takesavalueoffthereturnstackandpushesitontothe R> (n) parameterstack. I (n) Copiesthetopofthereturnstackwithoutaffectingit.
thisisthephrasethatwilldoit:
>RSWAPR>
>RSWAPR@
withallfourvaluesonthestackinthefollowingorder:
(abcx)
(remembertofactoroutfirst). Operator parameterstack abcx >R abc SWAPROT cba R@ cbax * cbax + cax+b R>* cx(ax+b) + x(ax+b)+c Goaheadandtryit.Loadthefollowingdefinition:
:QUADRATIC(abcxn) >RSWAPROTR@*+R>*+;
returnstack x x x x x
Nowtestit:
2793QUADRATIC48ok
AnIntroductiontoFloatingPointArithmetic
First,whatdoesfloatingpointmean?Takeapocketcalculator,forexample.Here'swhatthedisplaylooks likeaftereachstep: Youenter: 1.50x 2.23 = Display reads: 1.5 2.23 3.345
12x106
sincetentothesixthpowerequalsonemillion.Inacomputertwelvemillionisstoredastwonumbers:12 and6,whereitisunderstoodthat6isthepoweroftentobemultipliedby12,while3.345couldbestored as3345and3. Theideaoffloatingpointrepresentationisthatthecomputercanrepresentanenormousrangeof numbers,fromatomictoastronomic,withtworelativelysmallnumbers. Whatisfixedpointrepresentation?Itissimplythemethodofstoringnumbersinmemorywithoutstoring thepositionsofeachnumber'sdecimalpoint.Forexample,inworkingwithdollarsandcents,allvalues canbestoredincents.Theprogram,ratherthaneachindividualnumber,canrememberthelocationof thedecimalpoint. Forexample,let'scomparefixedpointandfloatingpointrepresentationsofdollarsandcentsvalues. Realworld Fixedpoint Floatingpoint value: representation: representation: 1.23 123 123(2) 10.98 1098 1098(2) 100.00 10000 1(2) 58.60 5860 586(1) Asyoucansee,withfixedpointallthevaluesmustconformtothesame"scale."Thedecimalpoints mustbeproperly"aligned"(inthiscasetwoplacesinfromtheright)eventhoughtheyarenotactually represented.Withfixedpoint,thecomputertreatsallthenumbersasthoughtheywereintegers.Ifthe programneedstoprintoutananswer,however,itsimplyinsertsthedecimalpointtwoplacesinfromthe rightbeforeitsendsthenumbertotheterminalortotheprinter.
WhyFixedPointisUseful
AForthprogrammerismostinterestedinmaximizingtheefficiencyofthemachine.Thatmeansheor shewantstomaketheprogramrunasfastaspossibleandrequireaslittlecomputermemoryaspossible. Unfortunately,notallprocessorsorcontrollersofferhardwarefloatingpointsupport.Therefore,insome environments,programsthatusefloatingpointfeaturesareredirectedthroughanemulationlibrary. Emulationcodecanbeuptothreetimesslowerthantheequivalentfixedpointcalculation.Ofcourse, thisdifferenceisonlyreallynoticeableinprogramswhichhavetodoalotofcalculationsbeforesending resultstoaterminalortakingsomeaction.Thecatchisthatcodefromanemulationlibraryisalsomany timeslargerthanitsfixedpointcounterpart,whichisquiteuneconomicalforsmallembeddedcontrollers andsuch. Youshouldnotecarefullythatwhenaprocessorsupportshardwarefloatingpoint,itisalmostalways muchfasterandmorecompactthanthefixedpointequivalent.Thespeeddifferencecanbebetween3 and15times. Everythingyoucandowithfloatingpoint,youcandowithfixedpointtoo,aswe'llshowinthe following.Butthereisonethingyoushouldminimizeasmuchaspossible,andthatisswitchingbackand forthbetweenfixedandfloatingpointformats.Formatconversionandadditionalscalingstepscostas muchorevenmoretimethandoingthecalculationsthemselves.
StarslashtheScalar
Here'samathoperatorthatisasusefulasitisunusual:*/. Multiplies,thendivides(n1*n2/n3).Usesa doublelengthintermediateresult. Asitsnameimplies,*/performsmultiplication,thendivision.Forexample,let'ssaythatthestack containsthesethreenumbers: */ (n1n2n3nresult)
(22532100)
StarandSlash: Practise,practise...
sothatbyenteringthenumber225andthenthephrase:
32%
returnsthecorrectanswerbecausetheendresultfallswithintherangeofsinglelengthnumbers. Thepreviousexamplebringsupanotherquestion:howtoroundoff. Let'sassumethatthisistheproblem: If32%ofthestudentseatingattheschoolcafetariausuallybuybananas,howmanybananas shouldbeonhandforacrowdof225?Naturally,weareonlyinterestedinwholebananas,so we'dliketoroundoffanydecimalremainder. Asourdefinitionnowstands,anyvaluetotherightofthedecimalissimplydropped.Inotherwords,the resultis"truncated." 32%of: Result: 225=72.00 72exactlycorrect 226=72.32 72correct,roundeddown(truncated) 227=72.64 72truncated,notrounded Thereisaway,however,withanydecimalvalueof.5orhigher,toroundupwardstothenextwhole banana.WecoulddefinethewordR%,for"roundedpercent,"likethis:
:R%10*/5+10/;
sothatthephrase:
22732R%.
willgiveyou73,whichiscorrectlyroundedup. Noticethatwefirstdivideby10ratherthanby100.Thisgivesusanextradecimalplacetoworkwith,to whichwecanaddfive: Operation */ 5+ 10/ Stack Contents 2273210 726 731 73
Thefinaldivisionbytensetsthevaluetoitsrightfuldecimalposition.Tryitandsee.
SomePerspectiveonScaling
Let'sbackupforaminute.Takethesimpleproblemofcomputingtwothirdsof171.Basically,thereare twowaystogoaboutit. 1. Wecouldcomputethevalueofthefraction2/3bydividing2by3toobtaintherepeatingdecimal
Nowthatwehavealittleperspective,let'stakeaslightlymorecomplicatedexample: Wewanttodistribute$150inproportiontotwovalues:
7,105? 5,145 ? 12,250150
Again,wecouldsolve theproblemthisway:
(7,105/12,250)x150
and
(5,145/12,250)x150
butforgreateraccuracyweshouldsay:
(7,105x150)/12,250
and
(5,145x150)/12,250
whichinForthiswritten:
710515012250*/.87ok
and
514515012250*/.63ok
Itcanbesaidthatthevalues87and63are"scaled"to7105and5145.Calculatingpercentages,aswedid earlier,isalsoaformofscaling.Forthisreason,*/iscalleda"scalingoperator." AnotherscalingoperatorinForthis*/MOD: Multiplies,thendivides (n1*n2/n3).Returnsthe */MOD (n1n2n3nremnresult) remainderandthequotient. Usesadoublelength intermediateresult.
We'llletyoudreamupagoodexamplefor*/MODyourself.
UsingRationalApproximations
Sofarwe'veonlyusedscalingoperationstoworkonrational numbers.Theycanalsobeused onrationalapproximationsofirrationalconstants,suchasorthe.Forexample,therealvalueofis:
3.14159265358979,etc.
buttostaywithintheboundsofsinglelengtharithmetic,wecouldwritethephrase:
3141610000*/
andgetaprettygoodapproximation. Nowwecanwriteadefinitiontocomputetheareaofacircle,givenitsradius.We'lltranslatetheformula:
r2
intoForth.Thevalueoftheradiuswillbeonthestack,soweDUPitandmultiplyitbyitself,thenstar slashtheresult:
:PIDUP*3141610000*/;
Tryitwithacirclewhoseradiusis10inches:
10PI.314ok
Butforevenmoreaccuracy,wemightwonderifthereisapairofintegersbeside3146and10000thatisa closerapproximationto.Surprisingly,thereis.Thefraction:
355113*/
isaccuratetomorethansixplacesbeyondthedecimal,asopposedtolessthanfourplaceswith31416. Ournewandimproveddefinition,then,is:
:PIDUP*355113*/;
2=1.732... e=2.718...
10=3.162...
122=1.059...
26797/25293 2040/11103
log(2)/1.6384=0.183...
ln(2)/16.384=0.042... 485/11464 Here'salistoftheForthwordswe'vecoveredinthischapter: 1+ 1 2+ 2 2* 2/ ABS NEGATE MIN MAX (nn+1) (nn1) (nn+1) (nn2) (nn*2) (nn/2) (n|n|) (nn) (n1n2nmin) (n1n2nmax)
Addsone. Subtractsone. Addstwo. Subtractstwo. Multipliesbytwo(arithmeticleftshift). Dividesbytwo(arithmeticrightshift). Returnstheabsolutevalue. Changesthesign. Returnstheminimum. Returnsthemaximum. Takesavalueofftheparameterstackandpushesitontothe >R (n) returnstack. Takesavalueoffthereturnstackandpushesitontothe R> (n) parameterstack. I (n) Copiesthetopofthereturnstackwithoutaffectingit. R@ (n) Copiesthetopofthereturnstackwithoutaffectingit. J (n) Copiesthethirditemofthereturnstackwithoutaffectingit. Multiplies,thendivides(n1*n2/n3).Usesadoublelength */ (n1n2n3nresult) intermediateresult. Multiplies,thendivides(n1*n2/n3).Returnstheremainderand */MOD (n1n2n3nremnresult) thequotient.Usesadoublelengthintermediateresult. ReviewofTerms adoublelengthvaluewhichiscreatedtemporarilybyatwopartoperator,suchas*/, Doublelength sothatthe"intermediateresult"(theresultofthefirstoperation)isallowedtoexceed intermediateresult therangeofasinglelengthnumber,evenwhentheinitialargumentsandthefinal resultarenot. arithmeticwhichdealswithnumberswhichdonotthemselvesindicatethelocation Fixedpoint ofdecimalpoints.Instead,foranygroupofnumbers,theprogramassumesthe arithmetic locationofthedecimalpointorkeepsthedecimallocationforallsuchnumbersasa separatenumber. arithmeticwhichdealswithnumberswhichthemselvesindicatethelocationoftheir Floatingpoint decimalpoints.Theprogrammustbeabletointerpretthetruevalueofeach arithmetic individualnumberbeforeanyarithmeticcanbeperformed. Parameterstack inForth,theregionofmemorywhichservesascommongroundbetweenvarious operationstopassarguments(numbers,flags,orwhatever)fromoneoperationto
Returnstack
Scaling
ProblemsChapter5
1. TranslatethefollowingalgebraicexpressionintoaForthdefinition:
ab c
given(abc)[answer] 2. Giventhesefournumbersonthestack:
(67012345)
writeanexpressionthatprintsthelargestvalue.[answer] 3. In"calculatorstyle,"convertthefollowingtemperatures,usingtheseformulas:
oC=(oF32)/1.8 oK=oC+273 oF=(oCx1.8)+32
4. 5. 6. 7. 8.
Testthemwiththeabovevalues.[answer]
6ThrowitforaLoop
InChap.4welearnedtoprogramthecomputertomake"decisions"bybranchingtodifferentpartsofa definitiondependingontheoutcomeofcertaintests.Conditionalbranchingisoneofthethingsthatmake computersasusefulastheyare. Inthischapter,we'llseehowtowritedefinitionsinwhichexecutioncanconditionallybranchbacktoan earlierpartofthesamedefinition,sothatsomesegmentwillrepeatagainandagain.Thistypeofcontrol constructiscalleda"loop."Theabilitytoperformloopsisprobablythemostsignificantthingthatmakes computersaspowerfulastheyare.Ifwecanprogramthecomputertomakeoutonepayrollcheck,we canprogramittomakeoutathousandofthem. Fornowwe'llwriteloopsthatdosimplethingslikeprintingnumbersatyourterminal.Inlaterchapters, we'lllearntodomuchmorewiththem.
DefiniteLoopsDO...LOOP
Onetypeofloopstructureiscalleda"definiteloop."You,theprogrammer,specifythenumberoftimes theloopwillloop.InForth,youdothisbyspecifyingabeginningnumberandanendingnumber(in reverseorder)beforethewordDO.Thenyouputthewordswhichyouwanttohaverepeatedbetweenthe wordsDOandLOOP.Forexample
:TEST100DOCR."Hello"LOOP;
willprintacarriagereturnand"Hello"tentimes,becausezerofromtenisten.
TEST Hello Hello Hello Hello Hello Hello Hello Hello Hello Hellook
HereiswhathappensinsideaDO...LOOP:
FirstDO
Iftheindexislessthanthelimit,LOOPreroutesexecutionbacktoDO,andaddsonetotheindex. Eventuallytheindexreachesten,andLOOPletsexecutionmoveontothenextwordinthedefinition.
RememberthattheForthwordIcopiesthetopoftheloopcontrolstackontotheparameterstack.You canuseItogetholdofthecurrentvalueoftheindexeachtimearound.Considerthedefinition
:DECADE100DOI.LOOP;
whichexecuteslikethis:
DECADE0123456789ok
Ofcourse,youcouldpickanyrangeofnumbers(withintherangeof2147483648to+2147483647):
:SAMPLE243250DOI.LOOP; SAMPLE250249248247246245244ok
Noticethatevennegativenumbersincreasebyoneeachtime.Thelimitisalwayshigherthantheindex. YoucanleaveanumberonthestacktoserveasanargumenttosomethinginsideaDOloop.Forinstance,
:MULTIPLICATIONSCR111DODUPI*.LOOPDROP;
willproducethefollowingresults:
7MULTIPLICATIONS 7142128354249566370ok
fortwentyyears. Firstwe'llloadR%,ourpreviouslydefinedwordfromChap.5,thenwe'lldefine
:COMPOUND(amtint) CRSWAP211DO."YEAR"I.3SPACES 2DUPR%+DUP."BALANCE".CR LOOP2DROP;
Eachtimethroughtheloop,wedoa2DUPsothatwealwaysmaintainarunningbalanceandan unchangedinterestrateforthenextgoround.Whenwe'refinallydone,we2DROPthem.
GettingIFfy
TheindexcanalsoserveasaconditionforanIFstatement.Inthiswayyoucanmakesomethingspecial happenoncertainpassesthroughtheloopbutnotonothers.Here'sasimpleexample:
:RECTANGLE2560DOI16MOD0=IFCRTHEN ."*" LOOP;
RECTANGLEwillprint256stars,andateverysixteenthstaritwillalsoperformacarriagereturnatyour terminal.Theresultshouldlooklikethis:
**************** **************** **************** **************** **************** **************** **************** **************** **************** **************** **************** **************** **************** **************** **************** ****************
Andhere'sanexamplefromtheworldofnurseryrhymes.We'llletyoufigurethisoneout.
:POEMCR111DOI.."Little" I3MOD0=IF."indians"CRTHEN LOOP ."indianboys.";
NestedLoops
InthelastsectionwedefinedawordcalledMULTIPLICATIONS,whichcontainsaDO...LOOP.Ifwe wantedto,wecouldputMULTIPLICATIONSinsideanotherDO...LOOP,likethis:
:TABLECR111DOIMULTIPLICATIONSLOOP;
Nowwe'llgetamultiplicationtablethatlookslikethis:
12345678910 2468101214161820 36912151821242730 etc. 102030405060708090100
becausetheIintheouterloopsuppliestheargumentforMULTIPLICATIONS. YoucanalsonestDOloopsinsideoneanotherallinthesamedefinition:
:TABLECR111DO 111DOIJ*5U.RLOOP CRLOOP;
Noticethisphraseintheinnerloop:
IJ*
+LOOP
Ifyouwanttheindextogoupbysomenumberotherthanoneeachtimearound,youcanusetheword
+LOOPinsteadofLOOP.+LOOPexpectsonthestackthenumberbywhichyouwanttheindexto change.Forexample,inthedefinition
:PENTAJUMPS500DOI.5+LOOP;
theindexwillgoupbyfiveeachtime,withthisresult:
PENTAJUMPS051015202530354045ok
whilein
:FALLING100DOI.1+LOOP;
theindexwillgodownbyoneeachtime,withthisresult:
FALLING012345678910ok
Theargumentfor+LOOP,whichiscalledthe"increment,"cancomefromanywhere,butitmustbeput onthestackeachtimearound.Considerthisexperimentalexample:
:INCCOUNTDOI.DUP+LOOPDROP;
Stepupbytwo:
250INCCOUNT024ok
Stepdownbythree:
31010INCCOUNT10741258ok
Ournextexampledemonstratesanincrementthatchangeseachtimethroughtheloop.
:DOUBLING327671DOI.I+LOOP;
Heretheindexitselfisusedastheincrement(I+LOOP),sothatstartingwithone,theindexdoubles eachtime,likethis:
DOUBLING 1248163264128256512102420484096819216384ok
Noticethatinthisexamplewedon'teverwanttheargumentfor+LOOPtobezero,becauseif itwerewe'dnevercomeoutoftheloop.Wewouldhavecreatedwhatisknownasan"infinite
loop."
DOingitForthStyle
ThereareafewthingstorememberbeforeyougooffandwritesomeDOloopsofyourown. First,keepthissimpleguideinmind: Reasonsfortermination Executionmakesitsexitfromaloopwhen,ingoingup,theindexhasreachedorpassedthelimit.
Theprogrammermightestthisversionattheterminal,using.or.Stochecktheresultofeachstep.The "conversation"mightlooklikethis:
insteadof
:FIVES1000DOI5*.LOOP;
Ifyouseethishappentoanyone(surelyitwillneverhappentoyou!)andifyouseethebeginnertyping inanendlesssuccessionofdotstoclearthestack,recommendtypingin
XX
XXisnotaForthword,sothetextinterpreterwillexecutethewordABORT",whichamongotherthings clearsallstacks.Thebeginnerwillbeendlesslygrateful.
IndefiniteLoops
WhileDOloopsarecalleddefiniteloops,Forthalsosupports"indefinite"loops.Thistypeofloopwill repeatindefinitelyoruntilsomeeventoccurs.Astandardformofindefiniteloopis
BEGIN...UNTIL
TheBEGIN...UNTILlooprepeatsuntilaconditionis"true." Theuseageis
BEGINxxxfUNTIL
AnexampleofadefinitionthatusesaBEGIN...UNTILstatementisonewementionedearlier,inour washingmachineexample:
:TILLFULLBEGIN?FULLUNTIL;
whichweusedinthehigherleveldefinition
:FILLFAUCETSOPENTILLFULLFAUCETSCLOSE;
TheIndefinitelyDefiniteLoop
Thereisawaytowriteadefiniteloopsothatitstopsshortoftheprescribedlimitifatruthcondition changesstate,byusingthewordLEAVE.LEAVEcausesthelooptoendimmediately. WatchhowwerewriteourearlierdefinitionofCOMPOUND.Insteadofjustlettingtheloopruntwenty times,let'sgetittoquitaftertwentytimesorassoonasourmoneyhasdoubled,whicheveroccursfirst. We'llsimplyaddthisphrase:
2000>IFLEAVETHEN
likethis:
:DOUBLED 61000211DOCR."YEAR"I2U.R 2DUPR%+DUP."BALANCE". DUP2000>IFCRCR."morethandoubledin" I.."years"LEAVE THEN LOOP2DROP;
Theresultwilllooklikethis:
DOUBLED YEAR1BALANCE1060 YEAR2BALANCE1124 YEAR3BALANCE1191 YEAR4BALANCE1262 YEAR5BALANCE1338 YEAR6BALANCE1418 YEAR7BALANCE1503 YEAR8BALANCE1593 YEAR9BALANCE1609
whichwillclearthescreenbeforeprintingtherectanglethatwedefinedearlierinthischapter.Oryou couldputPAGEatthebeginningofthedefinition.likethis:
:RECTANGLEPAGE2560DOI16MOD0=IFCRTHEN."*"LOOP;
Ifyoudon'twantthe"ok"toappearuponcompletionofexecution,usethewordQUIT.Again,youcan useQUITinteractively:
RECTANGLEQUIT
oryoucanmakeQUITthelastwordinthedefinition(justbeforethesemicolon). Here'salistoftheForthwordswe'vecoveredinthischapter: DO:(limitindex) DO...LOOP Setsupafiniteloop,giventheindexrange. LOOP:() DO:(limitindex) LikeDO...LOOPexceptaddsthevalueofn(instead DO...+LOOP +LOOP:() ofalwaysone)totheindex. LEAVE () Terminatetheloopimmediately. BEGIN...UNTIL UNTIL:(f) Setsupanindefiniteloopwhichendswhenfistrue. BEGINxxx Setsupanindefiniteloopwhichalwaysexecutesxxxand WHILEyyy WHILE:(f) alsoyyyiffistrue.Endswhenfisfalse. REPEAT Printstheunsignedsinglelengthnumber,rightjustified U.R (uwidth) withinthefieldwidth. Clearstheterminalscreenandresetstheterminal'scursor PAGE () totheupperlefthandcorner. Terminatesexecutionforthecurrenttaskandreturns QUIT () controltotheterminal. ReviewofTerms
aloopstructureinwhichthewordscontainedwithinthelooprepeatadefinitenumberof times.InForth,thisnumberdependsonthestartingandendingcounts(indexandlimit) whichareplacedonthestackpriortotheexecutionofthewordDO. aloopstructureinwhichthewordscontainedwithintheloopcontinuetorepeatwithoutany Infinite chanceofanexternaleventstoppingthem,exceptforclosingtheForthwindoworshutting loop downorresettingthecomputer. aloopstructureinwhichthewordscontainedwithintheloopcontinuetorepeatuntilsome Indefinite truthconditionchangesstate(truetofalseorfalsetotrue).InForth,theindefiniteloops loop beginwiththewordBEGIN. definite loop
ProblemsChapter6
InProblems1trough6,youwillcreateseveralwordswhichwillprintoutpatternsofstars(asterisks). ThesewillinvolvetheuseofDOloopsandBEGIN...UNTILloops. 1. FirstcreateawordnamedSTARSwhichwillprintoutnstarsonthesameline,givennonthe stack:
10STARS **********ok
******* ********* *********** ************* *************** ***************** ******************* ******************* ***************** *************** ************* *********** ********* ******* ***** *** * * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ******************* ***************** *************** ************* *********** ********* ******* ***** *** *
(sevensquared)
24**.16ok
7ANumberofKindsofNumbers
Sofarwe'veonlytalkedaboutsignedsinglelengthnumbers.Inthischapterwe'llintroduce unsignednumbersanddoublelengthnumbers,aswellasawholepasselofnewoperators togoalongwiththem. Thischapterisdividedintwosections: Forbeginnersthissectionexplainshowacomputerlooksatnumbersandexactlywhatis meantbythetermssignedorunsignedandbysinglelengthordoublelength. ForeveryonethissectioncontinuesourdiscussionofForthforbeginnersandexpertsalike, andexplainshowForthhandlessignedandunsigned,singleanddoublelengthnumbers.
Section1ForBeginners
SignedversusUnsignedNumbers
Alldigitalcomputersstorenumbersinbinaryform.InForth,thestackis(normally)thirtytwobitswide (a"bit"isa"binarydigit").Belowisaviewofthirtytwobits,showingthevalueofeachbit:
Ifeverybitweretocontaina1,thetotalwouldbe4294967295.Thusin32bitswecanexpressanyvalue between0and4294967295.Becausethiskindofnumberdoesnotletusexpressnegativevalues,wecall itan"unsignednumber."Wehavebeenindicatingunsignednumberswiththeletter"u"inourtablesand stacknotations. Butwhataboutnegativenumbers?Inordertobeabletoexpressapositiveornegativenumber,weneed tosacrificeonebitthatwillessentiallyindicatesign.Thisbitistheoneatthefarleft,the"highorder bit."In31bitswecanexpressanumberashighas2147483647.Whenthesignbitcontains1,thenwecan goanequaldistancebackintothenegativenumbers.Thuswithin32bitswecanrepresentanynumber from2147483648to+2147483647.Thisshouldlookfamiliartoyouastherangeofasinglelength number,whichwehavebeenindicatingwiththeletter"n." Beforeweleaveyouwithanymisconceptions,we'dbetterclarifythewaynegativenumbersare represented.Youmightthinkthatit'sasimplematterofsettingthesignbittoindicatewhetheranumber ispositiveornegative,butitdoesn'tworkthatway. Toexplainhownegativenumbersarerepresented,let'sreturntodecimalnotationandexamineacounter suchasthatfoundonmanyWWWinternetpages. Let'ssaythecounterhasthreedigits,notfive.Asmorepeoplevisitthepage,thecounterwheelsturnand thenumberincreases.Startingonceagainwiththecounterat0,nowimagineyoubadlyregrethaving visitedthepageandcould"unvisit"itbyrollingthecounterwheelsbackward.Thefirstnumberyousee is999,whichis,inasense,thesameas1.Thenextnumberwillbe998,whichisthesameas2,andso on.
Therepresentationofsignednumbersinacomputerissimilar. Startingwiththenumber
0000,0000,0000,0000,0000,0000,0000,0000
andgoingbackwardsonenumber,weget
1111,1111,1111,1111,1111,1111,1111,1111(thirtytwoones)
whichstandsfor4294967295inunsignednotationaswellasfor1insignednotation.Thenumber
1111,1111,1111,1111,1111,1111,1111,1110
Subtractingonefromtwoisthesameasaddingtwoplusnegativeone.Insinglelengthbinarynotation, thetwolookslikethis:
0000,0000,0000,0000,0000,0000,0000,0010
whilenegativeonelookslikethis:
1111,1111,1111,1111,1111,1111,1111,1111
Thecomputeraddsthemupthesamewaywewouldonpaper;thatiswhenthetotalofanycolumn exceedsone,itcarriesaoneintothenextcolumn.Theresultlookslikethis:
0000,0000,0000,0000,0000,0000,0000,0010 +1111,1111,1111,1111,1111,1111,1111,1111 10000,0000,0000,0000,0000,0000,0000,0001
Asyoucansee,thecomputerhadtocarryaoneintoeverycolumnallthewayacross,andendedupwith aoneinthethirtythirdplace.Butsincethestackisonlythirtytwobitswide,theresultissimply
0000,0000,0000,0000,0000,0000,0000,0001
ArithmeticShift
Whilewe'reonthesubjectofhowacomputerperformscertainmathematicaloperations,we'llexplain whatismeantbythemysteriousphrasesbackinChap.5:"arithmeticleftshift"and"arithmeticright shift." AForthInstantReplay 2* 2/ LSHIFT RSHIFT (nn*2) (nn/2) (nun*2^u) (nn/2^u) Multipliesbytwo(arithmeticleftshift) Dividesbytwo(arithmeticrightshift) Logicalleftshiftoverupositions Logicalrightshiftoverupositions
Toillustrate,let'spickanumber,saysix,andwriteitinbinaryform:
0000,0000,0000,0000,0000,0000,0000,0110
(4+2).Nowlet'sshifteverydigitoneplacetotheleft,andputazerointhevacantplaceintheone's column.
0000,0000,0000,0000,0000,0000,0000,1100
Thisisthebinaryrepresentationoftwelve(8+4),whichisexactlydoubletheoriginalnumber.Thisworks inallcases,anditalsoworksinreverse.Ifyoushifteverydigitoneplacetotherightandfillthevacant digitwithazero,theresultwillalwaysbehalfoftheoriginalvalue. Inarithmeticshift,thesignbitdoesnotgetshifted.Thismeansthatapositivenumberwillstaypositive andanegativenumberwillstaynegativewhenyoudivideormultiplyitbytwo. Whenthehighorderbitshiftswithalltheotherbits,thetermis"logicalshift."InForthyoucandoa logicalshiftofupto32placeswiththewordsLSHIFTandRSHIFT. Theimportantthingforyoutoknowisthatmostcomputerscanshiftdigitsmuchmorequickythanthey cangothroughallthefolderolofnormaldivisionormultiplication.Whenspeediscritical,it'smuch bettertosay
2*
than
2*
anditmayevenbebettertosay
2*2*2*
than
8*
dependingonyourparticularmodelofcomputer,butthistopicisgettingtootechnicalforrightnow.
AnIntroductiontoDoublelengthNumbers
Adoublelengthnumberisjustwhatyouprobablyexpecteditwouldbe:anumberthatisrepresentedin sixtyfourbitsinsteadofthirtytwo.Signeddoublelengthnumbershavearangeof+/ 18,446,744,073,709,551,615. InForth,adoublelengthnumbertakestheplaceoftwosinglelengthnumbersonthestack.Operators like2DUPareusefuleitherfordoublelengthnumbersorforpairsofsinglelengthnumbers. Onemorethingweshouldexplain:tothenonForthspeakingcomputerworld,theterm"doubleword" meansa32bitvalue,orfourbytes.ButinForth,"word"meansadefinedcommand.Soinordertoavoid confusion,Forthprogrammersrefertoa32bitvalueasa"cell."Adoublelengthnumberrequirestwo cells.
OtherNumberBases
Asyougetmoreinvolvedinprogramming,you'llneedtoemployothernumberbasesbesidesdecimaland binary,particularlyhexadecimal(base16)andoctal(base8).Sincewe'llbetalkingaboutthesetwo numberbaseslateroninthischapter,wethinkyoumightlikeanintroductionnow. Computerpeoplebeganusinghexadecimalandoctalnumbersforonemainreason:computersthinkin binaryandhumanbeingshaveahardtimereadinglongbinarynumbers.Forpeople,it'smucheasierto convertbinarytohexadecimalthanbinarytodecimal,becausesixteenisanevenpoweroftwo,whileten isnot.Thesameistruewithoctal.Soprogrammersusuallyusehexoroctaltoexpressthebinary numbersthatthecomputerusesforthingslikeaddressesandmachinecodes.Hexadecimal(orsimply "hex")looksstrangeatfirstsinceitusesthelettersAthroughF. Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Let'stakeasinglelengthbinarynumber: Binary Hexadecimal 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
00000000000000000111101110100001
Toconvertthisnumbertohexadecimal,wefirstsubdivideitintoeightunitsoffourbitseach:
|0000|0000|0000|0000|0111|1011|1010|0001|
thenconverteach4bitunittoitshexequivalent:
|0|0|0|0|7|B|A|1|
TheASCIICharacterSet
Ifthecomputerusesbinarynotationtostorenumbers,howdoesitstorecharactersandothersymbols? Binary,again,butinaspecialcodethatwasadoptedasanindustrystandardmanyyearsago.Thecodeis calledtheAmericanStandardCodeforInformationInterchange,usuallyabbreviatedASCII. Table71showseachASCIIcharacterinthesystem,itsISO6461983,ISO7bitcodedcharactersetfor informationinterchange,InternationalReferenceVersionequivalent(IRV),anditshexadecimalform. Thecharactersinthefirstcolumn(ASCIIcodes01Fhex)arecalled"controlcharacters"becausethey indicatethattheterminalorcomputerissupposedtodosomethinglikeringitsbell,backspace,starta newline,etc.Theremainingcharactersarecalled"printingcharacters"becausetheyproducevisible charactersincludingletters,thenumeralszerothroughnine,allavailablesymbolsandeventheblank space(hex20).TheonlyexceptionisDEL(hex7F)whichisasignaltothecomputertoignorethelast charactersent. InChap.1weintroducedthewordEMIT.EMITtakesanASCIIcodeonthestackandsendsittothe terminalsothattheterminalwillprintitasacharacter.Forexample,
65EMITAok 66EMITBok
PRINTABLESwillemiteveryprintablecharacterintheASCIIset;thatis,thecharactersfromdecimal 32todecimal126.(We'reusingtheASCIIcodesasourDOloopindex.)
PRINTABLES!"#$%&'()*+,./...ok
Table7.1Standardgraphiccharacters&Equivalents Hex ASCII Hex ASCII HexIRV ASCII HexIRV ASCII HexIRV ASCII HexIRV ASCII HexIRV ASCII HexIRV ASCII
00NUL 10DLE 01SOH 11DC1 02STX 12DC2 03ETX 13DC3 04EOT 14DC4 05ENQ 15NAK 06ACK 16SYN 07BEL 17ETB 08BS 09HT 0ALF 0BVT 0CFF 0DCR 0ESM 0FSI 18CAN 19EM 1ASUB 1BESC 1CFS 1DGS 1ERS 1FUS
20 21!! 22"" 23## 24$ 25%% 26&& 27'' 28(( 29)) 2A** 2B++ 2C,, 2D 2E.. 2F//
3000 3111 3222 3333 3444 3555 3666 3777 3888 3999 3A:: 3B;; 3C<< 3D== 3E>> 3F??
40@@ 41AA 42BB 43CC 44DD 45EE 46FF 47GG 48HH 49II 4AJJ 4BKK 4CLL 4DMM 4ENN 4FOO
50PP 51QQ 52RR 53SS 54TT 55UU 56VV 57WW 58XX 59YY 5AZZ 5B[[ 5C\\ 5D]] 5E^^ 5F
60`` 61aa 62bb 63cc 64dd 65ee 66ff 67gg 68hh 69ii 6Ajj 6Bkk 6Cll 6Dmm 6Enn 6Foo
70pp 71qq 72rr 73ss 74tt 75uu 76vv 77ww 78xx 79yy 7Azz 7B{{ 7C|| 7D}} 7E~~
Beginnersmaybeinterestedinsomeofthecontrolcharactersaswell.Forinstance,trythis:
7 EMITok
Youshouldhaveheardsomesortofbeep,whichisthevideoterminal'sversionofthemechanical printer's"typewriterbell." Othercontrolcharactersthataregoodtoknowincludethefollowing: name operation decimalequivalent BS backspace 8 LF linefeed 10 CR carriagereturn 13 Experimentwiththesecontrolcharacters,andseewhattheydo. ASCIIisdesignedsothateachcharactercanberepresentedbyonebyte.Thetablesinthisbookusethe letter"c"toindicateabytevaluethatisbeingusedasacodedASCIIcharacter.
BitLogic
ThewordsANDandOR(whichweintroducedinChap.4)use"bitlogic";thatis,eachbitistreated independently,andthereareno"carries"fromonebitplacetothenext.Forexample,let'sseewhat happenswhenweANDthesetwobinarynumbers:
0000,0000,0000,0000,0000,0000,1111,1111 0000,0000,0000,0000,0110,0101,1010,0010AND
0000,0000,0000,0000,0000,0000,1010,0010
is"1"or"0"bymaskingoutallotherflags,likethis:
1000,0100,0010,0001,1000,1001,0000,1001 0000,0000,0000,0000,1000,0000,0000,0000AND 0000,0000,0000,0000,1000,0000,0000,0000
Sincethebitwas"1,"theresultis"true."Haditbeen"0,"theresultwouldhavebeen"0"or"false." Wecouldsettheflagto"0"withoutaffectingtheotherflagsbyusingthistechnique:
1000,0100,0010,0001,1000,1001,0000,1001 1111,1111,1111,1111,0111,1111,1111,1111AND 1000,0100,0010,0001,0000,1001,0000,1001 ^
Weusedamaskthatcontainsall"1"sexceptforthebitwewantedtosetto"0."Wecansetthesameflag backto"1"byusingthistechnique:
1000,0100,0010,0001,0000,1001,0000,1001 0000,0000,0000,0000,1000,0000,0000,0000OR 1000,0100,0010,0001,1000,1001,0000,1001 ^
Section2ForEverybody
SignedandUnsignedNumbers
BackinChap.1weintroducedthewordNUMBER.IfthewordFINDcan'tfindanincomingstringinthe dictionary,ithandsitovertothewordNUMBER.NUMBERthenattemptstoconvertthestringintoa numberexpressedinbinaryform.IfNUMBERsucceeds,itpushesthebinaryequivalentontothestack.
ThismeansthatNUMBERdoesnotcheckwhetherthenumberyou'veenteredasasinglelengthnumber exceedstheproperrange.Ifyouenteragiantnumber,NUMBERconvertsitbutonlysavestheleast significantthirtytwodigits. NUMBERdoesnotdoanyrangechecking.Becauseofthis,NUMBERcanconverteithersignedor unsignednumbers. Forinstance,ifyouenteranynumberbetween2147483648and4294967295,NUMBERwillconvertitas anunsignednumber.Anyvaluebetween2147483648and1willbestoredasatwo'scomplement integer. Thisisanimportantpoint:thestackcanbeusedtoholdeithersignedorunsignednumbers.Whethera binaryvalueisinterpretedassignedorunsigneddependsontheoperatorsthatyouapplytoit.Youdecide whichformisbetterforagivensituation,thensticktoyourchoice. We'veintroducedtheword.,whichprintsavalueonthestackasasignednumber:
4294967295.1ok
ThewordU.printsthebinaryrepresentationasanunsignednumber:
4294967295U.4294967295ok
U.
(u)
Printstheunsignedsinglelengthnumber,followedbyaspace.
Inthisbooktheletter"n"signifiessignedsinglelengthnumbers,whiletheletter"u"signifiesunsigned singlelengthnumbers.(We'vealreadyintroducedU.R,whichprintsanunsignedsinglelengthnumber rightjustifiedwithinagivencolumnwidth.) Hereisatableofadditionalwordsthatuseunsignednumbers: UM* UM/MO D U< (u1u2ud) (udu1u2u3) (u1u2f) Multipliestwosinglelengthnumbers.Returnsadoublelength result.Allvaluesareunsigned. Dividesadoublelengthbyasinglelengthnumber.Returnsa singlelengthquotientu2andremainderu3.Allvaluesare unsigned. Leavestrueifu1<u2,wherebotharetreatedassinglelength unsignedintegers.
NumberBases
WhenyoufirststartForth,allnumberconversionsusebaseten(decimal),forbothinputandoutput.
DECIMAL
()
Returnsthebasetoten.
ToconverthexFintodecimal(rememberyouarealreadyinhex),enter
0FDECIMAL.15ok
Makeitahabit,startingrightnow,toprecedeeachhexadecimalvaluewithazero,asin
0A0B0F
ThispracticeavoidsmixupswithpossiblypredefinedwordsasDEADBEEF,BAD,DECetc. Beginnerswhowanttoseewhatnumberslooklikeinbinarynotationmayenterthisdefinition:
:BINARY2BASE!;
ThenewwordBINARYwilloperatejustlikeOCTALorHEXbutwillchangethenumberbasetotwo. OnsystemswhichdonothavethewordOCTAL,experimentersmaydefine
:OCTAL8BASE!;
DoublelengthNumbers
Doublelengthnumbersprovidearangeof+/18,446,744,073,709,551,615.ANSForthsystemssupport doublelengthnumberstosomedegree.Thewaytoenteradoublelengthnumberontothestack(whether fromthekeyboardorfromafile)istopunctuateitwithoneofthesefivepunctuationmarks:
,./:
Forexample,whenyoutype
200,000
Noticethatallofthefollowingnumbersareconvertedinexactlythesameway:
12345.D.12345ok 123.45D.12345ok 12345D.12345ok 1/23/45D.12345ok 1:23:45D.12345ok
Butthisisnotthesame:
12345
NumberFormattingDoublelengthUnsigned
$200.0012/31/8037284936:32:5998.6
ThewordTYPEprintsthecharactersthatrepresentthenumberatyourterminal.Noticethatthereisno spacebetweenthenumberandthe"ok."Togetaspace,youwouldsimplyaddthewordSPACE,likethis:
:UD.<##S#>TYPESPACE;
Nowlet'ssaywehaveaphonenumberonthestack,expressedasadoublelengthunsignedinteger.For example,wemayhavetypedin:
3728493
Eitherway,thecompileddefinitionwillbeexactlythesame. Nowlet'sformatanunsigneddoublelengthnumberasadate,inthefollowingform:
6/15/03
Hereisthedefinition:
:.DATE<###[CHAR]/HOLD##[CHAR]/HOLD#S#>TYPESPACE;
Let'sfollowtheabovedefinition,rememberingthatitiswritteninreverseorderfromthe output.Thephrase
##[CHAR]/HOLD
Table72summarizestheForthwordsthatareusedinnumberformatting.(Notethe"KEY"atthe bottom,whichservesasareminderofthemeaningsof"n,""d,"etc.) Table72NumberFormatting <# # #S cHOLD SIGN #> Beginsthenumberconversionprocess.Expectstheunsigneddoublelengthnumberon thestack. Convertsonedigitandputsitintoanoutputcharacterstring.#alwaysproducesa digitifyou'reoutofsignificantdigits,you'llstillgetazeroforevery#. Convertsthenumberuntiltheresultiszero.Alwaysproducesatleastonedigit(0ifthe valueiszero). Inserts,atthecurrentpositioninthecharacterstringbeingformatted,acharacterwhose ASCIIvalueisonthestack.HOLD(orawordwhichusesHOLD)mustbeused between<#and#>. Insertsaminussignintheoutputstringifthetopofstackisnegative.Usuallyused withROTimmediatelybefore#>foraleadingminussign. Completesnumberconversionbyleavingthecharactercountandaddressonthestack (thesearetheappropriateargumentsforTYPE). Stackeffectsfornumberformatting phrase <#...#> stack (udaddru) typeofarguments doublelengthunsigned doublelengthsigned(wherenisthehighordercellofd and|d|istheabsolutevalueofd). KEY n,n1,... singlelengthsigned
<#...ROTSIGN#> (n|d|addru)
NumberFormattingSignedandSinglelength
Sofarwehaveformattedonlyunsigneddoublelengthnumbers.The<#...#>formexpectsonlyunsigned doublelengthnumbers,butwecanuseitforothertypesofnumbersbymakingcertainarrangementson thestack. Forinstance,let'slookatasimplifiedversionofthesystemdefinitionofD.(whichprintsasigned doublelengthnumber):
:D.TUCKDABS<##SROTSIGN#>TYPESPACE;
inwhichcasewewouldplacethephraseROTSIGNattheleftsideofour<#...#>phrase,likethis:
<#ROTSIGN#S#>
:.$TUCKDABS<###[CHAR].HOLD#SROTSIGN[CHAR]$HOLD#> TYPESPACE;
Let'stryit:
2000.00.$$2000.00ok
oreven
2,000.00.$$2000.00ok
Herearethe"setup"phrasesthatareneededtoprintvariouskindsofnumbers: Numbertobeprinted doublelength,unsigned 63bit,plussign singlelength,unsigned 31bit,plussign (nothingneeded) TUCKDABS (tosavethesigninthethirdstackpositionforROTSIGN) 0 (togiveadummyhighorderpart) DUPABS0 (tosavethesign) Precede<#by
DoublelengthOperators
Hereisalistofdoublelengthmathoperators: D.R D+ D DNEGATE DMAX DMIN D= D0= D< DU< (dwidth) (d1d2dsum) (d1d2ddiff) (dd) (d1d2dmax) (d1d2dmin) (d1d2f) (df) (d1d2f) (ud1ud2f) Printsthesigneddoublelengthnumber,rightjustifiedwithin thefieldwidth. Addstwodoublelengthnumbers. Subtractstwodoublelengthnumbers(d1d2). Changesthesignofadoublelengthnumber. Returnsthemaximumoftwodoublelengthnumbers(d1d2). Returnstheminimumoftwodoublelengthnumbers(d1d2). Returnstrueifd1andd2areequal. Returnstrueifdiszero. Returnstrueifd1islessthand2. Returnstrueifud1islessthanud2.Bothnumbersare unsigned.
Here'sanexampleusingD+:
200,000300,000D+D.500000ok
MixedlengthOperators
Here'satableofveryusefulForthwordswhichoperateonacombinationofsingleanddoublelength numbers: M+ (dndsum) Addsadoublelengthnumbertoasingle lengthnumber.Returnsadoublelength result. Divided1byn1,givingthesymmetric quotientn3andtheremaindern2.Input andoutputstackargumentsaresigned. Anambiguousconditionexistsifn1is zeroorifthequotientliesoutsidethe rangeofasinglecellsignedinteger. Divided1byn1,givingthefloored quotientn3andtheremaindern2.Input andoutputstackargumentsaresigned. Anambiguousconditionexistsifn1is zeroorifthequotientliesoutsidethe rangeofasinglecellsignedinteger. Multipliestwosinglelengthnumbers. Returnsadoublelengthresult.Allvalues aresigned.
SM/R (dn1n2n3) EM
FM/M (dn1n2n3) OD
M*
(n1n2dprod)
Or,usingM*/,wecanredefineourearlierversionof%sothatitwillacceptadoublelengthargument:
:%100M*/;
asin
200.5015%D.3007ok
Ifyouhaveloadedthedefinitionof.$wegaveinthelastHandyHint,youcanenter
200.5015%.$$30.07ok
WecanredefineourearlierdefinitionofR%togetaroundeddoublelengthresult,likethis:
:R%10M*/5M+10SM/REMNIP;
then
987.6515R%.$$30.08ok
NoticethatM*/istheonlyreadymadeForthwordwhichperformsmultiplicationonadoublelength argument.Tomultiply200,000by3,forinstance,wemustsupplya"1"asadummydenominator:
200,00031M*/D.600000ok
since
3 1
NumbersinDefinitions
Whenadefinitioncontainsanumber,suchas
:SCOREMORE20+;
thenumberiscompiledintothedictionaryinbinaryform,justasitlooksonthestack.
Thenumber'sbinaryvaluedependsonthenumberbaseatthetimeyoucompilethedefinition.For example,ifyouweretoenter
HEX:SCOREMORE14+;DECIMAL
EXAMPLE14ok
Thenumberisoutputinhexadecimal. Fortherecord,anumberthatappearsinsideadefinitioniscalleda"literal."(Unlikethewordsintherest ofthedefinitionwhichalludetootherdefinitions,anumbermustbetakenliterally.) HereisalistoftheForthwordswe'vecoveredinthischapter: Unsignedoperators U. UM* (u) (u1u2ud) Printstheunsignedsinglelengthnumber,followedbyonespace. Multipliestwosinglelengthnumbers.Returnsadoublelengthresult. Allvaluesareunsigned. Dividesadoublelengthbyasinglelengthnumber.Returnsasingle lengthquotientandremainder.Allvaluesareunsigned. Leavestrueifu1<u2,wherebotharetreatedassinglelengthunsigned integers. Numberbases HEX OCTAL DECIMAL () () () Setsthebasetosixteen. Setsthebasetoeight(availableonsomesystems). Returnsthebasetoten. Numberformattingoperators <# # #S Beginsthenumberconversionprocess.Expectstheunsigneddoublelengthnumberonthe stack. Convertsonedigitandputsitintoanoutputcharacterstring.#alwaysproducesadigitif you'reoutofsignificantdigits,you'llstillgetazeroforevery#. Convertsthenumberuntiltheresultiszero.Alwaysproducesatleastonedigit(0ifthevalue iszero).
Inserts,atthecurrentpositioninthecharacterstringbeingformatted,acharacterwhose cHOLD ASCIIvalueisonthestack.HOLD(orawordwhichusesHOLD)mustbeusedbetween<# and#>. SIGN #> Insertsaminussignintheoutputstringifthetopofstackisnegative.UsuallyusedwithROT immediatelybefore#>foraleadingminussign. Completesnumberconversionbyleavingthecharactercountandaddressonthestack(these aretheappropriateargumentsforTYPE). Stackeffectsfornumberformatting phrase <#...#> stack (daddru) typeofarguments doublelengthunsigned doublelengthsigned(wherenisthehighordercellofd and|d|istheabsolutevalueofd).
<#...ROTSIGN#> (n|d|addru)
Doublelengthoperators
(d1d2dsum) Addstwodoublelengthnumbers. (d1d2ddiff) Subtractstwodoublelengthnumbers(d1d2). Changesthesignofadoublelengthnumber. (d1d2dmax) Returnsthemaximumoftwodoublelengthnumbers(d1d2). (d1d2dmin) Returnstheminimumoftwodoublelengthnumbers(d1d2). (d1d2f) (df) (d1d2f) (ud1ud2f) (dwidth) Returnstrueifd1andd2areequal. Returnstrueifdiszero. Returnstrueifd1islessthand2. Returnstrueifud1islessthanud2.Bothnumbersareunsigned. Printsthesigneddoublelengthnumber,rightjustifiedwithinthefield width. Mixedlengthoperators Addsadoublelengthnumbertoasinglelengthnumber.Returnsa doublelengthresult. Divided1byn1,givingthesymmetricquotientn3andtheremainder n2.Inputandoutputstackargumentsaresigned.Anambiguous conditionexistsifn1iszeroorifthequotientliesoutsidetherange ofasinglecellsignedinteger. Divided1byn1,givingtheflooredquotientn3andtheremainder n2.Inputandoutputstackargumentsaresigned.Anambiguous conditionexistsifn1iszeroorifthequotientliesoutsidetherange ofasinglecellsignedinteger. Multipliestwosinglelengthnumbers.Returnsadoublelength result.Allvaluesaresigned.
DNEGATE (dd)
M+
(dndsum)
SM/REM (dn1n2n3)
FM/MOD (dn1n2n3)
M* M*/
(n1n2dprod)
n,n1,... singlelengthsigned d,d1,... doublelengthsigned u,u1,... singlelengthunsigned addr c address ASCIIcharactervalue ReviewofTerms Arithmeticleftand theprocessofshiftingallbitsinanumber,exceptthesignbit,totheleftorright,in rightshift effectdoublingorhalvingthe(assumedsigned)number,respectively.
Logicalleftand rightshift
theprocessofshiftingallbitsinanumber,includingthesignbit,totheleftor right,ineffectdoublingorhalvingthe(assumedunsigned)number,respectively. astandarizedsystemofrepresentinginput/outputcharactersasbytevalues. ASCII AcronymforAmericanStandardCodeforInformationInterchange.(Pronounced askkey) Binary numberbase2. Byte thestandardtermforan8bitvalue. Cell theForthtermforasinglecellvalue. Decimal numberbase10. Hexadecimal numberbase16. ingeneral,anumberofsymbolwhichrepresentsonlyitself;inForth,anumber Literal thatappearsinsideadefinition. avaluewhichcanbe"superimposed"overanother,hidingcertainbitsand Mask revealingonlythosebitsthatweareinterestedin. theprocessofprintinganumber,usuallyinaspecialformsuchas3/13/03or Numberformatting $47.93. Octal numberbase8. Signbithighorder thebitwhich,forasignednumber,indicateswhetheritispositiveornegativeand, bit foranunsignednumber,representsthebitofthehighestmagnitude. foranynumber,thenumberofequalabsolutevaluebutoppositesign.Tocalculate Two'scomplement 104,thecomputerfirstproducesthetwo'scomplementof4,(i.e.,4),then computes10+(4). Unsignednumber anumberwhichisassumedtobepositive. Unsignedsingle anintegerwhichfallswithintherangeof0to2147483647. lengthnumber Word InForth,adefineddictionaryentry,elsewhere,atermfora16bitvalue. producesaquotientqandaremainderrbydividingoperandabyoperandb. Integerdivision Divisionoperationsreturnq,r,orboth.Theidentityb*q+r=aholdsforallaand b. isintegerdivisioninwhichtheremaindercarriesthesignofthedivisororiszero, Flooreddivision andthequotientisroundedtoitsarithmeticfloor. isintegerdivisioninwhichtheremaindercarriesthesignofthedividendoriszero Symmetricdivision andthequotientisthemathematicalquotient"roundedtowardszero"or "truncated".
ProblemsChapter7
1. VeronicaWainwrightcouldn'tremembertheupperlimitforasignedsinglelengthnumber,and shehadnobooktoreferto,onlyaForthterminal.SoshewroteadefinitioncalledNMAX,using
aBEGIN...UNTILloop.Whensheexecutedit,shegot
2147483647ok
3. Writeadefinitionthat"rings"yourterminal'sbellthreetimes.Makesurethatthereisenoughofa delaybetweenthebellssothattheyaredistinguishable.Eachtimethebellrings,theword "BEEP"shouldappearontheterminalscreen.[answer] a. Rewritethetemperatureconversiondefinitionswhichyoucreatedfortheproblemsin Chap.5.Thistimeassumethattheinputandresultingtemperaturesaretobedouble lengthsignedintegerswhicharescaled(i.e.,multiplied)byten.Forexample,if10.5 degreesisentered,itisa32bitintegerwithavalueof105.[answer] 4. Writeaformattedoutputwordnamed.DEGwhichwilldisplayadoublelengthsignedinteger scaledbytenasastringofdigits,adecimalpoint,andonefractionaldigit. Forexample:
12.3.DEG 12.3ok
[answer] 5. Solvethefollowingconversions: 0.0oFinCentigrade 212.0oFinCentigrade 20.0oFinCentigrade 16.0oCinFahrenheit 40.0oCinFahrenheit 100.0oKinCentigrade 100.0oKinFahrenheit 233.0oKinCentigrade 233.0oKinFahrenheit a. Writearoutinewhichevaluatesthequadraticequation
7x2+20x+5
8. Ifyouenter
..
8Variables,Constants,andArrays
Aswehaveseenthroughouttheprevioussevenchapters,Forthprogrammersusethestacktostore numberstemporarilywhiletheyperformcalculationsortopassargumentsfromonewordtoanother. Whenprogrammersneedtostorenumbersmorepermanently,theyusevariablesandconstants. Inthischapter,we'lllearnhowForthtreatsvariablesandconstants,andintheprocesswe'llseehowto directlyaccesslocationsinmemory.
Variables
Let'sstartwithanexampleofasituationinwhichyou'dwanttouseavariabletostoretheday'sdate. Firstwe'llcreateavariablecalledDATE.Wedothisbysaying
VARIABLEDATE
Iftodayisthetwelfth,wenowsay
12DATE!
thatis,wecannamethevariable,thenexecutetheword@,whichispronouncedfetch.Thisphrase fetchesthetwelveandputsitonthestack.Thusthephrase
DATE@.12ok
printsthedate. Tomakematterseveneasier,thereisaForthwordwhosedefinitionisthis:
:?@.;
Soinsteadof"DATEfetchdot,"wecansimplytype
DATE?12ok
ThevalueofDATEwillbetwelveuntilwechangeit.Tochangeit,wesimplystoreanewnumber
13DATE!ok DATE?13ok
Conceivablywecoulddefineadditionalvariablesforthemonthandyear:
VARIABLEDATEVARIABLEMONTHVARIABLEYEAR
thendefineawordcalled!DATE(for"storethedate")likethis:
:!DATEYEAR!DATE!MONTH!;
tobeusedlikethis:
73103!DATEok
thendefineawordcalled.DATE(for"printthedate")likethis:
:.DATEMONTH?DATE?YEAR?;
YoucanworkinanynumberbasebysimplystoringitintoBASE.
ForExperts Athreelettercodesuchasanairportterminalname,canbestoredasasinglelengthunsignednumberinbase 36.Forexample: :ALPHA36BASE!;ok ALPHAok ZAPU.ZAPok
Somewhereinthedefinitionsofthesystemwordswhichperforminputandoutputnumberconversions, youwillfindthephrase
BASE@
becausethecurrentvalueofBASEisusedintheconversionprocess.Thusasingleroutinecanconvert numbersinanybase.Thisleadsustomakeaformalstatementabouttheuseofvariables:
ACloserLookatVariables
WhenyoucreateavariablesuchasDATEbyusingthephrase
VARIABLEDATE
Twelvegoesontothestack,afterwhichthetextinterpreterlooksupDATEin thedictionaryand,findingit,pointsitouttoEXECUTE.
Usingthevalueonthestackasanaddress,theword@pushesthecontentsofthatlocationontothestack, "dropping"theaddress.(Thecontentsofthelocationremainintact.)
UsingaVariableasaCounter
InForth,avariableisidealforkeepingacountofsomething.Toreuseoureggpackerexample,wemight keeptrackofhowmanyeggsgodowntheconveyorbeltinasingleday.(Thisexamplewillworkatyour terminal,soenteritaswego.) Firstwecandefine
VARIABLEEGGS
tokeepthecountin.Tostartwithacleanslateeverymorning,wecouldstoreazerointoEGGSby executingawordwhosedefinitionlookslikethis:
:RESET0EGGS!;
Thensomewhereinoureggpackingapplication,wewoulddefineawordwhichexecutesthefollowing phraseeverytimeaneggpassesanelectriceyeontheconveyor:
1EGGS+!
Theword+!addsthegivenvaluetothecontentsofthegivenaddress.(Itdoesn'tbothertotellyouwhat thecontentsare.)Thusthephrase
1EGGS+!
incrementsthecountofeggsbyone.Forpurposesofillustration,let'sputthisphraseinsideadefinition likethis:
:EGG1EGGS+!;
Attheendoftheday,wewouldsay
EGGS?
tofindouthowmanyeggswentbysincemorning. Let'stryit:
RESETok EGGok EGGok EGGok EGGS?3ok
Here'sareviewofthewordswe'vecoveredinthechaptersofar: VARIABLExxx ! @ ? +! () Createsavariablenamedxxx;thewordxxxreturnsitsaddress xxx:(addr) whenexecuted. (naddr) (addrn) (addr) (naddr) Storesasinglelengthnumberintotheaddress. Replacestheaddresswithitscontents. Printsthecontentsoftheaddress,followedbyonespace. Addsasinglelengthnumbertothecontentsoftheaddress.
Constants
Whilevariablesarenormallyusedforvaluesthatmaychange,constants areusedforvaluesthatwon'tchange.InForth,wecreateaconstantand setitsvalueatthesametime,likethis:
220CONSTANTLIMIT
HerewehavedefinedaconstantnamedLIMIT,andgivenitthevalue220.Nowwecanusetheword LIMITinplaceofthevalue,likethis:
:?TOOHOTLIMIT>IF."Dangerreduceheat"THEN;
Ifthenumberonthestackisgreaterthan220,thenthewarningmessagewillbeprinted. Noticethatwhenwesay
LIMIT
HerethewordSHUTTERhasbeendefinedasaconstantsothatexecutionofSHUTTERreturnsthe hardwareaddressofthecamera'sshutter.Itmight,forexample,bedefined:
HEX FFFF3E27CONSTANTSHUTTER DECIMAL
ThewordsOPENandCLOSEmightbedefinedsimplyas
:OPEN1SWAP!; :CLOSE0SWAP!;
sothatthephrase
SHUTTEROPEN
writesa"1"totheshutteraddress,causingtheshuttertoopen. Herearesomesituationswhenit'sgoodtodefinenumbersasconstants: 1. Whenit'simportantthatyoumakeyourapplicationmorereadable.OneoftheelementsofForth styleisthatdefinitionsshouldbeselfdocumenting,asisthedefinitionofPHOTOGRAPHabove. 2. Whenit'smoreconvenienttouseanameinsteadofthenumber.Forexample,ifyouthinkyou mayhavetochangethevalue(because,forinstance,thehardwaremightgetchanged)youwill onlyhavetochangethevalueonceinthefilewheretheconstantisdefinedthenrecompileyour application. 3. (OnlytrueforlesssophisticatedForthcompilers)Whenyouareusingthesamevaluemanytimes inyourapplication.Inthecompiledformofadefinition,referencetoaconstantrequiresless memoryspace. CONSTANTxxx (n) Createsaconstantnamedxxxwiththevaluen;thewordxxxreturnsn xxx:(n) whenexecuted.
DoublelengthVariablesandConstants
Youcandefineadoublelengthvariablebyusingtheword2VARIABLE.Forexample,
2VARIABLEDATE
NowyoucanusetheForthwords2!(pronouncedtwostore)and2@(pronouncedtwofetch)toaccess thisdoublelengthvariable.Youcanstoreadoublelengthnumberintoitbysimplysaying
800,000DATE2!
andfetchitbackwith
DATE2@D.800000ok
Oryoucanstorethefullmonth/date/yearintoit,likethis:
7/17/03DATE2!
andfetchitbackwith
DATE2@.DATE7/17/03ok
assumingthatyou'veloadedtheversionof.DATEwegaveinthelastchapter. YoucandefineadoublelengthconstantbyusingtheForthword2CONSTANT,likethis:
200,0002CONSTANTAPPLES
NowthewordAPPLESwillplacethedoublelengthnumberonthestack.
APPLESD.200000ok
Ofcourse,wecando:
400,0002CONSTANTMUCH :MUCHMORE200,000D+MUCHD+;
inordertobeabletosay
APPLESMUCHMORED.800000ok
tomultiplyanumberbyacrudeapproximationof.Wecouldstorethesetwointegersasa2CONSTANT asfollows:
3551132CONSTANTPI
thensimplyusethephrase
PI*/
asin
10000PI*/.31415ok
Hereisareviewofthedoublelengthdatastructurewords: (d) xxx:(d) () 2VARIABLExxx xxx:(addr) 2CONSTANTxxx 2! 2@ (daddr) (addrd) Createsadoublelengthconstantnamedxxxwiththevalued; thewordxxxreturnsdwhenexecuted. Createsadoublelengthvariablenamedxxx;thewordxxx returnsitsaddresswhenexecuted. Storesadoublelengthnumberintotheaddress. Returnsthedoublelengthcontentsoftheaddress.
Arrays
Asyouknow,thephrase
VARIABLEDATE
A code
createsadefinitionwhichconceptuallylookslikethatattheright. Nowifyousay
1CELLSALLOT
roomfora singlelengthvalue
anadditionalcellisallottedinthedefinition,likethis: 4 D A T E Theresultisthesameasifyouhadused2VARIABLE.Bychangingthe argumenttoALLOT,however,youcandefineanynumberofvariablesunder thesamename.Suchagroupofvariablesiscalledan"array." Forexample,let'ssaythatinourlaboratory,wehavenotjustone,butfive burnersthatheatvariouskindsofliquids. Wecanmakeourword?TOOHOTcheckthatallfiveburnershavenot exceededtheirindividuallimitifwedefineLIMITusinganarrayratherthan aconstant. Let'sgivethearraythenameLIMITS,likethis:
VARIABLELIMITS4CELLSALLOT
becauseLIMITSreturnstheaddressofthefirstcellin thearray.Supposewewantthelimitforburner1tobe
340.Wecanstorethisvaluebyadding1CELLStotheaddressoftheoriginalcell,likethis:
340LIMITS1CELLS+!
Wecanstorelimitsforburners2,3,and4byaddingthe"offsets"2CELLS,3CELLS,and4CELLS, respectively,totheoriginaladdress.Wecandefinetheconvenientword
:LIMIT(burner#addr)CELLSLIMITS+;
totakeaburnernumberonthestackandcomputeanaddressthatreflectstheappropriateoffset. Nowifwewantthevalue170tobethelimitforburner2,wesimplysay
1702LIMIT!
orsimilarly,wecanfetchthelimitforburner2withthephrase
2LIMIT?170ok
ThistechniqueincreasestheusefulnessofthewordLIMIT,sothatwecanredefine?TOOHOTas follows:
:?TOOHOT(tempburner#) LIMIT@>IF."Dangerreduceheat"THEN;
whichworkslikethis:
2100?TOOHOTok 2300?TOOHOTDangerreduceheatok 3001?TOOHOTok 3501?TOOHOTDangerreduceheatok
etc.
AnotherExampleUsinganArrayforCounting
Meanwhile,backattheeggranch: Here'sanotherexampleofanarray.Inthisexample,eachelementofthearrayisusedasaseparate counter.Thuswecankeeptrackofhowmanycartonsof"extralarge"eggsthemachinehaspacked,how many"large,"andsoforth. RecallfromourpreviousdefinitionofEGGSIZE(inChap.4)thatweusedfourcategoriesofacceptable eggs,plustwocategoriesof"badeggs."
0CONSTANTREJECT 1CONSTANTSMALL 2CONSTANTMEDIUM 3CONSTANTLARGE 4CONSTANTEXTRALARGE 5CONSTANTERROR
Solet'screateanarraythatissixcellslong:
VARIABLECOUNTS5CELLSALLOT
Thecountswillbeincrementedusingtheword+!,sowemustbeabletosetalltheelementsofthearray tozerobeforewebegincounting.Thephrase
COUNTS6CELLS0FILL
Nowlet'sdefineawordwhichwillgiveustheaddressofoneofthecounters,dependingonthecategory numberitisgiven(0through5),likethis:
:COUNTERCELLSCOUNTS+;
andanotherwordwhichwilladdonetothecounterwhosenumberisgiven,likethis:
:TALLYCOUNTER1SWAP+!;
willincrementthecounterthatcorrespondstolargeeggs. Nowlet'sdefineawordwhichconvertstheweightperdozenintoacategorynumber:
:CATEGORY(weightcategory) DUP18<IFREJECTELSE DUP21<IFSMALLELSE DUP24<IFMEDIUMELSE DUP27<IFLARGEELSE DUP30<IFEXTRALARGEELSE ERROR THENTHENTHENTHENTHENNIP;
willleavethenumber3(LARGE)onthestack.TheabovedefinitionofCATEGORYresemblesourold
Forexample:
SMALLLABELsmallok
NowwecandefineEGGSIZEusingthreeofourownwords:
:EGGSIZECATEGORYDUPLABELTALLY;
Thusthephrase
23EGGSIZE
willprint
mediumok
(whichwouldtellushowmany"large"cartonswerepacked).Butlet'sgetalittlefancieranddefineour ownwordtoprintatableoftheday'sresultsinthisformat:
QUANTITYSIZE 1reject 112small 132medium 143large 159extralarge 0error
Sincewehavealreadydevisedcategorynumbers,wecansimplyuseaDOandindexonthecategory number,likethis:
:REPORT() PAGE."QUANTITYSIZE"CRCR 60DOICOUNTER@5U.R 7SPACES ILABELCR LOOP;
(Thephrase"ICOUNTER@5U.R"takesthecategorynumbergivenbyI,indexesintothearray, andprintsthecontentsoftheproperelementinafivecolumnfield.)
FactoringDefinitions
ThisisagoodtimetotalkaboutfactoringasitappliestoForthdefinitions.We'vejustseenanexamplein whichfactoringsimplifiedourproblem. OurfirstdefinitionofEGGSIZEfromChap.4,categorizedeggsbyweightandprintedthenameofthe categoriesattheterminal.Inourpresentversionwefactoredoutthe"categorizing"andthe"printing" intotwoseparatewords.WecanusethewordCATEGORYtoprovidetheargumenteitherfortheprinting wordorthecountertallyingword(orboth).Andwecanusetheprintingword,LABEL,inboth EGGSIZEandREPORT. AsCharlesMoore,theinventorofForth,haswritten: AgoodForthvocabularycontainsalargenumberofsmallwords.Itisnotenoughtobreaka problemintosmallpieces.Theobjectistoisolatewordsthatcanbereused. Forexample,intherecipe: Getacanoftomatosauce. Opencanoftomatosauce. Pourtomatosauceintopan. Getcanofmushrooms. Opencanofmushrooms. Pourmushroomsintopan. youcan"factorout"thegetting,opening,andpouring,sincetheyarecommontobothcans.Thenyoucan givethefactoredoutprocessanameandsimplywrite:
TOMATOESADD MUSHROOMSADD
whereCATEGORYpassesthevaluetoLABEL,andLABELpassesitontoTALLY.Certainlythis approachwouldhaveworked.Butthen,whenwedefinedREPORT,wewouldhavehadtosay
ILABELDROP
insteadofsimply
ILABEL
AnotherExample"Looping"throughanArray
We'dliketointroducealittletechniquethatisrelevanttoarrays.Wecanbestillustratethistechniqueby writingourowndefinitionofaForthwordcalledDUMP.DUMPisusedtoprintoutthecontentsofa seriesofmemoryaddresses.Theusageis
addrcountDUMP
Forinstance,wecouldenter
COUNTS6DUMP
Thekeyphrasehereis
CELLSOVER+SWAP
toprintthecontentsofeachelementofthearray.Sinceweareexaminingcells(@fetchesasinglelength, singlecellvalue),weincrementtheindexbyonecelleachtime,byusing
1CELLS+LOOP
ByteArrays
Forthletsyoucreateanarrayinwhicheachelementconsistsofasinglebyteratherthanafullcell.Thisis usefulanytimeyouarestoringaseriesofnumberswhoserangefitsintothatwhichcanbeexpressed withineightbits. Therangeofanunsigned8bitnumberis0to255.BytearraysarealsousedtostoreASCIIcharacter strings.Thebenefitofusingabytearrayinsteadofacellarrayisthatyoucangetthesameamountof datain25%(32bitForth)ofthememoryspace. Themechanicsofusingabytearrayarethesameasusingacellarrayexceptthat 1. youdon'thavetouseCELLStomanipulatetheoffset,sinceeachelementcorrespondstoone addressunit,and 2. youmustusethewordsC!andC@insteadof!and@.Thesewords,whichoperateonbytevalues only,havetheprefix"C"becausetheirtypicaluseisacceptingASCIIcharacters. C! C@ (baddr) (addrb) Storesan8bitvalueintotheaddress. Fetchesan8bitvaluefromtheaddress.
InitializinganArray
Manysituationscallforanarraywhosevaluesneverchangeduringtheoperationoftheapplicationand whichmayaswellbestoredintothearrayatthesametimethatthearrayiscreated,justasCONSTANTs are.ForthprovidesthemeanstoaccomplishthisthroughthetwowordsCREATEand,(pronounced createandcomma). SupposewewantpermanentvaluesinourLIMITSarray.Insteadofsaying
VARIABLELIMITS4CELLSALLOT
wecansay
CREATELIMITS220,340,170,100,190,
YoucanaccesstheelementsinaCREATEarrayjustasyouwouldtheelementsinaVARIABLEarray.
Forexample:
LIMITSCELL+@.340ok
ThiswouldallowustoredefineCATEGORYusingaDOloopratherthanasaseriesofnestedIF...THEN statements,asfollows
:CATEGORY60DODUPSIZESI+C@<IFDROPILEAVETHENLOOP;
Here'salistoftheForthwordswe'vecoveredinthischapter: CONSTANTxxx VARIABLExxx CREATExxx ! @ ? +! ALLOT , C! C@ FILL BASE 2CONSTANTxxx (n) xxx:(n) () xxx:(addr) () xxx:(addr) (naddr) (addrn) (addr) (naddr) (n) (n) (baddr) (addrb) (addrnb) (n) (d) xxx:(d) Createsaconstantnamedxxxwiththevaluen;thewordxxxreturns nwhenexecuted. Createsavariablenamedxxx;thewordxxxreturnsitsaddresswhen executed. Createsadictionaryentry(headandcodepointeronly)namedxxx; thewordxxxreturnsitsaddresswhenexecuted. Storesasinglelengthnumberintotheaddress. Replacestheaddresswithitscontents. Printsthecontentsoftheaddress,followedbyonespace. Addsasinglelengthnumbertothecontentsoftheaddress. Addsnbytestothebodyofthemostrecentlydefinedword. Compilesnintothenextavailablecellinthedictionary. Storesan8bitvalueintotheaddress. Fetchesan8bitvaluefromtheaddress. Fillsnbytesofmemory,beginningattheaddress,withvalueb. Avariablewhichcontainsthevalueofthenumberbasebeingused bythesystem. Createsadoublelengthconstantnamedxxxwiththevalued;the wordxxxreturnsdwhenexecuted.
Createsadoublelengthvariablenamedxxx;thewordxxxreturnsits addresswhenexecuted. Storesadoublelengthnumberintotheaddress. Returnsthedoublelengthcontentsoftheaddress. Compilesbintothenextavailablebyteinthedictionary. Displaysubytesofmemory,startingattheaddress. Storeszeroesintonbytesofmemory,beginningattheaddress. KEY n,n1,... singlelengthsigned d,d1,... doublelengthsigned u,u1,... singlelengthunsigned ud,ud1,... doublelengthunsigned addr address c ASCIIcharactervalue b 8bitbyte f Booleanflag ReviewofTerms
aseriesofmemorylocationswithasinglename.Valuescanbestoredandfetchedintothe individuallocationsbygivingthenameofthearrayandaddinganoffsettotheaddress. Constant avaluewhichhasaname.Thevalueisstoredinmemoryandusuallyneverchanges. asitappliestoprogramminginForth,simplifyingalargejobbyextractingthoseelements Factoring whichmightbereusedanddefiningthoseelementsasoperations. Fetch toretrieveavaluefromagivenmemorylocation. Initialize togiveavariable(orarray)itsinitialvalue(s)beforetherestoftheprogrambegins. anumberwhichcanbeaddedtotheaddressofthebeginningofanarraytoproducethe Offset addressofthedesiredlocationwithinthearray. Store toplaceavalueinagivenmemorylocation. Variable alocationinmemorywhichhasanameandinwhichvaluesarefrequentlystoredandfetched. Array
ProblemsChapter8
1. WritetwowordscalledBAKEPIEandEATPIE.Thefirstwordincreasesthenumber ofavailablePIESbyone.Theseconddecreasesthenumberbyoneandthanksyouforthe pie.Butiftherearenopies,ittypes"Whatpie?"(makesureyoustartoutwithnopies.)
EATPIEWhatpie? BAKEPIEok
EATPIEThankyou!ok
2. WriteawordcalledFREEZEPIESwhichtakesalltheavailablepiesandaddsthemto thenumberofpiesinthefreezer.Rememberthatfrozenpiescannotbeeaten.
BAKEPIEBAKEPIEFREEZEPIESok PIES?0ok FROZENPIES?2ok
returnstheaddressofthecellthatcontainsthecountofredpencils,etc.Thensetthesevariables toindicatethefollowingcounts:
23redpencils 15bluepencils 12greenpencils 0orangepencils
putsan"X"inbox4(countingstartswith1)andproducesthisdisplay:
|| X|| ||
Thenthephrase
3O!
putsan"O"inbox3andprintsthedisplay:
||O X|| ||
Useabytearraytorememberthecontentsoftheboard,withthevalue1tosignify"X,"a1to signifya"O,"anda0tosignifyanemptybox.[answer]
9UndertheHood
Let'sstopforachaptertoliftForth'shoodandseewhatgoesoninside. Someoftheinformationcontainedhereinwe'vegivenearlier,but,attheriskofredundancy,we'renow goingtoviewtheForth"machine"asawhole,toseehowitallfitstogether.
InsideINTERPRET
Backinthefirstchapterwelearnedthatthetextinterpreter,whosenameisINTERPRET,pickswordsout oftheinputstreamandtriestofindtheirdefinitionsinthedictionary.Ifitfindsaword,INTERPREThas itexecuted. Wecanperformtheseseparateoperationsourselvesbyusingwordsthatperformthecomponentfunctions ofINTERPRET.Forinstance,theword'(pronouncedtick)findsadefinitioninthedictionaryand returnsitsexecutiontoken.IfwehavedefinedGREETaswedidinChap.1,wecannowsay
'GREETU.4956608ok
'GREETEXECUTEHello,IspeakForthok
tofindoutwhetherGREEThasbeendefined,withoutactuallyhavingtoexecuteit(itwilleitherprintthe xtorrespondwithanerror).
YoucanalsousethexttoDUMPthecontentsofthedefinition,likethis:
'GREET12CELLSDUMP A054620:6813400000000000603D030A1548656C h.@.....`=...Hel A054630:6C6F2C204920737065616B20466F7274lo,Ispeak Fort A054640:68202020380241000000000000000000h 8.A......... ok
Oryoucanuseticktoimplementsomethingcalled"vectoredexecution."Whichbringsustothenext section...
VectoredExecution
Whileitsoundshairy,theideaofvectoredexecutionisreallyquitesimple.Insteadofexecutinga definitiondirectly,aswedidwiththephrase
'GREETEXECUTE
wecanexecuteitindirectlybykeepingitsxtinavariable,thenexecutingthecontentsofthevariable,like this:
'GREETpointer! pointer@EXECUTE
Alternatively,ifweexecutethephrase
'GOODBYE'aloha!
tostorethextofGOODBYEinto'aloha,wewillget
ALOHAGoodbyeok
thenenter
SAYHELLOok ALOHAHellook
or
SAYGOODBYEok ALOHAGoodbyeok
Nowwecansay
COMINGok ALOHAHellook GOINGok ALOHAGoodbyeok
TheStructureofaDictionaryEntry
Alldefinitions,whethertheyhavebeendefinedby:,byVARIABLE,byVALUE,byCREATE,orbyany other"definingword,"sharethesebasicparts:
namefield linkfield codepointerfield datafield
UsingthevariableDATEasanexample,here'showthesecomponentsarearrangedwithineach dictionaryentry.Inthisdiagram,eachhorizontallinerepresentsonecellinthedictionary:
Name
Inourexample,thefirstbytecontainsthenumberofcharactersinthefullnameofthedefinedword (therearefourlettersinDATE).ThenextfourbytescontaintheASCIIrepresentationsofthefourletters inthenameofthedefinedword. Noticethe"precedencebit"inthediagram.Thisbitisusedduringcompilationtoindicatewhetherthe wordissupposedtobeexecutedduringcompilation,ortosimplybecompiledintothenewdefinition. MoreonthisinChap.11.
Link
The"link"cellcontainstheaddressofthepreviousdefinitioninthedictionarylist.Thelinkcellcanbe usedinlinearlysearchingthedictionary.Tosimplifythingsabit,imaginethatitworksthisway: Eachtimethecompileraddsanewwordtothedictionary,hesetsthelinkfieldtopointtothe addressofthepreviousdefinition.HereheissettingthelinkfieldofCUISINARTtopointto thedefinitionofCAR. Atsearchtime,tick(orbrackettickbracket,etc.)startswiththemostrecentwordandfollows the"chain"backwards,usingtheaddressineachlinkcelltolocatethenextdefinitionback.
Thelinkfieldofthefirstdefinitioninthedictionarycontainsazero,whichtellsticktogiveup;theword isnotinthedictionary.
CodePointer
Nextisthe"codepointer."Thextcontainedinthispointeriswhatdistinguishesavariable fromaconstantoracolondefinition.Itistheaddressoftheinstructionthatisexecutedfirst whenaparticulartypeofwordisexecuted.Conceptually,inthecaseofavariable,thepointerpointsto codethatpushestheaddressofthevariableonthedatastack.Inthecaseofaconstant,thepointerpoints tocodethatpushesthecontentsoftheconstantonthedatastack.Inthecaseofacolondefinition,the pointerpointstocodethatexecutestherestofthewordsinthecolondefinition.Inpracticethereare manywaystoimplementthisconcept,includingnativecoderealizations. Thecodethatispointedtoiscalledthe"runtimecode"becauseitisusedwhenawordofthattypeis executed(notwhenawordofthattypeisdefinedorcompiled). Allvariables(conceptually)havethesamecodepointer;allconstantshavethesamecodepointeroftheir own,andsoon.
Datafield
Followingthecodepointeristhedatafield.Invariablesandconstants,thedatafieldisonlyonecell.Ina 2VARIABLEora2CONSTANT,thedatafieldistwocells.Inanarray,thedatafieldcanbeaslongas youwantit.Inacolondefinition,thelengthofthedatafielddependsonthelengthofthedefinition,as we'llexplaininthenextsection.Strictlyspeaking,thecolondefinitionofamodernForthdoesnothavea datafield. ThextthatissuppliedbytickandexpectedbyEXECUTEisthecodepointerdefinedabove.The beginningofthedatafieldcanbefoundwith>BODY,awordthatcomputesthedatafieldgivenanxt. >BODYdoesnotworkforcolondefinitions.SomeForthsmayevenforbidtheuseof>BODYonany systemdatastructure(variablesconstants,user,etc.).
TheBasicStructureofaColonDefinition
Whiletheformatoftheheadandcodepointeristhesameforalltypesofdefinitions,theformatofthe datafieldvariesfromtypetotype.Let'slookatthedatafieldofacolondefinition. Thedatafieldofacolondefinitioncontainsthextsofthepreviouslydefinedwordswhichcomprisethe definition.HereisthedictionaryentryforthedefinitionofPHOTOGRAPH,whichisdefinedas
:PHOTOGRAPHSHUTTEROPENTIMEEXPOSESHUTTERCLOSE;
NestedLevelsofExecution
ThefunctionofEXITistoreturntheflowofexecutiontothenexthigherleveldefinitionthatreferstothe currentdefinition.Let'sseehowthisworksinsimplifiedterms. SupposethatDINNERconsistsofthreecourses:
:DINNERSOUPENTREEDESSERT;
andthattonight'sENTREEconsistssimplyof
:ENTREECHICKENRICE;
OneStepBeyond
Nowyou'reofcoursewondering:whathappenswhenwefinallyexecutetheEXITinDINNER.Whose returnaddressisonthereturnstack?Whatdowereturnto? Well,rememberthatDINNERhasjustbeenexecutedbyEXECUTE,whichisacomponentof INTERPRET.INTERPRETisaloopwhichcheckstheentireinputstream.Assumingthatweentered afterDINNER,thenthereisnothingmoretointerpret.SowhenweexitINTERPRET,where doesthatleaveus?Intheoutermostdefinitionofeachterminal,calledQUIT. QUIT,insimplifiedform,lookslikethis:
:QUITBEGIN(clearreturnstack) (acceptinput) INTERPRET ."ok"CR AGAIN;
whyQUITcanbeusedtokeepthemessage"ok"fromappearingatourterminal. ThedefinitionofABORT"usesQUIT.
AbandoningtheNest
It'spossibletoincludeEXITinthemiddleofadefinition.Forexample,ifweweretoredefineENTREE asfollows:
:ENTREECHICKENEXITRICE;
ForthGeography
ThisisthememorymapofatypicalForthsystem:
SystemVariables
Thissectionofmemorycontains"systemvariables"whicharecreatedbythebasicForthcoreandused bytheentiresystem.Theyarenotgenerallyusedbytheuser.
UserDictionary
Thedictionarywillgrowintohighermemoryasyouaddyourowndefinitions.Thenextavailablecellin thedictionaryatanytimeispointedtobyavariablecalledCP.Duringtheprocessofcompilation,the pointerCPisadjustedcellbycellastheentryisbeingaddedtothedictionary.ThusCPisthecompiler's bookmark;itpointstotheplaceinthedictionarywherethecompilercannextcompile. CPisalsousedbythewordALLOT,whichadvancesCPbythenumberofbytesgiven.Forexample,the phrase
5CELLSALLOT
addstwentytoCPsothatthecompilerwillleaveroominthedictionaryforafivecellarray. ArelatedwordisHERE,whichissimplydefinedas
:HERECP@;
toputthevalueofCPonthestack.Theword,(comma),whichstoresasinglelengthvalueintothenext availablecellinthedictionary,issimplydefined
:,HERE!CELLALLOT;
indicatesthatthedefinitionsloadedbythefilerandom.frtfilled196bytesofmemoryspaceinthe dictionary.
ThePad
AtacertaindistancefromHEREinyourdictionary,youwillfindasmallregionofmemorycalledthe "pad."Likeascratchpad,itisusuallyusedtoholdASCIIcharacterstringsthatarebeingmanipulated priortobeingsentouttoaterminal.Forexample,thenumberformattingwordsusethepadtoholdthe ASCIInumeralsduringtheconversionprocess,priortoTYPE. Thesizeofthepadisindefinite.Inmostsystemstherearehundredsofkilobytesbetweenthebeginning ofthepadandthetopoftheparameterstack. Sincethepad'sbeginningaddressisdefinedrelativetothelastdictionaryentry,itmoveseverytimeyou addanewdefinitionorexecuteFORGETorMARKER.Thisarrangementprovessafe,however,because thepadisneverusedwhenanyoftheseeventsareoccurring.ThewordPADreturnsthecurrentaddress ofthebeginningofthepad.Itisdefinedsimply:
:PADHERE340+;
thatis,itreturnsanaddressthatisafixednumberofbytesbeyondHERE.(Theactualnumbervaries.)
ParameterStack
Farabovethepadinmemoryistheareareservedfortheparameterstack.Althoughweliketoimagine thatvaluesactuallymoveupordownsomewhereaswe"popthemoff"and"pushthemon,"inreality nothingmoves.Theonlythingthatchangesisapointertothe"top"ofthestack. Asyoucanseebelow,whenwe"putanumberonthestack,"whatreallyhappensisthatthepointeris "decremented"(sothatitpointstothenextavailablelocationtowardslowmemory),thenournumberis storedwherethepointerispointing.Whenwe"removeanumberfromthestack,"thenumberisfetched fromthelocationwherethepointerispointing,thenthepointerisincremented.Anynumbersabovethe stackpointeronourmaparemeaningless.
fetchesthecontentsofthetopofstack.Thisoperation,ofcourse,isidenticaltothatofDUP.Ifwehad fivevaluesonthestack,wecouldcopythefifthonedownwiththephrase
SP@4CELLS+@
InputMessageBuffer
TIBcontainsthestartingaddressforthe"inputmessagebuffer,"or"TerminalInputBuffer,"whichgrows towardshighmemory(thesamedirectionasthepad).Whenyouentertextfromtheterminal,itgets storedintothisbufferwherethetextinterpreterwillscanit.
ReturnStack
Abovethebufferresidesthereturnstack,whichoperatesidenticallytotheparameterstack.Thereareno highlevelForthwordsanalogoustoSP0andSP@thatrefertothereturnstack.
UserVariables
Thenextsectionofmemorycontains"uservariables."ThesevariablesincludeBASE,SP0,andmany othersthatwe'llcoverinanupcomingsection. ThiscompletesourjourneyacrossthememorymapofatypicalForthsystem.Herearethewordswe've justcoveredthatrelatetomemoryregionsintheForthsystem: HERE (addr) Returnsthenextavailabledictionarylocation. Returnsthebeginningaddressofascratchpadareausedtoholdcharacter stringsforintermediateprocessing. Uservariable.ReturntheaddressofthetopofthestackbeforeSP@is SP@ (addr) executed. PAD (addr) SP0 (addr) Uservariable.Containstheaddressofthebottomoftheparameterstack.
UserVariables
Thefollowinglistshowsmostoftheuservariables.Somewewon'tevermentionagain.Don'ttryto memorizethistable.Justrememberwhereyoucanfindit. TIB Containstheaddressofthestartoftheterminalinputbuffer. #TIB Containsthesizeoftheterminalinputbuffer. SCR Apointertothecurrentblocknumber(setbyLIST).
Ifnonzero,apointertotheblockbeinginterpretedbyLOAD.Azeroindicates interpretationfromtheterminal(viatheinputmessagebuffer). Uservariablesarenotlikeordinaryvariables.Withanordinaryvariable(onedefinedbytheword VARIABLE),thevalueiskeptinthebodyofthedictionaryentry.Eachuservariable,ontheotherhand, iskeptinanarraycalledthe"usertable."Thedictionaryentryforeachuservariableislocatedelsewhere; itsbodycontainsanoffsetintotheusertable.Whenyouexecutethenameofauservariable,suchasCP, thisoffsetisaddedtothebeginningaddressoftheusertable,allowingyoutouse@or!inthenormal way. Themainadvantageofuservariablesisthatanynumberoftaskscanusethesamedefinitionofavariable andeachgetitsownvalue(becauseeachtaskhasnotonlyitsownstacks,butalsoitsownusertable). Eachtaskthatexecutes
BASE@
getsthevalueforBASEfromitsownusertable.Thissavesalotofroominthesystemwhilestill allowingeachtasktoexecuteindependently. UservariablesaredefinedbythewordUSER.Thesequenceofuservariablesinthetableandtheiroffset valuesvaryfromonesystemtoanother. Tosummarize,therearethreekindsofvariables:SystemvariablescontainvaluesusedbytheentireForth system.Uservariablescontainvaluesthatareuniqueforeachtask,eventhoughthedefinitionscanbe usedbyalltasksinthesystem.Regularvariablescanbeaccessibleeithersystemwideorwithinasingle taskonly. Here'salistoftheForthwordswe'vecoveredinthischapter: 'xxx (addr) Attemptstofindtheexecutiontokenofxxx(thewordthatfollowsinthe inputstream)inthedictionary.
[']
compiletime () Usedonlyinacolondefinition,compilestheexecutiontokenofthe runtime nextwordinthedefinitionasaliteral. (addr) Executesthedictionaryentrywhoseexecutiontokenisonthestack. Whencompiledwithinacolondefinition,terminatesexecutionatthat point. Clearsallstacksandreturnscontroltotheterminal.Nomessageis given. Returnsthenextavailabledictionarylocation.
Returnsthebeginningaddressofascratchpadareausedtohold characterstringsforintermediateprocessing. Uservariable.Apointertothecurrentblocknumber(setbyLIST). Uservariable.Numberconversionbase. Uservariable.ReturntheaddressofthetopofthestackbeforeSP@is executed. Uservariable.Containstheaddressofthestartoftheterminalinput buffer. Uservariable.Containsthesizeoftheterminalinputbuffer. Uservariable.Containstheaddressofthebottomoftheparameter stack. Uservariable.Apointertothecurrentpositionintheinputstream. Uservariable.Ifnonzero,apointertotheblockbeinginterpretedby LOAD.Azeroindicatesinterpretationfromtheterminal(viatheinput messagebuffer). ReviewofTerms
ThesecondofForth'stwointerpreters,theonewhichexecutesthedata(listofaddresses, listofcalls,machinecode,...)foundinthedictionaryentryofacolondefinition.The addressinterpreteralsohandlesthenestingofexecutionlevelsforwordswithinwords. Body thecodeanddatafieldofaForthdictionaryentry. Cfa codefieldaddress;theaddressofadictionaryentry'scodepointerfield. thecellinadictionaryentrywhichsomehowpointsoutthextoftheruntimecodeforthis Codepointer particulartypeofdefinition.Forexample,inadictionaryentrycompiledby:,thefield field wouldpointouttheaddressinterpreter. aForthwordwhichcreatesadictionaryentry.Examplesinclude:,CONSTANT, Defining word VARIABLE,etc. Head thenameandlinkfieldsofaForthdictionaryentry. Input theregionofmemorywithinaterminaltaskthatisusedtostoretextasitarrivesfromthe message terminal.Incomingsourcetextisinterpretedhere. buffer thecellinadictionaryentrywhichcontainstheaddressofthepreviousdefinition,usedin Linkfield searchingthedictionary. theareaofadictionaryentrywhichcontainsthenameofthedefinedword,alongwiththe Namefield numberofcharactersinthename. theregionofmemorywithinaterminaltaskthatisusedasascratchareatoholdcharacter Pad stringsforintermediateprocessing. Datafield theareaofadictionaryentrywhichcontainsthe"contents"ofadefinition:fora Address interpreter
CONSTANT,thevalueoftheconstant,foraVARIABLE,thevalueofthevariable;fora colondefinition,thelistofxtsofwordsthataretobeexecutedinturnwhenthedefinition isexecuted. aroutine,compiledinmemory,whichspecifieswhathappenswhenamemberofagiven Runtime classofwordsisexecuted.Theruntimecodeforacolondefinitionistheaddress code interpreter;theruntimecodeforavariablepushestheaddressofthevariable'sbodyon thestack. System oneofasetofvariablesprovidedbyForth,whicharereferredtosystemwide(byany variable task).Contrastwith"uservariables.". inForth,apartitioninmemorythatcontainsatminimumaparameterandareturnstack Task andasetofuservariables. oneofasetofvariablesprovidedbyForth,whosevaluesareuniqueforeachtask. Uservariable Contrastwith"systemvariables." themethodofspecifyingcodetobeexecutedbyprovidingnottheaddressofthecode Vectored itself,buttheaddressofalocationwhichcontainsthextofthecode.Thislocationisoften execution called"thevector."Ascircumstanceschangewithinthesystem,thevectorcanberesetto pointtosomeotherpieceofcode.
ProblemsChapter9
1. FirstreviewChap.2,Prob.6.Withoutchanginganyofthosedefinitions,writeawordcalled COUNTSwhichwillallowthejudgetooptionallyenterthenumberofcountsforanycrime.For instance,theentry
CONVICTEDOFBOOKMAKING3COUNTSTAXEVASIONWILLSERVE yearsok 17
and
'DATE.
Whatisthedifferencebetweenthesetwophrases:
BASE.
and
'BASE.
[answer] 5. Inthisexerciseyouwillcreatea"vectoredexecutionarray,"thatis,anarraywhichcontainsxtsof Forthwords.Youwillalsocreateanoperationwordwhichwillexecuteonewordstoredinthe arraywhentheoperationwordisexecuted. Defineaonedimensionalarrayofcellswhichwillreturnthenthelement'saddresswhengivena subscriptn.Defineseveralwordswhichoutputsomethingatyourterminalandtakenoinputs. Storethextsoftheseoutputwordsinvariouselementsofthearray.Storetheaddressofado nothingwordinanyremainingelementsofthearray.Defineawordwhichwilltakeavalidarray indexandexecutethewordwhoseaddressisstoredinthereferencedelement. Forexample,
1DOSOMETHINGHello,IspeakForth.ok 2DOSOMETHING12345678910ok 3DOSOMETHING ********** ********** ********** ********** **********ok 4DOSOMETHINGok 5DOSOMETHINGok [answer]
10I/OandYou
Inthischapter,we'llexplainhowForthhandlesI/O terminal. ofcharacterstringstoandfromdiskandthe Specifically,we'lldiscussdiskaccesscommands,outputcommands,stringmanipulationcommands, inputcommands,andnumberinputconversion.
OutputOperators
ThewordEMITtakesasingleASCIIrepresentationonthestack,usingtheloworderbyteonly,and printsthecharacteratyourterminal.Forexample,indecimal:
65EMITAok 66EMITBok
ThewordTYPEprintsanentirestringofcharactersatyourterminal,giventhestartingaddressofthe stringinmemoryandthecount,inthisform:
(addru)
We'vealreadyseenTYPEinournumberformattingdefinitionswithoutworryingabouttheaddressand count,becausetheyareautomaticallysuppliedby#>.
Let'sgiveTYPEanaddressthatweknowcontainsacharacterstring.Rememberthatthestartingaddress oftheterminalinputbufferisreturnedbyTIB?Supposeweenterthefollowingcommand:
TIB#TIB@TYPE
Thiswilltype15charactersfromtheterminalinputbuffer,whichcontainsthecommandwejustentered:
TIB#TIB@TYPE TIB#TIB@TYPEok
where
'TEST>BODY
givesusthebodyaddressofTEST,
CELL+1+
offsetsuspasttheaddressandthecount,tothebeginningofthestring(theletter"s"),and
7TYPE
Onceweenter
'"LABEL">BODYCELL+1+
Nowlet'sredefineLABELsothatittakesacategorynumberfromzerothroughfiveandusesittoindex intothestringarray,likethis:
:LABEL8*[']"LABEL">BODYCELL+1++8TYPESPACE;
OutputtingStringsfromDisk
WementionedbeforethatthewordBLOCKcopiesagivenblockintoanavailablebufferandleavesthe addressofthebufferonthestack.Usingthisaddressasastartingpoint,wecanindexintooneofthe buffer's1,024bytesandtypeanystringwecareto.Forexample,toprintline0ofblock1,wecouldsay (assumingyou'veexecutedUSEblocks.fb)
CR1BLOCK64TYPE ok
Toprintlineeight,wecouldadd512(8x64)totheaddress,likethis:
CR1BLOCK512+64TYPE
TRAILINGcanbeusedimmediately HandyHint beforetheTYPEcommandsothat trailingblankswillnotbeprinted.For ARandomNumberGenerator instance,insertingitintoourfirst exampleabovewouldgiveus Thissimplerandomnumbergeneratorcanbeusefulforgames, althoughformoresophisticatedapplicationssuchassimulations, CR1BLOCK64 TRAILINGTYPE betterversionsareavailable.
ok (RandomnumbergenerationHighlevel) VARIABLErndHERErnd! :RANDOMrnd@31421*6927+DUPrnd!; :CHOOSE(u1u2)RANDOMUM*NIP; (whereCHOOSEreturnsarandominteger withintherange0=or<u2<u1.)
ThefollowingexampleusesTYPE
USEblocks.fb :POOF 16CHOOSE64* 2BLOCK+ CR64TRAILING TYPE;
tryit:
POOF qualifiedok POOF flexibleok POOF totalok
andCHOOSEwillleavetherandomnumberonthestack.
InternalStringOperators
Thecommandsformovingcharacterstringsordataarraysareverysimple.Eachrequiresthree arguments:asourceaddress,adestinationaddress,andacount. Copiesaregionofmemoryubyteslong,bytebybytebeginningat CMOVE (addr1addr2u) addr1,tomemorybeginningataddr2.Themovebeginswiththe contentsofaddr1andproceedstowardhighmemory. Ifuisgreaterthanzero,copyuconsecutivecharactersfromthedata CMOVE (addr1addr2u) spacestartingatcaddr1tothatstartingatcaddr2,proceeding > characterbycharacterfromhigheraddressestoloweraddresses. Afterthismove,theubytesataddr2containexactlywhattheu MOVE (addr1addr2u) bytesataddr1containedbeforethemove(no"clobbering"occurs). Noticethatthesecommandsfollowcertainconventionswe'veseenbefore:
TomovetheentirecontentsofabufferintothePAD,forexample,wewouldwrite
210BLOCKPAD1024CMOVE
althoughoncelladdressmachinesthemovemightbemadefasterifitwerecellbycell,likethis:
210BLOCKPAD1024MOVE
ThewordCMOVE>letsyoumoveastringtoaregionthatishigherinmemorybutthatoverlapsthe sourceregion. IfyouweretouseCMOVE,thefirstletterofthestringwouldgetcopiedtothe secondbyte,butthatwould"clobber"thesecondletterofthestring.Thefinal resultwouldbeastringcomposedofasinglecharacter. UsingCMOVE>inthissituationkeepsthestringfromclobberingitselfduringthemove. YouprobablynoticethatCMOVEcanbeusedtofillanarraywithacertainbyte.Onoldersystemsthe wordFILL,whichweintroducedearlier,mayhavebeendefinedusingthistrick.OnmodernForthsitis recommendedtoexplicitlyuseFILL,iffilliswhatyouwanttodo.Forexample,tostoreblanksinto1024 bytesofthepad,wesay
PAD1024CHARBLFILL
SinglecharacterInput
ThewordKEYawaitstheentryofasinglekeyfromyourterminalkeyboardandleavesthecharacter's ASCIIequivalentonthestackintheloworderbyte. Toexecuteitdirectly,youmustfollowitwithareturn,likethis:
KEY
InthiscasewedropthevalueleftbyKEYbecausewedon'tcarewhatitis.
StringInputCommands,fromtheBottomup
Thereareseveralwordsinvolvedwithstringinput.We'llstartwiththelowerleveloftheseandproceedto somehigherlevelwords.Herearethewordswewillcoverinthissection: Receivesucharacters(oracarriagereturn)fromtheterminal ACCEPT (caddru1u2) keyboardandstoresthem,startingattheaddress.Thecountof receivedcharactersisreturned. Readsonewordfromtheinputstream,usingthecharacter(usually WORD (caddr) blank)asadelimiter.Movesthestringtotheaddress(HERE)with thecountinthefirstbyte,leavingtheaddressonthestack. ThewordACCEPTstopsexecutionofthetaskandwaitsforinputfromyourkeyboard.Itexpectsagiven numberofkeystrokesoracarriagereturn,whichevercomesfirst.Theincomingtextisstoredbeginningat theaddressgivenasanargument,thecountofreceivedcharactersisreturnedonthestack. Forexample,thephrase
TIB80ACCEPT
buthowdoesthetextinterpreterscantheterminalinputbufferandpickouteachindividualwordthere? Withthephrase
BLWORD
WORDlooksforthegivendelimiterintheterminalinputbuffer,andmovesthesubstringtoWORD's bufferwiththecountinthefirstbyte. Whenyouareexecutingwordsdirectlyfromaterminal,WORDwillscantheinputbuffer,startingatTIB. Asitgoesalong,itadvancestheinputbufferpointer,called>IN,sothateachtimeyouexecuteWORD, youscanthenextwordintheinputstream.WORDknowstostopscanningwhen>IN@becomeslarger than#TIB@,thecountofreceivedcharacters. >INisa"relativepointer";thatis,itdoesnotcontaintheactualaddressbutratheranoffsetthatistobe addedtotheactualaddress,whichisisinthiscaseTIB.Forexample,afterWORDhasscannedthestring "STAR,"thevalueof>INisfive. WORDignoresinitialoccurencesofthedelimiter(untilanyothercharacterisencountered).Youcould type
STAR
Inthefirstlinewedefineanarraycalledmyname.InthesecondlinewedefineawordcalledI'M whichwillallowustoenter
I'MEDWARDok
ThedefinitionofI'Mbreaksdownasfollows:thephrase
BLTEXT
:GREET."Hello,"myname40TRAILINGTYPE.",IspeakForth.";
sothatbyexecutingGREET,weget
GREETHello,EDWARD,IspeakForth.ok
Whichallowsyoutoenter
VITALSAlice,blue,Fredok
thenenter
LETTER
atwhichpointexecutionstopssotheusercanenteraname:
GREET What'syourname?TravisMcGee Hello,TravisMcGee,IspeakForth.ok
Wecoulddothisasfollows:
:GREETCR."What'syourname?" TIB40ACCEPT#TIB!0>IN! 1TEXTCR."Hello," PAD40TRAILINGTYPE.",IspeakForth.";
We'veexplainedallthephrasesintheabovedefinitionexceptthisone:
#TIB!0>IN!
NumberInputConversion
Whenyoutypeanumberatyourterminal,Forthautomaticallyconvertsthischaracterstringintoabinary valueandpushesitontothestack.Forthalsoprovidesacommandwhichletyouconvertacharacter stringthatbeginsatanymemorylocationintoabinaryvalue. ud2istheunsignedresultofconvertingthecharacterswithinthe stringspecifiedbycaddr1u1intodigits,usingthenumberin BASE,andaddingeachintoud1aftermultiplyingud1bythe numberinBASE.Conversioncontinueslefttorightuntila (ud1caddr1u1 >NUMBER characterthatisnotconvertible,includingany"+"or"",is ud2caddr2u2) encounteredorthestringisentirelyconverted.caddr2isthe locationofthefirstunconvertedcharacterorthefirstcharacter pasttheendofthestringifthestringwasentirelyconverted.u2is thenumberofunconvertedcharactersinthestring. Here'sanexamplethatuses>NUMBER:
:PLUS0.BLWORDCOUNT>NUMBER2DROPDROP+."=".;
PLUSallowsustoprovetoanyskepticthatForthcoulduseinfixnotationifitwantedto.Wecanenter
2PLUS13
=15ok
WhenPLUSisexecuted,the"2"willbeputonthestackinbinaryform,whilethe"3"willstillbeinthe inputstreamasastring.Thephrase
0.BLWORD
readsthestringandprovidestheaccumulatorfor>NUMBER;>NUMBERconvertsittobinaryandputs thedoublelengthresultplusanunconvertedstringonthestack.Wedropthestringandthetophalfofthe doublelengthresult.Now+addsthetwosinglelengthvaluesand.printstheresult. Notethatyoucanuse>NUMBERtocreateyourownspecializednumberinputconversionroutines.Since >NUMBERreturnstheaddressofthefirstunconvertiblecharacter,youcanmakedecisionsbasedon whetherthecharacterisahyphen,dot,orwhatever.Youcanalsomakedecisionsbasedonthelocationof thenonconvertiblecharacterwithinthenumber.Forinstance,youcanwritearoutinethatletsyouentera numberwithadecimalpointinitandthenscalesitaccordingly. Togiveagoodexampleoftheuseof>NUMBER,Figure101showsadefinitionofNUMBER.This versionreadsanyofthecharacters
:,./
HereweusethevariablePUNCTtocontainaflagthatindicateswhetherpunctuationwasencountered. Wesuggestthatyouuseanavailableuservariableinstead. Figure101.ADefinitionofNUMBER Createsaflagthatwillcontain trueifthenumbercontains validpunctuation. Initializeflag,nopunctuation hasoccured. Getthefirstdigit. Isitaminussign? Savetheflagonthereturn stack. Ifthefirstcharacteris"",adds 1totheaddressand decrementsthecharacter count.Thiseffectivelyskips the""character,pointingto
VARIABLEpunct
0.2SWAP BEGIN >NUMBER DUP WHILE OVERC@DUP[CHAR]:= SWAP[CHAR],[CHAR]/1+WITHINOR DUPpunct! 0=ABORT"?" 1/STRING
therealfirstdigit. providesthedoublelength zeroasanaccumulator. Beginsconversion;converts untilaninvaliddigit. Whiletherearestillcharacters left,fetchtheinvaliddigit. acolon,or acomma,hyphen,periodor slash. Setpuncttoindicatewhether validpunctuationhasoccurred. Otherwiseissueanerror message. Skipthepunctuationcharacter. Exitshereifablankis detected;otherwiserepeats conversion. Dropthestringfromthestack. Iftheflagonthereturnstackis true,negatesd. Iftherewasnopunctuation, returnsasinglelengthvalueby droppingthehighordercell.
REPEAT
2DROP R>IFDNEGATETHEN
punct@0=IFDROPTHEN;
ACloserLookatWORD
SofarwehaveonlytalkedaboutusingWORDtoscantheterminalinputbuffer(whichholdsthe charactersthatareACCEPTedfromtheterminal).Butifwerecallthatthephrase
BLWORD
wordinthefirstbyteofWORD'sbufferandalsoleavestheaddressofthisbyteonthestack. ThewordCOUNTputsthecountonthestackandincrementstheaddress,likethis: leavingthestackwithastringaddressandacountasappropriateargumentsforTYPE,MOVE,etc. COUNTisusedinthedefinitionofTEXTwhichwegaveafewsectionsback. Convertsacharacterstring,whoselengthiscontainedinitsfirst COUNT (addraddr+1u) byte,intotheformappropriateforTYPE,byleavingtheaddressof thefirstcharacterandthelengthonthestack. WewillfurtherillustratetheuseofWORDinoneoftheexamplesinChap.12.
StringComparisons
HereisaForthwordthatyoucanusetocomparecharacterstrings: Comparethestringspecifiedbycaddr1andu1tothestring specifiedbycaddr2andu2.Thestringsarecompared,beginning atthegivenaddresses,characterbycharacteruptothelengthof theshorterstring,oruntiladifferenceisfound.Ifbothstringsare (caddr1u1 thesameuptothelengthoftheshorterstring,thenthelonger COMPARE caddr2u2n) stringisgreaterthantheshorterstring.nis1ifthestring specifiedbycaddr1andu1islessthanthestringspecifiedbyc addr2andu2.niszeroifthestringsareequal.nis1ifthestring specifiedbycaddr1andu1isgreaterthanthestringspecifiedby caddr2andu2. COMPAREcanbeusedtotestwhethertwocharacterstringsareequalorwhetheroneisalphabetically greaterorlesserthantheother. Here'salistoftheForthwordswe'vecoveredinthischapter: Transmitsucharacters,beginningataddress,tothecurrent outputdevice. Eliminatestrailingblanksfromthestringthatstartsatthe TRAILING (addru1addru2) addressbyreducingthecountfromu1(originalbytecount) tou2(shortenedbytecount). Afterthismove,theubytesataddr2containexactlywhat MOVE (addr1addr2u) theubytesataddr1containedbeforethemove(no "clobbering"occurs). Copiesaregionofmemoryubyteslong,bytebybyte beginningataddr1,tomemorybeginningataddr2.The CMOVE (addr1addr2u) movebeginswiththecontentsofaddr1andproceedstoward highmemory. TYPE (addru)
KEY ACCEPT
(c) (caddru1u2)
WORD
(caddr)
>NUMBER
(ud1caddr1u1 ud2caddr2u2)
COUNT
(addraddr+1u)
CMOVE>
(addr1addr2u)
COMPARE
(caddr1u1 caddr2u2n)
BLANK
(addru)
ReturnstheASCIIvalueofthenextavailablecharacterfrom thecurrentinputdevice. Receivesucharacters(oracarriagereturn)fromthe terminalkeyboardandstoresthem,startingattheaddress. Thecountofreceivedcharactersisreturned. Readsonewordfromtheinputstream,usingthecharacter (usuallyblank)asadelimiter.Movesthestringtothe address(HERE)withthecountinthefirstbyte,leavingthe addressonthestack. ud2istheunsignedresultofconvertingthecharacters withinthestringspecifiedbycaddr1u1intodigits,using thenumberinBASE,andaddingeachintoud1after multiplyingud1bythenumberinBASE.Conversion continueslefttorightuntilacharacterthatisnot convertible,includingany"+"or"",isencounteredorthe stringisentirelyconverted.caddr2isthelocationofthe firstunconvertedcharacterorthefirstcharacterpasttheend ofthestringifthestringwasentirelyconverted.u2isthe numberofunconvertedcharactersinthestring. Convertsacharacterstring,whoselengthiscontainedinits firstbyte,intotheformappropriateforTYPE,byleaving theaddressofthefirstcharacterandthelengthonthestack. Ifuisgreaterthanzero,copyuconsecutivecharactersfrom thedataspacestartingatcaddr1tothatstartingatcaddr2, proceedingcharacterbycharacterfromhigheraddressesto loweraddresses. Comparethestringspecifiedbycaddr1andu1tothestring specifiedbycaddr2andu2.Thestringsarecompared, beginningatthegivenaddresses,characterbycharacterup tothelengthoftheshorterstring,oruntiladifferenceis found.Ifbothstringsarethesameuptothelengthofthe shorterstring,thenthelongerstringisgreaterthanthe shorterstring.nis1ifthestringspecifiedbycaddr1and u1islessthanthestringspecifiedbycaddr2andu2.nis zeroifthestringsareequal.nis1ifthestringspecifiedby caddr1andu1isgreaterthanthestringspecifiedbyc addr2andu2. StoreASCIIblanksintoubytesofmemory,beginningat addr. ReviewofTerms
Virtual memory
ProblemsChapter10
1. Entersomefamousquotationsintoanavailableblock,say3.NowdefineawordcalledCHANGE whichtakestwoASCIIvaluesandchangesalloccurrenceswithinblock3ofthefirstcharacter intothesecondcharacter.Forexample,
CHARACHARECHANGE
willchangeallthe"A"sinto"E"s.[answer] 2. DefineawordcalledFORTUNEwhichwillprintapredictionatyourterminal,suchas"Youwill receivegoodnewsinthemail."Thepredictionshouldbechosenatrandomfromalistofsixteen orfewerpredictions.Eachpredictionissixtyfourcharacters,orless,long.[answer] 3. AccordingtoOrientallegend,Buddhaendowsallpersonsbornineachyearwithspecial,helpful characteristicsrepresentedbyoneoftwelveanimals.Adifferentanimalreignsovereachyear,and everytwelveyearsthecyclerepeatsitself.Forinstance,personsbornin1900aresaidtobeborn inthe"YearoftheRat."Theartoffortunetellingbasedontheseinfluencesofthenatalyearis called"Juneeshee." Hereistheorderofthecycle:
RatOxTigerRabbitDragonSnakeHorseRamMonkeyCockDogBoar
Writeawordcalled.ANIMALthattypesthenameoftheanimalcorrespondingtoitspositionin thecycleaslistedhere;e.g.,
0.ANIMALRATok
NowdefineLETTERSsothatitprintsoneletterforeachpersoninyourfile. 5. Inthisexerciseyouwillcreateanduseavirtualarray,thatis,anarraywhichresidesondiskbut
whichisreferencedlikeamemoryresidentarray(with@and!). Firstselectanunusedblock.Putthisblocknumberinavariable.Thendefineanaccessword whichacceptsacellsubscriptfromthestack,thencomputestheblocknumbercorrespondingto thissubscript,callsBLOCKandreturnsthememoryaddressofthesubscriptedcell.Thisaccess wordshouldalsocallUPDATE.Testyourworksofar. Nextusethefirstcellasacountofhowmanydataitemsarestoredinthearray.Defineaword PUTwhichwillstoreavalueintothenextavailablecellofthearray.Defineadisplayroutine whichwillprintthestoredelementsinthearray. NowusethisvirtualarrayfacilitytodefineawordENTERwhichwillacceptpairsofnumbers andstoretheminthearray. Finally,defineTABLEtoprintthedataenteredabove,eightmembersperline.[answer]
11Extendingthecompiler:Definingwordsand Compilingwords
Incomparisonwithconventionallanguages,Forth'scompileriscompletelybackwards.Traditional compilersarehugeprogramsdesignedtotranslateanyforeseeable,legalcombinationofavailable operatorsintomachinelanguage.InForth,however,mostoftheworkofcompilationisdonebyasingle definition,onlyafewlineslong.Specialstructureslikeconditionalsandloopsarenotcompiledbythe compilerbutbythewordsbeingcompiled(IF,DO,etc.) LestyouscoffatForth'ssimpleways,noticethatForthisuniqueamonglanguagesintheeasewithwhich youcanextendthecompiler.Definingnew,specializedcompilersisaseasyasdefininganyotherword, asyouwillsoonsee. Whenyou'vegotanextensiblecompiler,you'vegotaverypowerfullanguage!
Justaquestionoftime
Beforewegetfullyintothischapter,let'sreviewoneparticularconceptthatcanbeaproblemto beginningForthprogrammers.It'saquestionoftime. Wehaveusedtheterm"runtime"whenreferringtothingsthatoccurwhenawordisexecutedand "compiletime"whenreferringtothingsthathappenwhenawordiscompiled.Sofarsogood.Butthings getalittleconfusingwhenasinglewordhasbotharuntimeandacompiletimebehavior. Ingeneraltherearetwoclassesofwordswhichbehaveinbothways.Forpurposesofthisdiscussion, we'llcallthesetwoclasses"definingwords"and"compilingwords." Adefiningwordisaawordwhich,whenexecuted,compilesanewdefinition.Adefiningwordspecifies thecompiletimeandruntimebehaviorofeachmemberofthe"family"ofwordsthatitdefines.Using thedefiningwordCONSTANTasanexample,whenwesay
80CONSTANTMARGIN
HowtoDefineaDefiningWord
HerearethestandardForthdefiningwordswe'vecoveredsofar:
: VARIABLE 2VARIABLE CONSTANT 2CONSTANT CREATE
Whatdotheyallhaveincommon?Eachofthemisusedtodefineasetofwordswithsimilarcompile timeandruntimecharacteristics. Andhowareallthesedefiningwordsdefined?Firstwe'llanswerthisquestionmetaphorically. Let'ssayyou'reintheceramicsaltshakerbusiness.Ifyouplantomakeenoughsaltshakers, you'llfindit'seasiesttomakeamoldfirst.Amoldwillguaranteethatallyourshakerswillbe ofthesamedesign,whileallowingyoutomakeeachshakeradifferentcolor.Inmakingthemold,you mustconsidertwothings: 1. Howthemoldwillwork.(E.g.,howwillyougettheclayintoandoutofthemoldwithout breakingthemoldorlettingtheseamsshow?) 2. Howtheshakerwillwork.(E.g.,howmanyholesshouldtherebe?Howmuchsaltshouldithold? Etc.) TobringthisanalogybacktoForth,thedefinitionofadefiningwordmustspecifytwothings:the compiletimebehaviorandtheruntimebehaviorforthattypeofword. Holdthatthoughtamomentwhilewelookatthemostbasicofthedefiningwordsintheabovelist: CREATE.Atcompiletime,CREATEtakesanamefromtheinputstreamandcreatesadictionary headingforit. 7 E link X A M P L E Atruntime,CREATEpushesthebodyaddressofEXAMPLE
executiontoken (body)
WhenweexecuteVARIABLEasin
VARIABLEORANGES
Toillustrate,thefollowingcouldbeavaliddefinitionforCONSTANT(althoughinfactCONSTANTis usuallydefinedinmachinecode):
:CONSTANTCREATE,DOES>@;
Toseehowthisdefinitionworks,imaginewe'reusingittodefineaconstantnamedTROMBONES,like this:
76CONSTANTTROMBONES
runtime portion
ThewordsthatprecedeDOES>specifywhatthemoldwilldo;thewordsthatfollowDOES>specify whattheproductofthemoldwilldo.
DOES>
runtime: (addr)
DefiningWordsYouCandefineYourself
Herearesomeexamplesofdefiningwordsthatyoucancreateyourself. Recallthatinourdiscussionof"StringInputCommands"inChap.10,wegaveanexamplethatemployed characterstringarrayscalledNAME,EYES,andME.Everytimeweusedoneofthesenames,wefollowed itwithacharactercount.Intheinputdefinition,wewrote
...PADNAME14MOVE
andintheoutputdefinitionwewrote
...NAME14TRAILINGTYPE...
wewillcreateanarraycalledME,withtwentycharactersavailableforthecharacterstring. WhenweexecuteME,we'llgettheaddressofthearrayandthecountonthestack.Nowwecanwrite
PADMEMOVE
insteadof
PADME20MOVE
or
METRAILINGTYPE
insteadof
ME20TRAILINGTYPE
Here'showwemightdefineCHARACTERS: :CHARACTERS
tobeusedintheform
30STRINGVALVE
tocreateanarraythirtybytesinlength.Toaccessanybyteinthisarray,wemerelysay:
6VALVEC@
whichbreaksdownasfollows: DUP,ALLOT DOES>2DUP@ Compilesthecountandallotsthegivennumberofbytes. Atruntime,giventheargumentonthestack,produces(argpfaarg count). Teststhattheargumentisnotlessthanthemaximum,i.e.,the storedcount.SinceU<isanunsignedcompare,negativearguments willappearasveryhighnumbersandthuswillalsofailthetest. Checkifthecomparisontestfails. Otherwiseaddstheargumenttothebodyaddress,plusan additionalcelltoskipthecount.
Here'sanotherwaythattheuseofdefiningwordscanhelpduringdevelopment.Let'ssayyousuddenly decidethatallofthearraysyou'vedefinedwithSTRINGaretoolargetobekeptincomputermemory andshouldbekeptondiskinstead.AllyouhavetodoisredefinetheruntimeportionofSTRING.This newSTRINGwillcomputewhichrecordonthediskagivenbytewouldbecontainedin,readtherecord intoabufferusingINCLUDED,andreturntheaddressofthedesiredbytewithinthebuffer.Astring definedinthiswaycouldspanmanyconsecutiverecords(usingthesametechniqueasinProb.5,Chap. 10). Youcanusedefiningwordstocreateallkindsofdatastructures.Sometimes,forinstance,it'susefulto createmultidimensionalarrays.Here'sanexampleofadefiningwordwhichcreatestwodimensional bytearraysofgivensize:
:ARRAY(#rows#cols) CREATEDUP,*ALLOT DOES>(member:rowcoladdr) ROTOVER@*++CELL+;
c0 c1 c2 c3 r0 r1 r2 r3
Tocreateanarrayfourbytesbyfourbytes,wewouldsay
44ARRAYBOARD
Toaccess,say,thebyteinrow2,column1,wecouldsay
21BOARDC@
Here'showourARRAYworksingeneralterms.Sincethecomputeronlyallowsustohaveone dimensionalarrays,wemustsimulatetheseconddimension.Whileourimaginaryarraylookslikethis
Ifyouwanttheaddressofthebyteinrow2,column1,itcanbecomputedbymultiplyingyourrow number(2)bythenumberofcolumnsineachrow(4)andthenaddingyourcolumnnumber(1),which indicatesthatyouwanttheninthbyteintherealarray.ThiscalculationiswhatmembersofARRAYmust doatruntime.You'llnoticethat,toperformthiscalculation,eachmemberwordneedstoknowhow manycolumnsareineachrowofitsparticulararray.Forthisreason,ARRAYmuststorethisvalueinto thebeginningofthearrayatcompiletime. Forthecurious,herearethestackeffectsoftheruntimeportionofarray: Operation ... ROT OVER@ * ++ CELL+ Contentsofstack rowcolpfa colpfarow colpfarow#cols colpfarowindex address correctedaddress
.ROWprintsapatternofstarsandspacesthatcorrespondtothe8bitnumberonthestack.Forinstance:
2BASE!ok 00111001.ROW ****ok DECIMALok
ThedefiningwordSHAPEtakeseightargumentsfromthestackanddefinesashapewhich,when executed,printsan8by8gridthatcorrespondstotheeightarguments.Forexample:
MAN ** ** **** **** **** ** ** ** ok
HowtoControltheColonCompiler
Compilingwordsarewordsusedinsidecolondefinitionstodosomethingatcompiletime.Themost obviousexamplesofcompilingwordsarecontrolstructurewordssuchasIF,THEN,DO,LOOP,etc. BecauseForthprogrammersdon'toftenchangethewaytheseparticularwordswork,we'renotgoingto studythemanyfurther.Insteadwe'llexaminethegroupofwordsthatcontrolthecoloncompilerandthus canbeusedtocreateanytypeofcompilingword. Recallthatthecoloncompilerordinarilylooksupeachwordofasourcedefinitionandcompileseach word'saddressintothedictionaryentrythat'sall.Butthecoloncompilerdoesnotcompiletheaddressof acompilingworditexecutesit. Howdoesthecoloncompilerknowthedifference?Bycheckingthedefinition's"precedencebit."Ifthe bitis"off,"theaddressofthewordiscompiled.Ifthebitis"on,"thewordisexecutedimmediately;such
wordsarecalled"immediate"words. ThewordIMMEDIATEmakesaword"immediate."Itisusedintheform:
:namedefinition;IMMEDIATE
thatis,itisexecutedrightafterthecompilationofthedefinition. Togiveanimmediateexample,let'sdefine
:SAYHELLO."Hello";IMMEDIATE
WecanexecuteSAYHELLOinteractively,justaswecouldifitwerenotimmediate.
SAYHELLOHellook
ButifweputSAYHELLOinsideanotherdefinition,itwillexecuteatcompiletime:
:GREETSAYHELLO."IspeakForth";Hellook
ratherthanatexecutiontime:
GREETIspeakForthok
BEGINsimplysavestheaddressofHEREatcompiletimeonthestack.Why?Becausesoonerorlateran UNTILorREPEATisgoingtocomealong,andeitherhastoknowwhataddressinthedictionaryto returntointheeventthatitmustrepeat.ThisistheaddressthatBEGINleftonthestack. BEGIN'scompiletimebehaviorisleavingHEREonthestack.ButBEGINcompilesnothingintothe compilee;thereisnoruntimebehaviorforBEGIN. UnlikeBEGIN,mostcompilingwordsdohavearuntimebehavior.Tohavearuntimebehavior,aword hastocompileintothecompileetheaddressoftheruntimebehavior,whichmustalreadyhavebeen definedasaword. AgoodexampleisDO.LikeBEGIN,DOmustprovide,atcompiletime,aHEREforLOOPor+LOOP toreturnto.ButunlikeBEGIN,DOalsohasaruntimebehavior:itmustpushthelimitandindexonto thereturnstack. TheruntimebehaviorofDOisdefinedbyalowerlevelword,sometimescalled(DO)or2>R.The definitionofDOisthis:
:DOPOSTPONE2>RHERE;IMMEDIATE
ThewordPOSTPONEfindstheaddressofthenextwordinthedefinition(inthiscase2>R)and compilesitsaddressintothecompileedefinition,sothatatruntime2>Rwillbeexecuted.
Anotherexampleisthedefinitionof;.Atcompiletime,semicolonmustdothefollowingthings: 1. compiletheaddressofEXITintothedictionaryentrybeingcompiled, 2. exposethenewwordtothecoloncompiler,and 3. leavecompilationmode. Here'sthedefinitionofsemicolon: :;POSTPONEEXITREVEALPOSTPONE[;IMMEDIATE ThefirstphrasecompilesEXIT,providingtheruntimebehavior.Thesecondphrase,whichisthecompile timebehavior,firstexposesthewordbeingcompiledandthengetsoutofthecompiler. WhatisthereasonforREVEAL?Whenwordsareintheprocessofbeingcompiled,theyarenotyetfindable bythecoloncompiler.Thisisdonetomakeitpossibletoredefineexistingwordsintermsoftheolddefinition plusadditionalcode,forexample: :CRCRSPACE; IfduringthecompilationofthenewCRitsnamewerefindable,thenameoftheoriginalCRwouldbe blocked,andwewouldhavehadtodo,e.g.: :_cr_CR; :CR_cr_SPACE;
ThewordPOSTPONEcanalsobeusedtocompileanimmediatewordasthoughitwerenotimmediate. Givenourpreviousexample,inwhichSAYHELLOisanimmediatedefinition,wemightdefine
:GREETPOSTPONESAYHELLO."IspeakForth";ok
toforceSAYHELLOtobecompiledratherthanexecutedatcompiletime.Thus:
GREETHelloIspeakForthok
Besuretonotethe"intelligence"builtintoPOSTPONE.POSTPONEparsesthenextwordintheinput stream,decidesifitisimmediateornot,andproceedsaccordingly.Ifthewordwasnotimmediate, POSTPONEcompilestheaddressofthewordintoacompileedefinition;thinkofitasdeferred compilation.Ifthewordisimmediate,POSTPONEcompilestheaddressofthiswordintothedefinition currentlybeingdefined;thisisordinarycompilation,butofanimmediatewordwhichotherwisewould havebeenexecuted. Toreview,herearethetwowordswhichareusefulincreatingnewcompilingwords: IMMEDIATE () POSTPONExxx () Marksthemostrecentlydefinedwordasonewhich,whenencounteredduring compilation,willbeexecutedratherthanbeingcompiled. 1. Usedinthedefinitionofacompilingword.Whenthecompilingword,in turn,isusedinasourcedefinition,theexecutiontokenofxxxwillbe compiledintothedictionaryentrysothatwhenthenewdefinitionis
MoreCompilercontrollingWords
Therearetwoothercompilercontrolwordsyoushouldknow.Thewords[and]canbeusedinsidea colondefinitiontostopcompilationandstartitagain,respectively.Whateverwordsappearbetweenthem willbeexecuted"immediately",i.e.,atcompiletime. Considerthisexample:
:SAYHELLO."Hello"; :GREET[SAYHELLO]."IspeakForth";Hellook GREETIspeakForthok
Theuseofaliteralinacolondefinitionrequirestwocells.Thefirst containstheexecutiontokenofaroutinewhich,whenexecuted,will pushthecontentsofthesecondcell(thenumberitself)ontothestack. Thenameofthisroutinemayvary;let'scallitthe"runtimecodefora literal,"orsimply(LITERAL).Whenthecoloncompilerencountersa number,itfirstcompilestheruntimecodeforaliteral,thencompiles thenumberitself. ThewordyouwillusemostoftentocompilealiteralisLITERAL(no parentheses).LITERALcompilesboththeruntimecodeandthevalue itself.Toillustrate:
:FOURMORE[4]LITERAL+;
Nowlet'sassumewewillonlyaccessthearraythroughthewordLIMIT.Wecaneliminatetheheadof thearray(somebytesandonecell)byusingthisconstructioninstead:
HERE5CELLSALLOTBASE! :LIMIT(indexaddr)CELLS[BASE@]LITERAL+; DECIMAL
butit'stimeconsumingtoexecute
28*3+
everytimeweusethisdefinition.Alternatively,wecouldwrite
BOARD19CELL++
Here'sasillyexamplewhichmaygiveyousomeideasformorepracticalapplications. Thisdefinitionlet'syoupeekintotheinnardsoftheworditself:
:DUMPTHIS[HERE]LITERAL32DUMP."DUMPTHIS";
Firstitcompilestheaddressoftheruntimecode,thenitcompilesthevalueitself(usingcomma).
Curtaincalls
Thissectiongivesusachancetosay"Goodbye"tothetextinterpreterandthe coloncompilerandperhapstoseetheminanewlight. HereisadefinitionofINTERPRETthatwillworkinmostForthsystems:
:INTERPRET() BEGIN BLWORDFINDIFEXECUTE?STACKABORT"Stackempty" ELSENUMBER THEN AGAIN;
We'vecoveredeachofthewordscontainedinthisdefinition;wecandescribeINTERPRETinEnglishby simply"translating"itsdefinition,likethis:
Beginaloop.Withintheloop,trytolookupthenextwordfromtheinputstream.Ifit'snotdefined,tryto convertittoanumber.Ifitisdefined,executeit,thenchecktoseewhetherthestackisempty.(Ifitis,exitthe loopandprint"STACKEMPTY.")Thenrepeattheinfiniteloop.
Asyoucansee,theForthtextinterpreterisasimpleyetpowerfulstructure.Nowlet'scompareits structurewiththatofthecoloncompiler:
:]() BEGIN BLWORDFINDDUPIF1=IFEXECUTE?STACKABORT"Stackempty" ELSE, THEN ELSEDROP(NUMBER)POSTPONELITERAL
THEN AGAIN;
Hereisasummaryofthewordswe'vecoveredinthischapter: runtime: (addr) Usedincreatingadefiningword;markstheendofits compiletimeportionandthebeginningofitsruntime portion.Theruntimeoperationsarestatedinhigherlevel Forth.Atruntime,thebodyaddressofthedefinedwordwill beonthestack. Marksthemostrecentlydefinedwordasonewhich,when encounteredduringcompilation,willbeexecutedratherthan beingcompiled. 1. Usedinthedefinitionofacompilingword.Whenthe compilingword,inturn,isusedinasource definition,theexecutiontokenofxxxwillbe compiledintothedictionaryentrysothatwhenthe newdefinitionisexecuted,xxxwillbeexecuted. 2. Usedinacolondefinition,causestheimmediate wordxxxtobecompiledasthoughitwerenot immediate;xxxwillbeexecutedwhenthedefinition
DOES>
IMMEDIATE
()
POSTPONExxx ()
LITERAL [ ]
ReviewofTerms
Compiletime behavior 1. whenreferringtodefiningwords:thesequenceofinstructionswhichwillbe carriedoutwhenthedefiningwordisexecutedtheseinstructionsperform thecompilationofthememberwords; 2. whenreferringtocompilingwords:thebehaviorofacompilingword, containedwithinacolondefinition,duringcompilationofthedefinition. adefinitionbeingcompiled.Inrelationtoacompilingword,thecompileeisthe definitionwhosecompilationthecompilingwordaffects. awordusedinsideacolondefinitiontotakesomeactionduringthecompilation process. awordwhich,whenexecuted,compilesanewdictionaryentry.Adefiningword specifiesthecompiletimeandruntimebehaviorofeachmemberofthe"family"of wordsthatitdefines. InForthdictionaryentries,abitwhichindicateswhetherawordshouldbeexecuted ratherthanbecompiledwhenitisencounteredduringcompilation. 1. whenreferringtodefiningwords:thesequenceofinstructionswhichwillbe carriedoutwhenanymemberisexecuted; 2. whenreferringtocompilingwords:aroutinewhichwillbeexecutedwhen thecompileeisexecuted.Notallcompilingwordshaveruntimebehavior.
Compilee Compilingword
Definingword
Precedencebit
Runtime behavior
ProblemsChapter11
1. DefineadefiningwordnamedLOADEDBYthatwilldefinewordswhichincludeafilewhen theyareexecuted.Example:
S"mail.forth"LOADEDBYCORRESPONDENCE
woulddefineH.tobeawordwhichprintsthetopofthestackinhexbutdoesnotpermanently changeBASE.
DECIMAL 17DUPH.. 1117ok
willdefineCRSinthesamewayasthoughyouhaddefinedit
:CRS(times)0?DOCRLOOP;
[answer]
12ThreeExamples
ProgramminginForthismoreofan"art"thanprogramminginanyotherlanguage.Likepaintersdrawing brushstrokes,Forthprogrammershavecompletecontroloverwheretheyaregoingandhowtheywillget there.CharlesMoorehaswritten,"AgoodprogrammercandoafantasticjobwithForth;abad programmercandoadisastrousjob."AgoodForthprogrammermustbeconsciousof"style." Forthstyleisnoteasilytaught;it'sasubjectthatdeservesabookofitsown.Someelementsofgood Forthstyleinclude:
Onegoodwaytolearnstyle,asidefromtrialanderror,istostudyexistingForthapplications,including Forthitself.Inthisbookwe'veincludedthedefinitionsofmanyForthsystemwords,andweencourage youtocontinuethisstudyonyourown. ThischapterintroducesthreeapplicationswhichshouldserveasexamplesofgoodForthstyle. ThefirstexamplewillshowyouthetypicalprocessofprogramminginForth:startingoutwithaproblem andworkingstepbysteptowardsthesolution. Thesecondexampleinvolvesamorecomplexapplicationalreadywritten:youwillseetheuseofwell factoreddefinitionsandthecreationofanapplicationspecific"language." ThethirdexampledemonstratesthewaytotranslateamathematicalequationintoaForthdefinition;you willseethatworkingwithfixedpointarithmeticdoesnotnecessarilymeansacrificingspeedand compactness.
1.WORDgame
TheexampleinthissectionisarefinementofthebuzzphrasegeneratorweprogrammedbackinChap. 10.(Youmightwanttoreviewthatversionbeforereadingthissection.)Thepreviousversiondidnotkeep trackofitsowncarriagereturns,causingustoforceCRsintothedefinitionandcreatingaveryragged rightmargin.Thejobofdecidinghowmanywholewordscanfitonalineisareasonableapplicationfor acomputerandnotatrivialone. Theproblemisthis:todrafta"brief"whichconsistsoffourparagraphs,eachparagraphconsistingofan appropriateintroductionandsentence.Eachsentencewillconsistoffourrandomlychosenphraseslinked togetherbyfillerstocreategrammaticallylogicalsentencesandaperiodattheend. Thewordsandphraseshavealreadybeeneditedintothefilephrases.forth.Lookatthisfilenow, withoutlookingatwordgame.forth.(we'repretendingwehaven'twrittentheapplicationyet). Filephrases.forthdefinesthefourintroductions,compiledintotheINTROSstringarray.Thefour (ormore,INTROSisselforganizing)introductionsmustbeusedinsequence.Thesamefile phrases.forthcontainsfoursetsoffillers,inFILLER.Thefoursetsareusedinsequence,butany ofthethreeversionswithinaset(organizedincolumns)ischosenatrandom.Again,phrases.forth containsthethreecolumnsofbuzzwordsfromourpreviousversion,withsomeaddedwords.We've organizedthebuzzwordsinseparate1STADJECTIVE,2NDADJECTIVEandNOUNstringarrays. Youmighalsolookatatthesampleoutputthatprecedestheendofthissection,togetabetterideaofthe desiredresult. "Topdowndesign"isawidelyacceptedapproachtoprogrammingthatcanhelptoreducedevelopment time.Theideaisthatyoufirststudyyourapplicationasawhole,thenbreaktheproblemintosmaller processes,thenbreaktheseprocessesintostillsmallerunits.Onlywhenyouknowwhatalltheunits shoulddo,andhowtheywillconnecttogether,doyoubegintowritecode. TheForthlanguageencouragestopdowndesign.ButinForthyoucanactuallybegintowritetoplevel
definitionsimmediately.Alreadywecanimaginethatthe"ultimateword"inourapplicationmightbe calledPAPER,andthatitwillprobablybedefinedsomethinglikethis:
:PAPER40DOIINTROSENTENCELOOP;
whereINTROusestheloopindexasitsargumenttoselecttheappropriateintroduction.SENTENCE couldbedefined
:SENTENCE40DOIPHRASELOOPENDS;
wherePHRASEusestheloopindexasitsargumenttoselecttheappropriateset,thenchoosesoneofthe threeversionswithintheset.ENDStakescareofthefinal'.'andCRattheendofasentence. Usingourfavoriteeditor,wecanenterthesetopleveldefinitionsintowordgame.forth.Ofcoursewe can'tINCLUDEthisfileuntilwehavewrittenourlowerleveldefinitions. Incomplicatedapplications,Forthprogrammersoftentestthelogicoftheirtopleveldefinitionsbyusing "stubs"forthelowerlevelwords.Astubisatemporarydefinition.Itmightsimplyprintamessagetolet usknowitsbeenexecuted.Oritmaydonothingatall,exceptresolvethereferencetoitsnameinthe highleveldefinition. Whilethetopdownapproachhelpstoorganizetheprogrammingprocess,itisn'talwaysfeasibletocode inpurelytopdownfashion.Usuallywehavetofindouthowcertainlowlevelmechanismswillwork beforewecandesignthehigherleveldefinitions. Thebestcompromiseistokeepaperspectiveontheproblemasawholewhilelookingoutforlowlevel problemswhosesolutionsmayaffectthewholeapplication. Inourexampleapplication,wecanseethatitwillnolongerbepossibletoforceCRsatpredictable points.Insteadwe'vegottoinventamechanismwherebythecomputerwillperformcarriagereturns automatically. Theonlywaytosolvethisproblemistocounteverycharacterthatistyped.Beforeeachwordistyped, theapplicationmustdecidewhetherthereisroomtotypeitonthecurrentlineordoacarriagereturn first. Solet'sdefinethevariableLINECOUNTtokeepthecountandtheconstantRMARGINwiththevalue78, torepresentthemaximumcountperline.Eachtimewetypeawordwewilladditscountto LINECOUNT.Beforetypingeachwordwewillexecutethisphrase:
(lengthofnextword)LINECOUNT@+RMARGIN<0=IFCR
thatis,ifthelengthofthenextwordaddedtothecurrentlengthofthelineexceedsourrightmargin,then we'lldoacarriagereturn. Butwehaveanotherproblem:howdoweisolatewordswithaknowncountforeachword?Fornow,let's assumewehaveavailableawordSplitAtChar.Thiswordbreaksstringsapart,givenaspecific delimiter. Let'swriteouta"firstdraft"ofthislowlevelpartofourapplication.Itwilltypeasingleword,making appropriatecalculationsforcarriagereturn. BLSplitAt Char BreakstringintwoatfirstBL.Leavesthecountonthestack,with theaddressofthefirstcharacterunderneath.
DUP1+ LINECOUNT@+ RMARGIN> IFCR0 LINECOUNT! ELSESPACE THEN DUP1+ LINECOUNT+! TYPE
Leavestheincrementedcountandacopyoftheoriginalcountonthe stack. Computehowlongthecurrentlinewouldbeifaspaceplusthenew wordweretobeincludedonit. Decidesifitwouldexceedthemargin. Ifso,resetsthecarriageandthecount. Otherwise,leavesaspacebetweenthewords. Increasesthecountbythelengthofthewordtobetyped,plusone forthespace. TypesthewordusingthecountandtheaddressleftbySplitAt Char.
NowtheproblemisgettingSplitAtChartolookatthestringsinphrases.forth.Thisis handledbyINCLUDED,soifwesay
S"phrases.forth"INCLUDED
thenCREATEwillmakesureallnecessarystringsarecompiledinmemory. TohelpCREATEdothis,we'lldefinetheword$".Asyoucanseefromitsdefinition,$"compilesthe string(delimitedbyasecondquotationmark)intothedictionary,withthecountinthefirstbyte,and leavesitsaddressonthestackfor}$,}s$and}r$.Tocompilethecountandstringintothedictionary, wesimplyhavetoexecuteWORD,sinceWORD'sbufferisHERE.Wegetthestring'saddressasafillip, sinceWORDalsoleavesHERE. AllthatremainsistoALLOTtheappropriatenumberofbytes.Thisnumberisobtainedbyfetchingthe countfromthefirstbyteofthestringandaddingoneforthecount'sbyte. Wehavewritten$"tocompilethenextstringintothedictionary,butalsotopiletheaddressofthis stringonthestack,ontopoftheaddressesofotherstringsthatwerecompiledalreadyjustbeforethat.In ordertoletotherwordsknowhowmanystringaddressesareonthestack,$"alsoincrementsthetopof stack:
('string1'string2...stringNNnew_string_address)SWAP1+;
whiletheruntimepartishandledinDOES>parts,doingsomethinglike
DOES>(ixbodycaddru)SWAPCELLS+CELL+@COUNT;
Ourmechanismhasgivenusaveryeasywaytoselectstrings.Wecantestthisdefinitionbyitself,as follows:
0INTRO(or1,2or3INTRO) Inthispaperwewilldemonstratethatok
2.FileAway!
Oursecondexampleconsistsofasimplefilingsystem.Itisamoderatelyusefulapplication,andagood onetolearnForthfrom.Wehavedividedthissectionintofourparts: 1. 2. 3. 4. A"HowTo"fortheenduser.Thiswillgiveyouanideaofwhattheapplicationcando. Notesonthewaytheapplicationisstructuredandthewaycertaindefinitionswork. Aglossaryofallthedefinitionsintheapplication. Alistingoftheapplication,includingthedatafilesthemselves.
HowtoUsetheSimpleFileSystem
Thiscomputerfilingsystemletsyoustoreandretrieveinformationquickyandeasily.Atthemoment,itis setuptohandlepeople'snames,occupations,andphonenumbers.Notonlydoesitallowyoutoenter, change,andremoverecords,italsoallowsyoutosearchthefileforanypieceofinformation.For example,ifyourhaveaphonenumber,youcanfindtheperson'sname;or,givenaname,youcanfindthe person'sjob,etc. Foreachpersonthereisa"record"whichcontainsfour"fields."Thenameswhichspecifyeachofthese fourfieldsare
SURNAMEGIVENJOBPHONE
("Given,"ofcourse,referstoaperson'sgivenname,orfirstname.)
FileRetrieval
YoucansearchthefileforthecontentsofanyfieldbyusingthewordFIND,followedbythefieldname andthecontents,asin
FINDJOBnewscaster DanRatherok
and
ANOTHER FrankReynoldsok
JessicaSavitch FrankReynolds ok
FileMaintenance
Toenteranewrecord,usethecommandENTER,followedbythesurname,givenname,job,andphone, eachseparatedbyacommaonly.Forexample,
ENTERNureyev,Rudolf,Balletdancer,5551234 ok
Tochangethecontentsofasinglefieldwithinthecurrentrecord,usethecommandCHANGEfollowedby thenameofthefield,thenthenewstring.Forexample,
CHANGEJOBchoreographer ok
Tocompletelyremovethecurrentrecord,usethecommandREMOVE:
REMOVE ok
Comments
Thissectionismeantasaguide,forthenoviceForthprogrammer,totheglossaryandlistingwhich follow.We'lldescribethestructureofthisapplicationandcoversomeofthemorecomplicated definitions.Asyoureadthissection,studytheglossaryandlistingonyourown,andtrytounderstandas muchasyoucan. Turntothelistingnow.Neartheend,thisfilecontainsthedefinitionsforallnineendusercommands we'vejustdiscussed.Noticehowsimplethesedefinitionsare,comparedtotheirpower!
willputthecharacterstringthatfollowsintheinputstreamintothe"surname"fieldofthecurrentrecord. Thephrase
SURNAME.FIELD
willprintthecontentsofthe"surname"fieldofthecurrentrecord,etc. Therearetwopiecesofinformationthatareneededtoidentifyeachfield:thefield'sstartingaddress relativetothebeginningofarecordandthelengthofthefield. Inthisapplication,arecordislaidoutlikethis: offset contents size 0 surname 16 16 given 16 32 job 24 56 phone 8
Theliteral32iscomputedbythesystem,whichkeepstrackoftheactualoffsetintoarecordthrough updatingRLENGTH. Thuswhenweenterthenameofafield,weareputtingonthestacktheaddressofthetablethatdescribes the"job"field.Wecanfetcheitherorbothpiecesofinformationrelativetothisaddress. Let'scalleachoftheseentriesa"fieldspecifyingtable,"ora"spectable"forshort. PartofthedesignforthisapplicationisderivedfromtherequirementsofFIND,ANOTHER,andALL; thatis,FINDnotonlyhastofindagivenstringwithinagiventypeoffield,butalsoneedsto "remember"thestringandthetypeoffieldsothatANOTHERandALLcansearchforthesamething. Wecanspecifythekindoffieldwithjustonevalue,theaddressofthespectableforthattypeoffield. Thismeansthatwecan"remember"thetypeoffieldbystoringthisaddressintoKEEP. KINDwascreatedforthispurpose,toindicatethe"kind"offield. Torememberthestring,wehavedefinedabuffercalledWHATtowhichthestringcanbemoved. ThewordKEEPservesthedualpurposeofstoringthegivenfieldtypeintoKINDandthegivencharacter stringintoWHAT.IfyoulookatthedefinitionoftheenduserwordFIND,youwillseethatthefirstthing itdoesisKEEPtheinformationonwhatisbeingsear chedfor.ThenFINDexecutestheinternalword FIND,whichusestheinformationinKINDandWHATtofindamatchingstring. ANOTHERandALLalsouseFIND,buttheydon'tuseKEEP.Insteadtheylookforfieldsthatmatchthe onemostrecently"kept"byFIND. SothatwecanGETanypieceofinformationfromtherecordwehavejust"found,"weneedapointerto the"current"record.Thisneedismetbythevalue#RECORD.TheoperationsofthewordsSET,TOP andDOWNshouldbefairlyobvioustoyou. ThewordRECORD@usesitsstackparametertocomputetheabsoluteaddress(thecomputermemory address,somewhereinthediskbuffer)ofthebeginningofthecurrentrecord.RECORD@alsomakessure thattherecordreallyisinthediskbuffer. Whileaspectablecontainstherelativeaddressofthefieldanditslength,weusuallyneedtoknowthe field'sabsoluteaddressandlengthforwordssuchasTYPE,MOVE,andPARSE.Lookatthedefinition ofthewordFIELDtoseehowitconvertstheaddressofaspectableintoanabsoluteaddressandlength. ThenexaminehowFIELDisappliedinthedefinitionof.FIELD. ThewordPUTalsoemploysFIELD.Itsphrase
>RKBD,R>>FLD_
leavesonthestackthearguments
addrofstringcountofstringabsoluteaddroffieldsizeoffield
empty.Assoonasanemptyrecordisfound,LEAVEendstheloop.#RECORDwillcontainthenumberof thefreerecord. AnotherthingworthnotingaboutFREEisthatitabortsifthefileisfull,thatis,ifitrunsthroughallthe recordswithoutfindingoneempty.WecanuseaDOlooptorunthroughalltherecords,buthowcanwe tellthattheloophasrunoutbeforeithasfoundanemptyrecord? ThebestwayistoleaveaTRUEonthestack,toserveasaflag,beforebeginningtheloop.Ifanempty recordisfound,wecanchangetheflagtoFALSE(withthewordINVERT)beforeweleavetheloop. Whenwecomeoutoftheloop,we'llhaveaTRUEifweneverfoundanemptyrecord,aFALSEifwedid. ThisflagwillbetheargumentforABORT". WeuseasimilartechniqueinthedefinitionofFIND.FINDmustreturnaflagtothewordthat executedit:FIND,ANOTHER,ALLor(PAIR).Theflagindicateswhetheramatchwasfoundbefore theendofthefilewasreached.Eachoftheseouterwordsneedstomakeadifferentdecisionbasedonthe stateofthisflag.ThisflagisTRUEifamatchisnotfound(hencethenameFIND).Thedecisiontouse negativelogicwasbasedonthewayFINDisused. BecausetheflagneedstobeTRUEifamatchisnotfound,theeasiestwaytodesignthiswordistostart withaTRUEonthestackandchangeittoaFALSEonlyifamatchisfound. Nowthatyouunderstandthebasicdesignofthisapplication,youshouldhavenotroubleunderstanding therestofthelisting,usingtheglossaryasaguide.
FilerGlossary
/CR Aconstantthatdefinesthelengthinbytesofanewlinesequence. Aconstantthatdefinesthemaximumnumberofrecordsinthedata #MAXRECS file.Toincreasethisnumber,addlinescontainingRLENGTH spaces,followedbyanewline,tothedatafile. FILE Avaluethatholdsthehandleofthefilecontainingthedata. Avaluethatcontainstheaddressofthefieldspecifyingtableforthe KIND typeoffieldthatwaslastsearchedforbyFIND. RLENGTH Avaluethatcontainsthelengthinbytesofasinglerecord. #RECORD Avaluethatpointstothecurrentrecord. Adefiningwordtocreatefieldspecifyingtables.Takesthefield widthinbytesasaparameterandupdatesRLENGTH.Allusesof RECORD RECORDshouldhappenbefore#MAXRECSisdefined.Usage:10 RECORDfoo Returnstheaddressofthefieldspecifyingtableforthe"surname" SURNAME (lastname)field. Returnstheaddressofthefieldspecifyingtableforthe"given"(first GIVEN name)field. JOB Returnstheaddressofthefieldspecifyingtableforthe"job"field. Returnstheaddressofthefieldspecifyingtableforthe"phone" PHONE field. WHAT Returnstheaddressofabufferthatcontainsthestringthatisbeing
searchedfor,orwaslastsearchedfor,byFIND. RBUF Returnstheaddressofabufferthatcontainsthecurrentrecorddata. Makessureallchangeddataiscommittedtodisk,butdoesnotclose FLUSH thefile. UPDATE Writesthedataforthecurrentrecordtodisk. RECORD@ InsuresthatthespecifiedrecordisinRBUF. Giventheaddressofafieldspecifyingtable,returnstheaddressof >FLD_ theassociatedfieldinRBUF,alongwithitsassignedlength. Giventheaddressofafieldspecifyingtable,returnstheaddressof >FLD theassociatedfieldinRBUF,alongwithitsactuallength. Insuresthattheassociatedfieldinthecurrentrecordisinadisk FIELD bufferandreturnstheaddressofthefieldinthebufferalongwithits actuallength. Fromthecurrentrecord,typesthecontentsofthefieldthatis .FIELD associatedwiththefieldspecifyingtableataddr. SET Setstherecordpointertothespecifiedrecord. TOP Resetstherecordpointertothetopofthefile. DOWN Movestherecordpointerdownonerecord. .NAME Printsthefullnamefoundinthecurrentrecord. Movesacharacterstring,delimitedbyacommaorbyacarriage READ return,fromtheinputstreamtoatemporarybuffer,thenreturnsits addressandcount. Movesacharacterstring,delimitedbyacommaorbyacarriage PUT return,fromtheinputstreamintothefieldwhosefieldspecifying tableaddressisgivenonthestack. Movesacharacterstring,delimitedbyacommaorbyacarriage KEEP return,fromtheinputstreamintoWHAT,andsavestheaddressof thegivenfieldinKIND,forfutureusebyFIND. Startingatthetopofthefile,findsthefirstrecordthatisfree,thatis, FREE whosecountiszero.A bortsifthefileisfull. Beginningat#recordandproceedingdown,comparesthecontentsof FIND thefieldindicatedbyKINDagainstthecontentsofWHAT. Startingfromthetop,attemptstofindamatchonthecontentsof WHAT,usingKINDtoindicatethetypeoffield.Ifamatchismade, thenattemptstomatchasecondfield,whosetypeisindicatedby (PAIR) "field",withthecontents{caddru}.Ifbothmatch,printsthename; otherwiserepeatsuntilamatchismadeoruntiltheendofthefileis reached,inwhichcaseprintsanerrormessage. Findsthefirstfreerecord,thenmovesfourstringsdelimitedby ENTER commasintothesurname,given,jobandphonefieldsofthatrecord. Usage:ENTERlastname,firstname,job,phone REMOVE Erasesthecurrentrecord.
Changesthecontentsofthegivenfieldinthecurrentrecord. Usage:CHANGEfieldnamenewcontents Printsthecontentsofthegiventypeoffieldfromthecurrentrecord. GET Usage:GETfieldname Findstherecordinwhichthereisamatchbetweenthecontentsof FIND thegivenfieldandthegivenstring. Usage:FINDfieldnamestring Beginningwiththenextrecordafterthecurrentone,andusing ANOTHER KINDtodeterminetypeoffield,attemptstofindamatchonWHAT. Ifsuccessful,typesthename;otherwiseanerrormessage. Beginningatthetopofthefile,usesKINDtodeterminetypeoffield ALL andfindsallmatchesonWHAT.Typesthefullname(s). Findstherecordinwhichthereisamatchbetweenboththecontents ofthefirstgivenfieldandthefirstgivenstring,andalsothecontents PAIR ofthesecondgivenfieldandthesecondgivenstring.Commais delimiter. Usage:PAIRfield1string1,field2string2 Findstherecordinwhichthereisamatchonboththefirstandlast FULLNAME namesgiven. Usage:FULLNAMElastname,firstname CHANGE
FilerListing
Thelistingishere.
3.NoWeighting
Ourfinalexampleisamathproblemwhichmanypeoplewouldassumecouldonlybesolvedbyusing floatingpoint.Itwillillustratehowtohandleafairlycomplicatedequationwithfixedpointarithmetic anddemonstratethatforalltheadvantagesofusingfixedpoint,rangeandprecisionneednotsuffer.Of course,whenthehardwaredoeshavefloatingpointoneshouldpreferablyusethatinstead,andweshow howtodothat,too.Usingfixedpointhastheslightdisadvantagethat,inordertocorrectlycomputescale factors,wehavetoknowourForth'snumberofbitspercell.FormodernForthsthenumberofbitsper cellcanbe16,32,64,orevenhigher.Inordernottocomplicatethefollowingdescriptiontoomuch,we willassume16bithardware.Thatisprobablytheonlyenvironmentthisexamplewillbeusefulfor, anyway.Also,we'llassume1CHARSisequivalenttoonebyte. Inthisexamplewewillcomputetheweightofaconeshapedpileofmaterial,knowingtheheightofthe pile,theangleoftheslopeofthepile,andthedensityofthematerial. Tomaketheexamplemore"concrete,"let'sweighseveralhugepilesofsand,gravel,andcement.The slopeofeachpile,calledthe"angleofrepose,"dependsonthetypeofmaterial.Forexample,sandpiles itselfmoresteeplythangravel.
ForSceptics
Hereistheformulaforcomputingtheweightofa Thevolumeofacone,V,isgivenby conicalpilehfeettallwithanangleofreposeof ,wherebistheradiusofthebase thetadegrees,whereDisthedensityofthematerial inpoundspercubicfoot: andhistheheight.Wecancomputethebaseby knowingtheangleor,morespecifically,thetangent . oftheangle.Thetangentofanangleissimplythe ratioofthesegmentmarkedhtothesegment Thiswillbetheformulawhichwemustexpressin markedbinthisdrawing: Forth. Let'sdesignourapplicationsothatwecanenterthe nameofamaterialfirst,suchas
DRYSAND
thenentertheheightofapileandgettheresultfor drysand.
Ifwecall
Let'sassumethatforanyonetypeofmaterialthe densityandangleofreposenevervary.Wecanstore bothofthesevaluesforeachtypeofmaterialintoa table.Sinceweultimatelyneedeachangle'stangent, ratherthanthenumberofdegrees,wewillstorethe tangent.Forinstance,theangleofreposeforapile thisangle"theta",then .Thuswecan ofcementis35o,forwhichthetangentis.700.We willstorethisastheinteger700. computetheradiusofthebasewith . Bearinmindthatourgoalisnotjusttogetan answer;weareprogrammingacomputerordevice togettheanswerforusinthefastest,mostefficient, WhenwesubstitutethisintotheexpressionforV, andthenmultiplytheresultbythedensityDin poundspercubicfoot,wegettheformulashownin thetext.
andmostaccuratewaypossible.AsweindicatedinChap.5,towriteequationsusingfixedpoint arithmeticrequiresanextraamountofthought.Butonhardwarethatwouldhavetoemulatefloating point,theeffortpaysoffintwoways: 1. vastlyimprovedruntimespeed,whichcanbeveryimportantwhentherearemillions ofstepsinvolvedinasinglecalculation,orwhenwemustperformthousandsof calculationseveryminute.Also, 2. programsize,whichwouldbecriticalif,forinstance,wewantedtoputthisapplicationinahand helddevicespecificallydesignedasapilemeasuringcalculator.Forthisoftenusedinthistypeof instrument. Let'sapproachourproblembyfirstconsideringscale.Theheightofourpilesrangesfrom5to50feet.By workingoutourequationforapileofcement50feethigh,wefindthattheweightwillbenearly 3,500,000pounds. Butbecauseourpileswillnotbeshapedasperfectconesandbecauseourvaluesareaverages,wecannot expectbetterthanfourorfivedecimalplacesofaccuracy.Ifwescaleourresulttotons,wegetabout 17,500.Thisvaluewillcomfortablyfitwithintherangeofasinglelengthnumber,evenon16bit hardware.Forthisreason,let'swritethisapplicationentirelywithsinglelengtharithmeticoperators. (Althoughwewillassume16bithardwareinthefollowing,thecodeasshownwillrununmodifiedon anyANSForth.) Applicationswhichrequiregreateraccuracycanbewrittenusingdoublelengtharithmetic;toillustrate we'veevenwrittenasecondversionofthisapplicationusingdoublelengthmath,asyou'llseelateron. ButweintendtoshowtheaccuracythatForthcanachieveevenwith16bitmath. Byrunninganothertestwithapile40feethigh,wefindthatadifferenceofonetenthofafootinheight canmakeadifferenceof25tonsinweight.Sowedecidetoscaleourinputtofeetandinchesratherthan merelytowholefeet. We'dliketheusertobeabletoenter
15FOOT2INCHPILE
wherethewordsFOOTandINCHwillconvertthefeetandinchesintotenthsofaninch,andPILEwill dothecalculation.Here'showwemightdefineFOOTandINCH:
:FOOT10*; :INCH10012*/5+10/+;
Butbytesting,wefindthattheresultatthispointfora50footpileofcementwillbe34,991,whichjust exceedsthe15bitlimit.Nowisagoodtimetotakethe2000intoaccount.Insteadof
DENSITY100*/
wecansay
DENSITY200*/
andouranswerwillnowbescaledtowholetons. Youwillfindthisversioninthelisting.Aswementioned,wehavealsowrittenthisapplicationusing doublelengtharithmetic.Inthisversionyouentertheheightasadoublelengthnumberscaledtotenths ofafoot,followedbythewordFEET,asin50.0feet. Byusingdoublelengthintegerarithmetic,weareabletocomputetheweightofthepiletothenearest wholepound.Therangeofdoublelength32bitintegerarithmeticcompareswiththatofsingleprecision floatingpointarithmetic.Belowisacomparisonoftheresultsobtainedusinga10decimaldigitpocket calculator,singlelengthForth,doublelength(32bit)Forth,andfloatingpointForth.Thetestassumesa 50footpileofcement,usingthetablevalues. calculator Forth16bitsinglelength inpounds 34,995,633 intons 17,497.816 17,495 17,497.817 17,495 17,497.817 17,497.816
S"spiles.forth"INCLUDEDok cementok 10footpile=138tonsofcementok 10foot3inchpile=151tonsofcementok drysandok 10footpile=81tonsofdrysandok S"dpiles.forth"INCLUDEDcementok 10.0feet=279939poundsofcementor139.969tonsok S"fpiles.forth"INCLUDEDcementok 10efeet=279965.06373598pounds,or139.98253187tonsofcementok
Anoteon"
ThedefiningwordMATERIALtakesthreeargumentsforeachmaterial,oneofwhichistheaddressofa string..SUBSTANCEusesthisaddresstotypethenameofthematerial. ToputthestringinthedictionaryandtogiveanaddresstoMATERIAL,wehavedefinedawordcalled". Asyoucanseefromitsdefinition,"compilesthestring(delimitedbyasecondquotationmark)intothe dictionary,withthecountinthefirstbyte,andleavesitsaddressonthestackforMATERIAL.Tocompile thecountandstringintothedictionary,wesimplyhavetoexecuteWORD,sinceWORD'sbufferis HERE.Wegetthestring'saddressasafillip,sinceWORDalsoleavesHERE. AllthatremainsistoALLOTtheappropriatenumberofbytes.Thisnumberisobtainedbyfetchingthe countfromthefirstbyteofthestringandaddingoneforthecount'sbyte.
ABrowserInterfaceforFPILES
ThisinterfaceisForthsystemdependent.ItwillworkforiForth2.0,aftersomepreparations:
Send
ReviewofTerms
Stub inForth,atemporarydefinitioncreatedsolelytoallowtestingofahigherlevel definition. aprogrammingmethodologybywhichalargeapplicationisdividedintosmaller units,whichmaybefurthersubdividedasnecessary.Thedesignprocessstartswith theoverview,or"top,"andproceedsdowntothelowestlevelofdetail.Codingofthe lowlevelunitsbeginsonlyaftertheentirestructureoftheapplicationhasbeen designed.
Topdown Programming
200
Articles
TheprogramminglanguageForth
4. 5. 6. 7. 8.
Exampleofaprogrammingstylethatminimizesconditionalbranches. Robotarm Thisprogramcoordinatesprocessesthatrunwithdifferentspeeds: they'llstartatthesametime,runsmoothly,andstopatthesametime. CATCHandTHROWI CATCHandTHROWII MANYTIMES Aboutmanipulatingtheinputstream. KISScommandsinForth Aboutpostfix,infixandprefix.
:;