University Press of Florida
Programming from the Ground Up
Buy This Book ( Related Link )
CITATION PDF VIEWER
Full Citation
STANDARD VIEW MARC VIEW
Permanent Link: http://ufdc.ufl.edu/AA00011733/00001
 Material Information
Title: Programming from the Ground Up
Physical Description: Book
Language: en-US
Creator: Bartlett, Jonathan, Bruno, Dominick, Bartlett Publishing
Publication Date: 2003, 2003, 2003
 Subjects
Subjects / Keywords: computer science, computer programming, variables, functions, flow control, assembly language, computer architecture, code libraries, high level languages, GUI programming, x86 instructions, system calls, ASCII Codes, UNIX, LINUX, C programming, binary, hexadecimal, CPU, computer memory, GDP debugger, debugging, OGT+ ISBN: 9781616100643
Computer Science, Computer Science Education, Programming, Programming Languages
 Notes
Abstract: This is an introductory book to programming and computer science using assembly language. It assumes the reader has never programmed before, and introduces the concepts of variables, functions, and flow control. The reason for using assembly language is to get the reader thinking in terms of how the computer actually works underneath. Knowing how the computer works from a "bare-metal" standpoint is often the difference between top-level programmers and programmers who can never quite master their art. Contents: 1) Computer Architecture. 2) Functions. 3) Files. 4) Developing Robust Programs. 5) Sharing Functions with Code Libraries. 6) Counting Like A Computer. 7) High Level Languages. 8) GUI Programming. 9) Common x86 Instructions. 10) Important System Calls. 11) Table of ASCII Codes. 12) Idioms in Assembly Language. For further information and downloads, go to http://www.bartlettpublishing.com/site/bartpub/section/9
General Note: Expositive
General Note: Community College, Higher Education
General Note: http://www.ogtp-cart.com/product.aspx?ISBN=9781616100643
General Note: Adobe PDF Reader
General Note: Jonathan Bartlett
General Note: Textbook
General Note: http://www.bartlettpublishing.com/site/bartpub/section/9
General Note: http://florida.theorangegrove.org/og/file/6d9fe012-ffe1-1469-3a77-f65d56c0e41b/1/ProgrammingGroundUp-1-0-pdf.pdf
 Record Information
Source Institution: University of Florida
Holding Location: University Press of Florida
Rights Management: Copyright © 2003 by Jonathan Bartlett. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included …
Resource Identifier: isbn - 9781616100643
System ID: AA00011733:00001

Downloads

This item is only available as the following downloads:

( PDF )


Full Text

PAGE 1

ProgrammingfromtheGroundUpJonathanBartlettEditedbyDominickBruno,Jr.

PAGE 2

ProgrammingfromtheGroundUpbyJonathanBartlettEditedbyDominickBruno,Jr.Copyright2003byJonathanBartlettPermissionisgrantedtocopy,distributeand/ormodifythisdocumentunderthetermsoftheGNUFreeDocumentationLicense,Version1.1oranylaterversionpublishedbytheFreeSoftwareFoundation;withnoInvariantSections,withnoFront-CoverTexts,andwithnoBack-CoverTexts.Acopyofthelicenseisincludedin AppendixH .Inaddition,youaregrantedfullrightstousethecodeexamplesforanypurposewithoutevenhavingtocredittheauthors.Alltrademarksarepropertyoftheirrespectiveowners.Thisbookcanbepurchasedathttp://www.bartlettpublishing.com/Thisbookisnotareferencebook,itisanintroductorybook.Itisthereforenotsuitablebyitselftolearnhowtoprofessionallyprograminx86assemblylanguage,assomedetailshavebeenleftouttomakethelearningprocesssmoother.Thepointofthebookistohelpthestudentunderstandhowassemblylanguageandcomputerprogrammingworks,nottobeareferencetothesubject.Referenceinformationaboutaparticularprocessorcanbeobtainedbycontactingthecompanywhichmakesit.Toreceiveacopyofthisbookinelectronicform,pleasevisitthewebsitehttp://savannah.nongnu.org/projects/pgubook/ThissitecontainstheinstructionsfordownloadingatransparentcopyofthisbookasdenedbytheGNUFreeDocumentationLicense.

PAGE 3

TableofContents 1.Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WelcometoProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 YourTools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.ComputerArchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 StructureofComputerMemory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 TheCPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 SomeTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 InterpretingMemory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 DataAccessingMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.YourFirstPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 EnteringintheProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 OutlineofanAssemblyLanguageProgram . . . . . . . . . . . . . . . . . . . . . . . . . 22 PlanningtheProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 FindingaMaximumValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 AddressingModes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.AllAboutFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 DealingwithComplexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 HowFunctionsWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Assembly-LanguageFunctionsusingtheCCallingConvention . . . . . . . . . 52 AFunctionExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 RecursiveFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.DealingwithFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 TheUNIXFileConcept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Buffersand.bss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 StandardandSpecialFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 UsingFilesinaProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 iii

PAGE 4

Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 6.ReadingandWritingSimpleRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 WritingRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 ReadingRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 ModifyingtheRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 7.DevelopingRobustPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 WhereDoestheTimeGo? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 SomeTipsforDevelopingRobustPrograms . . . . . . . . . . . . . . . . . . . . . . . 118 HandlingErrorsEffectively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 MakingOurProgramMoreRobust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 8.SharingFunctionswithCodeLibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 UsingaSharedLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 HowSharedLibrariesWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 FindingInformationaboutLibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 UsefulFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 BuildingaSharedLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.IntermediateMemoryTopics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 HowaComputerViewsMemory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 TheMemoryLayoutofaLinuxProgram . . . . . . . . . . . . . . . . . . . . . . . . . . 149 EveryMemoryAddressisaLie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 GettingMoreMemory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 ASimpleMemoryManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 UsingourAllocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 MoreInformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 iv

PAGE 5

10.CountingLikeaComputer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Truth,Falsehood,andBinaryNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 TheProgramStatusRegister . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 OtherNumberingSystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 OctalandHexadecimalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 OrderofBytesinaWord . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 ConvertingNumbersforDisplay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 11.High-LevelLanguages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 CompiledandInterpretedLanguages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 YourFirstCProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 12.Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 WhentoOptimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 WheretoOptimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 LocalOptimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 GlobalOptimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 13.MovingOnfromHere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 FromtheBottomUp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 FromtheTopDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 FromtheMiddleOut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 SpecializedTopics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 FurtherResourcesonAssemblyLanguage . . . . . . . . . . . . . . . . . . . . . . . . . 237 v

PAGE 6

A.GUIProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 B.Commonx86Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 C.ImportantSystemCalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 D.TableofASCIICodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 E.CIdiomsinAssemblyLanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 F.UsingtheGDBDebugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 G.DocumentHistory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 H.GNUFreeDocumentationLicense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 I.PersonalDedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 vi

PAGE 7

Chapter1.IntroductionWelcometoProgrammingIloveprogramming.Ienjoythechallengetonotonlymakeaworkingprogram,buttodosowithstyle.Programmingislikepoetry.Itconveysamessage,notonlytothecomputer,buttothosewhomodifyanduseyourprogram.Withaprogram,youbuildyourownworldwithyourownrules.Youcreateyourworldaccordingtoyourconceptionofboththeproblemandthesolution.Masterfulprogrammerscreateworldswithprogramsthatareclearandsuccinct,muchlikeapoemoressay.Oneofthegreatestprogrammers,DonaldKnuth,describesprogrammingnotastellingacomputerhowtodosomething,buttellingapersonhowtheywouldinstructacomputertodosomething.Thepointisthatprogramsaremeanttobereadbypeople,notjustcomputers.Yourprogramswillbemodiedandupdatedbyotherslongafteryoumoveontootherprojects.Thus,programmingisnotasmuchaboutcommunicatingtoacomputerasitiscommunicatingtothosewhocomeafteryou.Aprogrammerisaproblem-solver,apoet,andaninstructorallatonce.Yourgoalistosolvetheproblemathand,doingsowithbalanceandtaste,andteachyoursolutiontofutureprogrammers.Ihopethatthisbookcanteachatleastsomeofthepoetryandmagicthatmakescomputingexciting.Mostintroductorybooksonprogrammingfrustratemetonoend.Attheendofthemyoucanstillask"howdoesthecomputerreallywork?"andnothaveagoodanswer.Theytendtopassovertopicsthataredifculteventhoughtheyareimportant.Iwilltakeyouthroughthedifcultissuesbecausethatistheonlywaytomoveontomasterfulprogramming.Mygoalistotakeyoufromknowingnothingaboutprogrammingtounderstandinghowtothink,write,andlearnlikeaprogrammer.Youwon'tknoweverything,butyouwillhaveabackgroundforhoweverythingtstogether.Attheendofthisbook,youshouldbeabletodothefollowing: 1

PAGE 8

Chapter1.Introduction Understandhowaprogramworksandinteractswithotherprograms Readotherpeople'sprogramsandlearnhowtheywork Learnnewprogramminglanguagesquickly LearnadvancedconceptsincomputersciencequicklyIwillnotteachyoueverything.Computerscienceisamassiveeld,especiallywhenyoucombinethetheorywiththepracticeofcomputerprogramming.However,Iwillattempttogetyoustartedonthefoundationssoyoucaneasilygowhereveryouwantafterwards.Thereissomewhatofachickenandeggprobleminteachingprogramming,especiallyassemblylanguage.Thereisalottolearn-it'salmosttoomuchtolearnalmostatonce,buteachpiecedependsonalltheothers.Therefore,youmustbepatientwithyourselfandthecomputerwhilelearningtoprogram.Ifyoudon'tunderstandsomethingthersttime,rereadit.Ifyoustilldon'tunderstandit,itissometimesbesttotakeitbyfaithandcomebacktoitlater.Oftenaftermoreexposuretoprogrammingtheideaswillmakemoresense.Don'tgetdiscouraged.It'salongclimb,butveryworthwhile.Attheendofeachchapterarethreesetsofreviewexercises.Therstsetismoreorlessregurgitation-theychecktoseeifcanyougivebackwhatyoulearnedinthechapter.Thesecondsetcontainsapplicationquestions-theychecktoseeifyoucanapplywhatyoulearnedtosolveproblems.Thenalsetistoseeifyouarecapableofbroadeningyourhorizons.Someofthesequestionsmaynotbeanswerableuntillaterinthebook,buttheygiveyousomethingstothinkabout.Otherquestionsrequiresomeresearchintooutsidesourcestodiscovertheanswer.Stillothersrequireyoutosimplyanalyzeyouroptionsandexplainabestsolution.Manyofthequestionsdon'thaverightorwronganswers,butthatdoesn'tmeantheyareunimportant.Learningtheissuesinvolvedinprogramming,learninghowtoresearchanswers,andlearninghowtolookaheadareallamajorpartofaprogrammer'swork.Ifyouhaveproblemsthatyoujustcan'tgetpast,thereisamailinglistforthis 2

PAGE 9

Chapter1.Introduction bookwherereaderscandiscussandgethelpwithwhattheyarereading.Theaddressispgubook-readers@nongnu.org.Thismailinglistisopenforanytypeofquestionordiscussionalongthelinesofthisbook.Youcansubscribetothislistbygoingtohttp://mail.nongnu.org/mailman/listinfo/pgubook-readers.YourToolsThisbookteachesassemblylanguageforx86processorsandtheGNU/Linuxoperatingsystem.ThereforewewillbegivingalloftheexamplesusingtheGNU/LinuxstandardGCCtoolset.IfyouarenotfamiliarwithGNU/LinuxandtheGCCtoolset,theywillbedescribedshortly.IfyouarenewtoLinux,youshouldcheckouttheguideavailableathttp://rute.sourceforge.net/1WhatIintendtoshowyouismoreaboutprogrammingingeneralthanusingaspecictoolsetonaspecicplatform,butstandardizingononemakesthetaskmucheasier.ThosenewtoLinuxshouldalsotrytogetinvolvedintheirlocalGNU/LinuxUser'sGroup.User'sGroupmembersareusuallyveryhelpfulfornewpeople,andwillhelpyoufromeverythingfrominstallingLinuxtolearningtouseitmostefciently.AlistingofGNU/LinuxUser'sGroupsisavailableathttp://www.linux.org/groups/AlloftheseprogramshavebeentestedusingRedHatLinux8.0,andshouldworkwithanyotherGNU/Linuxdistribution,too.2Theywillnotworkwithnon-LinuxoperatingsystemssuchasBSDorothersystems.However,alloftheskillslearnedinthisbookshouldbeeasilytransferabletoanyothersystem.IfyoudonothaveaccesstoaGNU/Linuxmachine,youcanlookforahostingproviderwhooffersaLinuxshellaccount,whichisacommand-lineonlyinterface 1. Thisisquitealargedocument.Youcertainlydon'tneedtoknoweverythingtogetstartedwiththisbook.Yousimplyneedtoknowhowtonavigatefromthecommandlineandhowtouseaneditorlikepico,emacs,orviorothers. 2. By"GNU/Linuxdistribution",Imeananx86GNU/Linuxdistribution.GNU/Linuxdis-tributionsforthePowerMacintosh,theAlphaprocessor,orotherprocessorswillnotworkwiththisbook. 3

PAGE 10

Chapter1.Introduction toaLinuxmachine.Therearemanylow-costshellaccountproviders,butyouhavetomakesurethattheymatchtherequirementsabovei.e.-Linuxonx86.SomeoneatyourlocalGNU/LinuxUser'sGroupmaybeabletogiveyouoneaswell.ShellaccountsonlyrequirethatyoualreadyhaveanInternetconnectionandatelnetprogram.IfyouuseWindows,youalreadyhaveatelnetclient-justclickonstart,thenrun,thentypeintelnet.However,itisusuallybettertodownloadPuTTYfromhttp://www.chiart.greenend.co.uk/~sgtatham/putty/becauseWindows'telnethassomeweirdproblems.TherearealotofoptionsfortheMacintosh,too.NiftyTelnetismyfavorite.Ifyoudon'thaveGNU/Linuxandcan'tndashellaccountservice,thenyoucandownloadKnoppixfromhttp://www.knoppix.org/KnoppixisaGNU/LinuxdistributionthatbootsfromCDsothatyoudon'thavetoactuallyinstallit.Onceyouaredoneusingit,youjustrebootandremovetheCDandyouarebacktoyourregularoperatingsystem.SowhatisGNU/Linux?GNU/LinuxisanoperatingsystemmodeledafterUNIX.TheGNUpartcomesfromtheGNUProjecthttp://www.gnu.org/3,whichincludesmostoftheprogramsyouwillrun,includingtheGCCtoolsetthatwewillusetoprogramwith.TheGCCtoolsetcontainsalloftheprogramsnecessarytocreateprogramsinvariouscomputerlanguages.Linuxisthenameofthekernel.Thekernelisthecorepartofanoperatingsystemthatkeepstrackofeverything.Thekernelisbothanfenceandagate.Asagate,itallowsprogramstoaccesshardwareinauniformway.Withoutthekernel,youwouldhavetowriteprogramstodealwitheverydevicemodelevermade.Thekernelhandlesalldevice-specicinteractionssoyoudon'thaveto.Italsohandlesleaccessandinteractionbetweenprocesses.Forexample,whenyoutype,yourtypinggoesthroughseveralprogramsbeforeithitsyoureditor.First,thekerneliswhathandlesyourhardware,soitisthersttoreceivenoticeaboutthekeypress.Thekeyboardsendsinscancodestothekernel,whichthenconvertsthemtotheactualletters,numbers,andsymbolstheyrepresent.Ifyouareusingawindowing 3. TheGNUProjectisaprojectbytheFreeSoftwareFoundationtoproduceacomplete,freeoperatingsystem. 4

PAGE 11

Chapter1.Introduction systemlikeMicrosoftWindowsortheXWindowSystem,thenthewindowingsystemreadsthekeypressfromthekernel,anddeliversittowhateverprogramiscurrentlyinfocusontheuser'sdisplay.Example1-1.HowthecomputerprocesseskeyboardsigalsKeyboard->Kernel->Windowingsystem->ApplicationprogramThekernelalsocontrolstheowofinformationbetweenprograms.Thekernelisaprogram'sgatetotheworldaroundit.Everytimethatdatamovesbetweenprocesses,thekernelcontrolsthemessaging.Inourkeyboardexampleabove,thekernelwouldhavetobeinvolvedforthewindowingsystemtocommunicatethekeypresstotheapplicationprogram.Asafence,thekernelpreventsprogramsfromaccidentallyoverwritingeachother'sdataandfromaccessinglesanddevicesthattheydon'thavepermissionto.Itlimitstheamountofdamageapoorly-writtenprogramcandotootherrunningprograms.Inourcase,thekernelisLinux.Now,thekernelallbyitselfwon'tdoanything.Youcan'tevenbootupacomputerwithjustakernel.Thinkofthekernelasthewaterpipesforahouse.Withoutthepipes,thefaucetswon'twork,butthepipesareprettyuselessiftherearenofaucets.Together,theuserapplicationsfromtheGNUprojectandotherplacesandthekernelLinuxmakeuptheentireoperatingsystem,GNU/Linux.Forthemostpart,thisbookwillbeusingthecomputer'slow-levelassemblylanguage.Thereareessentiallythreekindsoflanguages:MachineLanguage Thisiswhatthecomputeractuallyseesanddealswith.Everycommandthecomputerseesisgivenasanumberorsequenceofnumbers. 5

PAGE 12

Chapter1.Introduction AssemblyLanguage Thisisthesameasmachinelanguage,exceptthecommandnumbershavebeenreplacedbylettersequenceswhichareeasiertomemorize.Othersmallthingsaredonetomakeiteasieraswell.High-LevelLanguage High-levellanguagesaretheretomakeprogrammingeasier.Assemblylanguagerequiresyoutoworkwiththemachineitself.High-levellanguagesallowyoutodescribetheprograminamorenaturallanguage.Asinglecommandinahigh-levellanguageusuallyisequivalenttoseveralcommandsinanassemblylanguage.Inthisbookwewilllearnassemblylanguage,althoughwewillcoverabitofhigh-levellanguages.Hopefullybylearningassemblylanguage,yourunderstandingofhowprogrammingandcomputersworkwillputyouastepahead. 6

PAGE 13

Chapter2.ComputerArchitectureBeforelearninghowtoprogram,youneedtorstunderstandhowacomputerinterpretsprograms.Youdon'tneedadegreeinelectricalengineering,butyouneedtounderstandsomebasics.ModerncomputerarchitectureisbasedoffofanarchitecturecalledtheVonNeumannarchitecture,namedafteritscreator.TheVonNeumannarchitecturedividesthecomputerupintotwomainparts-theCPUforCentralProcessingUnitandthememory.Thisarchitectureisusedinallmoderncomputers,includingpersonalcomputers,supercomputers,mainframes,andevencellphones.StructureofComputerMemoryTounderstandhowthecomputerviewsmemory,imagineyourlocalpostofce.TheyusuallyhavearoomlledwithPOBoxes.Theseboxesaresimilartocomputermemoryinthateacharenumberedsequencesofxed-sizestoragelocations.Forexample,ifyouhave256megabytesofcomputermemory,thatmeansthatyourcomputercontainsroughly256millionxed-sizestoragelocations.Or,touseouranalogy,256millionPOBoxes.Eachlocationhasanumber,andeachlocationhasthesame,xed-lengthsize.ThedifferencebetweenaPOBoxandcomputermemoryisthatyoucanstorealldifferentkindsofthingsinaPOBox,butyoucanonlystoreasinglenumberinacomputermemorystoragelocation. 7

PAGE 14

Chapter2.ComputerArchitecture MemorylocationsarelikePOBoxesYoumaywonderwhyacomputerisorganizedthisway.Itisbecauseitissimpletoimplement.Ifthecomputerwerecomposedofalotofdifferently-sizedlocations,orifyoucouldstoredifferentkindsofdatainthem,itwouldbedifcultandexpensivetoimplement.Thecomputer'smemoryisusedforanumberofdifferentthings.Alloftheresultsofanycalculationsarestoredinmemory.Infact,everythingthatis"stored"isstoredinmemory.Thinkofyourcomputerathome,andimaginewhatallisstoredinyourcomputer'smemory. Thelocationofyourcursoronthescreen Thesizeofeachwindowonthescreen Theshapeofeachletterofeachfontbeingused Thelayoutofallofthecontrolsoneachwindow Thegraphicsforallofthetoolbaricons 8

PAGE 15

Chapter2.ComputerArchitecture Thetextforeacherrormessageanddialogbox Thelistgoesonandon...Inadditiontoallofthis,theVonNeumannarchitecturespeciesthatnotonlycomputerdatashouldliveinmemory,buttheprogramsthatcontrolthecomputer'soperationshouldlivethere,too.Infact,inacomputer,thereisnodifferencebetweenaprogramandaprogram'sdataexcepthowitisusedbythecomputer.Theyarebothstoredandaccessedthesameway.TheCPUSohowdoesthecomputerfunction?Obviously,simplystoringdatadoesn'tdomuchhelp-youneedtobeabletoaccess,manipulate,andmoveit.That'swheretheCPUcomesin.TheCPUreadsininstructionsfrommemoryoneatatimeandexecutesthem.Thisisknownasthefetch-executecycle.TheCPUcontainsthefollowingelementstoaccomplishthis: ProgramCounter InstructionDecoder Databus General-purposeregisters ArithmeticandlogicunitTheprogramcounterisusedtotellthecomputerwheretofetchthenextinstructionfrom.Wementionedearlierthatthereisnodifferencebetweenthewaydataandprogramsarestored,theyarejustinterpreteddifferentlybytheCPU.Theprogramcounterholdsthememoryaddressofthenextinstructiontobeexecuted.TheCPUbeginsbylookingattheprogramcounter,andfetchingwhatevernumberisstoredinmemoryatthelocationspecied.Itisthenpassedontotheinstruction 9

PAGE 16

Chapter2.ComputerArchitecture decoderwhichguresoutwhattheinstructionmeans.Thisincludeswhatprocessneedstotakeplaceaddition,subtraction,multiplication,datamovement,etc.andwhatmemorylocationsaregoingtobeinvolvedinthisprocess.Computerinstructionsusuallyconsistofboththeactualinstructionandthelistofmemorylocationsthatareusedtocarryitout.Nowthecomputerusesthedatabustofetchthememorylocationstobeusedinthecalculation.ThedatabusistheconnectionbetweentheCPUandmemory.Itistheactualwirethatconnectsthem.Ifyoulookatthemotherboardofthecomputer,thewiresthatgooutfromthememoryareyourdatabus.Inadditiontothememoryontheoutsideoftheprocessor,theprocessoritselfhassomespecial,high-speedmemorylocationscalledregisters.Therearetwokindsofregisters-generalregistersandspecial-purposeregisters.General-purposeregistersarewherethemainactionhappens.Addition,subtraction,multiplication,comparisions,andotheroperationsgenerallyusegeneral-purposeregistersforprocessing.However,computershaveveryfewgeneral-purposeregisters.Mostinformationisstoredinmainmemory,broughtintotheregistersforprocessing,andthenputbackintomemorywhentheprocessingiscompleted.special-purposeregistersareregisterswhichhaveveryspecicpurposes.Wewilldiscusstheseaswecometothem.NowthattheCPUhasretrievedallofthedataitneeds,itpassesonthedataandthedecodedinstructiontothearithmeticandlogicunitforfurtherprocessing.Heretheinstructionisactuallyexecuted.Aftertheresultsofthecomputationhavebeencalculated,theresultsarethenplacedonthedatabusandsenttotheappropriatelocationinmemoryorinaregister,asspeciedbytheinstruction.Thisisaverysimpliedexplanation.Processorshaveadvancedquiteabitinrecentyears,andarenowmuchmorecomplex.Althoughthebasicoperationisstillthesame,itiscomplicatedbytheuseofcachehierarchies,superscalarprocessors,pipelining,branchprediction,out-of-orderexecution,microcodetranslation,coprocessors,andotheroptimizations.Don'tworryifyoudon'tknowwhatthosewordsmean,youcanjustusethemasInternetsearchtermsifyouwant 10

PAGE 17

Chapter2.ComputerArchitecture tolearnmoreabouttheCPU.SomeTermsComputermemoryisanumberedsequenceofxed-sizestoragelocations.Thenumberattachedtoeachstoragelocationiscalledit'saddress.Thesizeofasinglestoragelocationiscalledabyte.Onx86processors,abyteisanumberbetween0and255.Youmaybewonderinghowcomputerscandisplayandusetext,graphics,andevenlargenumberswhenalltheycandoisstorenumbersbetween0and255.Firstofall,specializedhardwarelikegraphicscardshavespecialinterpretationsofeachnumber.Whendisplayingtothescreen,thecomputerusesASCIIcodetablestotranslatethenumbersyouaresendingitintoletterstodisplayonthescreen,witheachnumbertranslatingtoexactlyoneletterornumeral.1Forexample,thecapitalletterAisrepresentedbythenumber65.Thenumeral1isrepresentedbythenumber49.So,toprintout"HELLO",youwouldactuallygivethecomputerthesequenceofnumbers72,69,76,76,79.Toprintoutthenumber100,youwouldgivethecomputerthesequenceofnumbers49,48,48.AlistofASCIIcharactersandtheirnumericcodesisfoundin AppendixD .InadditiontousingnumberstorepresentASCIIcharacters,youastheprogrammergettomakethenumbersmeananythingyouwantthemto,aswell.Forexample,ifIamrunningastore,IwoulduseanumbertorepresenteachitemIwasselling.EachnumberwouldbelinkedtoaseriesofothernumberswhichwouldbetheASCIIcodesforwhatIwantedtodisplaywhentheitemswerescannedin.Iwouldhavemorenumbersfortheprice,howmanyIhaveininventory,andsoon. 1. WiththeadventofinternationalcharactersetsandUnicode,thisisnotentirelytrueanymore.However,forthepurposesofkeepingthissimpleforbeginners,wewillusetheassumptionthatonenumbertranslatesdirectlytoonecharacter.Formoreinformation,see AppendixD 11

PAGE 18

Chapter2.ComputerArchitecture Sowhataboutifweneednumberslargerthan255?Wecansimplyuseacombinationofbytestorepresentlargernumbers.Twobytescanbeusedtorepresentanynumberbetween0and65536.Fourbytescanbeusedtorepresentanynumberbetween0and4294967295.Now,itisquitedifculttowriteprogramstostickbytestogethertoincreasethesizeofyournumbers,andrequiresabitofmath.Luckily,thecomputerwilldoitforusfornumbersupto4byteslong.Infact,four-bytenumbersarewhatwewillworkwithbydefault.Wementionedearlierthatinadditiontotheregularmemorythatthecomputerhas,italsohasspecial-purposestoragelocationscalledregisters.Registersarewhatthecomputerusesforcomputation.Thinkofaregisterasaplaceonyourdesk-itholdsthingsyouarecurrentlyworkingon.Youmayhavelotsofinformationtuckedawayinfoldersanddrawers,butthestuffyouareworkingonrightnowisonthedesk.Registerskeepthecontentsofnumbersthatyouarecurrentlymanipulating.Onthecomputersweareusing,registersareeachfourbyteslong.Thesizeofatypicalregisteriscalledacomputer'swordsize.x86processorshavefour-bytewords.Thismeansthatitismostnaturalonthesecomputerstodocomputationsfourbytesatatime.Thisgivesusroughly4billionvalues.Addressesarealsofourbyteswordlong,andthereforealsotintoaregister.x86processorscanaccessupto4294967296bytesifenoughmemoryisinstalled.Noticethatthismeansthatwecanstoreaddressesthesamewaywestoreanyothernumber.Infact,thecomputercan'ttellthedifferencebetweenavaluethatisanaddress,avaluethatisanumber,avaluethatisanASCIIcode,oravaluethatyouhavedecidedtouseforanotherpurpose.AnumberbecomesanASCIIcodewhenyouattempttodisplayit.Anumberbecomesanaddresswhenyoutrytolookupthebyteitpointsto.Takeamomenttothinkaboutthis,becauseitiscrucialtounderstandinghowcomputerprogramswork.Addresseswhicharestoredinmemoryarealsocalledpointers,becauseinsteadofhavingaregularvalueinthem,theypointyoutoadifferentlocationinmemory.Aswe'vementioned,computerinstructionsarealsostoredinmemory.Infact, 12

PAGE 19

Chapter2.ComputerArchitecture theyarestoredexactlythesamewaythatotherdataisstored.Theonlywaythecomputerknowsthatamemorylocationisaninstructionisthataspecial-purposeregistercalledtheinstructionpointerpointstothematonepointoranother.Iftheinstructionpointerpointstoamemoryword,itisloadedasaninstruction.Otherthanthat,thecomputerhasnowayofknowingthedifferencebetweenprogramsandothertypesofdata.2InterpretingMemoryComputersareveryexact.Becausetheyareexact,programmershavetobeequallyexact.Acomputerhasnoideawhatyourprogramissupposedtodo.Therefore,itwillonlydoexactlywhatyoutellittodo.IfyouaccidentallyprintoutaregularnumberinsteadoftheASCIIcodesthatmakeupthenumber'sdigits,thecomputerwillletyou-andyouwillwindupwithjibberishonyourscreenitwilltrytolookupwhatyournumberrepresentsinASCIIandprintthat.Ifyoutellthecomputertostartexecutinginstructionsatalocationcontainingdatainsteadofprograminstructions,whoknowshowthecomputerwillinterpretthat-butitwillcertainlytry.Thecomputerwillexecuteyourinstructionsintheexactorderyouspecify,evenifitdoesn'tmakesense.Thepointis,thecomputerwilldoexactlywhatyoutellit,nomatterhowlittlesenseitmakes.Therefore,asaprogrammer,youneedtoknowexactlyhowyouhaveyourdataarrangedinmemory.Remember,computerscanonlystorenumbers,soletters,pictures,music,webpages,documents,andanythingelsearejustlongsequencesofnumbersinthecomputer,whichparticularprogramsknowhowtointerpret.Forexample,saythatyouwantedtostorecustomerinformationinmemory.Onewaytodosowouldbetosetamaximumsizeforthecustomer'snameandaddress-say50ASCIIcharactersforeach,whichwouldbe50bytesforeach.Then,after 2. Notethatherewearetalkingaboutgeneralcomputertheory.Someprocessorsandop-eratingsystemsactuallymarktheregionsofmemorythatcanbeexecutedwithaspecialmarkerthatindicatesthis. 13

PAGE 20

Chapter2.ComputerArchitecture that,haveanumberforthecustomer'sageandtheircustomerid.Inthiscase,youwouldhaveablockofmemorythatwouldlooklikethis:StartofRecord:Customer'snamebytes-startofrecordCustomer'saddressbytes-startofrecord+50bytesCustomer'sageword-4bytes-startofrecord+100bytesCustomer'sidnumberword-4bytes-startofrecord+104bytesThisway,giventheaddressofacustomerrecord,youknowwheretherestofthedatalies.However,itdoeslimitthecustomer'snameandaddresstoonly50ASCIIcharacterseach.Whatifwedidn'twanttospecifyalimit?Anotherwaytodothiswouldbetohaveinourrecordpointerstothisinformation.Forexample,insteadofthecustomer'sname,wewouldhaveapointertotheirname.Inthiscase,thememorywouldlooklikethis:StartofRecord:Customer'snamepointerword-startofrecordCustomer'saddresspointerword-startofrecord+4Customer'sageword-startofrecord+8Customer'sidnumberword-startofrecord+12Theactualnameandaddresswouldbestoredelsewhereinmemory.Thisway,itiseasytotellwhereeachpartofthedataisfromthestartoftherecord,withoutexplicitlylimittingthesizeofthenameandaddress.Ifthelengthoftheeldswithinourrecordscouldchange,wewouldhavenoideawherethenexteldstarted.Becauserecordswouldbedifferentsizes,itwouldalsobehardtondwherethenextrecordbegan.Therefore,almostallrecordsareofxedlengths.Variable-lengthdataisusuallystoreseparatelyfromtherestoftherecord. 14

PAGE 21

Chapter2.ComputerArchitecture DataAccessingMethodsProcessorshaveanumberofdifferentwaysofaccessingdata,knownasaddressingmodes.Thesimplestmodeisimmediatemode,inwhichthedatatoaccessisembeddedintheinstructionitself.Forexample,ifwewanttoinitializearegisterto0,insteadofgivingthecomputeranaddresstoreadthe0from,wewouldspecifyimmediatemode,andgiveitthenumber0.Intheregisteraddressingmode,theinstructioncontainsaregistertoaccess,ratherthanamemorylocation.Therestofthemodeswilldealwithaddresses.Inthedirectaddressingmode,theinstructioncontainsthememoryaddresstoaccess.Forexample,Icouldsay,pleaseloadthisregisterwiththedataataddress2002.Thecomputerwouldgodirectlytobytenumber2002andcopythecontentsintoourregister.Intheindexedaddressingmode,theinstructioncontainsamemoryaddresstoaccess,andalsospeciesanindexregistertooffsetthataddress.Forexample,wecouldspecifyaddress2002andanindexregister.Iftheindexregistercontainsthenumber4,theactualaddressthedataisloadedfromwouldbe2006.Thisway,ifyouhaveasetofnumbersstartingatlocation2002,youcancyclebetweeneachofthemusinganindexregister.Onx86processors,youcanalsospecifyamultiplierfortheindex.Thisallowsyoutoaccessmemoryabyteatatimeorawordatatimebytes.Ifyouareaccessinganentireword,yourindexwillneedtobemultipliedby4togettheexactlocationofthefourthelementfromyouraddress.Forexample,ifyouwantedtoaccessthefourthbytefromlocation2002,youwouldloadyourindexregisterwith3remember,westartcountingat0andsetthemultiplierto1sinceyouaregoingabyteatatime.Thiswouldgetyoulocation2005.However,ifyouwantedtoaccessthefourthwordfromlocation2002,youwouldloadyourindexregisterwith3andsetthemultiplierto4.Thiswouldloadfromlocation2014-thefourthword.Takethetimetocalculatetheseyourselftomakesureyouunderstandhowitworks.Intheindirectaddressingmode,theinstructioncontainsaregisterthatcontainsapointertowherethedatashouldbeaccessed.Forexample,ifweusedindirect 15

PAGE 22

Chapter2.ComputerArchitecture addressingmodeandspeciedthe%eaxregister,andthe%eaxregistercontainedthevalue4,whatevervaluewasatmemorylocation4wouldbeused.Indirectaddressing,wewouldjustloadthevalue4,butinindirectaddressing,weuse4astheaddresstousetondthedatawewant.Finally,thereisthebasepointeraddressingmode.Thisissimilartoindirectaddressing,butyoualsoincludeanumbercalledtheoffsettoaddtotheregister'svaluebeforeusingitforlookup.Wewillusethismodequiteabitinthisbook.In theSectioncalledInterpretingMemory wediscussedhavingastructureinmemoryholdingcustomerinformation.Let'ssaywewantedtoaccessthecustomer'sage,whichwastheeighthbyteofthedata,andwehadtheaddressofthestartofthestructureinaregister.Wecouldusebasepointeraddressingandspecifytheregisterasthebasepointer,and8asouroffset.Thisisalotlikeindexedaddressing,withthedifferencethattheoffsetisconstantandthepointerisheldinaregister,andinindexedaddressingtheoffsetisinaregisterandthepointerisconstant.Thereareotherformsofaddressing,butthesearethemostimportantones.ReviewKnowtheConcepts Describethefetch-executecycle. Whatisaregister?Howwouldcomputationbemoredifcultwithoutregisters? Howdoyourepresentnumberslargerthan255? Howbigaretheregistersonthemachineswewillbeusing? Howdoesacomputerknowhowtointerpretagivenbyteorsetofbytesofmemory? 16

PAGE 23

Chapter2.ComputerArchitecture Whataretheaddressingmodesandwhataretheyusedfor? Whatdoestheinstructionpointerdo?UsetheConcepts Whatdatawouldyouuseinanemployeerecord?Howwouldyoulayitoutinmemory? IfIhadthepointerthethebeginningoftheemployeerecordabove,andwantedtoaccessaparticularpieceofdatainsideofit,whataddressingmodewouldIuse? Inbasepointeraddressingmode,ifyouhavearegisterholdingthevalue3122,andanoffsetof20,whataddresswouldyoubetryingtoaccess? Inindexedaddressingmode,ifthebaseaddressis6512,theindexregisterhasa5,andthemultiplieris4,whataddresswouldyoubetryingtoaccess? Inindexedaddressingmode,ifthebaseaddressis123472,theindexregisterhasa0,andthemultiplieris4,whataddresswouldyoubetryingtoaccess? Inindexedaddressingmode,ifthebaseaddressis9123478,theindexregisterhasa20,andthemultiplieris1,whataddresswouldyoubetryingtoaccess?GoingFurther Whataretheminimumnumberofaddressingmodesneededforcomputation? Whyincludeaddressingmodesthataren'tstrictlyneeded? Researchandthendescribehowpipeliningoroneoftheothercomplicatingfactorsaffectsthefetch-executecycle. 17

PAGE 24

Chapter2.ComputerArchitecture Researchandthendescribethetradeoffsbetweenxed-lengthinstructionsandvariable-lengthinstructions. 18

PAGE 25

Chapter3.YourFirstProgramsInthischapteryouwilllearntheprocessforwritingandbuildingLinuxassembly-languageprograms.Inaddition,youwilllearnthestructureofassembly-languageprograms,andafewassembly-languagecommands.Asyougothroughthischapter,youmaywanttoreferalsoto AppendixB and AppendixF .Theseprogramsmayoverwhelmyouatrst.However,gothroughthemwithdiligence,readthemandtheirexplanationsasmanytimesasnecessary,andyouwillhaveasolidfoundationofknowledgetobuildon.Pleasetinkeraroundwiththeprogramsasmuchasyoucan.Evenifyourtinkeringdoesnotwork,everyfailurewillhelpyoulearn.EnteringintheProgramOkay,thisrstprogramissimple.Infact,it'snotgoingtodoanythingbutexit!It'sshort,butitshowssomebasicsaboutassemblylanguageandLinuxprogramming.Youneedtoentertheprograminaneditorexactlyaswritten,withthelenameexit.s.Theprogramfollows.Don'tworryaboutnotunderstandingit.Thissectiononlydealswithtypingitinandrunningit.In theSectioncalledOutlineofanAssemblyLanguageProgram wewilldescribehowitworks.#PURPOSE:Simpleprogramthatexitsandreturnsa#statuscodebacktotheLinuxkernel##INPUT:none##OUTPUT:returnsastatuscode.Thiscanbeviewed#bytyping##echo$?# 19

PAGE 26

Chapter3.YourFirstPrograms #afterrunningtheprogram##VARIABLES:#%eaxholdsthesystemcallnumber#%ebxholdsthereturnstatus#.section.data.section.text.globl_start_start:movl$1,%eax#thisisthelinuxkernelcommand#numbersystemcallforexiting#aprogrammovl$0,%ebx#thisisthestatusnumberwewill#returntotheoperatingsystem.#Changethisaroundanditwill#returndifferentthingsto#echo$?int$0x80#thiswakesupthekerneltorun#theexitcommandWhatyouhavetypediniscalledthesourcecode.Sourcecodeisthehuman-readableformofaprogram.Inordertotransformitintoaprogramthatacomputercanrun,weneedtoassembleandlinkit.Therststepistoassembleit.Assemblingistheprocessthattransformswhatyoutypedintoinstructionsforthemachine.Themachineitselfonlyreadssetsofnumbers,buthumanspreferwords.Anassemblylanguageisamorehuman-readableformoftheinstructionsacomputerunderstands.Assembling 20

PAGE 27

Chapter3.YourFirstPrograms transformsthehuman-readableleintoamachine-readableone.Toassemblytheprogramtypeinthecommandasexit.s-oexit.oasisthecommandwhichrunstheassembler,exit.sisthesourcele,and-oexit.otellstheassembletoputit'soutputintheleexit.o.exit.oisanobjectle.Anobjectleiscodethatisinthemachine'slanguage,buthasnotbeencompletelyputtogether.Inmostlargeprograms,youwillhaveseveralsourceles,andyouwillconverteachoneintoanobjectle.Thelinkeristheprogramthatisresponsibleforputtingtheobjectlestogetherandaddinginformationtoitsothatthekernelknowshowtoloadandrunit.Inourcase,weonlyhaveoneobjectle,sothelinkerisonlyaddingtheinformationtoenableittorun.Tolinkthele,enterthecommandldexit.o-oexitldisthecommandtorunthelinker,exit.oistheobjectlewewanttolink,and-oexitinstructsthelinkertooutputthenewprogramintoalecalledexit.1Ifanyofthesecommandsreportederrors,youhaveeithermistypedyourprogramorthecommand.Aftercorrectingtheprogram,youhavetore-runallthecommands.Youmustalwaysre-assembleandre-linkprogramsafteryoumodifythesourceleforthechangestooccurintheprogram.Youcanrunexitbytypinginthecommand./exitThe./isusedtotellthecomputerthattheprogramisn'tinoneofthenormalprogramdirectories,butisthecurrentdirectoryinstead2.You'llnoticewhenyoutypethiscommand,theonlythingthathappensisthatyou'llgotothenextline. 1. IfyouarenewtoLinuxandUNIX,youmaynotbeawarethatlesdon'thavetohaveextensions.Infact,whileWindowsusesthe.exeextensiontosignifyanexecutableprogram,UNIXexecutablesusuallyhavenoextension. 2. .referstothecurrentdirectoryinLinuxandUNIXsystems. 21

PAGE 28

Chapter3.YourFirstPrograms That'sbecausethisprogramdoesnothingbutexit.However,immediatelyafteryouruntheprogram,ifyoutypeinecho$?Itwillsay0.WhatishappeningisthateveryprogramwhenitexitsgivesLinuxanexitstatuscode,whichtellsitifeverythingwentallright.Ifeverythingwasokay,itreturns0.UNIXprogramsreturnnumbersotherthanzerotoindicatefailureorothererrors,warnings,orstatuses.Theprogrammerdetermineswhateachnumbermeans.Youcanviewthiscodebytypinginecho$?.Inthefollowingsectionwewilllookatwhateachpartofthecodedoes.OutlineofanAssemblyLanguageProgramTakealookattheprogramwejustentered.Atthebeginningtherearelotsoflinesthatbeginwithhashes#.Thesearecomments.Commentsarenottranslatedbytheassembler.Theyareusedonlyfortheprogrammertotalktoanyonewholooksatthecodeinthefuture.Mostprogramsyouwritewillbemodiedbyothers.Getintothehabitofwritingcommentsinyourcodethatwillhelpthemunderstandbothwhytheprogramexistsandhowitworks.Alwaysincludethefollowinginyourcomments: Thepurposeofthecode Anoverviewoftheprocessinginvolved Anythingstrangeyourprogramdoesandwhyitdoesit3Afterthecomments,thenextlinesays 3. You'llndthatmanyprogramsendupdoingthingsstrangeways.Usuallythereisarea-sonforthat,but,unfortunately,programmersneverdocumentsuchthingsintheircomments.So,futureprogrammerseitherhavetolearnthereasonthehardwaybymodifyingthecodeandwatchingitbreak,orjustleavingitalonewhetheritisstillneededornot.Youshouldalwaysdocumentanystrangebehavioryourprogramperforms.Unfortunately,guringoutwhatisstrangeandwhatisstraightforwardcomesmostlywithexperience. 22

PAGE 29

Chapter3.YourFirstPrograms .section.dataAnythingstartingwithaperiodisn'tdirectlytranslatedintoamachineinstruction.Instead,it'saninstructiontotheassembleritself.Thesearecalledassemblerdirectivesorpseudo-operationsbecausetheyarehandledbytheassemblerandarenotactuallyrunbythecomputer.The.sectioncommandbreaksyourprogramupintosections.Thiscommandstartsthedatasection,whereyoulistanymemorystorageyouwillneedfordata.Ourprogramdoesn'tuseany,sowedon'tneedthesection.It'sjusthereforcompleteness.Almosteveryprogramyouwriteinthefuturewillhavedata.Rightafterthisyouhave.section.textwhichstartsthetextsection.Thetextsectionofaprogramiswheretheprograminstructionslive.Thenextinstructionis.globl_startThisinstructstheassemblerthat_startisimportanttoremember._startisasymbol,whichmeansthatitisgoingtobereplacedbysomethingelseeitherduringassemblyorlinking.Symbolsaregenerallyusedtomarklocationsofprogramsordata,soyoucanrefertothembynameinsteadofbytheirlocationnumber.Imagineifyouhadtorefertoeverymemorylocationbyit'saddress.Firstofall,itwouldbeveryconfusingbecauseyouwouldhavetomemorizeorlookupthenumericmemoryaddressofeverypieceofcodeordata.Inaddition,everytimeyouhadtoinsertapieceofdataorcodeyouwouldhavetochangealltheaddressesinyourprogram!Symbolsareusedsothattheassemblerandlinkercantakecareofkeepingtrackofaddresses,andyoucanconcentrateonwritingyourprogram. 23

PAGE 30

Chapter3.YourFirstPrograms .globlmeansthattheassemblershouldn'tdiscardthissymbolafterassembly,becausethelinkerwillneedit._startisaspecialsymbolthatalwaysneedstobemarkedwith.globlbecauseitmarksthelocationofthestartoftheprogram.Withoutmarkingthislocationinthisway,whenthecomputerloadsyourprogramitwon'tknowwheretobeginrunningyourprogram.Thenextline_start:denesthevalueofthe_startlabel.Alabelisasymbolfollowedbyacolon.Labelsdeneasymbol'svalue.Whentheassemblerisassemblingtheprogram,ithastoassigneachdatavalueandinstructionanaddress.Labelstelltheassemblertomakethesymbol'svaluebewhereverthenextinstructionordataelementwillbe.Thisway,iftheactualphysicallocationofthedataorinstructionchanges,youdon'thavetorewriteanyreferencestoit-thesymbolautomaticallygetsthenewvalue.Nowwegetintoactualcomputerinstructions.Therstsuchinstructionisthis:movl$1,%eaxWhentheprogramruns,thisinstructiontransfersthenumber1intothe%eaxregister.Inassemblylanguage,manyinstructionshaveoperands.movlhastwooperands-thesourceandthedestination.Inthiscase,thesourceistheliteralnumber1,andthedestinationisthe%eaxregister.Operandscanbenumbers,memorylocationreferences,orregisters.Differentinstructionsallowdifferenttypesofoperands.See AppendixB formoreinformationonwhichinstructionstakewhichkindsofoperands.Onmostinstructionswhichhavetwooperands,therstoneisthesourceoperandandthesecondoneisthedestination.Notethatinthesecases,thesourceoperandisnotmodiedatall.Otherinstructionsofthistypeare,forexample,addl,subl,andimull.Theseadd/subtract/multiplythesourceoperandfrom/to/bythedestinationoperandandandsavetheresultinthedestinationoperand.Other 24

PAGE 31

Chapter3.YourFirstPrograms instructionsmayhaveanoperandhardcodedin.idivl,forexample,requiresthatthedividendbein%eax,and%edxbezero,andthequotientisthentransferredto%eaxandtheremainderto%edx.However,thedivisorcanbeanyregisterormemorylocation.Onx86processors,thereareseveralgeneral-purposeregisters4allofwhichcanbeusedwithmovl: %eax %ebx %ecx %edx %edi %esiInadditiontothesegeneral-purposeregisters,therearealsoseveralspecial-purposeregisters,including: %ebp %esp %eip %eflagsWe'lldiscusstheselater,justbeawarethattheyexist.5Someoftheseregisters, 4. Notethatonx86processors,eventhegeneral-purposeregistershavesomespecialpur-poses,orusedtobeforeitwent32-bit.However,thesearegeneral-purposeregistersformostinstructions.Eachofthemhasatleastoneinstructionwhereitisusedinaspecialway.However,formostofthem,thoseinstructionsaren'tcoveredinthisbook. 5. Youmaybewondering,whydoalloftheseregistersbeginwiththelettere?Thereasonisthatearlygenerationsofx86processorswere16bitsratherthan32bits.Therefore,theregisterswereonlyhalfthelengththeyarenow.Inlatergenerationsofx86processors,thesizeoftheregistersdoubled.Theykepttheoldnamestorefertothersthalfoftheregister, 25

PAGE 32

Chapter3.YourFirstPrograms like%eipand%eflagscanonlybeaccessedthroughspecialinstructions.Theotherscanbeaccessedusingthesameinstructionsasgeneral-purposeregisters,buttheyhavespecialmeanings,specialuses,oraresimplyfasterwhenusedinaspecicway.So,themovlinstructionmovesthenumber1into%eax.Thedollar-signinfrontoftheoneindicatesthatwewanttouseimmediatemodeaddressingreferbackto theSectioncalledDataAccessingMethodsinChapter2 .Withoutthedollar-signitwoulddodirectaddressing,loadingwhatevernumberisataddress1.Wewanttheactualnumber1loadedin,sowehavetouseimmediatemode.Thereasonwearemovingthenumber1into%eaxisbecausewearepreparingtocalltheLinuxKernel.Thenumber1isthenumberoftheexitsystemcall.Wewilldiscusssystemcallsinmoredepthsoon,butbasicallytheyarerequestsfortheoperatingsystem'shelp.Normalprogramscan'tdoeverything.Manyoperationssuchascallingotherprograms,dealingwithles,andexitinghavetobehandledbytheoperatingsystemthroughsystemcalls.Whenyoumakeasystemcall,whichwewilldoshortly,thesystemcallnumberhastobeloadedinto%eaxforacompletelistingofsystemcallsandtheirnumbers,see AppendixC .Dependingonthesystemcall,otherregistersmayhavetohavevaluesinthemaswell.Notethatsystemcallsisnottheonlyuseoreventhemainuseofregisters.Itisjusttheonewearedealingwithinthisrstprogram.Laterprogramswilluseregistersforregularcomputation.Theoperatingsystem,however,usuallyneedsmoreinformationthanjustwhichcalltomake.Forexample,whendealingwithles,theoperatingsystemneedstoknowwhichleyouaredealingwith,whatdatayouwanttowrite,andotherdetails.Theextradetails,calledparametersarestoredinotherregisters.Inthecaseoftheexitsystemcall,theoperatingsystemrequiresastatuscodebeloaded andaddedanetorefertotheextendedversionsoftheregister.Usuallyyouwillonlyusetheextendedversions.Newermodelsalsooffera64-bitmode,whichdoublesthesizeoftheseregistersyetagainandusesanrprextoindicatethelargerregistersi.e.%raxisthe64-bitversionof%eax.However,theseprocessorsarenotwidelyused,andarenotcoveredinthisbook. 26

PAGE 33

Chapter3.YourFirstPrograms in%ebx.Thisvalueisthenreturnedtothesystem.Thisisthevalueyouretrievedwhenyoutypedecho$?.So,weload%ebxwith0bytypingthefollowing:movl$0,%ebxNow,loadingregisterswiththesenumbersdoesn'tdoanythingitself.Registersareusedforallsortsofthingsbesidessystemcalls.Theyarewhereallprogramlogicsuchasaddition,subtraction,andcomparisonstakeplace.Linuxsimplyrequiresthatcertainregistersbeloadedwithcertainparametervaluesbeforemakingasystemcall.%eaxisalwaysrequiredtobeloadedwiththesystemcallnumber.Fortheotherregisters,however,eachsystemcallhasdifferentrequirements.Intheexitsystemcall,%ebxisrequiredtobeloadedwiththeexitstatus.Wewilldiscussdifferentsystemcallsastheyareneeded.Foralistofcommonsystemcallsandwhatisrequiredtobeineachregister,see AppendixC Thenextinstructionisthe"magic"one.Itlookslikethis:int$0x80Theintstandsforinterrupt.The0x80istheinterruptnumbertouse.6Aninterruptinterruptsthenormalprogramow,andtransferscontrolfromourprogramtoLinuxsothatitwilldoasystemcall.7.YoucanthinkofitaslikesignalingBatmanorLarry-Boy8,ifyouprefer.Youneedsomethingdone,yousendthesignal,andthenhecomestotherescue.Youdon'tcarehowhedoeshiswork-it'smoreorlessmagic-andwhenhe'sdoneyou'rebackincontrol.Inthiscase,allwe'redoingisaskingLinuxtoterminatetheprogram,inwhichcasewe 6. Youmaybewonderingwhyit's0x80insteadofjust80.Thereasonisthatthenumberiswritteninhexadecimal.Inhexadecimal,asingledigitcanhold16valuesinsteadofthenormal10.Thisisdonebyutilizingthelettersathroughfinadditiontotheregulardigits.arepresents10,brepresents11,andsoon.0x10representsthenumber16,andsoon.Thiswillbediscussedmoreindepthlater,butjustbeawarethatnumbersstartingwith0xareinhexadecimal.TackingonanHattheendisalsosometimesusedinstead,butwewon'tdothatinthisbook.Formoreinformationaboutthis,see Chapter10 7. Actually,theinterrupttransferscontroltowhoeversetupaninterrupthandlerfortheinterruptnumber.InthecaseofLinux,allofthemaresettobehandledbytheLinuxkernel. 8. Ifyoudon'twatchVeggieTales,youshould.StartwithDaveandtheGiantPickle. 27

PAGE 34

Chapter3.YourFirstPrograms won'tbebackincontrol.Ifwedidn'tsignaltheinterrupt,thennosystemcallwouldhavebeenperformed.QuickSystemCallReview:Torecap-OperatingSystemfeaturesareaccessedthroughsystemcalls.Theseareinvokedbysettinguptheregistersinaspecialwayandissuingtheinstructionint$0x80.Linuxknowswhichsystemcallwewanttoaccessbywhatwestoredinthe%eaxregister.Eachsystemcallhasotherrequirementsastowhatneedstobestoredintheotherregisters.Systemcallnumber1istheexitsystemcall,whichrequiresthestatuscodetobeplacedin%ebx.Nowthatyou'veassembled,linked,run,andexaminedtheprogram,youshouldmakesomebasicedits.Dothingslikechangethenumberthatisloadedinto%ebx,andwatchitcomeoutattheendwithecho$?.Don'tforgettoassembleandlinkitagainbeforerunningit.Addsomecomments.Don'tworry,theworsethingthatwouldhappenisthattheprogramwon'tassembleorlink,orwillfreezeyourscreen.That'sjustpartoflearning!PlanningtheProgramInournextprogramwewilltrytondthemaximumofalistofnumbers.Computersareverydetail-oriented,soinordertowritetheprogramwewillhavetohaveplannedoutanumberofdetails.Thesedetailsinclude: Wherewilltheoriginallistofnumbersbestored? Whatprocedurewillweneedtofollowtondthemaximumnumber? Howmuchstoragedoweneedtocarryoutthatprocedure? Willallofthestoragetintoregisters,ordoweneedtousesomememoryaswell? 28

PAGE 35

Chapter3.YourFirstPrograms Youmightnotthinkthatsomethingassimpleasndingthemaximumnumberfromalistwouldtakemuchplanning.Youcanusuallytellpeopletondthemaximumnumber,andtheycandosowithlittletrouble.However,ourmindsareusedtoputtingtogethercomplextasksautomatically.Computersneedtobeinstructedthroughtheprocess.Inaddition,wecanusuallyholdanynumberofthingsinourmindwithoutmuchtrouble.Weusuallydon'tevenrealizewearedoingit.Forexample,ifyouscanalistofnumbersforthemaximum,youwillprobablykeepinmindboththehighestnumberyou'veseensofar,andwhereyouareinthelist.Whileyourminddoesthisautomatically,withcomputersyouhavetoexplicitlysetupstorageforholdingthecurrentpositiononthelistandthecurrentmaximumnumber.Youalsohaveotherproblemssuchashowtoknowwhentostop.Whenreadingapieceofpaper,youcanstopwhenyourunoutofnumbers.However,thecomputeronlycontainsnumbers,soithasnoideawhenithasreachedthelastofyournumbers.Incomputers,youhavetoplaneverystepoftheway.So,let'sdoalittleplanning.Firstofall,justforreference,let'snametheaddresswherethelistofnumbersstartsasdata_items.Let'ssaythatthelastnumberinthelistwillbeazero,soweknowwheretostop.Wealsoneedavaluetoholdthecurrentpositioninthelist,avaluetoholdthecurrentlistelementbeingexamined,andthecurrenthighestvalueonthelist.Let'sassigneachofthesearegister: %ediwillholdthecurrentpositioninthelist. %ebxwillholdthecurrenthighestvalueinthelist. %eaxwillholdthecurrentelementbeingexamined.Whenwebegintheprogramandlookattherstiteminthelist,sincewehaven'tseenanyotheritems,thatitemwillautomaticallybethecurrentlargestelementinthelist.Also,wewillsetthecurrentpositioninthelisttobezero-therstelement.Fromthen,wewillfollowthefollowingsteps: 29

PAGE 36

Chapter3.YourFirstPrograms 1. Checkthecurrentlistelement%eaxtoseeifit'szerotheterminatingelement. 2. Ifitiszero,exit. 3. Increasethecurrentposition%edi. 4. Loadthenextvalueinthelistintothecurrentvalueregister%eax.Whataddressingmodemightweusehere?Why? 5. Comparethecurrentvalue%eaxwiththecurrenthighestvalue%ebx. 6. Ifthecurrentvalueisgreaterthanthecurrenthighestvalue,replacethecurrenthighestvaluewiththecurrentvalue. 7. Repeat.Thatistheprocedure.ManytimesinthatprocedureImadeuseoftheword"if".Theseplacesarewheredecisionsaretobemade.Yousee,thecomputerdoesn'tfollowtheexactsamesequenceofinstructionseverytime.Dependingonwhich"if"sarecorrect,thecomputermayfollowadifferentsetofinstructions.Thesecondtimethrough,itmightnothavethehighestvalue.Inthatcase,itwillskipstep6,butcomebacktostep7.Ineverycaseexceptthelastone,itwillskipstep2.Inmorecomplicatedprograms,theskippingaroundincreasesdramatically.These"if"sareaclassofinstructionscalledowcontrolinstructions,becausetheytellthecomputewhichstepstofollowandwhichpathstotake.Inthepreviousprogram,wedidnothaveanyowcontrolinstructions,astherewasonlyonepossiblepathtotake-exit.Thisprogramismuchmoredynamicinthatitisdirectedbydata.Dependingonwhatdataitreceives,itwillfollowdifferentinstructionpaths.Inthisprogram,thiswillbeaccomplishedbytwodifferentinstructions,theconditionaljumpandtheunconditionaljump.Theconditionaljumpchangespathsbasedontheresultsofapreviouscomparisonorcalculation.Theunconditionaljumpjustgoesdirectlytoadifferentpathnomatterwhat.Theunconditionaljumpmayseemuseless,butitisverynecessarysincealloftheinstructionswillbelaid 30

PAGE 37

Chapter3.YourFirstPrograms outonaline.Ifapathneedstoconvergebacktothemainpath,itwillhavetodothisbyanunconditionaljump.Wewillseemoreofbothofthesejumpsinthenextsection.Anotheruseofowcontrolisinimplementingloops.Aloopisapieceofprogramcodethatismeanttoberepeated.Inourexample,therstpartoftheprogramsettingthecurrentpositionto0andloadingthecurrenthighestvaluewiththecurrentvaluewasonlydoneonce,soitwasn'taloop.However,thenextpartisrepeatedoverandoveragainforeverynumberinthelist.Itisonlyleftwhenwehavecometothelastelement,indicatedbyazero.Thisiscalledaloopbecauseitoccursoverandoveragain.Itisimplementedbydoingunconditionaljumpstothebeginningoftheloopattheendoftheloop,whichcausesittostartover.However,youhavetoalwaysremembertohaveaconditionaljumptoexittheloopsomewhere,ortheloopwillcontinueforever!Thisconditioniscalledaninniteloop.Ifweaccidentallyleftoutstep1,2,or3,theloopandourprogramwouldneverend.Inthenextsection,wewillimplementthisprogramthatwehaveplanned.Programplanningsoundscomplicated-anditis,tosomedegree.Whenyourststartprogramming,it'softenhardtoconvertournormalthoughtprocessintoaprocedurethatthecomputercanunderstand.Weoftenforgetthenumberof"temporarystoragelocations"thatourmindsareusingtoprocessproblems.Asyoureadandwriteprograms,however,thiswilleventuallybecomeverynaturaltoyou.Justhavepatience.FindingaMaximumValueEnterthefollowingprogramasmaximum.s:#PURPOSE:Thisprogramfindsthemaximumnumberofa#setofdataitems.# 31

PAGE 38

Chapter3.YourFirstPrograms #VARIABLES:Theregistershavethefollowinguses:##%edi-Holdstheindexofthedataitembeingexamined#%ebx-Largestdataitemfound#%eax-Currentdataitem##Thefollowingmemorylocationsareused:##data_items-containstheitemdata.A0isused#toterminatethedata#.section.datadata_items:#Thesearethedataitems.long3,67,34,222,45,75,54,34,44,33,22,11,66,0.section.text.globl_start_start:movl$0,%edi#move0intotheindexregistermovldata_items,%edi,4,%eax#loadthefirstbyteofdatamovl%eax,%ebx#sincethisisthefirstitem,%eaxis#thebiggeststart_loop:#startloopcmpl$0,%eax#checktoseeifwe'vehittheendjeloop_exitincl%edi#loadnextvaluemovldata_items,%edi,4,%eaxcmpl%ebx,%eax#comparevaluesjlestart_loop#jumptoloopbeginningifthenew#oneisn'tbiggermovl%eax,%ebx#movethevalueasthelargest 32

PAGE 39

Chapter3.YourFirstPrograms jmpstart_loop#jumptoloopbeginningloop_exit:#%ebxisthestatuscodefortheexitsystemcall#anditalreadyhasthemaximumnumbermovl$1,%eax#1istheexitsyscallint$0x80Now,assembleandlinkitwiththesecommands:asmaximum.s-omaximum.oldmaximum.o-omaximumNowrunit,andcheckit'sstatus../maximumecho$?You'llnoticeitreturnsthevalue222.Let'stakealookattheprogramandwhatitdoes.Ifyoulookinthecomments,you'llseethattheprogramndsthemaximumofasetofnumbersaren'tcommentswonderful!.Youmayalsonoticethatinthisprogramweactuallyhavesomethinginthedatasection.Theselinesarethedatasection:data_items:#Thesearethedataitems.long3,67,34,222,45,75,54,34,44,33,22,11,66,0Letslookatthis.data_itemsisalabelthatreferstothelocationthatfollowsit.Then,thereisadirectivethatstartswith.long.Thatcausestheassemblertoreservememoryforthelistofnumbersthatfollowit.data_itemsreferstothelocationoftherstone.Becausedata_itemsisalabel,anytimeinourprogramwhereweneedtorefertothisaddresswecanusethedata_itemssymbol,andtheassemblerwillsubstituteitwiththeaddresswherethenumbersstartduringassembly.Forexample,theinstructionmovldata_items,%eaxwouldmove 33

PAGE 40

Chapter3.YourFirstPrograms thevalue3into%eax.Thereareseveraldifferenttypesofmemorylocationsotherthan.longthatcanbereserved.Themainonesareasfollows:.byte Bytestakeuponestoragelocationforeachnumber.Theyarelimitedtonumbersbetween0and255..int Intswhichdifferfromtheintinstructiontakeuptwostoragelocationsforeachnumber.Thesearelimittedtonumbersbetween0and65535.9.long Longstakeupfourstoragelocations.Thisisthesameamountofspacetheregistersuse,whichiswhytheyareusedinthisprogram.Theycanholdnumbersbetween0and4294967295..ascii The.asciidirectiveistoenterincharactersintomemory.Characterseachtakeuponestoragelocationtheyareconvertedintobytesinternally.So,ifyougavethedirective.ascii"Hellothere",theassemblerwouldreserve12storagelocationsbytes.TherstbytecontainsthenumericcodeforH,thesecondbytecontainsthenumericcodefore,andsoforth.Thelastcharacterisrepresentedby,anditistheterminatingcharacteritwillneverdisplay,itjusttellsotherpartsoftheprogramthatthat'stheendofthecharacters.Lettersandnumbersthatstartwithabackslashrepresentcharactersthatarenottypeableonthekeyboardoreasilyviewableonthescreen.Forexample,nreferstothe"newline"characterwhichcausesthe 9. NotethatnonumbersinassemblylanguageoranyothercomputerlanguageI'veseenhavecommasembeddedinthem.So,alwayswritenumberslike65535,andneverlike65,535. 34

PAGE 41

Chapter3.YourFirstPrograms computertostartoutputonthenextlineandtreferstothe"tab"character.Allofthelettersinan.asciidirectiveshouldbeinquotes.Inourexample,theassemblerreserves14.longs,onerightafteranother.Sinceeachlongtakesup4bytes,thatmeansthatthewholelisttakesup56bytes.Thesearethenumberswewillbesearchingthroughtondthemaximum.data_itemsisusedbytheassemblertorefertotheaddressoftherstofthesevalues.Takenotethatthelastdataiteminthelistisazero.Idecidedtouseazerototellmyprogramthatithashittheendofthelist.Icouldhavedonethisotherways.Icouldhavehadthesizeofthelisthard-codedintotheprogram.Also,Icouldhaveputthelengthofthelistastherstitem,orinaseparatelocation.Ialsocouldhavemadeasymbolwhichmarkedthelastlocationofthelistitems.NomatterhowIdoit,Imusthavesomemethodofdeterminingtheendofthelist.Thecomputerknowsnothing-itcanonlydowhatitstold.It'snotgoingtostopprocessingunlessIgiveitsomesortofsignal.Otherwiseitwouldcontinueprocessingpasttheendofthelistintothedatathatfollowsit,andeventolocationswherewehaven'tputanydata.Noticethatwedon'thavea.globldeclarationfordata_items.Thisisbecauseweonlyrefertotheselocationswithintheprogram.Nootherleorprogramneedstoknowwheretheyarelocated.Thisisincontrasttothe_startsymbol,whichLinuxneedstoknowwhereitissothatitknowswheretobegintheprogram'sexecution.It'snotanerrortowrite.globldata_items,it'sjustnotnecessary.Anyway,playaroundwiththislineandaddyourownnumbers.Eventhoughtheyare.long,theprogramwillproducestrangeresultsifanynumberisgreaterthan255,becausethat'sthelargestallowedexitstatus.Alsonoticethatifyoumovethe0toearlierinthelist,therestgetignored.Rememberthatanytimeyouchangethesourcele,youhavetore-assembleandre-linkyourprogram.Dothisnowandseetheresults.Allright,we'veplayedwiththedataalittlebit.Nowlet'slookatthecode.Inthecommentsyouwillnoticethatwe'vemarkedsomevariablesthatweplantouse.Avariableisadedicatedstoragelocationusedforaspecicpurpose,usually 35

PAGE 42

Chapter3.YourFirstPrograms givenadistinctnamebytheprogrammer.Wetalkedabouttheseintheprevioussection,butdidn'tgivethemaname.Inthisprogram,wehaveseveralvariables: avariableforthecurrentmaximumnumberfound avariableforwhichnumberofthelistwearecurrentlyexamining,calledtheindex avariableholdingthecurrentnumberbeingexaminedInthiscase,wehavefewenoughvariablesthatwecanholdthemallinregisters.Inlargerprograms,youhavetoputtheminmemory,andthenmovethemtoregisterswhenyouarereadytousethem.Wewilldiscusshowtodothatlater.Whenpeoplestartoutprogramming,theyusuallyunderestimatethenumberofvariablestheywillneed.Peoplearenotusedtohavingtothinkthrougheverydetailofaprocess,andthereforeleaveoutneededvariablesintheirrstprogrammingattempts.Inthisprogram,weareusing%ebxasthelocationofthelargestitemwe'vefound.%ediisusedastheindextothecurrentdataitemwe'relookingat.Now,let'stalkaboutwhatanindexis.Whenwereadtheinformationfromdata_items,wewillstartwiththerstonedataitemnumber0,thengotothesecondonedataitemnumber1,thenthethirddataitemnumber2,andsoon.Thedataitemnumberistheindexofdata_items.You'llnoticethattherstinstructionwegivetothecomputeris:movl$0,%ediSinceweareusing%ediasourindex,andwewanttostartlookingattherstitem,weload%ediwith0.Now,thenextinstructionistricky,butcrucialtowhatwe'redoing.Itsays:movldata_items,%edi,4,%eaxNowtounderstandthisline,youneedtokeepseveralthingsinmind: 36

PAGE 43

Chapter3.YourFirstPrograms data_itemsisthelocationnumberofthestartofournumberlist. Eachnumberisstoredacross4storagelocationsbecausewedeclareditusing.long %ediisholding0atthispointSo,basicallywhatthislinedoesissay,"startatthebeginningofdata_items,andtaketherstitemnumberbecause%ediis0,andrememberthateachnumbertakesupfourstoragelocations."Thenitstoresthatnumberin%eax.Thisishowyouwriteindexedaddressingmodeinstructionsinassemblylanguage.Theinstructioninageneralformisthis:movlBEGINNINGADDRESS,%INDEXREGISTER,WORDSIZEInourcasedata_itemswasourbeginningaddress,%ediwasourindexregister,and4wasourwordsize.Thistopicisdiscussedfurtherin theSectioncalledAddressingModes .Ifyoulookatthenumbersindata_items,youwillseethatthenumber3isnowin%eax.If%ediwassetto1,thenumber67wouldbein%eax,andifitwassetto2,thenumber34wouldbein%eax,andsoforth.Verystrangethingswouldhappenifweusedanumberotherthan4asthesizeofourstoragelocations.10Thewayyouwritethisisveryawkward,butifyouknowwhateachpiecedoes,it'snottoodifcult.Formoreinformationaboutthis,see theSectioncalledAddressingModes Let'slookatthenextline:movl%eax,%ebx 10. Theinstructiondoesn'treallyuse4forthesizeofthestoragelocations,althoughlookingatitthatwayworksforourpurposesnow.It'sactuallywhat'scalledamultiplier.basically,thewayitworksisthatyoustartatthelocationspeciedbydata_items,thenyouadd%edi*4storagelocations,andretrievethenumberthere.Usually,youusethesizeofthenumbersasyourmultiplier,butinsomecircumstancesyou'llwanttodootherthings. 37

PAGE 44

Chapter3.YourFirstPrograms Wehavetherstitemtolookatstoredin%eax.Sinceitistherstitem,weknowit'sthebiggestonewe'velookedat.Westoreitin%ebx,sincethat'swherewearekeepingthelargestnumberfound.Also,eventhoughmovlstandsformove,itactuallycopiesthevalue,so%eaxand%ebxbothcontainthestartingvalue.11Nowwemoveintoaloop.Aloopisasegmentofyourprogramthatmightrunmorethanonce.Wehavemarkedthestartinglocationoftheloopinthesymbolstart_loop.Thereasonwearedoingaloopisbecausewedon'tknowhowmanydataitemswehavetoprocess,buttheprocedurewillbethesamenomatterhowmanythereare.Wedon'twanttohavetorewriteourprogramforeverylistlengthpossible.Infact,wedon'tevenwanttohavetowriteoutcodeforacomparisonforeverylistitem.Therefore,wehaveasinglesectionofcodealoopthatweexecuteoverandoveragainforeveryelementindata_items.Intheprevioussection,weoutlinedwhatthisloopneededtodo.Let'sreview: Checktoseeifthecurrentvaluebeinglookedatiszero.Ifso,thatmeansweareattheendofourdataandshouldexittheloop. Wehavetoloadthenextvalueofourlist. Wehavetoseeifthenextvalueisbiggerthanourcurrentbiggestvalue. Ifitis,wehavetocopyittothelocationweareholdingthelargestvaluein. Nowweneedtogobacktothebeginningoftheloop.Okay,sonowletsgotothecode.Wehavethebeginningoftheloopmarkedwithstart_loop.Thatissoweknowwheretogobacktoattheendofourloop.Thenwehavetheseinstructions:cmpl$0,%eaxjeend_loop 11. Also,thelinmovlstandsformovelongsincewearemovingavaluethattakesupfourstoragelocations. 38

PAGE 45

Chapter3.YourFirstPrograms Thecmplinstructioncomparesthetwovalues.Here,wearecomparingthenumber0tothenumberstoredin%eaxThiscompareinstructionalsoaffectsaregisternotmentionedhere,the%eflagsregister.Thisisalsoknownasthestatusregister,andhasmanyuseswhichwewilldiscusslater.Justbeawarethattheresultofthecomparisonisstoredinthestatusregister.Thenextlineisaowcontrolinstructionwhichsaystojumptotheend_looplocationifthevaluesthatwerejustcomparedareequalthat'swhattheeofjemeans.Itusesthestatusregistertoholdthevalueofthelastcomparison.Weusedje,buttherearemanyjumpstatementsthatyoucanuse:je Jumpifthevalueswereequaljg Jumpifthesecondvaluewasgreaterthantherstvalue12jge Jumpifthesecondvaluewasgreaterthanorequaltotherstvaluejl Jumpifthesecondvaluewaslessthantherstvaluejle Jumpifthesecondvaluewaslessthanorequaltotherstvaluejmp Jumpnomatterwhat.Thisdoesnotneedtobepreceededbyacomparison. 12. noticethatthecomparisonistoseeifthesecondvalueisgreaterthantherst.Iwouldhavethoughtittheotherwayaround.Youwillndalotofthingslikethiswhenlearningprogramming.Itoccursbecausedifferentthingsmakesensetodifferentpeople.Anyway,you'lljusthavetomemorizesuchthingsandgoon. 39

PAGE 46

Chapter3.YourFirstPrograms Thecompletelistisdocumentedin AppendixB .Inthiscase,wearejumpingif%eaxholdsthevalueofzero.Ifso,wearedoneandwegotoloop_exit.13Ifthelastloadedelementwasnotzero,wegoontothenextinstructions:incl%edimovldata_items,%edi,4,%eaxIfyourememberfromourpreviousdiscussion,%edicontainstheindextoourlistofvaluesindata_items.inclincrementsthevalueof%edibyone.Thenthemovlisjustliketheonewedidbeforehand.However,sincewealreadyincremented%edi,%eaxisgettingthenextvaluefromthelist.Now%eaxhasthenextvaluetobetested.So,let'stestit!cmpl%ebx,%eaxjlestart_loopHerewecompareourcurrentvalue,storedin%eaxtoourbiggestvaluesofar,storedin%ebx.Ifthecurrentvalueislessorequaltoourbiggestvaluesofar,wedon'tcareaboutit,sowejustjumpbacktothebeginningoftheloop.Otherwise,weneedtorecordthatvalueasthelargestone:movl%eax,%ebxjmpstart_loopwhichmovesthecurrentvalueinto%ebx,whichweareusingtostorethecurrentlargestvalue,andstartstheloopoveragain.Okay,sotheloopexecutesuntilitreachesa0,whenitjumpstoloop_exit.ThispartoftheprogramcallstheLinuxkerneltoexit.Ifyourememberfromthelast 13. Thenamesofthesesymbolscanbeanythingyouwantthemtobe,aslongastheyonlycontainlettersandtheunderscorecharacter_.Theonlyonethatisforcedis_start,andpossiblyothersthatyoudeclarewith.globl.However,ifitsasymbolyoudeneandonlyyouuse,feelfreetocallitanythingyouwantthatisadequatelydescriptiverememberthatotherswillhavetomodifyyourcodelater,andwillhavetogureoutwhatyoursymbolsmean. 40

PAGE 47

Chapter3.YourFirstPrograms program,whenyoucalltheoperatingsystemrememberit'slikesignalingBatman,youstorethesystemcallnumberin%eaxfortheexitcall,andstoretheothervaluesintheotherregisters.Theexitcallrequiresthatweputourexitstatusin%ebxWealreadyhavetheexitstatustheresinceweareusing%ebxasourlargestnumber,soallwehavetodoisload%eaxwiththenumberoneandcallthekerneltoexit.Likethis:movl$1,%eaxint0x80Okay,thatwasalotofworkandexplanation,especiallyforsuchasmallprogram.Buthey,you'relearningalot!Now,readthroughthewholeprogramagain,payingspecialattentiontothecomments.Makesurethatyouunderstandwhatisgoingonateachline.Ifyoudon'tunderstandaline,gobackthroughthissectionandgureoutwhatthelinemeans.Youmightalsograbapieceofpaper,andgothroughtheprogramstep-by-step,recordingeverychangetoeveryregister,soyoucanseemoreclearlywhatisgoingon.AddressingModesIn theSectioncalledDataAccessingMethodsinChapter2 welearnedthedifferenttypesofaddressingmodesavailableforuseinassemblylanguage.Thissectionwilldealwithhowthoseaddressingmodesarerepresentedinassemblylanguageinstructions.Thegeneralformofmemoryaddressreferencesisthis:ADDRESS_OR_OFFSET%BASE_OR_OFFSET,%INDEX,MULTIPLIERAlloftheeldsareoptional.Tocalculatetheaddress,simplyperformthefollowingcalculation: 41

PAGE 48

Chapter3.YourFirstPrograms FINALADDRESS=ADDRESS_OR_OFFSET+%BASE_OR_OFFSET+MULTIPLIER*%INDEXADDRESS_OR_OFFSETandMULTIPLIERmustbothbeconstants,whiletheothertwomustberegisters.Ifanyofthepiecesisleftout,itisjustsubstitutedwithzerointheequation.Alloftheaddressingmodesmentionedin theSectioncalledDataAccessingMethodsinChapter2 exceptimmediate-modecanberepresentedinthisfashion.directaddressingmode ThisisdonebyonlyusingtheADDRESS_OR_OFFSETportion.Example:movlADDRESS,%eaxThisloads%eaxwiththevalueatmemoryaddressADDRESS.indexedaddressingmode ThisisdonebyusingtheADDRESS_OR_OFFSETandthe%INDEXportion.Youcanuseanygeneral-purposeregisterastheindexregister.Youcanalsohaveaconstantmultiplierof1,2,or4fortheindexregister,tomakeiteasiertoindexbybytes,double-bytes,andwords.Forexample,let'ssaythatwehadastringofbytesasstring_startandwantedtoaccessthethirdoneanindexof2sincewestartcountingtheindexatzero,and%ecxheldthevalue2.Ifyouwantedtoloaditinto%eaxyoucoulddothefollowing:movlstring_start,%ecx,1,%eaxThisstartsatstring_start,andadds1*%ecxtothataddress,andloadsthevalueinto%eax.indirectaddressingmode Indirectaddressingmodeloadsavaluefromtheaddressindicatedbyaregister.Forexample,if%eaxheldanaddress,wecouldmovethevalueat 42

PAGE 49

Chapter3.YourFirstPrograms thataddressto%ebxbydoingthefollowing:movl%eax,%ebxbasepointeraddressingmode Base-pointeraddressingissimilartoindirectaddressing,exceptthatitaddsaconstantvaluetotheaddressintheregister.Forexample,ifyouhavearecordwheretheagevalueis4bytesintotherecord,andyouhavetheaddressoftherecordin%eax,youcanretrievetheageinto%ebxbyissuingthefollowinginstruction:movl4%eax,%ebximmediatemode Immediatemodeisverysimple.Itdoesnotfollowthegeneralformwehavebeenusing.Immediatemodeisusedtoloaddirectvaluesintoregistersormemorylocations.Forexample,ifyouwantedtoloadthenumber12into%eax,youwouldsimplydothefollowing:movl$12,%eaxNoticethattoindicateimmediatemode,weusedadollarsigninfrontofthenumber.Ifwedidnot,itwouldbedirectaddressingmode,inwhichcasethevaluelocatedatmemorylocation12wouldbeloadedinto%eaxratherthanthenumber12itself.registeraddressingmode Registermodesimplymovesdatainoroutofaregister.Inallofourexamples,registeraddressingmodewasusedfortheotheroperand.Theseaddressingmodesareveryimportant,aseverymemoryaccesswilluseoneofthese.Everymodeexceptimmediatemodecanbeusedaseitherthesourceordestinationoperand.Immediatemodecanonlybeasourceoperand. 43

PAGE 50

Chapter3.YourFirstPrograms Inadditiontothesemodes,therearealsodifferentinstructionsfordifferentsizesofvaluestomove.Forexample,wehavebeenusingmovltomovedataawordatatime.inmanycases,youwillonlywanttomovedataabyteatatime.Thisisaccomplishedbytheinstructionmovb.However,sincetheregisterswehavediscussedareword-sizedandnotbyte-sized,youcannotusethefullregister.Instead,youhavetouseaportionoftheregister.Takeforinstance%eax.Ifyouonlywantedtoworkwithtwobytesatatime,youcouldjustuse%ax.%axistheleast-signicanthalfi.e.-thelastpartofthenumberofthe%eaxregister,andisusefulwhendealingwithtwo-bytequantities.%axisfurtherdividedupinto%aland%ah.%alistheleast-signicantbyteof%ax,and%ahisthemostsignicantbyte.14Loadingavalueinto%eaxwillwipeoutwhateverwasin%aland%ahandalso%ax,since%axismadeupofthem.Similarly,loadingavalueintoeither%alor%ahwillcorruptanyvaluethatwasformerlyin%eax.Basically,it'swisetoonlyusearegisterforeitherabyteoraword,butneverbothatthesametime. 14. Whenwetalkaboutthemostorleastsignicantbyte,itmaybealittleconfusing.Let'stakethenumber5432.Inthatnumber,54isthemostsignicanthalfofthatnumberand32istheleastsignicanthalf.Youcan'tquitedivideitlikethatforregisters,sincetheyoperateonbase2ratherthanbase10numbers,butthat'sthebasicidea.Formoreinformationonthistopic,see Chapter10 44

PAGE 51

Chapter3.YourFirstPrograms Layoutofthe%eaxregisterForamorecomprehensivelistofinstructions,see AppendixB .ReviewKnowtheConcepts Whatdoesifmeanifalineintheprogramstartswiththe'#'character? Whatisthedifferencebetweenanassemblylanguageleandanobjectcodele? Whatdoesthelinkerdo? Howdoyouchecktheresultstatuscodeofthelastprogramyouran? Whatisthedifferencebetweenmovl$1,%eaxandmovl1,%eax? 45

PAGE 52

Chapter3.YourFirstPrograms Whichregisterholdsthesystemcallnumber? Whatareindexesusedfor? Whydoindexesusuallystartat0? IfIissuedthecommandmovldata_items,%edi,4,%eaxanddata_itemswasaddress3634and%ediheldthevalue13,whataddresswouldyoubeusingtomoveinto%eax? Listthegeneral-purposeregisters. Whatisthedifferencebetweenmovlandmovb? Whatisowcontrol? Whatdoesaconditionaljumpdo? Whatthingsdoyouhavetoplanforwhenwritingaprogram? Gothrougheveryinstructionandlistwhataddressingmodeisbeingusedforeachoperand.UsetheConcepts Modifytherstprogramtoreturnthevalue3. Modifythemaximumprogramtondtheminimuminstead. Modifythemaximumprogramtousethenumber255toendthelistratherthanthenumber0 Modifythemaximumprogramtouseanendingaddressratherthanthenumber0toknowwhentostop. Modifythemaximumprogramtousealengthcountratherthanthenumber0toknowwhentostop. 46

PAGE 53

Chapter3.YourFirstPrograms Whatwouldtheinstructionmovl_start,%eaxdo?Bespecic,basedonyourknowledgeofbothaddressingmodesandthemeaningof_start.Howwouldthisdifferfromtheinstructionmovl$_start,%eax?GoingFurther Modifytherstprogramtoleaveofftheintinstructionline.Assemble,link,andexecutethenewprogram.Whaterrormessagedoyouget.Whydoyouthinkthismightbe? Sofar,wehavediscussedthreeapproachestondingtheendofthelist-usingaspecialnumber,usingtheendingaddress,andusingthelengthcount.Whichapproachdoyouthinkisbest?Why?Whichapproachwouldyouuseifyouknewthatthelistwassorted?Why? 47

PAGE 54

Chapter3.YourFirstPrograms 48

PAGE 55

Chapter4.AllAboutFunctionsDealingwithComplexityIn Chapter3 ,theprogramswewroteonlyconsistedofonesectionofcode.However,ifwewroterealprogramslikethat,itwouldbeimpossibletomaintainthem.Itwouldbereallydifculttogetmultiplepeopleworkingontheproject,asanychangeinonepartmightadverselyaffectanotherpartthatanotherdeveloperisworkingon.Toassistprogrammersinworkingtogetheringroups,itisnecessarytobreakprogramsapartintoseparatepieces,whichcommunicatewitheachotherthroughwell-denedinterfaces.Thisway,eachpiececanbedevelopedandtestedindependentlyoftheothers,makingiteasierformultipleprogrammerstoworkontheproject.Programmersusefunctionstobreaktheirprogramsintopieceswhichcanbeindependentlydevelopedandtested.Functionsareunitsofcodethatdoadenedpieceofworkonspeciedtypesofdata.Forexample,inawordprocessorprogram,Imayhaveafunctioncalledhandle_typed_characterwhichisactivatedwheneverausertypesinakey.Thedatathefunctionuseswouldprobablybethekeypressitselfandthedocumenttheusercurrentlyhasopen.Thefunctionwouldthenmodifythedocumentaccordingtothekeypressitwastoldabout.Thedataitemsafunctionisgiventoprocessarecalledit'sparameters.Inthewordprocessingexample,thekeywhichwaspressedandthedocumentwouldbeconsideredparameterstothehandle_typed_charactersfunction.Theparameterlistandtheprocessingexpectationsofafunctionwhatitisexpectedtodowiththeparametersarecalledthefunction'sinterface.Muchcaregoesintodesigningfunctioninterfaces,becauseiftheyarecalledfrommanyplaceswithinaproject,itisdifculttochangethemifnecessary.Atypicalprogramiscomposedofhundredsorthousandsoffunctions,eachwitha 49

PAGE 56

Chapter4.AllAboutFunctions small,well-denedtasktoperform.However,ultimatelytherearethingsthatyoucannotwritefunctionsforwhichmustbeprovidedbythesystem.Thosearecalledprimitivefunctionsorjustprimitives-theyarethebasicswhicheverythingelseisbuiltoffof.Forexample,imagineaprogramthatdrawsagraphicaluserinterface.Therehastobeafunctiontocreatethemenus.Thatfunctionprobablycallsotherfunctionstowritetext,towriteicons,topaintthebackground,calculatewherethemousepointeris,etc.However,ultimately,theywillreachasetofprimitivesprovidedbytheoperatingsystemtodobasiclineorpointdrawing.Programmingcaneitherbeviewedasbreakingalargeprogramdownintosmallerpiecesuntilyougettotheprimitivefunctions,orincrementallybuildingfunctionsontopofprimitivesuntilyougetthelargepictureinfocus.Inassemblylanguage,theprimitivesareusuallythesamethingasthesystemcalls,eventhoughsystemcallsaren'ttruefunctionsaswewilltalkaboutinthischapter.HowFunctionsWorkFunctionsarecomposedofseveraldifferentpieces:functionname Afunction'snameisasymbolthatrepresentstheaddresswherethefunction'scodestarts.Inassemblylanguage,thesymbolisdenedbytypingthethefunction'snameasalabelbeforethefunction'scode.Thisisjustlikelabelsyouhaveusedforjumping.functionparameters Afunction'sparametersarethedataitemsthatareexplicitlygiventothefunctionforprocessing.Forexample,inmathematics,thereisasinefunction.Ifyouweretoaskacomputertondthesineof2,sinewouldbethefunction'sname,and2wouldbetheparameter.Somefunctionshave 50

PAGE 57

Chapter4.AllAboutFunctions manyparameters,othershavenone.1localvariables Localvariablesaredatastoragethatafunctionuseswhileprocessingthatisthrownawaywhenitreturns.It'skindoflikeascratchpadofpaper.Functionsgetanewpieceofpapereverytimetheyareactivated,andtheyhavetothrowitawaywhentheyarenishedprocessing.Localvariablesofafunctionarenotaccessibletoanyotherfunctionwithinaprogram.staticvariables Staticvariablesaredatastoragethatafunctionuseswhileprocessingthatisnotthrownawayafterwards,butisreusedforeverytimethefunction'scodeisactivated.Thisdataisnotaccessibletoanyotherpartoftheprogram.Staticvariablesaregenerallynotusedunlessabsolutelynecessary,astheycancauseproblemslateron.globalvariables Globalvariablesaredatastoragethatafunctionusesforprocessingwhicharemanagedoutsidethefunction.Forexample,asimpletexteditormayputtheentirecontentsoftheleitisworkingoninaglobalvariablesoitdoesn'thavetobepassedtoeveryfunctionthatoperatesonit.2Congurationvaluesarealsooftenstoredinglobalvariables.returnaddress Thereturnaddressisan"invisible"parameterinthatitisn'tdirectlyusedduringthefunction.Thereturnaddressisaparameterwhichtellsthefunction 1. Functionparameterscanalsobeusedtoholdpointerstodatathatthefunctionwantstosendbacktotheprogram. 2. Thisisgenerallyconsideredbadpractice.Imagineifaprogramiswrittenthisway,andinthenextversiontheydecidedtoallowasingleinstanceoftheprogrameditmultipleles.Eachfunctionwouldthenhavetobemodiedsothatthelethatwasbeingmanipulatedwouldbepassedasaparameter.Ifyouhadsimplypasseditasaparametertobeginwith,mostofyourfunctionscouldhavesurvivedyourupgradeunchanged. 51

PAGE 58

Chapter4.AllAboutFunctions wheretoresumeexecutingafterthefunctioniscompleted.Thisisneededbecausefunctionscanbecalledtodoprocessingfrommanydifferentpartsofyourprogram,andthefunctionneedstobeabletogetbacktowhereveritwascalledfrom.Inmostprogramminglanguages,thisparameterispassedautomaticallywhenthefunctioniscalled.Inassemblylanguage,thecallinstructionhandlespassingthereturnaddressforyou,andrethandlesusingthataddresstoreturnbacktowhereyoucalledthefunctionfrom.returnvalue Thereturnvalueisthemainmethodoftransferringdatabacktothemainprogram.Mostprogramminglanguagesonlyallowasinglereturnvalueforafunction.Thesepiecesarepresentinmostprogramminglanguages.Howyouspecifyeachpieceisdifferentineachone,however.Thewaythatthevariablesarestoredandtheparametersandreturnvaluesaretransferredbythecomputervariesfromlanguagetolanguageaswell.Thisvarianceisknownasalanguage'scallingconvention,becauseitdescribeshowfunctionsexpecttogetandreceivedatawhentheyarecalled.3Assemblylanguagecanuseanycallingconventionitwantsto.Youcanevenmakeoneupyourself.However,ifyouwanttointeroperatewithfunctionswritteninotherlanguages,youhavetoobeytheircallingconventions.WewillusethecallingconventionoftheCprogramminglanguageforourexamplesbecauseitisthemostwidelyused,andbecauseitisthestandardforLinuxplatforms. 3. Aconventionisawayofdoingthingsthatisstandardized,butnotforciblyso.Forex-ample,itisaconventionforpeopletoshakehandswhentheymeet.IfIrefusetoshakehandswithyou,youmaythinkIdon'tlikeyou.Followingconventionsisimportantbe-causeitmakesiteasierforotherstounderstandwhatyouaredoing,andmakesiteasierforprogramswrittenbymultipleindependentauthorstoworktogether. 52

PAGE 59

Chapter4.AllAboutFunctions Assembly-LanguageFunctionsusingtheCCallingConventionYoucannotwriteassembly-languagefunctionswithoutunderstandinghowthecomputer'sstackworks.Eachcomputerprogramthatrunsusesaregionofmemorycalledthestacktoenablefunctionstoworkproperly.Thinkofastackasapileofpapersonyourdeskwhichcanbeaddedtoindenitely.Yougenerallykeepthethingsthatyouareworkingontowardthetop,andyoutakethingsoffasyouarenishedworkingwiththem.Yourcomputerhasastack,too.Thecomputer'sstacklivesattheverytopaddressesofmemory.Youcanpushvaluesontothetopofthestackthroughaninstructioncalledpushl,whichpusheseitheraregisterormemoryvalueontothetopofthestack.Well,wesayit'sthetop,butthe"top"ofthestackisactuallythebottomofthestack'smemory.Althoughthisisconfusing,thereasonforitisthatwhenwethinkofastackofanything-dishes,papers,etc.-wethinkofaddingandremovingtothetopofit.However,inmemorythestackstartsatthetopofmemoryandgrowsdownwardduetoarchitecturalconsiderations.Therefore,whenwerefertothe"topofthestack"rememberit'satthebottomofthestack'smemory.Youcanalsopopvaluesoffthetopusinganinstructioncalledpopl.Thisremovesthetopvaluefromthestackandplacesitintoaregisterormemorylocationofyourchoosing..Whenwepushavalueontothestack,thetopofthestackmovestoaccomodatetheadditionalvalue.Wecanactuallycontinuallypushvaluesontothestackanditwillkeepgrowingfurtherandfurtherdowninmemoryuntilwehitourcodeordata.Sohowdoweknowwherethecurrent"top"ofthestackis?Thestackregister,%esp,alwayscontainsapointertothecurrenttopofthestack,whereveritis.Everytimewepushsomethingontothestackwithpushl,%espgetssubtractedby4sothatitpointstothenewtopofthestackremember,eachwordisfourbyteslong,andthestackgrowsdownward.Ifwewanttoremovesomethingfromthestack,wesimplyusethepoplinstruction,whichadds4to%espandputstheprevioustopvalueinwhateverregisteryouspecied.pushlandpopleachtake 53

PAGE 60

Chapter4.AllAboutFunctions oneoperand-theregistertopushontothestackforpushl,orreceivethedatathatispoppedoffthestackforpopl.Ifwesimplywanttoaccessthevalueonthetopofthestackwithoutremovingit,wecansimplyusethe%espregisterinindirectaddressingmode.Forexample,thefollowingcodemoveswhateverisatthetopofthestackinto%eax:movl%esp,%eaxIfweweretojustdothis:movl%esp,%eaxthen%eaxwouldjustholdthepointertothetopofthestackratherthanthevalueatthetop.Putting%espinparenthesiscausesthecomputertogotoindirectaddressingmode,andthereforewegetthevaluepointedtoby%esp.Ifwewanttoaccessthevaluerightbelowthetopofthestack,wecansimplyissuethisinstruction:movl4%esp,%eaxThisinstructionusesthebasepointeraddressingmodesee theSectioncalledDataAccessingMethodsinChapter2 whichsimplyadds4to%espbeforelookingupthevaluebeingpointedto.IntheClanguagecallingconvention,thestackisthekeyelementforimplementingafunction'slocalvariables,parameters,andreturnaddress.Beforeexecutingafunction,aprogrampushesalloftheparametersforthefunctionontothestackinthereverseorderthattheyaredocumented.Thentheprogramissuesacallinstructionindicatingwhichfunctionitwishestostart.Thecallinstructiondoestwothings.Firstitpushestheaddressofthenextinstruction,whichisthereturnaddress,ontothestack.Thenitmodiestheinstructionpointer%eiptopointtothestartofthefunction.So,atthetimethefunctionstarts,thestacklookslikethisthe"top"ofthestackisatthebottomon 54

PAGE 61

Chapter4.AllAboutFunctions thisexample:Parameter#N...Parameter2Parameter1ReturnAddress<---%espEachoftheparametersofthefunctionhavebeenpushedontothestack,andnallythereturnaddressisthere.Nowthefunctionitselfhassomeworktodo.Therstthingitdoesissavethecurrentbasepointerregister,%ebp,bydoingpushl%ebp.Thebasepointerisaspecialregisterusedforaccessingfunctionparametersandlocalvariables.Next,itcopiesthestackpointerto%ebpbydoingmovl%esp,%ebp.Thisallowsyoutobeabletoaccessthefunctionparametersasxedindexesfromthebasepointer.Youmaythinkthatyoucanusethestackpointerforthis.However,duringyourprogramyoumaydootherthingswiththestacksuchaspushingargumentstootherfunctions.Copyingthestackpointerintothebasepointeratthebeginningofafunctionallowsyoutoalwaysknowwhereyourparametersareandaswewillsee,localvariablestoo,evenwhileyoumaybepushingthingsonandoffthestack.%ebpwillalwaysbewherethestackpointerwasatthebeginningofthefunction,soitismoreorlessaconstantreferencetothestackframethestackframeconsistsofallofthestackvariablesusedwithinafunction,includingparameters,localvariables,andthereturnaddress.Atthispoint,thestacklookslikethis:Parameter#N<---N*4+4%ebp...Parameter2<---12%ebpParameter1<---8%ebpReturnAddress<---4%ebpOld%ebp<---%espand%ebp 55

PAGE 62

Chapter4.AllAboutFunctions Asyoucansee,eachparametercanbeaccessedusingbasepointeraddressingmodeusingthe%ebpregister.Next,thefunctionreservesspaceonthestackforanylocalvariablesitneeds.Thisisdonebysimplymovingthestackpointeroutoftheway.Let'ssaythatwearegoingtoneedtwowordsofmemorytorunafunction.Wecansimplymovethestackpointerdowntwowordstoreservethespace.Thisisdonelikethis:subl$8,%espThissubtracts8from%espremember,awordisfourbyteslong.4Thisway,wecanusethestackforvariablestoragewithoutworringaboutclobberingthemwithpushesthatwemaymakeforfunctioncalls.Also,sinceitisallocatedonthestackframeforthisfunctioncall,thevariablewillonlybealiveduringthisfunction.Whenwereturn,thestackframewillgoaway,andsowillthesevariables.That'swhytheyarecalledlocal-theyonlyexistwhilethisfunctionisbeingcalled.Nowwehavetwowordsforlocalstorage.Ourstacknowlookslikethis:Parameter#N<---N*4+4%ebp...Parameter2<---12%ebpParameter1<---8%ebpReturnAddress<---4%ebpOld%ebp<---%ebpLocalVariable1<----4%ebpLocalVariable2<----8%ebpand%espSowecannowaccessallofthedataweneedforthisfunctionbyusingbasepointeraddressingusingdifferentoffsetsfrom%ebp.%ebpwasmadespecicallyforthispurpose,whichiswhyitiscalledthebasepointer.Youcanuseotherregistersinbasepointeraddressingmode,butthex86architecturemakesusingthe%ebpregisteralotfaster. 4. Justareminder-thedollarsigninfrontoftheeightindicatesimmediatemodeaddress-ing,meaningthatweloadthenumber8into%espratherthanthevalueataddress8. 56

PAGE 63

Chapter4.AllAboutFunctions Globalvariablesandstaticvariablesareaccessedjustlikewehavebeenaccessingmemoryinpreviouschapters.Theonlydifferencebetweentheglobalandstaticvariablesisthatstaticvariablesareonlyusedbyonefunction,whileglobalvariablesareusedbymanyfunctions.Assemblylanguagetreatsthemexactlythesame,althoughmostotherlanguagesdistinguishthem.Whenafunctionisdoneexecuting,itdoesthreethings: 1. Itstoresit'sreturnvaluein%eax. 2. Itresetsthestacktowhatitwaswhenitwascalleditgetsridofthecurrentstackframeandputsthestackframeofthecallingcodebackintoeffect. 3. Itreturnscontrolbacktowhereveritwascalledfrom.Thisisdoneusingtheretinstruction,whichpopswhatevervalueisatthetopofthestack,andsetstheinstructionpointer,%eip,tothatvalue.So,beforeafunctionreturnscontroltothecodethatcalledit,itmustrestorethepreviousstackframe.Notealsothatwithoutdoingthis,retwouldn'twork,becauseinourcurrentstackframe,thereturnaddressisnotatthetopofthestack.Therefore,beforewereturn,wehavetoresetthestackpointer%espandbasepointer%ebptowhattheywerewhenthefunctionbegan.Thereforetoreturnfromthefunctionyouhavetodothefollowing:movl%ebp,%esppopl%ebpretAtthispoint,youshouldconsideralllocalvariablestobedisposedof.Thereasonisthatafteryoumovethestackpointerback,futurestackpusheswilllikelyoverwriteeverythingyouputthere.Therefore,youshouldneversavetheaddressofalocalvariablepastthelifeofthefunctionitwascreatedin,orelseitwillbeoverwrittenafterthelifeofit'sstackframeends. 57

PAGE 64

Chapter4.AllAboutFunctions Controlhasnowbeenhandedbacktothecallingcode,whichcannowexamine%eaxforthereturnvalue.Thecallingcodealsoneedstopopoffalloftheparametersitpushedontothestackinordertogetthestackpointerbackwhereitwasyoucanalsosimplyadd4*numberofparamtersto%espusingtheaddlinstruction,ifyoudon'tneedthevaluesoftheparametersanymore.5 DestructionofRegistersWhenyoucallafunction,youshouldassumethateverythingcurrentlyinyourregisterswillbewipedout.Theonlyregisterthatisguaranteedtobeleftwiththevalueitstartedwithis%ebp.%eaxisguaranteedtobeoverwritten,andtheotherslikelyare.Ifthereareregistersyouwanttosavebeforecallingafunction,youneedtosavethembypushingthemonthestackbeforepushingthefunction'sparamters.Youcanthenpopthembackoffinreverseorderafterpoppingofftheparameters.Evenifyouknowafunctiondoesnotoverwritearegisteryoushouldsaveit,becausefutureversionsofthatfunctionmay.Otherlanguages'callingconventionsmaybedifferent.Forexample,othercallingconventionsmayplacetheburdenonthefunctiontosaveanyregistersituses.Besuretochecktomakesurethecallingconventionsofyourlanguagesarecompatiblebeforetryingtomixlanguages.Orinthecaseofassemblylanguage,besureyouknowhowtocalltheotherlanguage'sfunctions.them. ExtendedSpecication:DetailsoftheClanguagecallingconventionalsoknownastheABI,orApplicationBinaryInterfaceisavailableonline.Wehaveoversimpliedandleftoutseveralimportantpiecestomakethissimplerfornewprogrammers.Forfulldetails,youshouldcheckoutthedocumentsavailableathttp://www.linuxbase.org/spec/refspecs/Specically,youshould 5. Thisisnotalwaysstrictlyneededunlessyouaresavingregistersonthestackbeforeafunctioncall.Thebasepointerkeepsthestackframeinareasonablyconsistentstate.However,itisstillagoodidea,andisabsolutelynecessaryifyouaretemporarilysavingregistersonthestack.. 58

PAGE 65

Chapter4.AllAboutFunctions lookfortheSystemVApplicationBinaryInterface-Intel386ArchitectureProcessorSupplement.AFunctionExampleLet'stakealookathowafunctioncallworksinarealprogram.Thefunctionwearegoingtowriteisthepowerfunction.Wewillgivethepowerfunctiontwoparameters-thenumberandthepowerwewanttoraiseitto.Forexample,ifwegaveittheparamters2and3,itwouldraise2tothepowerof3,or2*2*2,giving8.Inordertomakethisprogramsimple,wewillonlyallownumbers1andgreater.Thefollowingisthecodeforthecompleteprogram.Asusual,anexplanationfollows.Namethelepower.s.#PURPOSE:Programtoillustratehowfunctionswork#Thisprogramwillcomputethevalueof#2^3+5^2##Everythinginthemainprogramisstoredinregisters,#sothedatasectiondoesn'thaveanything..section.data.section.text.globl_start_start:pushl$3#pushsecondargumentpushl$2#pushfirstargumentcallpower#callthefunctionaddl$8,%esp#movethestackpointerback 59

PAGE 66

Chapter4.AllAboutFunctions pushl%eax#savethefirstanswerbefore#callingthenextfunctionpushl$2#pushsecondargumentpushl$5#pushfirstargumentcallpower#callthefunctionaddl$8,%esp#movethestackpointerbackpopl%ebx#Thesecondanswerisalready#in%eax.Wesavedthe#firstanswerontothestack,#sonowwecanjustpopit#outinto%ebxaddl%eax,%ebx#addthemtogether#theresultisin%ebxmovl$1,%eax#exit%ebxisreturnedint$0x80#PURPOSE:Thisfunctionisusedtocompute#thevalueofanumberraisedto#apower.##INPUT:Firstargument-thebasenumber#Secondargument-thepowerto#raiseitto##OUTPUT:Willgivetheresultasareturnvalue##NOTES:Thepowermustbe1orgreater##VARIABLES:#%ebx-holdsthebasenumber#%ecx-holdsthepower 60

PAGE 67

Chapter4.AllAboutFunctions ##-4%ebp-holdsthecurrentresult##%eaxisusedfortemporarystorage#.typepower,@functionpower:pushl%ebp#saveoldbasepointermovl%esp,%ebp#makestackpointerthebasepointersubl$4,%esp#getroomforourlocalstoragemovl8%ebp,%ebx#putfirstargumentin%eaxmovl12%ebp,%ecx#putsecondargumentin%ecxmovl%ebx,-4%ebp#storecurrentresultpower_loop_start:cmpl$1,%ecx#ifthepoweris1,wearedonejeend_powermovl-4%ebp,%eax#movethecurrentresultinto%eaximull%ebx,%eax#multiplythecurrentresultby#thebasenumbermovl%eax,-4%ebp#storethecurrentresultdecl%ecx#decreasethepowerjmppower_loop_start#runforthenextpowerend_power:movl-4%ebp,%eax#returnvaluegoesin%eaxmovl%ebp,%esp#restorethestackpointerpopl%ebp#restorethebasepointerret 61

PAGE 68

Chapter4.AllAboutFunctions Typeintheprogram,assembleit,andrunit.Trycallingpowerfordifferentvalues,butrememberthattheresulthastobelessthan256whenitispassedbacktotheoperatingsystem.Alsotrysubtractingtheresultsofthetwocomputations.Tryaddingathirdcalltothepowerfunction,andaddit'sresultbackin.Themainprogramcodeisprettysimple.Youpushtheargumentsontothestack,callthefunction,andthenmovethestackpointerback.Theresultisstoredin%eax.Notethatbetweenthetwocallstopower,wesavetherstvalueontothestack.Thisisbecausetheonlyregisterthatisguaranteedtobesavedis%ebp.Thereforewepushthevalueontothestack,andpopthevaluebackoffafterthesecondfunctioncalliscomplete.Let'slookathowthefunctionitselfiswritten.Noticethatbeforethefunction,thereisdocumentationastowhatthefunctiondoes,whatit'sargumentsare,andwhatitgivesasareturnvalue.Thisisusefulforprogrammerswhousethisfunction.Thisisthefunction'sinterface.Thisletstheprogrammerknowwhatvaluesareneededonthestack,andwhatwillbein%eaxattheend.Wethenhavethefollowingline:.typepower,@functionThistellsthelinkerthatthesymbolpowershouldbetreatedasafunction.Sincethisprogramisonlyinonele,itwouldworkjustthesamewiththisleftout.However,itisgoodpractice.Afterthat,wedenethevalueofthepowerlabel:power:Asmentionedpreviously,thisdenesthesymbolpowertobetheaddresswheretheinstructionsfollowingthelabelbegin.Thisishowcallpowerworks.Ittransferscontroltothisspotoftheprogram.Thedifferencebetweencallandjmpisthatcallalsopushesthereturnaddressontothestacksothatthefunctioncanreturn,whilethejmpdoesnot. 62

PAGE 69

Chapter4.AllAboutFunctions Next,wehaveourinstructionstosetupourfunction:pushl%ebpmovl%esp,%ebpsubl$4,%espAtthispoint,ourstacklookslikethis:BaseNumber<---12%ebpPower<---8%ebpReturnAddress<---4%ebpOld%ebp<---%ebpCurrentresult<----4%ebpand%espAlthoughwecouldusearegisterfortemporarystorage,thisprogramusesalocalvariableinordertoshowhowtosetitup.Oftentimestherejustaren'tenoughregisterstostoreeverything,soyouhavetoofoadthemintolocalvariables.Othertimes,yourfunctionwillneedtocallanotherfunctionandsenditapointertosomeofyourdata.Youcan'thaveapointertoaregister,soyouhavetostoreitinalocalvariableinordertosendapointertoit.Basically,whattheprogramdoesisstartwiththebasenumber,andstoreitbothasthemultiplierstoredin%ebxandthecurrentvaluestoredin-4%ebp.Italsohasthepowerstoredin%ecxItthencontinuallymultipliesthecurrentvaluebythemultiplier,decreasesthepower,andleavestheloopifthepowerin%ecxgetsdownto1.Bynow,youshouldbeabletogothroughtheprogramwithouthelp.Theonlythingsyoushouldneedtoknowisthatimulldoesintegermultiplicationandstorestheresultinthesecondoperand,anddecldecreasesthegivenregisterby1.Formoreinformationontheseandotherinstructions,see AppendixB Agoodprojecttotrynowistoextendtheprogramsoitwillreturnthevalueofanumberifthepoweris0hint,anythingraisedtothezeropoweris1.Keeptrying.Ifitdoesn'tworkatrst,trygoingthroughyourprogrambyhandwitha 63

PAGE 70

Chapter4.AllAboutFunctions scrapofpaper,keepingtrackofwhere%ebpand%esparepointing,whatisonthestack,andwhatthevaluesareineachregister.RecursiveFunctionsThenextprogramwillstretchyourbrainsevenmore.Theprogramwillcomputethefactorialofanumber.Afactorialistheproductofanumberandallthenumbersbetweenitandone.Forexample,thefactorialof7is7*6*5*4*3*2*1,andthefactorialof4is4*3*2*1.Now,onethingyoumightnoticeisthatthefactorialofanumberisthesameastheproductofanumberandthefactorialjustbelowit.Forexample,thefactorialof4is4timesthefactorialof3.Thefactorialof3is3timesthefactorialof2.2is2timesthefactorialof1.Thefactorialof1is1.Thistypeofdenitioniscalledarecursivedenition.Thatmeans,thedenitionofthefactorialfunctionincludesthefactorialfuntionitself.However,sinceallfunctionsneedtoend,arecursivedenitionmustincludeabasecase.Thebasecaseisthepointwhererecursionwillstop.Withoutabasecase,thefunctionwouldgoonforevercallingitselfuntiliteventuallyranoutofstackspace.Inthecaseofthefactorial,thebasecaseisthenumber1.Whenwehitthenumber1,wedon'trunthefactorialagain,wejustsaythatthefactorialof1is1.So,let'srunthroughwhatwewantthecodetolooklikeforourfactorialfunction: 1. Examinethenumber 2. Isthenumber1? 3. Ifso,theanswerisone 4. Otherwise,theansweristhenumbertimesthefactorialofthenumberminusoneThiswouldbeproblematicifwedidn'thavelocalvariables.Inotherprograms,storingvaluesinglobalvariablesworkedne.However,globalvariablesonlyprovideonecopyofeachvariable.Inthisprogram,wewillhavemultiplecopies 64

PAGE 71

Chapter4.AllAboutFunctions ofthefunctionrunningatthesametime,allofthemneedingtheirowncopiesofthedata!6Sincelocalvariablesexistonthestackframe,andeachfunctioncallgetsitsownstackframe,weareokay.Let'slookatthecodetoseehowthisworks:#PURPOSE-Givenanumber,thisprogramcomputesthe#factorial.Forexample,thefactorialof#3is3*2*1,or6.Thefactorialof#4is4*3*2*1,or24,andsoon.##Thisprogramshowshowtocallafunctionrecursively..section.data#Thisprogramhasnoglobaldata.section.text.globl_start.globlfactorial#thisisunneededunlesswewanttoshare#thisfunctionamongotherprograms_start:pushl$4#Thefactorialtakesoneargument-the#numberwewantafactorialof.So,it#getspushedcallfactorial#runthefactorialfunctionaddl$4,%esp#Scrubstheparameterthatwaspushedon#thestackmovl%eax,%ebx#factorialreturnstheanswerin%eax,but#wewantitin%ebxtosenditasourexit#status 6. By"runningatthesametime"Iamtalkingaboutthefactthatonewillnothavenishedbeforeanewoneisactivated.Iamnotimplyingthattheirinstructionsarerunningatthesametime. 65

PAGE 72

Chapter4.AllAboutFunctions movl$1,%eax#callthekernel'sexitfunctionint$0x80#Thisistheactualfunctiondefinition.typefactorial,@functionfactorial:pushl%ebp#standardfunctionstuff-wehaveto#restore%ebptoitspriorstatebefore#returning,sowehavetopushitmovl%esp,%ebp#Thisisbecausewedon'twanttomodify#thestackpointer,soweuse%ebp.movl8%ebp,%eax#Thismovesthefirstargumentto%eax#4%ebpholdsthereturnaddress,and#8%ebpholdsthefirstparametercmpl$1,%eax#Ifthenumberis1,thatisourbase#case,andwesimplyreturnis#alreadyin%eaxasthereturnvaluejeend_factorialdecl%eax#otherwise,decreasethevaluepushl%eax#pushitforourcalltofactorialcallfactorial#callfactorialmovl8%ebp,%ebx#%eaxhasthereturnvalue,sowe#reloadourparameterinto%ebximull%ebx,%eax#multiplythatbytheresultofthe#lastcalltofactorialin%eax#theanswerisstoredin%eax,which#isgoodsincethat'swherereturn#valuesgo.end_factorial:movl%ebp,%esp#standardfunctionreturnstuff-wepopl%ebp#havetorestore%ebpand%esptowhere#theywerebeforethefunctionstartedret#returntothefunctionthispopsthe 66

PAGE 73

Chapter4.AllAboutFunctions #returnvalue,tooAssemble,link,andrunitwiththesecommands:asfactorial.s-ofactorial.oldfactorial.o-ofactorial./factorialecho$?Thisshouldgiveyouthevalue24.24isthefactorialof4,youcantestitoutyourselfwithacalculator:4*3*2*1=24.I'mguessingyoudidn'tunderstandthewholecodelisting.Let'sgothroughitalineatatimetoseewhatishappening._start:pushl$4callfactorialOkay,thisprogramisintendedtocomputethefactorialofthenumber4.Whenprogrammingfunctions,youaresupposedtoputtheparametersofthefunctiononthetopofthestackrightbeforeyoucallit.Remember,afunction'sparametersarethedatathatyouwantthefunctiontoworkwith.Inthiscase,thefactorialfunctiontakes1parameter-thenumberyouwantthefactorialof.Thepushlinstructionputsthegivenvalueatthetopofthestack.Thecallinstructionthenmakesthefunctioncall.Nextwehavetheselines:addl$4,%espmovl%eax,%ebxmovl$1,%eax 67

PAGE 74

Chapter4.AllAboutFunctions int$0x80Thistakesplaceafterfactorialhasnishedandcomputedthefactorialof4forus.Nowwehavetocleanupthestack.Theaddlinstructionmovesthestackpointerbacktowhereitwasbeforewepushedthe$4ontothestack.Youshouldalwayscleanupyourstackparametersafterafunctioncallreturns.Thenextinstructionmoves%eaxto%ebx.What'sin%eax?Itisfactorial'sreturnvalue.Inourcase,itisthevalueofthefactorialfunction.With4asourparameter,24shouldbeourreturnvalue.Remember,returnvaluesarealwaysstoredin%eax.Wewanttoreturnthisvalueasthestatuscodetotheoperatingsystem.However,Linuxrequiresthattheprogram'sexitstatusbestoredin%ebx,not%eax,sowehavetomoveit.Thenwedothestandardexitsystemcall.Thenicethingaboutfunctioncallsisthat: Otherprogrammersdon'thavetoknowanythingaboutthemexceptit'sargumentstousethem. Theyprovidestandardizedbuildingblocksfromwhichyoucanformaprogram. Theycanbecalledmultipletimesandfrommultiplelocationsandtheyalwaysknowhowtogetbacktowheretheyweresincecallpushesthereturnaddressontothestack.Thesearethemainadvantagesoffunctions.Largerprogramsalsousefunctionstobreakdowncomplexpiecesofcodeintosmaller,simplerones.Infact,almostallofprogrammingiswritingandcallingfunctions.Let'snowtakealookathowthefactorialfunctionitselfisimplemented.Beforethefunctionstarts,wehavethisdirective:.typefactorial,@functionfactorial: 68

PAGE 75

Chapter4.AllAboutFunctions The.typedirectivetellsthelinkerthatfactorialisafunction.Thisisn'treallyneededunlesswewereusingfactorialinotherprograms.Wehaveincludeditforcompleteness.Thelinethatsaysfactorial:givesthesymbolfactorialthestoragelocationofthenextinstruction.That'showcallknewwheretogowhenwesaidcallfactorial.Therstrealinstructionsofthefunctionare:pushl%ebpmovl%esp,%ebpAsshowninthepreviousprogram,thiscreatesthestackframeforthisfunction.Thesetwolineswillbethewayyoushouldstarteveryfunction.Thenextinstructionisthis:movl8%ebp,%eaxThisusesbasepointeraddressingtomovetherstparameterofthefunctioninto%eax.Remember,%ebphastheold%ebp,4%ebphasthereturnaddress,and8%ebpisthelocationoftherstparametertothefunction.Ifyouthinkback,thiswillbethevalue4ontherstcall,sincethatwaswhatwepushedonthestackbeforecallingthefunctionwithpushl$4.parameterinto%eax.Asthisfunctioncallsitself,itwillhaveothervalues,too.Next,wechecktoseeifwe'vehitourbasecaseaparameterof1.Ifso,wejumptotheinstructionatthelabelend_factorial,whereitwillbereturned.Et'salreadyin%eaxwhichwementionedearlieriswhereyouputreturnvalues.Thatisaccomplishedbytheselines:cmpl$1,%eaxjeend_factorialIfit'snotourbasecase,whatdidwesaywewoulddo?Wewouldcallthefactorialfunctionagainwithourparameterminusone.So,rstwedecrease%eaxbyone: 69

PAGE 76

Chapter4.AllAboutFunctions decl%eaxdeclstandsfordecrement.Itsubtracts1fromthegivenregisterormemorylocation%eaxinourcase.inclistheinverse-itadds1.Afterdecrementing%eaxwepushitontothestacksinceit'sgoingtobetheparameterofthenextfunctioncall.Andthenwecallfactorialagain!pushl%eaxcallfactorialOkay,nowwe'vecalledfactorial.Onethingtorememberisthatafterafunctioncall,wecanneverknowwhattheregistersareexcept%espand%ebp.Soeventhoughwehadthevaluewewerecalledwithin%eax,it'snotthereanymore.Therefore,weneedpullitoffthestackfromthesameplacewegotitthersttimeat8%ebp.So,wedothis:movl8%ebp,%ebxNow,wewanttomultiplythatnumberwiththeresultofthefactorialfunction.Ifyourememberourpreviousdiscussion,theresultoffunctionsareleftin%eax.So,weneedtomultiply%ebxwith%eax.Thisisdonewiththisinstruction:imull%ebx,%eaxThisalsostorestheresultin%eax,whichisexactlywherewewantthereturnvalueforthefunctiontobe!Sincethereturnvalueisinplacewejustneedtoleavethefunction.Ifyouremember,atthestartofthefunctionwepushed%ebp,andmoved%espinto%ebptocreatethecurrentstackframe.Nowwereversetheoperationtodestroythecurrentstackframeandreactivatethelastone:end_factorial:movl%ebp,%esppopl%ebpNowwe'realreadytoreturn,soweissuethefollowingcommand 70

PAGE 77

Chapter4.AllAboutFunctions retThispopsthetopvalueoffofthestack,andthenjumpstoit.Ifyourememberourdiscussionaboutcall,wesaidthatcallrstpushedtheaddressofthenextinstructionontothestackbeforeitjumpedtothebeginningofthefunction.So,herewepopitbackoffsowecanreturnthere.Thefunctionisdone,andwehaveouranswer!Likeourpreviousprogram,youshouldlookovertheprogramagain,andmakesureyouknowwhateverythingdoes.Lookbackthroughthissectionandtheprevioussectionsfortheexplanationofanythingyoudon'tunderstand.Then,takeapieceofpaper,andgothroughtheprogramstep-by-step,keepingtrackofwhatthevaluesoftheregistersareateachstep,andwhatvaluesareonthestack.Doingthisshoulddeepenyourunderstandingofwhatisgoingon.ReviewKnowtheConcepts Whatareprimitives? Whatarecallingconventions? Whatisthestack? Howdopushlandpoplaffectthestack?Whatspecial-purposeregisterdotheyaffect? Whatarelocalvariablesandwhataretheyusedfor? Whyarelocalvariablessonecessaryinrecursivefunctions? Whatare%ebpand%espusedfor? Whatisastackframe? 71

PAGE 78

Chapter4.AllAboutFunctions UsetheConcepts Writeafunctioncalledsquarewhichreceivesoneargumentandreturnsthesquareofthatargument. Writeaprogramtotestyoursquarefunction. Convertthemaximumprogramgivenin theSectioncalledFindingaMaximumValueinChapter3 sothatitisafunctionwhichtakesapointertoseveralvaluesandreturnstheirmaximum.Writeaprogramthatcallsmaximumwith3differentlists,andreturnstheresultofthelastoneastheprogram'sexitstatuscode. Explaintheproblemsthatwouldarisewithoutastandardcallingconvention.GoingFurther Doyouthinkit'sbetterforasystemtohavealargesetofprimitivesorasmallone,assumingthatthelargersetcanbewrittenintermsofthesmallerone? Thefactorialfunctioncanbewrittennon-recursively.Doso. Findanapplicationonthecomputeryouuseregularly.Trytolocateaspecicfeature,andpracticebreakingthatfeatureoutintofunctions.Denethefunctioninterfacesbetweenthatfeatureandtherestoftheprogram. Comeupwithyourowncallingconvention.Rewritetheprogramsinthischapterusingit.Anexampleofadifferentcallingconventionwouldbetopassparamtersinregistersratherthanthestack,topasstheminadifferentorder,toreturnvaluesinotherregistersormemorylocations.Whateveryoupick,beconsistentandapplyitthroughoutthewholeprogram. Canyoubuildacallingconventionwithoutusingthestack?Whatlimitationsmightithave? 72

PAGE 79

Chapter4.AllAboutFunctions Whattestcasesshouldweuseinourexampleprogramtochecktoseeifitisworkingproperly? 73

PAGE 80

Chapter4.AllAboutFunctions 74

PAGE 81

Chapter5.DealingwithFilesAlotofcomputerprogrammingdealswithles.Afterall,whenwerebootourcomputers,theonlythingthatremainsfromprevioussessionsarethethingsthathavebeenputondisk.Datawhichisstoredinlesiscalledpersistentdata,becauseitpersistsinlesthatremainonthediskevenwhentheprogramisn'trunning..TheUNIXFileConceptEachoperatingsystemhasit'sownwayofdealingwithles.However,theUNIXmethod,whichisusedonLinux,isthesimplestandmostuniversal.UNIXles,nomatterwhatprogramcreatedthem,canallbeaccessedasasequentialstreamofbytes.Whenyouaccessale,youstartbyopeningitbyname.Theoperatingsystemthengivesyouanumber,calledaledescriptor,whichyouusetorefertotheleuntilyouarethroughwithit.Youcanthenreadandwritetotheleusingitsledescriptor.Whenyouaredonereadingandwriting,youthenclosethele,whichthenmakestheledescriptoruseless.Inourprogramswewilldealwithlesinthefollowingways: 1. TellLinuxthenameoftheletoopen,andinwhatmodeyouwantitopenedread,write,bothreadandwrite,createitifitdoesn'texist,etc..Thisishandledwiththeopensystemcall,whichtakesalename,anumberrepresentingthemode,andapermissionsetasitsparameters.%eaxwillholdthesystemcallnumber,whichis5.Theaddressoftherstcharacterofthelenameshouldbestoredin%ebx.Theread/writeintentions,representedasanumber,shouldbestoredin%ecx.Fornow,use0forlesyouwanttoreadfrom,and03101forlesyouwanttowritetoyoumustincludetheleadingzero.1Finally,thepermissionsetshouldbestoredasanumberin%edx.If 1. Thiswillbeexplainedinmoredetailin theSectioncalledTruth,Falsehood,andBinaryNumbersinChapter10 75

PAGE 82

Chapter5.DealingwithFiles youareunfamiliarwithUNIXpermissions,justuse0666forthepermissionsagain,youmustincludetheleadingzero. 2. Linuxwillthenreturntoyoualedescriptorin%eax.Remember,thisisanumberthatyouusetorefertothislethroughoutyourprogram. 3. Nextyouwilloperateontheledoingreadsand/orwrites,eachtimegivingLinuxtheledescriptoryouwanttouse.readissystemcall3,andtocallityouneedtohavetheledescriptorin%ebx,theaddressofabufferforstoringthedatathatisreadin%ecx,andthesizeofthebufferin%edx.Bufferswillbeexplainedin theSectioncalledBuffersand.bss .readwillreturnwitheitherthenumberofcharactersreadfromthele,oranerrorcode.Errorcodescanbedistinguishedbecausetheyarealwaysnegativenumbersmoreinformationonnegativenumberscanbefoundin Chapter10 .writeissystemcall4,anditrequiresthesameparametersasthereadsystemcall,exceptthatthebuffershouldalreadybelledwiththedatatowriteout.Thewritesystemcallwillgivebackthenumberofbyteswrittenin%eaxoranerrorcode. 4. Whenyouarethroughwithyourles,youcanthentellLinuxtoclosethem.Afterwards,yourledescriptorisnolongervalid.Thisisdoneusingclose,systemcall6.Theonlyparametertocloseistheledescriptor,whichisplacedin%ebxBuffersand.bssIntheprevioussectionwementionedbufferswithoutexplainingwhattheywere.Abufferisacontinuousblockofbytesusedforbulkdatatransfer.Whenyourequesttoreadale,theoperatingsystemneedstohaveaplacetostorethedataitreads.Thatplaceiscalledabuffer.Usuallybuffersareonlyusedtostoredatatemporarily,anditisthenreadfromthebuffersandconvertedtoaformthatiseasierfortheprogramstohandle.Ourprogramswon'tbecomplicatedenoughtoneedthatdone.Foranexample,let'ssaythatyouwanttoreadinasinglelineoftextfromalebutyoudonotknowhowlongthatlineis.Youwouldthensimply 76

PAGE 83

Chapter5.DealingwithFiles readalargenumberofbytes/charactersfromtheleintoabuffer,lookfortheend-of-linecharacter,andcopyallofthecharacterstothatend-of-linecharactertoanotherlocation.Ifyoudidn'tndandend-of-linecharacter,youwouldallocateanotherbufferandcontinuereading.Youwouldprobablywindupwithsomecharactersleftoverinyourbufferinthiscase,whichyouwoulduseasthestartingpointwhenyounextneeddatafromthele.2Anotherthingtonoteisthatbuffersareaxedsize,setbytheprogrammer.So,ifyouwanttoreadindata500bytesatatime,yousendthereadsystemcalltheaddressofa500-byteunusedlocation,andsenditthenumber500soitknowshowbigitis.Youcanmakeitsmallerorbigger,dependingonyourapplication'sneeds.Tocreateabuffer,youneedtoeitherreservestaticordynamicstorage.Staticstorageiswhatwehavetalkedaboutsofar,storagelocationsdeclaredusing.longor.bytedirectives.Dynamicstoragewillbediscussedin theSectioncalledGettingMoreMemoryinChapter9 .Thereareproblems,though,withdeclaringbuffersusing.byte.First,itistedioustotype.Youwouldhavetotype500numbersafterthe.bytedeclaration,andtheywouldn'tbeusedforanythingbuttotakeupspace.Second,itusesupspaceintheexecutable.Intheexampleswe'veusedsofar,itdoesn'tuseuptoomuch,butthatcanchangeinlargerprograms.Ifyouwant500bytesyouhavetotypein500numbersanditwastes500bytesintheexecutable.Thereisasolutiontobothofthese.Sofar,wehavediscussedtwoprogramsections,the.textandthe.datasections.Thereisanothersectioncalledthe.bss.Thissectionislikethedatasection,exceptthatitdoesn'ttakeupspaceintheexecutable.Thissectioncanreservestorage,butitcan'tinitializeit.Inthe.datasection,youcouldreservestorageandsetittoaninitialvalue.Inthe.bsssection,youcan'tsetaninitialvalue.Thisisusefulforbuffersbecausewedon'tneedtoinitializethemanyway,wejustneedtoreservestorage.Inordertodothis,wedothefollowingcommands:.section.bss 2. Whilethissoundscomplicated,mostofthetimeinprogrammingyouwillnotneedtodealdirectlywithbuffersandledescriptors.In Chapter8 youwilllearnhowtouseexistingcodepresentinLinuxtohandlemostofthecomplicationsofleinput/outputforyou. 77

PAGE 84

Chapter5.DealingwithFiles .lcommmy_buffer,500Thisdirective,.lcomm,willcreateasymbol,my_buffer,thatreferstoa500-bytestoragelocationthatwecanuseasabuffer.Wecanthendothefollowing,assumingwehaveopenedaleforreadingandhaveplacedtheledescriptorin%ebx:movl$my_buffer,%ecxmovl500,%edxmovl3,%eaxint$0x80Thiswillreadupto500bytesintoourbuffer.Inthisexample,Iplacedadollarsigninfrontofmy_buffer.Rememberthatthereasonforthisisthatwithoutthedollarsign,my_bufferistreatedasamemorylocation,andisaccessedindirectaddressingmode.Thedollarsignswitchesittoimmediatemodeaddressing,whichactuallyloadsthenumberrepresentedbymy_bufferi.e.-theaddressofthestartofourbuffer.whichistheaddressofmy_bufferitselfinto%ecx.StandardandSpecialFilesYoumightthinkthatprogramsstartwithoutanylesopenbydefault.Thisisnottrue.Linuxprogramsusuallyhaveatleastthreeopenledescriptorswhentheybegin.Theyare:STDIN Thisisthestandardinput.Itisaread-onlyle,andusuallyrepresentsyourkeyboard.3Thisisalwaysledescriptor0. 3. Aswementionedearlier,inLinux,almosteverythingisa"le".Yourkeyboardinputisconsideredale,andsoisyourscreendisplay. 78

PAGE 85

Chapter5.DealingwithFiles STDOUT Thisisthestandardoutput.Itisawrite-onlyle,andusuallyrepresentsyourscreendisplay.Thisisalwaysledescriptor1.STDERR Thisisyourstandarderror.Itisawrite-onlyle,andusuallyrepresentsyourscreendisplay.MostregularprocessingoutputgoestoSTDOUT,butanyerrormessagesthatcomeupintheprocessgotoSTDERR.Thisway,ifyouwantto,youcansplitthemupintoseparateplaces.Thisisalwaysledescriptor2.Anyofthese"les"canberedirectedfromortoarealle,ratherthanascreenorakeyboard.Thisisoutsidethescopeofthisbook,butanygoodbookontheUNIXcommand-linewilldescribeitindetail.Theprogramitselfdoesnotevenneedtobeawareofthisindirection-itcanjustusethestandardledescriptorsasusual.Noticethatmanyofthelesyouwritetoaren'tlesatall.UNIX-basedoperatingsystemstreatallinput/outputsystemsasles.Networkconnectionsaretreatedasles,yourserialportistreatedlikeale,evenyouraudiodevicesaretreatedasles.Communicationbetweenprocessesisusuallydonethroughspeciallescalledpipes.Someoftheseleshavedifferentmethodsofopeningandcreatingthemthanregularlesi.e.-theydon'tusetheopensystemcall,buttheycanallbereadfromandwrittentousingthestandardreadandwritesystemcalls.UsingFilesinaProgramWearegoingtowriteasimpleprogramtoillustratetheseconcepts.Theprogramwilltaketwoles,andreadfromone,convertallofitslower-caseletterstoupper-case,andwritetotheotherle.Beforewedoso,let'sthinkaboutwhatweneedtodotogetthejobdone: Haveafunctionthattakesablockofmemoryandconvertsittoupper-case.Thisfunctionwouldneedanaddressofablockofmemoryanditssizeas 79

PAGE 86

Chapter5.DealingwithFiles parameters. Haveasectionofcodethatrepeatedlyreadsintoabuffer,callsourconversionfunctiononthebuffer,andthenwritesthebufferbackouttotheotherle. Begintheprogrambyopeningthenecessaryles.NoticethatI'vespeciedthingsinreverseorderthattheywillbedone.That'sausefultrickinwritingcomplexprograms-rstdecidethemeatofwhatisbeingdone.Inthiscase,it'sconvertingblocksofcharacterstoupper-case.Then,youthinkaboutwhatallneedstobesetupandprocessedtogetthattohappen.Inthiscase,youhavetoopenles,andcontinuallyreadandwriteblockstodisk.Oneofthekeysofprogrammingiscontinuallybreakingdownproblemsintosmallerandsmallerchunksuntilit'ssmallenoughthatyoucaneasilysolvetheproblem.Thenyoucanbuildthesechunksbackupuntilyouhaveaworkingprogram.4Youmayhavebeenthinkingthatyouwillneverrememberallofthesenumbersbeingthrownatyou-thesystemcallnumbers,theinterruptnumber,etc.Inthisprogramwewillalsointroduceanewdirective,.equwhichshouldhelpout..equallowsyoutoassignnamestonumbers.Forexample,ifyoudid.equLINUX_SYSCALL,0x80,anytimeafterthatyouwroteLINUX_SYSCALL,theassemblerwouldsubstitue0x80forthat.Sonow,youcanwriteint$LINUX_SYSCALLwhichismucheasiertoread,andmucheasiertoremember.Codingiscomplex,buttherearealotofthingswecandolikethistomakeiteasier.Hereistheprogram.Notethatwehavemorelabelsthanweactuallyuseforjumps,becausesomeofthemarejustthereforclarity.Trytotracethroughtheprogramandseewhathappensinvariouscases.Anin-depthexplanationoftheprogramwillfollow.#PURPOSE:Thisprogramconvertsaninputfile 4. MaureenSprankle'sProblemSolvingandProgrammingConceptsisanexcellentbookontheproblem-solvingprocessappliedtocomputerprogramming. 80

PAGE 87

Chapter5.DealingwithFiles #toanoutputfilewithallletters#convertedtouppercase.##PROCESSING:1Opentheinputfile#2Opentheoutputfile#4Whilewe'renotattheendoftheinputfile#areadpartoffileintoourmemorybuffer#bgothrougheachbyteofmemory#ifthebyteisalower-caseletter,#convertittouppercase#cwritethememorybuffertooutputfile.section.data#######CONSTANTS#########systemcallnumbers.equSYS_OPEN,5.equSYS_WRITE,4.equSYS_READ,3.equSYS_CLOSE,6.equSYS_EXIT,1#optionsforopenlookat#/usr/include/asm/fcntl.hfor#variousvalues.Youcancombinethem#byaddingthemorORingthem#Thisisdiscussedatgreaterlength#in"CountingLikeaComputer".equO_RDONLY,0.equO_CREAT_WRONLY_TRUNC,03101#standardfiledescriptors.equSTDIN,0.equSTDOUT,1 81

PAGE 88

Chapter5.DealingwithFiles .equSTDERR,2#systemcallinterrupt.equLINUX_SYSCALL,0x80.equEND_OF_FILE,0#Thisisthereturnvalue#ofreadwhichmeanswe've#hittheendofthefile.equNUMBER_ARGUMENTS,2.section.bss#Buffer-thisiswherethedataisloadedinto#fromthedatafileandwrittenfrom#intotheoutputfile.Thisshould#neverexceed16,000forvarious#reasons..equBUFFER_SIZE,500.lcommBUFFER_DATA,BUFFER_SIZE.section.text#STACKPOSITIONS.equST_SIZE_RESERVE,8.equST_FD_IN,-4.equST_FD_OUT,-8.equST_ARGC,0#Numberofarguments.equST_ARGV_0,4#Nameofprogram.equST_ARGV_1,8#Inputfilename.equST_ARGV_2,12#Outputfilename.globl_start_start:###INITIALIZEPROGRAM####savethestackpointer 82

PAGE 89

Chapter5.DealingwithFiles movl%esp,%ebp#Allocatespaceforourfiledescriptors#onthestacksubl$ST_SIZE_RESERVE,%espopen_files:open_fd_in:###OPENINPUTFILE####opensyscallmovl$SYS_OPEN,%eax#inputfilenameinto%ebxmovlST_ARGV_1%ebp,%ebx#read-onlyflagmovl$O_RDONLY,%ecx#thisdoesn'treallymatterforreadingmovl$0666,%edx#callLinuxint$LINUX_SYSCALLstore_fd_in:#savethegivenfiledescriptormovl%eax,ST_FD_IN%ebpopen_fd_out:###OPENOUTPUTFILE####openthefilemovl$SYS_OPEN,%eax#outputfilenameinto%ebxmovlST_ARGV_2%ebp,%ebx#flagsforwritingtothefilemovl$O_CREAT_WRONLY_TRUNC,%ecx#modefornewfileifit'screatedmovl$0666,%edx#callLinux 83

PAGE 90

Chapter5.DealingwithFiles int$LINUX_SYSCALLstore_fd_out:#storethefiledescriptorheremovl%eax,ST_FD_OUT%ebp###BEGINMAINLOOP###read_loop_begin:###READINABLOCKFROMTHEINPUTFILE###movl$SYS_READ,%eax#gettheinputfiledescriptormovlST_FD_IN%ebp,%ebx#thelocationtoreadintomovl$BUFFER_DATA,%ecx#thesizeofthebuffermovl$BUFFER_SIZE,%edx#Sizeofbufferreadisreturnedin%eaxint$LINUX_SYSCALL###EXITIFWE'VEREACHEDTHEEND####checkforendoffilemarkercmpl$END_OF_FILE,%eax#iffoundoronerror,gototheendjleend_loopcontinue_read_loop:###CONVERTTHEBLOCKTOUPPERCASE###pushl$BUFFER_DATA#locationofbufferpushl%eax#sizeofthebuffercallconvert_to_upperpopl%eax#getthesizebackaddl$4,%esp#restore%esp###WRITETHEBLOCKOUTTOTHEOUTPUTFILE### 84

PAGE 91

Chapter5.DealingwithFiles #sizeofthebuffermovl%eax,%edxmovl$SYS_WRITE,%eax#filetousemovlST_FD_OUT%ebp,%ebx#locationofthebuffermovl$BUFFER_DATA,%ecxint$LINUX_SYSCALL###CONTINUETHELOOP###jmpread_loop_beginend_loop:###CLOSETHEFILES####NOTE-wedon'tneedtodoerrorchecking#onthese,becauseerrorconditions#don'tsignifyanythingspecialheremovl$SYS_CLOSE,%eaxmovlST_FD_OUT%ebp,%ebxint$LINUX_SYSCALLmovl$SYS_CLOSE,%eaxmovlST_FD_IN%ebp,%ebxint$LINUX_SYSCALL###EXIT###movl$SYS_EXIT,%eaxmovl$0,%ebxint$LINUX_SYSCALL#PURPOSE:Thisfunctionactuallydoesthe#conversiontouppercaseforablock##INPUT:Thefirstparameteristhelocation 85

PAGE 92

Chapter5.DealingwithFiles #oftheblockofmemorytoconvert#Thesecondparameteristhelengthof#thatbuffer##OUTPUT:Thisfunctionoverwritesthecurrent#bufferwiththeupper-casifiedversion.##VARIABLES:#%eax-beginningofbuffer#%ebx-lengthofbuffer#%edi-currentbufferoffset#%cl-currentbytebeingexamined#firstpartof%ecx####CONSTANTS###Thelowerboundaryofoursearch.equLOWERCASE_A,'a'#Theupperboundaryofoursearch.equLOWERCASE_Z,'z'#Conversionbetweenupperandlowercase.equUPPER_CONVERSION,'A'-'a'###STACKSTUFF###.equST_BUFFER_LEN,8#Lengthofbuffer.equST_BUFFER,12#actualbufferconvert_to_upper:pushl%ebpmovl%esp,%ebp###SETUPVARIABLES###movlST_BUFFER%ebp,%eaxmovlST_BUFFER_LEN%ebp,%ebxmovl$0,%edi 86

PAGE 93

Chapter5.DealingwithFiles #ifabufferwithzerolengthwasgiven#tous,justleavecmpl$0,%ebxjeend_convert_loopconvert_loop:#getthecurrentbytemovb%eax,%edi,1,%cl#gotothenextbyteunlessitisbetween#'a'and'z'cmpb$LOWERCASE_A,%cljlnext_bytecmpb$LOWERCASE_Z,%cljgnext_byte#otherwiseconvertthebytetouppercaseaddb$UPPER_CONVERSION,%cl#andstoreitbackmovb%cl,%eax,%edi,1next_byte:incl%edi#nextbytecmpl%edi,%ebx#continueunless#we'vereachedthe#endjneconvert_loopend_convert_loop:#noreturnvalue,justleavemovl%ebp,%esppopl%ebpretTypeinthisprogramastoupper.s,andthenenterinthefollowingcommands: 87

PAGE 94

Chapter5.DealingwithFiles astoupper.s-otoupper.oldtoupper.o-otoupperThisbuildsaprogramcalledtoupper,whichconvertsallofthelowercasecharactersinaletouppercase.Forexample,toconverttheletoupper.stouppercase,typeinthefollowingcommand:./touppertoupper.stoupper.uppercaseYouwillnowndintheletoupper.uppercaseanuppercaseversionofyouroriginalle.Let'sexaminehowtheprogramworks.TherstsectionoftheprogramismarkedCONSTANTS.Inprogramming,aconstantisavaluethatisassignedwhenaprogramassemblesorcompiles,andisneverchanged.Imakeahabitofplacingallofmyconstantstogetheratthebeginningoftheprogram.It'sonlynecessarytodeclarethembeforeyouusethem,butputtingthemallatthebeginningmakesthemeasytond.Makingthemallupper-casemakesitobviousinyourprogramwhichvaluesareconstantsandwheretondthem.5Inassemblylanguage,wedeclareconstantswiththe.equdirectiveasmentionedbefore.Here,wesimplygivenamestoallofthestandardnumberswe'veusedsofar,likesystemcallnumbers,thesyscallinterruptnumber,andleopenoptions.ThenextsectionismarkedBUFFERS.Weonlyuseonebufferinthisprogram,whichwecallBUFFER_DATA.Wealsodeneaconstant,BUFFER_SIZE,whichholdsthesizeofthebuffer.Ifwealwaysrefertothisconstantratherthantypingoutthenumber500wheneverweneedtousethesizeofthebuffer,ifitlaterchanges,weonlyneedtomodifythisvalue,ratherthanhavingtogothroughtheentireprogramandchangingallofthevaluesindividually.Insteadofgoingonthethe_startsectionoftheprogram,gototheendwherewedenetheconvert_to_upperfunction.Thisisthepartthatactuallydoesthe 5. Thisisfairlystandardpracticeamongprogrammersinalllanguages. 88

PAGE 95

Chapter5.DealingwithFiles conversion.ThissectionbeginswithalistofconstantsthatwewilluseThereasontheseareputhereratherthanatthetopisthattheyonlydealwiththisonefunction.Wehavethesedenitions:.equLOWERCASE_A,'a'.equLOWERCASE_Z,'z'.equUPPER_CONVERSION,'A'-'a'Thersttwosimplydenethelettersthataretheboundariesofwhatwearesearchingfor.Rememberthatinthecomputer,lettersarerepresentedasnumbers.Therefore,wecanuseLOWERCASE_Aincomparisons,additions,subtractions,oranythingelsewecanusenumbersin.Also,noticewedenetheconstantUPPER_CONVERSION.Sincelettersarerepresentedasnumbers,wecansubtractthem.Subtractinganupper-caseletterfromthesamelower-caselettergivesushowmuchweneedtoaddtoalower-caselettertomakeituppercase.Ifthatdoesn'tmakesense,lookattheASCIIcodetablesthemselvessee AppendixD .You'llnoticethatthenumberforthecharacterAis65andthecharacterais97.Theconversionfactoristhen-32.Foranylowercaseletterifyouadd-32,youwillgetit'scapitalequivalent.Afterthis,wehavesomeconstantslabelledSTACKPOSITIONS.Rememberthatfunctionparametersarepushedontothestackbeforefunctioncalls.TheseconstantsprexedwithSTforclaritydenewhereinthestackweshouldexpecttondeachpieceofdata.Thereturnaddressisatposition4+%esp,thelengthofthebufferisatposition8+%esp,andtheaddressofthebufferisatposition12+%esp.Usingsymbolsforthesenumbersinsteadofthenumbersthemselvesmakesiteasiertoseewhatdataisbeingusedandmoved.Nextcomesthelabelconvert_to_upper.Thisistheentrypointofthefunction.Thersttwolinesareourstandardfunctionlinestosavethestackpointer.ThenexttwolinesmovlST_BUFFER%ebp,%eax 89

PAGE 96

Chapter5.DealingwithFiles movlST_BUFFER_LEN%ebp,%ebxmovethefunctionparametersintotheappropriateregistersforuse.Then,weloadzerointo%edi.Whatwearegoingtodoisiteratethrougheachbyteofthebufferbyloadingfromthelocation%eax+%edi,incrementing%edi,andrepeatinguntil%ediisequaltothebufferlengthstoredin%ebx.Thelinescmpl$0,%ebxjeend_convert_looparejustasanitychecktomakesurethatnoonegaveusabufferofzerosize.Iftheydid,wejustcleanupandleave.Guardingagainstpotentialuserandprogrammingerrorsisanimportanttaskofaprogrammer.Youcanalwaysspecifythatyourfunctionshouldnottakeabufferofzerosize,butit'sevenbettertohavethefunctioncheckandhaveareliableexitplanifithappens.Nowwestartourloop.First,itmovesabyteinto%cl.Thecodeforthisismovb%eax,%edi,1,%clItisusinganindexedindirectaddressingmode.Itsaystostartat%eaxandgo%edilocationsforward,witheachlocationbeing1bytebig.Ittakesthevaluefoundthere,andputitin%cl.Afterthisitcheckstoseeifthatvalueisintherangeoflower-caseatolower-casez.Tochecktherange,itsimplycheckstoseeiftheletterissmallerthana.Ifitis,itcan'tbealower-caseletter.Likewise,ifitislargerthanz,itcan'tbealower-caseletter.So,ineachofthesecases,itsimplymoveson.Ifitisintheproperrange,itthenaddstheuppercaseconversion,andstoresitbackintothebuffer.Eitherway,itthengoestothenextvaluebyincrementing%cl;.Nextitcheckstoseeifweareattheendofthebuffer.Ifwearenotattheend,wejumpbacktothebeginningofthelooptheconvert_looplabel.Ifweareattheend,itsimplycontinuesontotheendofthefunction.Becausewearemodifyingthebufferdirectly,wedon'tneedtoreturnanythingtothecallingprogram-thechangesare 90

PAGE 97

Chapter5.DealingwithFiles alreadyinthebuffer.Thelabelend_convert_loopisnotneeded,butit'stheresoit'seasytoseewherethepartsoftheprogramare.Nowweknowhowtheconversionprocessworks.Nowweneedtogureouthowtogetthedatainandoutoftheles.Beforereadingandwritingtheleswemustopenthem.TheUNIXopensystemcalliswhathandlesthis.Ittakesthefollowingparameters: %eaxcontainsthesystemcallnumberasusual-5inthiscase. %ebxcontainsapointertoastringthatisthenameoftheletoopen.Thestringmustbeterminatedwiththenullcharacter. %ecxcontainstheoptionsusedforopeningthele.ThesetellLinuxhowtoopenthele.Theycanindicatethingssuchasopenforreading,openforwriting,openforreadingandwriting,createifitdoesn'texist,deletetheleifitalreadyexists,etc.Wewillnotgointohowtocreatethenumbersfortheoptionsuntil theSectioncalledTruth,Falsehood,andBinaryNumbersinChapter10 .Fornow,justtrustthenumberswecomeupwith. %edxcontainsthepermissionsthatareusedtoopenthele.Thisisusedincasethelehastobecreatedrst,soLinuxknowswhatpermissionstocreatethelewith.Theseareexpressedinoctal,justlikeregularUNIXpermissions.6Aftermakingthesystemcall,theledescriptorofthenewly-openedleisstoredin%eax.So,whatlesareweopening?Inthisexample,wewillbeopeningthelesspeciedonthecommand-line.Fortunately,command-lineparametersarealreadystoredbyLinuxinaneasy-to-accesslocation,andarealreadynull-terminated.WhenaLinuxprogrambegins,allpointerstocommand-lineargumentsarestoredonthestack.Thenumberofargumentsisstoredat8%esp,thenameoftheprogramisstoredat12%esp,andtheargumentsarestoredfrom16%espon. 6. Ifyouaren'tfamiliarwithUNIXpermissions,justput$0666here.Don'tforgettheleadingzero,asitmeansthatthenumberisanoctalnumber. 91

PAGE 98

Chapter5.DealingwithFiles IntheCProgramminglanguage,thisisreferredtoastheargvarray,sowewillrefertoitthatwayinourprogram.Therstthingourprogramdoesissavethecurrentstackpositionin%ebpandthenreservesomespaceonthestacktostoretheledescriptors.Afterthis,itstartsopeningles.Therstletheprogramopensistheinputle,whichistherstcommand-lineargument.Wedothisbysettingupthesystemcall.Weputthelenameinto%ebx,theread-onlymodenumberinto%ecx,thedefaultmodeof$0666into%edx,andthesystemcallnumberinto%eaxAfterthesystemcall,theleisopenandtheledescriptorisstoredin%eax.7Theledescriptoristhentransferredtoit'sappropriateplaceonthestack.Thesameisthendonefortheoutputle,exceptthatitiscreatedwithawrite-only,create-if-doesn't-exist,truncate-if-does-existmode.Itsledescriptorisstoredaswell.Nowwegettothemainpart-theread/writeloop.Basically,wewillreadxed-sizechunksofdatafromtheinputle,callourconversionfunctiononit,andwriteitbacktotheoutputle.Althoughwearereadingxed-sizechunks,thesizeofthechunksdon'tmatterforthisprogram-wearejustoperatingonstraightsequencesofcharacters.Wecouldreaditinwithaslittleoraslargeofchunksaswewant,anditstillwouldworkproperly.Therstpartoftheloopistoreadthedata.Thisusesthereadsystemcall.Thiscalljusttakesaledescriptortoreadfrom,abuffertowriteinto,andthesizeofthebufferi.e.-themaximumnumberofbytesthatcouldbewritten.Thesystemcallreturnsthenumberofbytesactuallyread,orend-of-lethenumber0. 7. Noticethatwedon'tdoanyerrorcheckingonthis.Thatisdonejusttokeeptheprogramsimple.Innormalprograms,everysystemcallshouldnormallybecheckedforsuccessorfailure.Infailurecases,%eaxwillholdanerrorcodeinsteadofareturnvalue.Errorcodesarenegative,sotheycanbedetectedbycomparing%eaxtozeroandjumpingifitislessthanzero. 92

PAGE 99

Chapter5.DealingwithFiles Afterreadingablock,wecheck%eaxforanend-of-lemarker.Iffound,itexitstheloop.Otherwisewekeepongoing.Afterthedataisread,theconvert_to_upperfunctioniscalledwiththebufferwejustreadinandthenumberofcharactersreadintheprevioussystemcall.Afterthisfunctionexecutes,thebuffershouldbecapitalizedandreadytowriteout.Theregistersarethenrestoredwithwhattheyhadbefore.Finally,weissueawritesystemcall,whichisexactlylikethereadsystemcall,exceptthatitmovesthedatafromthebufferouttothele.Nowwejustgobacktothebeginningoftheloop.Aftertheloopexitsremember,itexitsif,afteraread,itdetectstheendofthele,itsimplyclosesitsledescriptorsandexits.Theclosesystemcalljusttakestheledescriptortoclosein%ebx.Theprogramisthennished!ReviewKnowtheConcepts Describethelifecycleofaledescriptor. Whatarethestandardledescriptorsandwhataretheyusedfor? Whatisabuffer? Whatisthedifferencebetweenthe.datasectionandthe.bsssection? Whatarethesystemcallsrelatedtoreadingandwritingles? 93

PAGE 100

Chapter5.DealingwithFiles UsetheConcepts ModifythetoupperprogramsothatitreadsfromSTDINandwritestoSTDOUTinsteadofusingthelesonthecommand-line. Changethesizeofthebuffer. Rewritetheprogramsothatitusesstorageinthe.bsssectionratherthanthestacktostoretheledescriptors. Writeaprogramthatwillcreatealecalledheynow.txtandwritethewords"Heydiddlediddle!"intoit.GoingFurther Whatdifferencedoesthesizeofthebuffermake? Whaterrorresultscanbereturnedbyeachofthesesystemcalls? Maketheprogramabletoeitheroperateoncommand-lineargumentsoruseSTDINorSTDOUTbasedonthenumberofcommand-lineargumentsspeciedbyARGC. Modifytheprogramsothatitcheckstheresultsofeachsystemcall,andprintsoutanerrormessagetoSTDOUTwhenitoccurs. 94

PAGE 101

Chapter6.ReadingandWritingSimpleRecordsAsmentionedin Chapter5 ,manyapplicationsdealwithdatathatispersistent-meaningthatthedataliveslongerthantheprogrambybeingstoredondiskinfles.Youcanshutdowntheprogramandopenitbackup,andyouarebackwhereyoustarted.Now,therearetwobasickindsofpersistentdata-structuredandunstructured.Unstructureddataislikewhatwedealtwithinthetoupperprogram.Itjustdealtwithtextlesthatwereenteredbyaperson.Thecontentsofthelesweren'tusablebyaprogrambecauseaprogramcan'tinterpretwhattheuseristryingtosayinrandomtext.Structureddata,ontheotherhand,iswhatcomputersexcelathandling.Structureddataisdatathatisdividedupintoeldsandrecords.Forthemostpart,theeldsandrecordsarexed-length.Becausethedataisdividedintoxed-lengthrecordsandxed-formatelds,thecomputercaninterpretthedata.Structureddatacancontainvariable-lengthelds,butatthatpointyouareusuallybetteroffwithadatabase.1Thischapterdealswithreadingandwritingsimplexed-lengthrecords.Let'ssaywewantedtostoresomebasicinformationaboutpeopleweknow.Wecouldimaginethefollowingexamplexed-lengthrecordaboutpeople: Firstname-40bytes Lastname-40bytes Address-240bytes Age-4bytes 1. Adatabaseisaprogramwhichhandlespersistentstructureddataforyou.Youdon'thavetowritetheprogramstoreadandwritethedatatodisk,todolookups,oreventodobasicprocessing.Itisaveryhigh-levelinterfacetostructureddatawhich,althoughitaddssomeoverheadandadditionalcomplexity,isveryusefulforcomplexdataprocessingtasks.Referencesforlearninghowdatabasesworkarelistedin Chapter13 95

PAGE 102

Chapter6.ReadingandWritingSimpleRecords Inthis,everythingischaracterdataexceptfortheage,whichissimplyanumericeld,usingastandard4-bytewordwecouldjustuseasinglebyteforthis,butkeepingitatawordmakesiteasiertoprocess.Inprogramming,youoftenhavecertaindenitionsthatyouwilluseoverandoveragainwithintheprogram,orperhapswithinseveralprograms.Itisgoodtoseparatetheseoutintolesthataresimplyincludedintotheassemblylanguagelesasneeded.Forexample,inournextprogramswewillneedtoaccessthedifferentpartsoftherecordabove.Thismeansweneedtoknowtheoffsetsofeacheldfromthebeginningoftherecordinordertoaccessthemusingbasepointeraddressing.Thefollowingconstantsdescribetheoffsetstotheabovestructure.Puttheminalenamedrecord-def.s:.equRECORD_FIRSTNAME,0.equRECORD_LASTNAME,40.equRECORD_ADDRESS,80.equRECORD_AGE,320.equRECORD_SIZE,324Inaddition,thereareseveralconstantsthatwehavebeendeningoverandoverinourprograms,anditisusefultoputtheminale,sothatwedon'thavetokeepenteringthem.Putthefollowingconstantsinalecalledlinux.s:#CommonLinuxDefinitions#SystemCallNumbers.equSYS_EXIT,1.equSYS_READ,3.equSYS_WRITE,4.equSYS_OPEN,5.equSYS_CLOSE,6.equSYS_BRK,45 96

PAGE 103

Chapter6.ReadingandWritingSimpleRecords #SystemCallInterruptNumber.equLINUX_SYSCALL,0x80#StandardFileDescriptors.equSTDIN,0.equSTDOUT,1.equSTDERR,2#CommonStatusCodes.equEND_OF_FILE,0Wewillwritethreeprogramsinthischapterusingthestructuredenedinrecord-def.s.Therstprogramwillbuildalecontainingseveralrecordsasdenedabove.Thesecondprogramwilldisplaytherecordsinthele.Thethirdprogramwilladd1yeartotheageofeveryrecord.Inadditiontothestandardconstantswewillbeusingthroughouttheprograms,therearealsotwofunctionsthatwewillbeusinginseveraloftheprograms-onewhichreadsarecordandonewhichwritesarecord.Whatparametersdothesefunctionsneedinordertooperate?Webasicallyneed: Thelocationofabufferthatwecanreadarecordinto TheledescriptorthatwewanttoreadfromorwritetoLet'slookatourreadingfunctionrst:.include"record-def.s".include"linux.s"#PURPOSE:Thisfunctionreadsarecordfromthefile 97

PAGE 104

Chapter6.ReadingandWritingSimpleRecords #descriptor##INPUT:Thefiledescriptorandabuffer##OUTPUT:Thisfunctionwritesthedatatothebuffer#andreturnsastatuscode.##STACKLOCALVARIABLES.equST_READ_BUFFER,8.equST_FILEDES,12.section.text.globlread_record.typeread_record,@functionread_record:pushl%ebpmovl%esp,%ebppushl%ebxmovlST_FILEDES%ebp,%ebxmovlST_READ_BUFFER%ebp,%ecxmovl$RECORD_SIZE,%edxmovl$SYS_READ,%eaxint$LINUX_SYSCALL#NOTE-%eaxhasthereturnvalue,whichwewill#givebacktoourcallingprogrampopl%ebxmovl%ebp,%esppopl%ebpretIt'saprettysimplyfunction.Itjustreadsdatathesizeofourstructureintoanappropriatelysizedbufferfromthegivenledescriptor.Thewritingoneissimilar: 98

PAGE 105

Chapter6.ReadingandWritingSimpleRecords .include"linux.s".include"record-def.s"#PURPOSE:Thisfunctionwritesarecordto#thegivenfiledescriptor##INPUT:Thefiledescriptorandabuffer##OUTPUT:Thisfunctionproducesastatuscode##STACKLOCALVARIABLES.equST_WRITE_BUFFER,8.equST_FILEDES,12.section.text.globlwrite_record.typewrite_record,@functionwrite_record:pushl%ebpmovl%esp,%ebppushl%ebxmovl$SYS_WRITE,%eaxmovlST_FILEDES%ebp,%ebxmovlST_WRITE_BUFFER%ebp,%ecxmovl$RECORD_SIZE,%edxint$LINUX_SYSCALL#NOTE-%eaxhasthereturnvalue,whichwewill#givebacktoourcallingprogrampopl%ebxmovl%ebp,%esppopl%ebpret 99

PAGE 106

Chapter6.ReadingandWritingSimpleRecords Nowthatwehaveourbasicdenitionsdown,wearereadytowriteourprograms.WritingRecordsThisprogramwillsimplywritesomehardcodedrecordstodisk.Itwill: Openthele Writethreerecords ClosetheleTypethefollowingcodeintoalecalledwrite-records.s:.include"linux.s".include"record-def.s".section.data#Constantdataoftherecordswewanttowrite#Eachtextdataitemispaddedtotheproper#lengthwithnulli.e.0bytes.#.reptisusedtopadeachitem..repttells#theassemblertorepeatthesectionbetween#.reptand.endrthenumberoftimesspecified.#Thisisusedinthisprogramtoaddextranull#charactersattheendofeachfieldtofill#ituprecord1:.ascii"Fredrick".rept31#Paddingto40bytes.byte0.endr.ascii"Bartlett" 100

PAGE 107

Chapter6.ReadingandWritingSimpleRecords .rept31#Paddingto40bytes.byte0.endr.ascii"4242SPrairienTulsa,OK55555".rept209#Paddingto240bytes.byte0.endr.long45record2:.ascii"Marilyn".rept32#Paddingto40bytes.byte0.endr.ascii"Taylor".rept33#Paddingto40bytes.byte0.endr.ascii"2224SJohannanStnChicago,IL12345".rept203#Paddingto240bytes.byte0.endr.long29record3:.ascii"Derrick".rept32#Paddingto40bytes.byte0.endr 101

PAGE 108

Chapter6.ReadingandWritingSimpleRecords .ascii"McIntire".rept31#Paddingto40bytes.byte0.endr.ascii"500WOaklandnSanDiego,CA54321".rept206#Paddingto240bytes.byte0.endr.long36#Thisisthenameofthefilewewillwritetofile_name:.ascii"test.dat".equST_FILE_DESCRIPTOR,-4.globl_start_start:#Copythestackpointerto%ebpmovl%esp,%ebp#Allocatespacetoholdthefiledescriptorsubl$4,%esp#Openthefilemovl$SYS_OPEN,%eaxmovl$file_name,%ebxmovl$0101,%ecx#Thissaystocreateifit#doesn'texist,andopenfor#writingmovl$0666,%edxint$LINUX_SYSCALL#Storethefiledescriptorawaymovl%eax,ST_FILE_DESCRIPTOR%ebp 102

PAGE 109

Chapter6.ReadingandWritingSimpleRecords #WritethefirstrecordpushlST_FILE_DESCRIPTOR%ebppushl$record1callwrite_recordaddl$8,%esp#WritethesecondrecordpushlST_FILE_DESCRIPTOR%ebppushl$record2callwrite_recordaddl$8,%esp#WritethethirdrecordpushlST_FILE_DESCRIPTOR%ebppushl$record3callwrite_recordaddl$8,%esp#Closethefiledescriptormovl$SYS_CLOSE,%eaxmovlST_FILE_DESCRIPTOR%ebp,%ebxint$LINUX_SYSCALL#Exittheprogrammovl$SYS_EXIT,%eaxmovl$0,%ebxint$LINUX_SYSCALLThisisafairlysimpleprogram.Itmerelyconsistsofdeningthedatawewanttowriteinthe.datasection,andthencallingtherightsystemcallsandfunctioncallstoaccomplishit.Forarefresherofallofthesystemcallsused,see AppendixC 103

PAGE 110

Chapter6.ReadingandWritingSimpleRecords Youmayhavenoticedthelines:.include"linux.s".include"record-def.s"Thesestatementscausethegivenlestobasicallybepastedrightthereinthecode.Youdon'tneedtodothiswithfunctions,becausethelinkercantakecareofcombiningfunctionsexportedwith.globl.However,constantsdenedinanotherledoneedtobeimportedinthisway.Also,youmayhavenoticedtheuseofanewassemblerdirective,.rept.Thisdirectiverepeatsthecontentsofthelebetweenthe.reptandthe.endrdirectivesthenumberoftimesspeciedafter.rept.Thisisusuallyusedthewayweusedit-topadvaluesinthe.datasection.Inourcase,weareaddingnullcharacterstotheendofeachelduntiltheyaretheirdenedlengths.Tobuildtheapplication,runthecommands:aswrite-records.s-owrite-record.oaswrite-record.s-owrite-record.oldwrite-record.owrite-records.o-owrite-recordsHereweareassemblingtwolesseparately,andthencombiningthemtogetherusingthelinker.Toruntheprogram,justtypethefollowing:./write-recordsThiswillcausealecalledtest.dattobecreatedcontainingtherecords.However,sincetheycontainnon-printablecharactersthenullcharacter,specically,theymaynotbeviewablebyatexteditor.Thereforeweneedthenextprogramtoreadthemforus. 104

PAGE 111

Chapter6.ReadingandWritingSimpleRecords ReadingRecordsNowwewillconsidertheprocessofreadingrecords.Inthisprogram,wewillreadeachrecordanddisplaytherstnamelistedwitheachrecord.Sinceeachperson'snameisadifferentlength,wewillneedafunctiontocountthenumberofcharacterswewanttowrite.Sincewepadeacheldwithnullcharacters,wecansimplycountcharactersuntilwereachanullcharacter.2Notethatthismeansourrecordsmustcontainatleastonenullcharactereach.Hereisthecode.Putitinalecalledcount-chars.s:#PURPOSE:Countthecharactersuntilanullbyteisreached.##INPUT:Theaddressofthecharacterstring##OUTPUT:Returnsthecountin%eax##PROCESS:#Registersused:#%ecx-charactercount#%al-currentcharacter#%edx-currentcharacteraddress.typecount_chars,@function.globlcount_chars#Thisiswhereouroneparameterisonthestack.equST_STRING_START_ADDRESS,8count_chars:pushl%ebpmovl%esp,%ebp#Counterstartsatzeromovl$0,%ecx 2. IfyouhaveusedC,thisiswhatthestrlenfunctiondoes. 105

PAGE 112

Chapter6.ReadingandWritingSimpleRecords #StartingaddressofdatamovlST_STRING_START_ADDRESS%ebp,%edxcount_loop_begin:#Grabthecurrentcharactermovb%edx,%al#Isitnull?cmpb$0,%al#Ifyes,we'redonejecount_loop_end#Otherwise,incrementthecounterandthepointerincl%ecxincl%edx#Gobacktothebeginningoftheloopjmpcount_loop_begincount_loop_end:#We'redone.Movethecountinto%eax#andreturn.movl%ecx,%eaxpopl%ebpretAsyoucansee,it'safairlystraightforwardfunction.Itsimplyloopsthroughthebytes,countingasitgoes,untilithitsanullcharacter.Thenitreturnsthecount.Ourrecord-readingprogramwillbefairlystraightforward,too.Itwilldothefollowing: Openthele Attempttoreadarecord 106

PAGE 113

Chapter6.ReadingandWritingSimpleRecords Ifweareattheendofthele,exit Otherwise,countthecharactersoftherstname WritetherstnametoSTDOUT WriteanewlinetoSTDOUT GobacktoreadanotherrecordTowritethis,weneedonemoresimplefunction-afunctiontowriteoutanewlinetoSTDOUT.Putthefollowingcodeintowrite-newline.s:.include"linux.s".globlwrite_newline.typewrite_newline,@function.section.datanewline:.ascii"n".section.text.equST_FILEDES,8write_newline:pushl%ebpmovl%esp,%ebpmovl$SYS_WRITE,%eaxmovlST_FILEDES%ebp,%ebxmovl$newline,%ecxmovl$1,%edxint$LINUX_SYSCALLmovl%ebp,%esppopl%ebpret 107

PAGE 114

Chapter6.ReadingandWritingSimpleRecords Nowwearereadytowritethemainprogram.Hereisthecodetoread-records.s:.include"linux.s".include"record-def.s".section.datafile_name:.ascii"test.dat".section.bss.lcommrecord_buffer,RECORD_SIZE.section.text#Mainprogram.globl_start_start:#Thesearethelocationsonthestackwhere#wewillstoretheinputandoutputdescriptors#FYI-wecouldhaveusedmemoryaddressesin#a.datasectioninstead.equST_INPUT_DESCRIPTOR,-4.equST_OUTPUT_DESCRIPTOR,-8#Copythestackpointerto%ebpmovl%esp,%ebp#Allocatespacetoholdthefiledescriptorssubl$8,%esp#Openthefilemovl$SYS_OPEN,%eaxmovl$file_name,%ebxmovl$0,%ecx#Thissaystoopenread-only 108

PAGE 115

Chapter6.ReadingandWritingSimpleRecords movl$0666,%edxint$LINUX_SYSCALL#Savefiledescriptormovl%eax,ST_INPUT_DESCRIPTOR%ebp#Eventhoughit'saconstant,weare#savingtheoutputfiledescriptorin#alocalvariablesothatifwelater#decidethatitisn'talwaysgoingto#beSTDOUT,wecanchangeiteasily.movl$STDOUT,ST_OUTPUT_DESCRIPTOR%ebprecord_read_loop:pushlST_INPUT_DESCRIPTOR%ebppushl$record_buffercallread_recordaddl$8,%esp#Returnsthenumberofbytesread.#Ifitisn'tthesamenumberwe#requested,thenit'seitheran#end-of-file,oranerror,sowe're#quittingcmpl$RECORD_SIZE,%eaxjnefinished_reading#Otherwise,printoutthefirstname#butfirst,wemustknowit'ssizepushl$RECORD_FIRSTNAME+record_buffercallcount_charsaddl$4,%esp 109

PAGE 116

Chapter6.ReadingandWritingSimpleRecords movl%eax,%edxmovlST_OUTPUT_DESCRIPTOR%ebp,%ebxmovl$SYS_WRITE,%eaxmovl$RECORD_FIRSTNAME+record_buffer,%ecxint$LINUX_SYSCALLpushlST_OUTPUT_DESCRIPTOR%ebpcallwrite_newlineaddl$4,%espjmprecord_read_loopfinished_reading:movl$SYS_EXIT,%eaxmovl$0,%ebxint$LINUX_SYSCALLTobuildthisprogram,weneedtoassembleallofthepartsandlinkthemtogether:asread-record.s-oread-record.oascount-chars.s-ocount-chars.oaswrite-newline.s-owrite-newline.oasread-records.s-oread-records.oldread-record.ocount-chars.owrite-newline.oread-records.o-oread-recordsThebackslashintherstlinesimplymeansthatthecommandcontinuesonthenextline.Youcanrunyourprogrambydoing./read-records.Asyoucansee,thisprogramopenstheleandthenrunsaloopofreading,checkingfortheendofle,andwritingtherstname.Theoneconstructthatmightbenewisthelinethatsays:pushl$RECORD_FIRSTNAME+record_buffer 110

PAGE 117

Chapter6.ReadingandWritingSimpleRecords Itlookslikewearecombiningandaddinstructionwithapushinstruction,butwearenot.Yousee,bothRECORD_FIRSTNAMEandrecord_bufferareconstants.Therstisadirectconstant,createdthroughtheuseofa.equdirective,whilethelatterisdenedautomaticallybytheassemblerthroughitsuseasalabelit'svaluebeingtheaddressthatthedatathatfollowsitwillstartat.Sincetheyarebothconstantsthattheassemblerknows,itisabletoaddthemtogetherwhileitisassemblingyourprogram,sothewholeinstructionisasingleimmediate-modepushofasingleconstant.TheRECORD_FIRSTNAMEconstantisthenumberofbytesafterthebeginningofarecordbeforewehittherstname.record_bufferisthenameofourbufferforholdingrecords.Addingthemtogethergetsustheaddressoftherstnamememberoftherecordstoredinrecord_buffer.ModifyingtheRecordsInthissection,wewillwriteaprogramthat: Opensaninputandoutputle Readsrecordsfromtheinput Incrementstheage WritesthenewrecordtotheoutputleLikemostprogramswe'veencounteredrecently,thisprogramisprettystraightforward.3.include"linux.s".include"record-def.s" 3. Youwillndthatafterlearningthemechanicsofprogramming,mostprogramsareprettystraightforwardonceyouknowexactlywhatitisyouwanttodo.Mostofthemini-tializedata,dosomeprocessinginaloop,andthencleaneverythingup. 111

PAGE 118

Chapter6.ReadingandWritingSimpleRecords .section.datainput_file_name:.ascii"test.dat"output_file_name:.ascii"testout.dat".section.bss.lcommrecord_buffer,RECORD_SIZE#Stackoffsetsoflocalvariables.equST_INPUT_DESCRIPTOR,-4.equST_OUTPUT_DESCRIPTOR,-8.section.text.globl_start_start:#Copystackpointerandmakeroomforlocalvariablesmovl%esp,%ebpsubl$8,%esp#Openfileforreadingmovl$SYS_OPEN,%eaxmovl$input_file_name,%ebxmovl$0,%ecxmovl$0666,%edxint$LINUX_SYSCALLmovl%eax,ST_INPUT_DESCRIPTOR%ebp#Openfileforwritingmovl$SYS_OPEN,%eaxmovl$output_file_name,%ebxmovl$0101,%ecx 112

PAGE 119

Chapter6.ReadingandWritingSimpleRecords movl$0666,%edxint$LINUX_SYSCALLmovl%eax,ST_OUTPUT_DESCRIPTOR%ebploop_begin:pushlST_INPUT_DESCRIPTOR%ebppushl$record_buffercallread_recordaddl$8,%esp#Returnsthenumberofbytesread.#Ifitisn'tthesamenumberwe#requested,thenit'seitheran#end-of-file,oranerror,sowe're#quittingcmpl$RECORD_SIZE,%eaxjneloop_end#Incrementtheageinclrecord_buffer+RECORD_AGE#WritetherecordoutpushlST_OUTPUT_DESCRIPTOR%ebppushl$record_buffercallwrite_recordaddl$8,%espjmploop_beginloop_end:movl$SYS_EXIT,%eaxmovl$0,%ebxint$LINUX_SYSCALL 113

PAGE 120

Chapter6.ReadingandWritingSimpleRecords Youcantypeitinasadd-year.s.Tobuildit,typethefollowing4:asadd-year.s-oadd-year.oldadd-year.oread-record.owrite-record.o-oadd-yearToruntheprogram,justtypeinthefollowing5:./add-yearThiswilladdayeartoeveryrecordlistedintest.datandwritethenewrecordstotheletestout.dat.Asyoucansee,writingxed-lengthrecordsisprettysimple.Youonlyhavetoreadinblocksofdatatoabuffer,processthem,andwritethembackout.Unfortunately,thisprogramdoesn'twritethenewagesouttothescreensoyoucanverifyyourprogram'seffectiveness.Thisisbecausewewon'tgettodisplayingnumbersuntil Chapter8 and Chapter10 .Afterreadingthoseyoumaywanttocomebackandrewritethisprogramtodisplaythenumericdatathatwearemodifying.ReviewKnowtheConcepts Whatisarecord? Whatistheadvantageofxed-lengthrecordsovervariable-lengthrecords? Howdoyouincludeconstantsinmultipleassemblysourceles? 4. Thisassumesthatyouhavealreadybuilttheobjectlesread-record.oandwrite-record.ointhepreviousexamples.Ifnot,youwillhavetodoso. 5. Thisisassumingyoucreatedtheleinapreviousrunofwrite-records.Ifnot,youneedtorunwrite-recordsrstbeforerunningthisprogram. 114

PAGE 121

Chapter6.ReadingandWritingSimpleRecords Whymightyouwanttosplitupaprojectintomultiplesourceles? Whatdoestheinstructioninclrecord_buffer+RECORD_AGEdo?Whataddressingmodeisitusing?Howmanyoperandsdoestheinclinstructionshaveinthiscase?Whichpartsarebeinghandledbytheassemblerandwhichpartsarebeinghandledwhentheprogramisrun?UsetheConcepts Addanotherdatamembertothepersonstructuredenedinthischapter,andrewritethereadingandwritingfunctionsandprogramstotakethemintoaccount.Remembertoreassembleandrelinkyourlesbeforerunningyourprograms. Createaprogramthatusesalooptowrite30identicalrecordstoale. Createaprogramtondthelargestageintheleandreturnthatageasthestatuscodeoftheprogram. Createaprogramtondthesmallestageintheleandreturnthatageasthestatuscodeoftheprogram.GoingFurther Rewritetheprogramsinthischaptertousecommand-lineargumentstospecifythelesnames. Researchthelseeksystemcall.Rewritetheadd-yearprogramtoopenthesourceleforbothreadingandwritinguse$2fortheread/writemode,andwritethemodiedrecordsbacktothesameletheywerereadfrom. Researchthevariouserrorcodesthatcanbereturnedbythesystemcallsmadeintheseprograms.Pickonetorewrite,andaddcodethatchecks%eaxforerror 115

PAGE 122

Chapter6.ReadingandWritingSimpleRecords conditions,and,ifoneisfound,writesamessageaboutittoSTDERRandexit. Writeaprogramthatwilladdasinglerecordtothelebyreadingthedatafromthekeyboard.Remember,youwillhavetomakesurethatthedatahasatleastonenullcharacterattheend,andyouneedtohaveawayfortheusertoindicatetheyaredonetyping.Becausewehavenotgottenintocharacterstonumbersconversion,youwillnotbeabletoreadtheageinfromthekeyboard,soyou'llhavetohaveadefaultage. Writeafunctioncalledcompare-stringsthatwillcomparetwostringsupto5characters.Thenwriteaprogramthatallowstheusertoenter5characters,andhavetheprogramreturnallrecordswhoserstnamestartswiththose5characters. 116

PAGE 123

Chapter7.DevelopingRobustProgramsThischapterdealswithdevelopingprogramsthatarerobust.Robustprogramsareabletohandleerrorconditionsgracefully.Theyareprogramsthatdonotcrashnomatterwhattheuserdoes.Buildingrobustprogramsisessentialtothepracticeofprogramming.Writingrobustprogramstakesdisciplineandwork-itusuallyentailsndingeverypossibleproblemthatcanoccur,andcomingupwithanactionplanforyourprogramtotake.WhereDoestheTimeGo?Programmersschedulepoorly.Inalmosteveryprogrammingproject,programmerswilltaketwo,four,oreveneighttimesaslongtodevelopaprogramorfunctionthantheyoriginallyestimated.Therearemanyreasonsforthisproblem,including: Programmersdon'talwaysscheduletimeformeetingsorothernon-codingactivitiesthatmakeupeveryday. Programmersoftenunderestimatefeedbacktimeshowlongittakestopasschangerequestsandapprovalsbackandforthforprojects. Programmersdon'talwaysunderstandthefullscopeofwhattheyareproducing. Programmersoftenhavetoestimateascheduleonatotallydifferentkindofprojectthantheyareusedto,andthusareunabletoscheduleaccurately. Programmersoftenunderestimatetheamountoftimeittakestogetaprogramfullyrobust.Thelastitemistheoneweareinterestedinhere.Ittakesalotoftimeandefforttodeveloprobustprograms.Moresothanpeopleusuallyguess,including 117

PAGE 124

Chapter7.DevelopingRobustPrograms experiencedprogrammers.Programmersgetsofocusedonsimplysolvingtheproblemathandthattheyfailtolookatthepossiblesideissues.Inthetoupperprogram,wedonothaveanycourseofactioniftheletheuserselectsdoesnotexist.Theprogramwillgoaheadandtrytoworkanyway.Itdoesn'treportanyerrormessagesotheuserwon'tevenknowthattheytypedinthenamewrong.Let'ssaythatthedestinationleisonanetworkdrive,andthenetworktemporarilyfails.Theoperatingsystemisreturningastatuscodetousin%eax,butwearen'tcheckingit.Therefore,ifafailureoccurs,theuseristotallyunaware.Thisprogramisdenitelynotrobust.Asyoucansee,eveninasimpleprogramtherearealotofthingsthatcangowrongthataprogrammermustcontendwith.Inalargeprogram,itgetsmuchmoreproblematic.Thereareusuallymanymorepossibleerrorconditionsthanpossiblesuccessfulconditions.Therefore,youshouldalwaysexpecttospendthemajorityofyourtimecheckingstatuscodes,writingerrorhandlers,andperformingsimilartaskstomakeyourprogramrobust.Ifittakestwoweekstodevelopaprogram,itwilllikelytakeatleasttwomoretomakeitrobust.Rememberthateveryerrormessagethatpopsuponyourscreenhadtobeprogrammedinbysomeone.SomeTipsforDevelopingRobustProgramsUserTestingTestingisoneofthemostessentialthingsaprogrammerdoes.Ifyouhaven'ttestedsomething,youshouldassumeitdoesn'twork.However,testingisn'tjustaboutmakingsureyourprogramworks,it'saboutmakingsureyourprogramdoesn'tbreak.Forexample,ifIhaveaprogramthatisonlysupposedtodealwithpositivenumbers,youneedtotestwhathappensiftheuserentersanegativenumber.Oraletter.Orthenumberzero.Youmusttestwhathappensiftheyputspacesbeforetheirnumbers,spacesaftertheirnumbers,andotherlittle 118

PAGE 125

Chapter7.DevelopingRobustPrograms possibilities.Youneedtomakesurethatyouhandletheuser'sdatainawaythatmakessensetotheuser,andthatyoupassonthatdatainawaythatmakessensetotherestofyourprogram.Whenyourprogramndsinputthatdoesn'tmakesense,itneedstoperformappropriateactions.Dependingonyourprogram,thismayincludeendingtheprogram,promptingtheusertore-entervalues,notifyingacentralerrorlog,rollingbackanoperation,orignoringitandcontinuing.Notonlyshouldyoutestyourprograms,youneedtohaveotherstestitaswell.Youshouldenlistotherprogrammersandusersofyourprogramtohelpyoutestyourprogram.Ifsomethingisaproblemforyourusers,evenifitseemsokaytoyou,itneedstobexed.Iftheuserdoesn'tknowhowtouseyourprogramcorrectly,thatshouldbetreatedasabugthatneedstobexed.Youwillndthatusersndalotmorebugsinyourprogramthanyouevercould.Thereasonisthatusersdon'tknowwhatthecomputerexpects.Youknowwhatkindsofdatathecomputerexpects,andthereforearemuchmorelikelytoenterdatathatmakessensetothecomputer.Usersenterdatathatmakessensetothem.Allowingnon-programmerstouseyourprogramfortestingpurposesusuallygivesyoumuchmoreaccurateresultsastohowrobustyourprogramtrulyis.DataTestingWhendesigningprograms,eachofyourfunctionsneedstobeveryspecicaboutthetypeandrangeofdatathatitwillorwon'taccept.Youthenneedtotestthesefunctionstomakesurethattheyperformtospecicationwhenhandedtheappropriatedata.Mostimportantistestingcornercasesoredgecases.Cornercasesaretheinputsthataremostlikelytocauseproblemsorbehaveunexpectedly.Whentestingnumericdata,thereareseveralcornercasesyoualwaysneedtotest: Thenumber0 Thenumber1 119

PAGE 126

Chapter7.DevelopingRobustPrograms Anumberwithintheexpectedrange Anumberoutsidetheexpectedrange Therstnumberintheexpectedrange Thelastnumberintheexpectedrange Therstnumberbelowtheexpectedrange TherstnumberabovetheexpectedrangeForexample,ifIhaveaprogramthatissupposedtoacceptvaluesbetween5and200,Ishouldtest0,1,4,5,153,200,201,and255ataminimumand255wererandomlychoseninsideandoutsidetherange,respectively.Thesamegoesforanylistsofdatayouhave.Youneedtotestthatyourprogrambehavesasexpectedforlistsof0items,1item,massivenumbersofitems,andsoon.Inaddition,youshouldalsotestanyturningpointsyouhave.Forexample,ifyouhavedifferentcodetohandlepeopleunderandoverage30,forexample,youwouldneedtotestitonpeopleofages29,30,and31atleast.Therewillbesomeinternalfunctionsthatyouassumegetgooddatabecauseyouhavecheckedforerrorsbeforethispoint.However,whileindevelopmentyouoftenneedtocheckforerrorsanyway,asyourothercodemayhaveerrorsinit.Toverifytheconsistencyandvalidityofdataduringdevelopment,mostlanguageshaveafacilitytoeasilycheckassumptionsaboutdatacorrectness.IntheClanguagethereistheassertmacro.Youcansimplyputinyourcodeasserta>b;,anditwillgiveanerrorifitreachesthatcodewhentheconditionisnottrue.Inaddition,sincesuchacheckisawasteoftimeafteryourcodeisstable,theassertmacroallowsyoutoturnoffassertsatcompile-time.Thismakessurethatyourfunctionsarereceivinggooddatawithoutcausingunnecessaryslowdownsforcodereleasedtothepublic.ModuleTestingNotonlyshouldyoutestyourprogramasawhole,youneedtotesttheindividual 120

PAGE 127

Chapter7.DevelopingRobustPrograms piecesofyourprogram.Asyoudevelopyourprogram,youshouldtestindividualfunctionsbyprovidingitwithdatayoucreatetomakesureitrespondsappropriately.Inordertodothiseffectively,youhavetodevelopfunctionswhosesolepurposeistocallfunctionsfortesting.Thesearecalleddriversnottobeconfusedwithhardwaredrivers.Theysimplyloadsyourfunction,supplyitwithdata,andchecktheresults.Thisisespeciallyusefulifyouareworkingonpiecesofanunnishedprogram.Sinceyoucan'ttestallofthepiecestogether,youcancreateadriverprogramthatwilltesteachfunctionindividually.Also,thecodeyouaretestingmaymakecallstofunctionsnotdevelopedyet.Inordertoovercomethisproblem,youcanwriteasmallfunctioncalledastubwhichsimplyreturnsthevaluesthatfunctionneedstoproceed.Forexample,inane-commerceapplication,Ihadafunctioncalledis_ready_to_checkout.BeforeIhadtimetoactuallywritethefunctionIjustsetittoreturntrueoneverycallsothatthefunctionswhichreliedonitwouldhaveananswer.Thisallowedmetotestfunctionswhichreliedonis_ready_to_checkoutwithoutthefunctionbeingfullyimplemented.HandlingErrorsEffectivelyNotonlyisitimportanttoknowhowtotest,butitisalsoimportanttoknowwhattodowhenanerrorisdetected.HaveanErrorCodeforEverythingTrulyrobustsoftwarehasauniqueerrorcodeforeverypossiblecontingency.Bysimplyknowingtheerrorcode,youshouldbeabletondthelocationinyourcodewherethaterrorwassignalled. 121

PAGE 128

Chapter7.DevelopingRobustPrograms Thisisimportantbecausetheerrorcodeisusuallyalltheuserhastogoonwhenreportingerrors.Therefore,itneedstobeasusefulaspossible.Errorcodesshouldalsobeaccompaniedbydescriptiveerrormessages.However,onlyinrarecircumstancesshouldtheerrormessagetrytopredictwhytheerroroccurred.Itshouldsimplyrelatewhathappened.Backin1995IworkedforanInternetServiceProvider.Oneofthewebbrowserswesupportedtriedtoguessthecauseforeverynetworkerror,ratherthanjustreportingtheerror.Ifthecomputerwasn'tconnectedtotheInternetandtheusertriedtoconnecttoawebsite,itwouldsaythattherewasaproblemwiththeInternetServiceProvider,thattheserverwasdown,andthattheusershouldcontacttheirInternetServiceProvidertocorrecttheproblem.Nearlyaquarterofourcallswerefrompeoplewhohadreceivedthismessage,butmerelyneededtoconnecttotheInternetbeforetryingtousetheirbrowser.Asyoucansee,tryingtodiagnosewhattheproblemiscanleadtoalotmoreproblemsthanitxes.Itisbettertojustreporterrorcodesandmessages,andhaveseparateresourcesfortheusertotroubleshootingtheapplication.Atroubleshootingguide,nottheprogramitself,isanappropriateplacetolistpossiblereasonsandcoursesforactionforeacherrormessage.RecoveryPointsInordertosimplifyerrorhandling,itisoftenusefultobreakyourprogramapartintodistinctunits,whereeachunitfailsandisrecoveredasawhole.Forexample,youcouldbreakyourprogramupsothatreadingthecongurationlewasaunit.Ifreadingthecongurationlefailedatanypointopeningthele,readingthele,tryingtodecodethele,etc.thentheprogramwouldsimplytreatitasacongurationleproblemandskiptotherecoverypointforthatproblem.Thiswayyougreatlyreducethenumberoferror-handlingmechanismyouneedforyourprogram,becauseerrorrecoveryisdoneonamuchmoregenerallevel.Notethatevenwithrecoverypoints,yourerrormessagesneedtobespecicastowhattheproblemwas.Recoverypointsarebasicunitsforerrorrecovery,notforerrordetection.Errordetectionstillneedstobeextremelyexact,andtheerror 122

PAGE 129

Chapter7.DevelopingRobustPrograms reportsneedexacterrorcodesandmessages.Whenusingrecoverypoints,youoftenneedtoincludecleanupcodetohandledifferentcontingencies.Forexample,inourcongurationleexample,therecoveryfunctionwouldneedtoincludecodetocheckandseeifthecongurationlewasstillopen.Dependingonwheretheerroroccurred,thelemayhavebeenleftopen.Therecoveryfunctionneedstocheckforthiscondition,andanyotherconditionthatmightleadtosysteminstability,andreturntheprogramtoaconsistentstate.Thesimplestwaytohandlerecoverypointsistowrapthewholeprogramintoasinglerecoverypoint.Youwouldjusthaveasimpleerror-reportingfunctionthatyoucancallwithanerrorcodeandamessage.Thefunctionwouldprintthemandandsimplyexittheprogram.Thisisnotusuallythebestsolutionforreal-worldsituations,butitisagoodfall-back,lastresortmechanism.MakingOurProgramMoreRobustThissectionwillgothroughmakingtheadd-year.sprogramfrom Chapter6 alittlemorerobust.Sincethisisaprettysimpleprogram,wewilllimitourselvestoasinglerecoverypointthatcoversthewholeprogram.Theonlythingwewilldotorecoveristoprinttheerrorandexit.Thecodetodothatisprettysimple:.include"linux.s".equST_ERROR_CODE,8.equST_ERROR_MSG,12.globlerror_exit.typeerror_exit,@functionerror_exit:pushl%ebpmovl%esp,%ebp 123

PAGE 130

Chapter7.DevelopingRobustPrograms #WriteouterrorcodemovlST_ERROR_CODE%ebp,%ecxpushl%ecxcallcount_charspopl%ecxmovl%eax,%edxmovl$STDERR,%ebxmovl$SYS_WRITE,%eaxint$LINUX_SYSCALL#WriteouterrormessagemovlST_ERROR_MSG%ebp,%ecxpushl%ecxcallcount_charspopl%ecxmovl%eax,%edxmovl$STDERR,%ebxmovl$SYS_WRITE,%eaxint$LINUX_SYSCALLpushl$STDERRcallwrite_newline#Exitwithstatus1movl$SYS_EXIT,%eaxmovl$1,%ebxint$LINUX_SYSCALLEnteritinalecallederror-exit.s.Tocallit,youjustneedtopushtheaddressofanerrormessage,andthenanerrorcodeontothestack,andcallthefunction.Nowlet'slookforpotentialerrorspotsinouradd-yearprogram.Firstofall,wedon'tchecktoseeifeitherofouropensystemcallsactuallycompleteproperly. 124

PAGE 131

Chapter7.DevelopingRobustPrograms Linuxreturnsitsstatuscodein%eax,soweneedtocheckandseeifthereisanerror.#Openfileforreadingmovl$SYS_OPEN,%eaxmovl$input_file_name,%ebxmovl$0,%ecxmovl$0666,%edxint$LINUX_SYSCALLmovl%eax,INPUT_DESCRIPTOR%ebp#Thiswilltestandseeif%eaxis#negative.Ifitisnotnegative,it#willjumptocontinue_processing.#Otherwiseitwillhandletheerror#conditionthatthenegativenumber#represents.cmpl$0,%eaxjlcontinue_processing#Sendtheerror.section.datano_open_file_code:.ascii"0001:"no_open_file_msg:.ascii"Can'tOpenInputFile".section.textpushl$no_open_file_msgpushl$no_open_file_codecallerror_exitcontinue_processing: 125

PAGE 132

Chapter7.DevelopingRobustPrograms #RestofprogramSo,aftercallingthesystemcall,wecheckandseeifwehaveanerrorbycheckingtoseeiftheresultofthesystemcallislessthanzero.Ifso,wecallourerrorreportingandexitroutine.Aftereverysystemcall,functioncall,orinstructionwhichcanhaveerroneousresultsyoushouldadderrorcheckingandhandlingcode.Toassembleandlinktheles,do:asadd-year.s-oadd-year.oaserror-exit.s-oerror-exit.oldadd-year.owrite-newline.oerror-exit.oread-record.owrite-record.ocount-chars.o-oadd-yearNowtrytorunitwithoutthenecessaryles.Itnowexitscleanlyandgracefully!ReviewKnowtheConcepts Whatarethereasonsprogrammer'shavetroublewithscheduling? Findyourfavoriteprogram,andtrytouseitinacompletelywrongmanner.Openuplesofthewrongtype,chooseinvalidoptions,closewindowsthataresupposedtobeopen,etc.Counthowmanydifferenterrorscenariostheyhadtoaccountfor. Whatarecornercases?Canyoulistexamplesofnumericcornercases? Whyisusertestingsoimportant? Whatarestubsanddriversusedfor?What'sthedifferencebetweenthetwo? 126

PAGE 133

Chapter7.DevelopingRobustPrograms Whatarerecoverypointsusedfor? Howmanydifferenterrorcodesshouldaprogramhave?UsetheConcepts Gothroughtheadd-year.sprogramandadderror-checkingcodeaftereverysystemcall. Findoneotherprogramwehavedonesofar,andadderror-checkingtothatprogram. Addarecoverymechanismforadd-year.sthatallowsittoreadfromSTDINifitcannotopenthestandardle.GoingFurther What,ifanything,shouldyoudoifyourerror-reportingfunctionfails?Why? Trytondbugsinatleastoneopen-sourceprogram.Fileabugreportforit. Trytoxthebugyoufoundinthepreviousexercise. 127

PAGE 134

Chapter7.DevelopingRobustPrograms 128

PAGE 135

Chapter8.SharingFunctionswithCodeLibrariesBynowyoushouldrealizethatthecomputerhastodoalotofworkevenforsimpletasks.Becauseofthat,youhavetodoalotofworktowritethecodeforacomputertoevendosimpletasks.Inaddition,programmingtasksareusuallynotverysimple.Therefore,weneeedawaytomakethisprocesseasieronourselves.Thereareseveralwaystodothis,including: Writecodeinahigh-levellanguageinsteadofassemblylanguage Havelotsofpre-writtencodethatyoucancutandpasteintoyourownprograms HaveasetoffunctionsonthesystemthataresharedamonganyprogramthatwishestouseitAllthreeoftheseareusuallyusedtosomedegreeinanygivenproject.Therstoptionwillbeexploredfurtherin Chapter11 .Thesecondoptionisusefulbutitsuffersfromsomedrawbacks,including: Codethatiscopiedoftenhastobemajorlymodiedtotthesurroundingcode. Everyprogramcontainingthecopiedcodehasthesamecodeinit,thuswastingalotofspace. Ifabugisfoundinanyofthecopiedcodeithastobexedineveryapplicationprogram.Therefore,thesecondoptionisusuallyusedsparingly.Itisusuallyonlyusedincaseswhereyoucopyandpasteskeletoncodeforaspecictypeoftask,andaddinyourprogram-specicdetails.Thethirdoptionistheonethatisusedthemostoften.Thethirdoptionincludeshavingacentralrepositoryofsharedcode.Then,insteadofeachprogramwastingspacestoringthesamecopiesoffunctions,theycansimplypointtothesharedlibrarieswhichcontainthefunctionstheyneed.Ifa 129

PAGE 136

Chapter8.SharingFunctionswithCodeLibraries bugisfoundinoneofthesefunctions,itonlyhastobexedwithinthesinglefunctionlibraryle,andallapplicationswhichuseitareautomaticallyupdated.Themaindrawbackwiththisapproachisthatitcreatessomedependencyproblems,including: Ifmultipleapplicationsareallusingthesharedle,howdoweknowwhenitissafetodeletethele?Forexample,ifthreeapplicationsaresharingaleoffunctionsand2oftheprogramsaredeleted,howdoesthesystemknowthattherestillexistsanapplicationthatusesthatcode,andthereforeitshouldn'tbedeleted? Someprogramsinadvertantlyrelyonbugswithinsharedfunctions.Therefore,ifupgradingthesharedprogramxesabugthataprogramdependedon,itcouldcausethatapplicationtoceasefunctioning.Theseproblemsarewhatleadtowhatisknownas"DLLhell".However,itisgenerallyassumedthattheadvantagesoutweighthedisadvantages.Inprogramming,thesesharedcodelesarereferredtoassharedlibraries,sharedobjects,dynamic-linklibraries,DLLs,or.soles.Wewillrefertothemassharedlibraries.UsingaSharedLibraryTheprogramwewillexaminehereissimple-itwritesthecharactershelloworldtothescreenandexits.Theregularprogram,helloworld-nolib.s,lookslikethis:#PURPOSE:Thisprogramwritesthemessage"helloworld"and#exits#.include"linux.s" 130

PAGE 137

Chapter8.SharingFunctionswithCodeLibraries .section.datahelloworld:.ascii"helloworldn"helloworld_end:.equhelloworld_len,helloworld_end-helloworld.section.text.globl_start_start:movl$STDOUT,%ebxmovl$helloworld,%ecxmovl$helloworld_len,%edxmovl$SYS_WRITE,%eaxint$LINUX_SYSCALLmovl$0,%ebxmovl$SYS_EXIT,%eaxint$LINUX_SYSCALLThat'snottoolong.However,takealookathowshorthelloworld-libiswhichusesalibrary:#PURPOSE:Thisprogramwritesthemessage"helloworld"and#exits#.section.datahelloworld: 131

PAGE 138

Chapter8.SharingFunctionswithCodeLibraries .ascii"helloworldn".section.text.globl_start_start:pushl$helloworldcallprintfpushl$0callexitIt'sevenshorter!Now,buildingprogramswhichusesharedlibrariesisalittledifferentthannormal.Youcanbuildtherstprogramnormallybydoingthis:ashelloworld-nolib.s-ohelloworld-nolib.oldhelloworld-nolib.o-ohelloworld-nolibHowever,inordertobuildthesecondprogram,youhavetodothis:ashelloworld-lib.s-ohelloworld-lib.old-dynamic-linker/lib/ld-linux.so.2-ohelloworld-libhelloworld-lib.o-lcRemember,thebackslashintherstlinesimplymeansthatthecommandcontinuesonthenextline.Theoption-dynamic-linker/lib/ld-linux.so.2allowsourprogramtobelinkedtolibraries.Thisbuildstheexecutablesothatbeforeexecuting,theoperatingsystemwillloadtheprogram/lib/ld-linux.so.2toloadinexternallibrariesandlinkthemwiththeprogram.Thisprogramisknownasadynamiclinker.The-lcoptionsaystolinktotheclibrary,namedlibc.soonGNU/Linuxsystems.Givenalibraryname,cinthiscaseusuallylibrarynamesarelongerthanasingleletter,theGNU/Linuxlinkerprependsthestringlibtothebeginningof 132

PAGE 139

Chapter8.SharingFunctionswithCodeLibraries thelibrarynameandappends.sototheendofittoformthelibrary'slename.Thislibrarycontainsmanyfunctionstoautomatealltypesoftasks.Thetwoweareusingareprintf,whichprintsstrings,andexit,whichexitstheprogram.Noticethatthesymbolsprintfandexitaresimplyreferredtobynamewithintheprogram.Inpreviouschapters,thelinkerwouldresolveallofthenamestophysicalmemoryaddresses,andthenameswouldbethrownaway.Whenusingdynamiclinking,thenameitselfresideswithintheexecutable,andisresolvedbythedynamiclinkerwhenitisrun.Whentheprogramisrunbytheuser,thedynamiclinkerloadsthesharedlibrarieslistedinourlinkstatement,andthenndsallofthefunctionandvariablenamesthatwerenamedbyourprogrambutnotfoundatlinktime,andmatchesthemupwithcorrespondingentriesinthesharedlibrariesitloads.Itthenreplacesallofthenameswiththeaddresseswhichtheyareloadedat.Thissoundstime-consuming.Itistoasmalldegree,butitonlyhappensonce-atprogramstartuptime.HowSharedLibrariesWorkInourrstprograms,allofthecodewascontainedwithinthesourcele.Suchprogramsarecalledstatically-linkedexecutables,becausetheycontainedallofthenecessaryfunctionalityfortheprogramthatwasn'thandledbythekernel.Intheprogramswewrotein Chapter6 ,weusedbothourmainprogramleandlescontainingroutinesusedbymultipleprograms.Inthesecases,wecombinedallofthecodetogetherusingthelinkeratlink-time,soitwasstillstatically-linked.However,inthehelloworld-libprogram,westartedusingsharedlibraries.Whenyouusesharedlibraries,yourprogramisthendynamically-linked,whichmeansthatnotallofthecodeneededtoruntheprogramisactuallycontainedwithintheprogramleitself,butinexternallibraries.Whenweputthe-lconthecommandtolinkthehelloworldprogram,ittoldthelinkertousetheclibrarylibc.sotolookupanysymbolsthatweren'talreadydenedinhelloworld.o.However,itdoesn'tactuallyaddanycodeto 133

PAGE 140

Chapter8.SharingFunctionswithCodeLibraries ourprogram,itjustnotesintheprogramwheretolook.Whenthehelloworldprogrambegins,thele/lib/ld-linux.so.2isloadedrst.Thisisthedynamiclinker.Thislooksatourhelloworldprogramandseesthatitneedstheclibrarytorun.So,itsearchesforalecalledlibc.sointhestandardplaceslistedin/etc/ld.so.confandinthecontentsoftheLD_LIBRARY_PATHenvironmentvariable,thenlooksinitforalltheneededsymbolsprintfandexitinthiscase,andthenloadsthelibraryintotheprogram'svirtualmemory.Finally,itreplacesallinstancesofprintfintheprogramwiththeactuallocationofprintfinthelibrary.Runthefollowingcommand:ldd./helloworld-nolibItshouldreportbacknotadynamicexecutable.Thisisjustlikewesaid-helloworld-nolibisastatically-linkedexecutable.However,trythis:ldd./helloworld-libItwillreportbacksomethinglikelibc.so.6=>/lib/libc.so.6x4001d000/lib/ld-linux.so.2=>/lib/ld-linux.so.2x400000000Thenumbersinparenthesismaybedifferentonyoursystem.Thismeansthattheprogramhelloworldislinkedtolibc.so.6the.6istheversionnumber,whichisfoundat/lib/libc.so.6,and/lib/ld-linux.so.2isfoundat/lib/ld-linux.so.2.Theselibrarieshavetobeloadedbeforetheprogramcanberun.Ifyouareinterested,runthelddprogramonvariousprogramsthatareonyourLinuxdistribution,andseewhatlibrariestheyrelyon.FindingInformationaboutLibrariesOkay,sonowthatyouknowaboutlibraries,thequestionis,howdoyoundout 134

PAGE 141

Chapter8.SharingFunctionswithCodeLibraries whatlibrariesyouhaveonyoursystemandwhattheydo?Well,let'sskipthatquestionforaminuteandaskanotherquestion:Howdoprogrammersdescribefunctionstoeachotherintheirdocumentation?Let'stakealookatthefunctionprintf.It'scallinginterfaceusuallyreferredtoasaprototypelookslikethis:intprintfchar*string,...;InLinux,functionsaredescribedintheCprogramminglanguage.Infact,mostLinuxprogramsarewritteninC.ThatiswhymostdocumentationandbinarycompatibilityisdenedusingtheClanguage.TheinterfacetotheprintffunctionaboveisdescribedusingtheCprogramminglanguage.Thisdenitionmeansthatthereisafunctionprintf.Thethingsinsidetheparenthesisarethefunction'sparametersorarguments.Therstparameterhereischar*string.Thismeansthereisaparameternamedstringthenameisn'timportant,excepttousefortalkingaboutit,whichhasatypechar*.charmeansthatitwantsasingle-bytecharacter.The*afteritmeansthatitdoesn'tactuallywantacharacterasanargument,butinsteaditwantstheaddressofacharacterorsequenceofcharacters.Ifyoulookbackatourhelloworldprogram,youwillnoticethatthefunctioncalllookedlikethis:pushl$hellocallprintfSo,wepushedtheaddressofthehellostring,ratherthantheactualcharacters.Youmightnoticethatwedidn'tpushthelengthofthestring.Thewaythatprintffoundtheendofthestringwasbecauseweendeditwithanullcharacter.Manyfunctionsworkthatway,especiallyClanguagefunctions.Theintbeforethefunctiondenitiontellwhattypeofvaluethefunctionwillreturnin%eaxwhenitreturns.printfwillreturnanintwhenit'sthrough.Now,afterthechar*string,wehaveaseriesofperiods,....Thismeansthatitcantakeanindenitenumberofadditionalargumentsafterthestring.Mostfunctionscanonlytakeaspeciednumberofarguments.printf,however,cantakemany.Itwilllookintothestringparameter,andeverywhereitseesthecharacters%s,itwill 135

PAGE 142

Chapter8.SharingFunctionswithCodeLibraries lookforanotherstringfromthestacktoinsert,andeverywhereitsees%ditwilllookforanumberfromthestacktoinsert.Thisisbestdescribedusinganexample:#PURPOSE:Thisprogramistodemonstratehowtocallprintf#.section.data#Thisstringiscalledtheformatstring.It'sthefirst#parameter,andprintfusesittofindouthowmanyparameters#itwasgiven,andwhatkindtheyare.firststring:.ascii"Hello!%sisa%swholovesthenumber%dn"name:.ascii"Jonathan"personstring:.ascii"person"#Thiscouldalsohavebeenan.equ,butwedecidedtogiveit#arealmemorylocationjustforkicksnumberloved:.long3.section.text.globl_start_start:#notethattheparametersarepassedinthe#reverseorderthattheyarelistedinthe#function'sprototype.pushlnumberloved#Thisisthe%dpushl$personstring#Thisisthesecond%spushl$name#Thisisthefirst%spushl$firststring#Thisistheformatstring#intheprototypecallprintf 136

PAGE 143

Chapter8.SharingFunctionswithCodeLibraries pushl$0callexitTypeitinwiththelenameprintf-example.s,andthendothefollowingcommands:asprintf-example.s-oprintf-example.oldprintf-example.o-oprintf-example-lc-dynamic-linker/lib/ld-linux.so.2Thenruntheprogramwith./printf-example,anditshouldsaythis:Hello!Jonathanisapersonwholovesthenumber3Now,ifyoulookatthecode,you'llseethatweactuallypushtheformatstringlast,eventhoughit'stherstparameterlisted.Youalwayspushafunctionsparametersinreverseorder.1Youmaybewonderinghowtheprintffunctionknowshowmanyparametersthereare.Well,itsearchesthroughyourstring,andcountshowmany%dsand%ssitnds,andthengrabsthatnumberofparametersfromthestack.Iftheparametermatchesa%d,ittreatsitasanumber,andifitmatchesa%s,ittreatsitasapointertoanull-terminatedstring.printfhasmanymorefeaturesthanthis,butthesearethemost-usedones.So,asyoucansee,printfcanmakeoutputaloteasier,butitalsohasalotofoverhead,becauseithastocountthenumberofcharactersinthestring,lookthroughitforallofthecontrolcharactersitneedstoreplace,pullthemoffthestack,convertthemtoa 1. Thereasonthatparametersarepushedinthereverseorderisbecauseoffunctionswhichtakeavariablenumberofparameterslikeprintf.Theparameterspushedinlastwillbeinaknownpositionrelativetothetopofthestack.Theprogramcanthenusetheseparameterstodeterminewhereonthestacktheadditionalargumentsare,andwhattypetheyare.Forexample,printfusestheformatstringtodeterminehowmanyotherparametersarebeingsent.Ifwepushedtheknownargumentsrst,youwouldn'tbeabletotellwheretheywereonthestack. 137

PAGE 144

Chapter8.SharingFunctionswithCodeLibraries suitablerepresentationnumbershavetobeconvertedtostrings,etc,andstickthemalltogetherappropriately.We'veseenhowtousetheCprogramminglanguageprototypestocalllibraryfunctions.Tousethemeffectively,however,youneedtoknowseveralmoreofthepossibledatatypesforreadingfunctions.Herearethemainones:int Anintisanintegernumberbytesonx86processor.long Alongisalsoanintegernumberbytesonanx86processor.longlong Alonglongisanintegernumberthat'slargerthanalongbytesonanx86processor.short Ashortisanintegernumberthat'sshorterthananintbytesonanx86processor.char Acharisasingle-byteintegernumber.Thisismostlyusedforstoringcharacterdata,sinceASCIIstringsusuallyarerepresentedwithonebytepercharacter.float Afloatisaoating-pointnumberbytesonanx86processor.Floating-pointnumberswillbeexplainedinmoredepthin theSectioncalledFloating-pointNumbersinChapter10 138

PAGE 145

Chapter8.SharingFunctionswithCodeLibraries double Adoubleisaoating-pointnumberthatislargerthanaoatbytesonanx86processor.unsigned unsignedisamodierusedforanyoftheabovetypeswhichkeepsthemfrombeingusedassignedquantities.Thedifferencebetweensignedandunsignednumberswillbediscussedin Chapter10 .* Anasterisk*isusedtodenotethatthedataisn'tanactualvalue,butinsteadisapointertoalocationholdingthegivenvaluebytesonanx86processor.So,let'ssayinmemorylocationmy_locationyouhavethenumber20stored.Iftheprototypesaidtopassanint,youwouldusedirectaddressingmodeanddopushlmy_location.However,iftheprototypesaidtopassanint*,youwoulddopushl$my_location-animmediatemodepushoftheaddressthatthevalueresidesin.Inadditiontoindicatingtheaddressofasinglevalue,pointerscanalsobeusedtopassasequenceofconsecutivelocations,startingwiththeonepointedtobythegivenvalue.Thisiscalledanarray.struct Astructisasetofdataitemsthathavebeenputtogetherunderaname.Forexampleyoucoulddeclare:structteststruct{inta;char*b;};andanytimeyouranintostructteststructyouwouldknowthatitisactuallytwowordsrightnexttoeachother,therstbeinganinteger,andthesecondapointertoacharacterorgroupofcharacters.Youneverseestructs 139

PAGE 146

Chapter8.SharingFunctionswithCodeLibraries passedasargumentstofunctions.Instead,youusuallyseepointerstostructspassedasarguments.Thisisbecausepassingstructstofunctionsisfairlycomplicated,sincetheycantakeupsomanystoragelocations.typedef Atypedefbasicallyallowsyoutorenameatype.Forexample,Icandotypedefintmyowntype;inaCprogram,andanytimeItypedmyowntype,itwouldbejustasifItypedint.Thiscangetkindofannoying,becauseyouhavetolookupwhatallofthetypedefsandstructsinafunctionprototypereallymean.However,typedefsareusefulforgivingtypesmoremeaningfulanddescriptivenames.CompatibilityNote:Thelistedsizesareforintel-compatiblex86machines.Othermachineswillhavedifferentsizes.Also,evenwhenparametersshorterthanawordarepassedtofunctions,theyarepassedaslongsonthestack.That'showtoreadfunctiondocumentation.Now,let'sgetbacktothequestionofhowtondoutaboutlibraries.Mostofyoursystemlibrariesarein/usr/libor/lib.Ifyouwanttojustseewhatsymbolstheydene,justrunobjdump-RFILENAMEwhereFILENAMEisthefullpathtothelibrary.Theoutputofthatisn'ttoohelpful,though,forndinganinterfacethatyoumightneed.Usually,youhavetoknowwhatlibraryyouwantatthebeginning,andthenjustreadthedocumentation.Mostlibrarieshavemanualsormanpagesfortheirfunctions.Thewebisthebestsourceofdocumentationforlibraries.MostlibrariesfromtheGNUprojectalsohaveinfopagesonthem,whicharealittlemorethoroughthanmanpages. 140

PAGE 147

Chapter8.SharingFunctionswithCodeLibraries UsefulFunctionsSeveralusefulfunctionsyouwillwanttobeawareoffromtheclibraryinclude: size_tstrlenconstchar*scalculatesthesizeofnull-terminatedstrings. intstrcmpconstchar*s1,constchar*s2comparestwostringsalphabetically. char*strdupconstchar*stakesthepointertoastring,andcreatesanewcopyinanewlocation,andreturnsthenewlocation. FILE*fopenconstchar*filename,constchar*opentypeopensamanaged,bufferedleallowseasierreadingandwritingthanusingledescriptorsdirectly.23 intfcloseFILE*streamclosesaleopenedwithfopen. char*fgetschar*s,intcount,FILE*streamfetchesalineofcharactersintostrings. intfputsconstchar*s,FILE*streamwritesastringtothegivenopenle. intfprintfFILE*stream,constchar*template,...isjustlikeprintf,butitusesanopenleratherthandefaultingtousingstandardoutput.Youcanndthecompletemanualonthislibrarybygoingtohttp://www.gnu.org/software/libc/manual/ 2. stdin,stdout,andstderralllowercasecanbeusedintheseprogramstorefertothelesoftheircorrespondingledescriptors. 3. FILEisastruct.Youdon'tneedtoknowit'scontentstouseit.Youonlyhavetostorethepointerandpassittotherelevantotherfunctions. 141

PAGE 148

Chapter8.SharingFunctionswithCodeLibraries BuildingaSharedLibraryLet'ssaythatwewantedtotakeallofoursharedcodefrom Chapter6 andbuilditintoasharedlibrarytouseinourprograms.Therstthingwewoulddoisassemblethemlikenormal:aswrite-record.s-owrite-record.oasread-record.s-oread-record.oNow,insteadoflinkingthemintoaprogram,wewanttolinkthemintoasharedlibrary.Thischangesourlinkercommandtothis:ld-sharedwrite-record.oread-record.o-olibrecord.soThislinksbothoftheselestogetherintoasharedlibrarycalledlibrecord.so.Thislecannowbeusedformultipleprograms.Ifweneedtoupdatethefunctionscontainedwithinit,wecanjustupdatethisoneleandnothavetoworryaboutwhichprogramsuseit.Let'slookathowwewouldlinkagainstthislibrary.Tolinkthewrite-recordsprogram,wewoulddothefollowing:aswrite-records.s-owrite-recordsld-L.-dynamic-linker/lib/ld-linux.so.2-owrite-records-lrecordwrite-records.oInthiscommand,-L.toldthelinkertolookforlibrariesinthecurrentdirectoryitusuallyonlysearches/libdirectory,/usr/libdirectory,andafewothers.Aswe'veseen,theoption-dynamic-linker/lib/ld-linux.so.2speciedthedynamiclinker.Theoption-lrecordtellsthelinkertosearchforfunctionsinthelenamedlibrecord.so.Nowthewrite-recordsprogramisbuilt,butitwillnotrun.Ifwetryit,wewillgetanerrorlikethefollowing:./write-records:errorwhileloadingsharedlibraries: 142

PAGE 149

Chapter8.SharingFunctionswithCodeLibraries librecord.so:cannotopensharedobjectfile:NosuchfileordirectoryThisisbecause,bydefault,thedynamiclinkeronlysearches/lib,/usr/lib,andwhateverdirectoriesarelistedin/etc/ld.so.confforlibraries.Inordertoruntheprogram,youeitherneedtomovethelibrarytooneofthesedirectories,orexecutethefollowingcommand:LD_LIBRARY_PATH=.exportLD_LIBRARY_PATHAlternatively,ifthatgivesyouanerror,dothisinstead:setenvLD_LIBRARY_PATH.Now,youcanrunwrite-recordsnormallybytyping./write-records.SettingLD_LIBRARY_PATHtellsthelinkertoaddwhateverpathsyougiveittothelibrarysearchpathfordynamiclibrariesForfurtherinformationaboutdynamiclinking,seethefollowingsourcesontheInternet: Themanpageforld.socontainsalotofinformationabouthowtheLinuxdynamiclinkerworks. http://www.benyossef.com/presentations/dlink/isagreatpresentationondynamiclinkinginLinux. http://www.linuxjournal.com/article.php?sid=1059andhttp://www.linuxjournal.com/article.php?sid=1060provideagoodintroductiontotheELFleformat,withmoredetailavailableathttp://www.cs.ucdavis.edu/~haungs/paper/node10.html http://www.iecc.com/linker/linker10.htmlcontainsagreatdescriptionofhowdynamiclinkingworkswithELFles. 143

PAGE 150

Chapter8.SharingFunctionswithCodeLibraries ReviewKnowtheConcepts Whataretheadvantagesanddisadvantagesofsharedlibraries? Givenalibrarynamed'foo',whatwouldthelibrary'slenamebe? Whatdoesthelddcommanddo? Let'ssaywehadthelesfoo.oandbar.o,andyouwantedtolinkthemtogether,anddynamicallylinkthemtothelibrary'kramer'.Whatwouldthelinkingcommandbetogeneratethenalexecutable? Whatistypedeffor? Whatarestructsfor? Whatisthedifferencebetweenadataelementoftypeintandint*?Howwouldyouaccessthemdifferentlyinyourprogram? Ifyouhadaobjectlecalledfoo.o,whatwouldbethecommandtocreateasharedlibrarycalled'bar'? WhatisthepurposeofLD_LIBRARY_PATH?UsetheConcepts Rewriteoneormoreoftheprogramsfromthepreviouschapterstoprinttheirresultstothescreenusingprintfratherthanreturningtheresultastheexitstatuscode.Also,maketheexitstatuscodebe0. Usethefactorialfunctionyoudevelopedin theSectioncalledRecursiveFunctionsinChapter4 tomakeasharedlibrary.Thenre-writethemainprogramsothatitlinkswiththelibrarydynamically. 144

PAGE 151

Chapter8.SharingFunctionswithCodeLibraries Rewritetheprogramabovesothatitalsolinkswiththe'c'library.Usethe'c'library'sprintffunctiontodisplaytheresultofthefactorialcall. Rewritethetoupperprogramsothatitusestheclibraryfunctionsforlesratherthansystemcalls.GoingFurther MakealistofalltheenvironmentvariablesusedbytheGNU/Linuxdynamiclinker. Researchthedifferenttypesofexecutableleformatsinusetodayandinthehistoryofcomputing.Tellthestrengthsandweaknessesofeach. Whatkindsofprogrammingareyouinterestedingraphics,databbases,science,etc.?Findalibraryforworkinginthatarea,andwriteaprogramthatmakessomebasicuseofthatlibrary. ResearchtheuseofLD_PRELOAD.Whatisitusedfor?Trybuildingasharedlibrarythatcontainedtheexitfunction,andhaveitwriteamessagetoSTDERRbeforeexitting.UseLD_PRELOADandrunvariousprogramswithit.Whataretheresults? 145

PAGE 152

Chapter8.SharingFunctionswithCodeLibraries 146

PAGE 153

Chapter9.IntermediateMemoryTopicsHowaComputerViewsMemoryLet'sreviewhowmemorywithinacomputerworks.Youmayalsowanttore-read Chapter2 .Acomputerlooksatmemoryasalongsequenceofnumberedstoragelocations.Asequenceofmillionsofnumberedstoragelocations.Everythingisstoredintheselocations.Yourprogramsarestoredthere,yourdataisstoredthere,everything.Eachstoragelocationlookslikeeveryotherone.Thelocationsholdingyourprogramarejustliketheonesholdingyourdata.Infact,thecomputerhasnoideawhicharewhich,exceptthattheexecutableletellsitwheretostartexecuting.Thesestoragelocationsarecalledbytes.Thecomputercancombineuptofourofthemtogetherintoasingleword.Normallynumericdataisoperatedonawordatatime.Aswementioned,instructionsarealsostoredinthissamememory.Eachinstructionisadifferentlength.Mostinstructionstakeuponeortwostoragelocationsfortheinstructionitself,andthenstoragelocationsfortheinstruction'sarguments.Forexample,theinstructionmovldata_items,%edi,4,%ebxtakesup7storagelocations.Thersttwoholdtheinstruction,thethirdonetellswhichregisterstouse,andthenextfourholdthestoragelocationofdata_items.Inmemory,instructionslookjustlikealltheothernumbers,andtheinstructionsthemselvescanbemovedintoandoutofregistersjustlikenumbers,becausethat'swhattheyare.Thischapterisfocusedonthedetailsofcomputermemory.Togetstartedlet'sreviewsomebasictermsthatwewillbeusinginthischapter: 147

PAGE 154

Chapter9.IntermediateMemoryTopics Byte Thisisthesizeofastoragelocation.Onx86processors,abytecanholdnumbersbetween0and255.Word Thisisthesizeofanormalregister.Onx86processors,awordisfourbyteslong.Mostcomputeroperationshandleawordatatime.Address Anaddressisanumberthatreferstoabyteinmemory.Forexample,therstbyteonacomputerhasanaddressof0,thesecondhasanaddressof1,andsoon.1Everypieceofdataonthecomputernotinaregisterhasanaddress.Theaddressofdatawhichspansseveralbytesisthesameastheaddressofitsrstbyte.Normally,wedon'tevertypethenumericaddressofanything,butwelettheassemblerdoitforus.Whenweuselabelsincode,thesymbolusedinthelabelwillbeequivalenttotheaddressitislabelling.Theassemblerwillthenreplacethatsymbolwithitsaddresswhereveryouuseitinyourprogram.Forexample,sayyouhavethefollowingcode:.section.datamy_data:.long2,3,4Now,anytimeintheprogramthatmy_dataisused,itwillbereplacedbytheaddressoftherstvalueofthe.longdirective.Pointer Apointerisaregisterormemorywordwhosevalueisanaddress.Inourprogramsweuse%ebpasapointertothecurrentstackframe.Allbase 1. Youactuallyneveruseaddressesthislow,butitworksfordiscussion. 148

PAGE 155

Chapter9.IntermediateMemoryTopics pointeraddressinginvolvespointers.Programmingusesalotofpointers,soit'sanimportantconcepttograsp.TheMemoryLayoutofaLinuxProgramWhenyouprogramisloadedintomemory,each.sectionisloadedintoitsownregionofmemory.Allofthecodeanddatadeclaredineachsectionisbroughttogether,eveniftheywereseparatedinyoursourcecode.Theactualinstructionsthe.textsectionareloadedattheaddress0x08048000numbersstartingwith0xareinhexadecimal,whichwillbediscussedin Chapter10 .The.datasectionisloadedimmediatelyafterthat,followedbythe.bsssection.ThelastbytethatcanbeaddressedonLinuxislocation0xbfffffff.Linuxstartsthestackhereandgrowsitdownwardtowardtheothersections.Betweenthemisahugegap.Theinitiallayoutofthestackisasfollows:Atthebottomofthestackthebottomofthestackisthetopaddressofmemory-see Chapter4 ,thereisawordofmemorythatiszero.Afterthatcomesthenull-terminatednameoftheprogramusingASCIIcharacters.Aftertheprogramnamecomestheprogram'senvironmentvariablesthesearenotimportanttousinthisbook.Thencometheprogram'scommand-linearguments.Thesearethevaluesthattheusertypedinonthecommandlinetorunthisprogram.Whenwerunas,forexample,wegiveitseveralarguments-as,sourcefile.s,-o,andobjectfile.o.Afterthese,wehavethenumberofargumentsthatwereused.Whentheprogrambegins,thisiswherethestackpointer,%esp,ispointing.Furtherpushesonthestackmove%espdowninmemory.Forexample,theinstructionpushl%eaxisequivalenttomovl%eax,%esp 149

PAGE 156

Chapter9.IntermediateMemoryTopics subl$4,%espLikewise,theinstructionpopl%eaxisthesameasmovl%esp,%eaxaddl$4,%espYourprogram'sdataregionstartsatthebottomofmemoryandgoesup.Thestackstartsatthetopofmemory,andmovesdownwardwitheachpush.Thismiddlepartbetweenthestackandyourprogram'sdatasectionsisinaccessiblememory-youarenotallowedtoaccessituntilyoutellthekernelthatyouneedit.2Ifyoutry,youwillgetanerrortheerrormessageisusually"segmentationfault".Thesamewillhappenifyoutrytoaccessdatabeforethebeginningofyourprogram,0x08048000.Thelastaccessiblememoryaddresstoyourprogramiscalledthesystembreakalsocalledthecurrentbreakorjustthebreak. 2. Thestackcanaccessitasitgrowsdownward,andyoucanaccessthestackregionsthrough%esp.However,yourprogram'sdatasectiondoesn'tgrowthatway.Thewaytogrowthatwillbeexplainedshortly. 150

PAGE 157

Chapter9.IntermediateMemoryTopics MemoryLayoutofaLinuxProgramatStartupEveryMemoryAddressisaLieSo,whydoesthecomputernotallowyoutoaccessmemoryinthebreakarea?Toanswerthisquestion,wewillhavetodelveintothedepthsofhowyourcomputerreallyhandlesmemory.Youmayhavewondered,sinceeveryprogramgetsloadedintothesameplacein 151

PAGE 158

Chapter9.IntermediateMemoryTopics memory,don'ttheysteponeachother,oroverwriteeachother?Itwouldseemso.However,asaprogramwriter,youonlyaccessvirtualmemory.PhysicalmemoryreferstotheactualRAMchipsinsideyourcomputerandwhattheycontain.It'susuallybetween16and512Megabytesonmoderncomputers.Ifwetalkaboutaphysicalmemoryaddress,wearetalkingaboutwhereexactlyonthesechipsapieceofmemoryislocated.Virtualmemoryisthewayyourprogramthinksaboutmemory.Beforeloadingyourprogram,Linuxndsanemptyphysicalmemoryspacelargeenoughtotyourprogram,andthentellstheprocessortopretendthatthismemoryisactuallyattheaddress0x0804800toloadyourprograminto.Confusedyet?Letmeexplainfurther.Eachprogramgetsitsownsandboxtoplayin.Everyprogramrunningonyourcomputerthinksthatitwasloadedatmemoryaddress0x0804800,andthatit'sstackstartsat0xbffffff.WhenLinuxloadsaprogram,itndsasectionofunusedmemory,andthentellstheprocessortousethatsectionofmemoryastheaddress0x0804800forthisprogram.Theaddressthataprogrambelievesitusesiscalledthevirtualaddress,whiletheactualaddressonthechipsthatitreferstoiscalledthephysicaladdress.Theprocessofassigningvirtualaddressestophysicaladdressesiscalledmapping.Earlierwetalkedabouttheinaccessiblememorybetweenthe.bssandthestack,butwedidn'ttalkaboutwhyitwasthere.Thereasonisthatthisregionofvirtualmemoryaddresseshasn'tbeenmappedontophysicalmemoryaddresses.Themappingprocesstakesupconsiderabletimeandspace,soifeverypossiblevirtualaddressofeverypossibleprogramweremapped,youwouldnothaveenoughphysicalmemorytoevenrunoneprogram.So,thebreakisthebeginningoftheareathatcontainsunmappedmemory.Withthestack,however,Linuxwillautomaticallymapinmemorythatisaccessedfromstackpushes.Ofcourse,thisisaverysimpliedviewofvirtualmemory.Thefullconceptismuchmoreadvanced.Forexample,Virtualmemorycanbemappedtomorethanjustphysicalmemory;itcanbemappedtodiskaswell.SwappartitionsonLinuxallowLinux'svirtualmemorysystemtomapmemorynotonlytophysicalRAM, 152

PAGE 159

Chapter9.IntermediateMemoryTopics butalsotodiskblocksaswell.Forexample,let'ssayyouonlyhave16Megabytesofphysicalmemory.Let'salsosaythat8MegabytesarebeingusedbyLinuxandsomebasicapplications,andyouwanttorunaprogramthatrequires20Megabytesofmemory.Canyou?Theanswerisyes,butonlyifyouhavesetupaswappartition.Whathappensisthatafterallofyourremaining8Megabytesofphysicalmemoryhavebeenmappedintovirtualmemory,Linuxstartsmappingpartsofyourapplication'svirtualmemorytodiskblocks.So,ifyouaccessa"memory"locationinyourprogram,thatlocationmaynotactuallybeinmemoryatall,butondisk.Astheprogrammeryouwon'tknowthedifference,though,becauseitisallhandledbehindthescenesbyLinux.Now,x86processorscannotruninstructionsdirectlyfromdisk,norcantheyaccessdatadirectlyfromdisk.Thisrequiresthehelpoftheoperatingsystem.Whenyoutrytoaccessmemorythatismappedtodisk,theprocessornoticesthatitcan'tserviceyourmemoryrequestdirectly.ItthenasksLinuxtostepin.Linuxnoticesthatthememoryisactuallyondisk.Therefore,itmovessomedatathatiscurrentlyinmemoryontodisktomakeroom,andthenmovesthememorybeingaccessedfromthediskbackintophysicalmemory.Itthenadjuststheprocessor'svirtual-to-physicalmemorylookuptablessothatitcanndthememoryinthenewlocation.Finally,Linuxreturnscontroltotheprogramandrestartsitattheinstructionwhichwastryingtoaccessthedataintherstplace.Thisinstructioncannowbecompletedsuccessfully,becausethememoryisnowinphysicalRAM.3HereisanoverviewofthewaymemoryaccessesarehandledunderLinux: Theprogramtriestoloadmemoryfromavirtualaddress. Theprocessor,usingtablessuppliedbyLinux,transformsthevirtualmemoryaddressintoaphysicalmemoryaddressonthey. 3. NotethatnotonlycanLinuxhaveavirtualaddressmaptoadifferentphysicaladdress,itcanalsomovethosemappingsaroundasneeded. 153

PAGE 160

Chapter9.IntermediateMemoryTopics Iftheprocessordoesnothaveaphysicaladdresslistedforthememoryaddress,itsendsarequesttoLinuxtoloadit. Linuxlooksattheaddress.Ifitismappedtoadisklocation,itcontinuesontothenextstep.Otherwise,itterminatestheprogramwithasegmentationfaulterror. Ifthereisnotenoughroomtoloadthememoryfromdisk,Linuxwillmoveanotherpartoftheprogramoranotherprogramontodisktomakeroom. Linuxthenmovesthedataintoafreephysicalmemoryaddress. Linuxupdatestheprocessor'svirtual-to-physicalmemorymappingtablestoreectthechanges. Linuxrestorescontroltotheprogram,causingittore-issuetheinstructionwhichcausedthisprocesstohappen. Theprocessorcannowhandletheinstructionusingthenewly-loadedmemoryandtranslationtables.It'salotofworkfortheoperatingsystem,butitgivestheuserandtheprogrammergreatexibilitywhenitcomestomemorymanagement.Now,inordertomaketheprocessmoreefcient,memoryisseparatedoutintogroupscalledpages.WhenrunningLinuxonx86processors,apageis4096bytesofmemory.Allofthememorymappingsaredoneapageatatime.Physicalmemoryassignment,swapping,mapping,etc.arealldonetomemorypagesinsteadofindividualmemoryaddresses.Whatthismeanstoyouasaprogrammeristhatwheneveryouareprogramming,youshouldtrytokeepmostmemoryaccesseswithinthesamebasicrangeofmemory,soyouwillonlyneedapageortwoofmemoryatatime.Otherwise,Linuxmayhavetokeepmovingpagesonandoffofdisktosatisfyyourmemoryneeds.Diskaccessisslow,sothiscanreallyslowdownyourprogram.Sometimessomanyprogramscanbeloadedthatthereishardlyenoughphysicalmemoryforthem.Theywindupspendingmoretimejustswappingmemoryon 154

PAGE 161

Chapter9.IntermediateMemoryTopics andoffofdiskthantheydoactuallyprocessingit.Thisleadstoaconditioncalledswapdeathwhichleadstoyoursystembeingunresponsiveandunproductive.It'susuallyusuallyrecoverableifyoustartterminatingyourmemory-hungryprograms,butit'sapain.ResidentSetSize:Theamountofmemorythatyourprogramcurrentlyhasinphysicalmemoryiscalledit'sresidentsetsize,andcanbeviewedbyusingtheprogramtop.Theresidentsetsizeislistedunderthecolumnlabelled"RSS".GettingMoreMemoryWenowknowthatLinuxmapsallofourvirtualmemoryintophysicalmemoryorswap.Ifyoutrytoaccessapieceofvirtualmemorythathasn'tbeenmappedyet,ittriggersanerrorknownasasegmentationfault,whichwillterminateyourprogram.Theprogrambreakpoint,ifyouremember,isthelastvalidaddressyoucanuse.Now,thisisallgreatifyouknowbeforehandhowmuchstorageyouwillneed.Youcanjustaddallthememoryyouneedtoyour.dataor.bsssections,anditwillallbethere.However,let'ssayyoudon'tknowhowmuchmemoryyouwillneed.Forexample,withatexteditor,youdon'tknowhowlongtheperson'slewillbe.Youcouldtrytondamaximumlesize,andjusttelltheuserthattheycan'tgobeyondthat,butthat'sawasteiftheleissmall.ThereforeLinuxhasafacilitytomovethebreakpointtoaccomodateanapplication'smemoryneeds.Ifyouneedmorememory,youcanjusttellLinuxwhereyouwantthenewbreakpointtobe,andLinuxwillmapallthememoryyouneedbetweenthecurrentandnewbreakpoint,andthenmovethebreakpointtothespotyouspecify.Thatmemoryisnowavailableforyourprogramtouse.ThewaywetellLinuxtomovethebreakpointisthroughthebrksystemcall.Thebrksystemcalliscallnumber 155

PAGE 162

Chapter9.IntermediateMemoryTopics 45whichwillbein%eax.%ebxshouldbeloadedwiththerequestedbreakpoint.Thenyoucallint$0x80tosignalLinuxtodoitswork.Aftermappinginyourmemory,Linuxwillreturnthenewbreakpointin%eax.Thenewbreakpointmightactuallybelargerthanwhatyouaskedfor,becauseLinuxroundsuptothenearestpage.Ifthereisnotenoughphysicalmemoryorswaptofulllyourrequest,Linuxwillreturnazeroin%eax.Also,ifyoucallbrkwithazeroin%ebx,itwillsimplyreturnthelastusablememoryaddress.Theproblemwiththismethodiskeepingtrackofthememorywerequest.Let'ssayIneedtomovethebreaktohaveroomtoloadale,andthenneedtomoveabreakagaintoloadanotherle.Let'ssayIthengetridoftherstle.Younowhaveagiantgapinmemorythat'smapped,butthatyouaren'tusing.Ifyoucontinuetomovethebreakinthiswayforeachleyouload,youcaneasilyrunoutofmemory.So,whatisneededisamemorymanager.Amemorymanagerisasetofroutinesthattakescareofthedirtyworkofgettingyourprogrammemoryforyou.Mostmemorymanagershavetwobasicfunctions-allocateanddeallocate.4Wheneveryouneedacertainamountofmemory,youcansimplytellallocatehowmuchyouneed,anditwillgiveyoubackanaddresstothememory.Whenyou'redonewithit,youtelldeallocatethatyouarethroughwithit.allocatewillthenbeabletoreusethememory.Thispatternofmemorymanagementiscalleddynamicmemoryallocation.Thisminimizesthenumberof"holes"inyourmemory,makingsurethatyouaremakingthebestuseofityoucan.Thepoolofmemoryusedbymemorymanagersiscommonlyreferredtoastheheap.Thewaymemorymanagersworkisthattheykeeptrackofwherethesystembreakis,andwherethememorythatyouhaveallocatedis.Theymarkeachblockofmemoryintheheapasbeingusedorunused.Whenyourequestmemory,thememorymanagercheckstoseeifthereareanyunusedblocksoftheappropriatesize.Ifnot,itcallsthebrksystemcalltorequestmorememory.Whenyoufree 4. Thefunctionnamesusuallyaren'tallocateanddeallocate,butthefunctionalitywillbethesame.IntheCprogramminglanguage,forexample,theyarenamedmallocandfree. 156

PAGE 163

Chapter9.IntermediateMemoryTopics memoryitmarkstheblockasunusedsothatfuturerequestscanretrieveit.Inthenextsectionwewilllookatbuildingourownmemorymanager.ASimpleMemoryManagerHereIwillshowyouasimplememorymanager.Itisveryprimitivebutitshowstheprinciplesquitewell.Asusual,Iwillgiveyoutheprogramrstforyoutolookthrough.Afterwardswillfollowanin-depthexplanation.Itlookslong,butitismostlycomments.#PURPOSE:Programtomanagememoryusage-allocates#anddeallocatesmemoryasrequested##NOTES:Theprogramsusingtheseroutineswillask#foracertainsizeofmemory.Weactually#usemorethanthatsize,butweputit#atthebeginning,beforethepointer#wehandback.Weaddasizefieldand#anAVAILABLE/UNAVAILABLEmarker.So,the#memorylookslikethis#############################################################AvailableMarker#Sizeofmemory#Actualmemorylocations############################################################^--Returnedpointer#pointshere#Thepointerwereturnonlypointstotheactual#locationsrequestedtomakeiteasierforthe#callingprogram.Italsoallowsustochangeour#structurewithoutthecallingprogramhavingto#changeatall..section.data 157

PAGE 164

Chapter9.IntermediateMemoryTopics #######GLOBALVARIABLES#########Thispointstothebeginningofthememorywearemanagingheap_begin:.long0#Thispointstoonelocationpastthememorywearemanagingcurrent_break:.long0######STRUCTUREINFORMATION#####sizeofspaceformemoryregionheader.equHEADER_SIZE,8#Locationofthe"available"flagintheheader.equHDR_AVAIL_OFFSET,0#Locationofthesizefieldintheheader.equHDR_SIZE_OFFSET,4###########CONSTANTS###########.equUNAVAILABLE,0#Thisisthenumberwewillusetomark#spacethathasbeengivenout.equAVAILABLE,1#Thisisthenumberwewillusetomark#spacethathasbeenreturned,andis#availableforgiving.equSYS_BRK,45#systemcallnumberforthebreak#systemcall.equLINUX_SYSCALL,0x80#makesystemcallseasiertoread.section.text 158

PAGE 165

Chapter9.IntermediateMemoryTopics ##########FUNCTIONS##############allocate_init###PURPOSE:callthisfunctiontoinitializethe#functionsspecifically,thissetsheap_beginand#current_break.Thishasnoparametersandno#returnvalue..globlallocate_init.typeallocate_init,@functionallocate_init:pushl%ebp#standardfunctionstuffmovl%esp,%ebp#Ifthebrksystemcalliscalledwith0in%ebx,it#returnsthelastvalidusableaddressmovl$SYS_BRK,%eax#findoutwherethebreakismovl$0,%ebxint$LINUX_SYSCALLincl%eax#%eaxnowhasthelastvalid#address,andwewantthe#memorylocationafterthatmovl%eax,current_break#storethecurrentbreakmovl%eax,heap_begin#storethecurrentbreakasour#firstaddress.Thiswillcause#theallocatefunctiontoget#morememoryfromLinuxthe#firsttimeitisrunmovl%ebp,%esp#exitthefunctionpopl%ebp 159

PAGE 166

Chapter9.IntermediateMemoryTopics ret#####ENDOFFUNCTION#########allocate###PURPOSE:Thisfunctionisusedtograbasectionof#memory.Itcheckstoseeifthereareany#freeblocks,and,ifnot,itasksLinux#foranewone.##PARAMETERS:Thisfunctionhasoneparameter-thesize#ofthememoryblockwewanttoallocate##RETURNVALUE:#Thisfunctionreturnstheaddressofthe#allocatedmemoryin%eax.Ifthereisno#memoryavailable,itwillreturn0in%eax#######PROCESSING#########Variablesused:##%ecx-holdthesizeoftherequestedmemory#first/onlyparameter#%eax-currentmemoryregionbeingexamined#%ebx-currentbreakposition#%edx-sizeofcurrentmemoryregion##Wescanthrougheachmemoryregionstartingwith#heap_begin.Welookatthesizeofeachone,andif#ithasbeenallocated.Ifit'sbigenoughforthe#requestedsize,anditsavailable,itgrabsthatone.#Ifitdoesnotfindaregionlargeenough,itasks#Linuxformorememory.Inthatcase,itmoves#current_breakup 160

PAGE 167

Chapter9.IntermediateMemoryTopics .globlallocate.typeallocate,@function.equST_MEM_SIZE,8#stackpositionofthememorysize#toallocateallocate:pushl%ebp#standardfunctionstuffmovl%esp,%ebpmovlST_MEM_SIZE%ebp,%ecx#%ecxwillholdthesize#wearelookingforwhichisthefirst#andonlyparametermovlheap_begin,%eax#%eaxwillholdthecurrent#searchlocationmovlcurrent_break,%ebx#%ebxwillholdthecurrent#breakalloc_loop_begin:#hereweiteratethrougheach#memoryregioncmpl%ebx,%eax#needmorememoryiftheseareequaljemove_break#grabthesizeofthismemorymovlHDR_SIZE_OFFSET%eax,%edx#Ifthespaceisunavailable,gotothecmpl$UNAVAILABLE,HDR_AVAIL_OFFSET%eaxjenext_location#nextonecmpl%edx,%ecx#Ifthespaceisavailable,comparejleallocate_here#thesizetotheneededsize.Ifits#bigenough,gotoallocate_here 161

PAGE 168

Chapter9.IntermediateMemoryTopics next_location:addl$HEADER_SIZE,%eax#Thetotalsizeofthememoryaddl%edx,%eax#regionisthesumofthesize#requestedcurrentlystored#in%edx,plusanother8bytes#fortheheaderforthe#AVAILABLE/UNAVAILABLEflag,#and4forthesizeofthe#region.So,adding%edxand$8#to%eaxwillgettheaddress#ofthenextmemoryregionjmpalloc_loop_begin#golookatthenextlocationallocate_here:#ifwe'vemadeithere,#thatmeansthatthe#regionheaderoftheregion#toallocateisin%eax#markspaceasunavailablemovl$UNAVAILABLE,HDR_AVAIL_OFFSET%eaxaddl$HEADER_SIZE,%eax#move%eaxpasttheheaderto#theusablememorysince#that'swhatwereturnmovl%ebp,%esp#returnfromthefunctionpopl%ebpretmove_break:#ifwe'vemadeithere,that#meansthatwehaveexhausted#alladdressablememory,and#weneedtoaskformore.#%ebxholdsthecurrent 162

PAGE 169

Chapter9.IntermediateMemoryTopics #endpointofthedata,#and%ecxholdsitssize#weneedtoincrease%ebxto#wherewe_want_memory#toend,soweaddl$HEADER_SIZE,%ebx#addspacefortheheaders#structureaddl%ecx,%ebx#addspacetothebreakfor#thedatarequested#nowitstimetoaskLinux#formorememorypushl%eax#saveneededregisterspushl%ecxpushl%ebxmovl$SYS_BRK,%eax#resetthebreak%ebxhas#therequestedbreakpointint$LINUX_SYSCALL#undernormalconditions,thisshould#returnthenewbreakin%eax,which#willbeeither0ifitfails,or#itwillbeequaltoorlargerthan#weaskedfor.Wedon'tcare#inthisprogramwhereitactually#setsthebreak,soaslongas%eax#isn't0,wedon'tcarewhatitiscmpl$0,%eax#checkforerrorconditionsjeerrorpopl%ebx#restoresavedregisterspopl%ecx 163

PAGE 170

Chapter9.IntermediateMemoryTopics popl%eax#setthismemoryasunavailable,sincewe'reaboutto#giveitawaymovl$UNAVAILABLE,HDR_AVAIL_OFFSET%eax#setthesizeofthememorymovl%ecx,HDR_SIZE_OFFSET%eax#move%eaxtotheactualstartofusablememory.#%eaxnowholdsthereturnvalueaddl$HEADER_SIZE,%eaxmovl%ebx,current_break#savethenewbreakmovl%ebp,%esp#returnthefunctionpopl%ebpreterror:movl$0,%eax#onerror,wereturnzeromovl%ebp,%esppopl%ebpret########ENDOFFUNCTION##########deallocate###PURPOSE:#Thepurposeofthisfunctionistogiveback#aregionofmemorytothepoolafterwe'redone#usingit.##PARAMETERS:#Theonlyparameteristheaddressofthememory#wewanttoreturntothememorypool. 164

PAGE 171

Chapter9.IntermediateMemoryTopics ##RETURNVALUE:#Thereisnoreturnvalue##PROCESSING:#Ifyouremember,weactuallyhandtheprogramthe#startofthememorythattheycanuse,whichis#8storagelocationsaftertheactualstartofthe#memoryregion.Allwehavetodoisgoback#8locationsandmarkthatmemoryasavailable,#sothattheallocatefunctionknowsitcanuseit..globldeallocate.typedeallocate,@function#stackpositionofthememoryregiontofree.equST_MEMORY_SEG,4deallocate:#sincethefunctionissosimple,we#don'tneedanyofthefancyfunctionstuff#gettheaddressofthememorytofree#normallythisis8%ebp,butsince#wedidn'tpush%ebpormove%espto#%ebp,wecanjustdo4%espmovlST_MEMORY_SEG%esp,%eax#getthepointertotherealbeginningofthememorysubl$HEADER_SIZE,%eax#markitasavailablemovl$AVAILABLE,HDR_AVAIL_OFFSET%eax#returnret########ENDOFFUNCTION########## 165

PAGE 172

Chapter9.IntermediateMemoryTopics Therstthingtonoticeisthatthereisno_startsymbol.Thereasonisthatthisisjustasetoffunctions.Amemorymanagerbyitselfisnotafullprogram-itdoesn'tdoanything.Itissimplyautilitytobeusedbyotherprograms.Toassembletheprogram,dothefollowing:asalloc.s-oalloc.oOkay,nowlet'slookatthecode.VariablesandConstantsAtthebeginningoftheprogram,wehavetwolocationssetup:heap_begin:.long0current_break:.long0Remember,thesectionofmemorybeingmanagediscommonlyreferredtoastheheap.Whenweassembletheprogram,wehavenoideawherethebeginningoftheheapis,norwherethecurrentbreakis.Therefore,wereservespacefortheiraddresses,butjustllthemwitha0forthetimebeing.Nextwehaveasetofconstantstodenethestructureoftheheap.Thewaythismemorymanagerworksisthatbeforeeachregionofmemoryallocated,wewillhaveashortrecorddescribingthememory.Thisrecordhasawordreservedfortheavailableagandawordfortheregion'ssize.Theactualmemoryallocatedimmediatelyfollowsthisrecord.Theavailableagisusedtomarkwhetherthisregionisavailableforallocations,orifitiscurrentlyinuse.Thesizeeldletsusknowbothwhetherornotthisregionisbigenoughforanallocationrequest,as 166

PAGE 173

Chapter9.IntermediateMemoryTopics wellasthelocationofthenextmemoryregion.Thefollowingconstantsdescribethisrecord:.equHEADER_SIZE,8.equHDR_AVAIL_OFFSET,0.equHDR_SIZE_OFFSET,4Thissaysthattheheaderis8bytestotal,theavailableagisoffset0bytesfromthebeginning,andthesizeeldisoffset4bytesfromthebeginning.Ifwearecarefultoalwaysusetheseconstants,thenweprotectourselvesfromhavingtodotoomuchworkifwelaterdecidetoaddmoreinformationtotheheader.Thevaluesthatwewilluseforouravailableeldareeither0forunavailable,or1foravailable.Tomakethiseasiertoread,wehavethefollowingdenitions:.equUNAVAILABLE,0.equAVAILABLE,1Finally,wehaveourLinuxsystemcalldenitions:.equBRK,45.equLINUX_SYSCALL,0x80Theallocate_initfunctionOkay,thisisasimplefunction.Allitdoesissetuptheheap_beginandcurrent_breakvariableswediscussedearlier.So,ifyourememberthediscussionearlier,thecurrentbreakcanbefoundusingthebrksystemcall.So,thefunctionstartslikethis:pushl%ebpmovl%esp,%ebpmovl$SYS_BRK,%eax 167

PAGE 174

Chapter9.IntermediateMemoryTopics movl$0,%ebxint$LINUX_SYSCALLAnyway,afterint$LINUX_SYSCALL,%eaxholdsthelastvalidaddress.Weactuallywanttherstinvalidaddressinsteadofthelastvalidaddress,sowejustincrement%eax.Thenwemovethatvaluetotheheap_beginandcurrent_breaklocations.Thenweleavethefunction.Thecodelookslikethis:incl%eaxmovl%eax,current_breakmovl%eax,heap_beginmovl%ebp,%esppopl%ebpretTheheapconsistsofthememorybetweenheap_beginandcurrent_break,sothissaysthatwestartoffwithaheapofzerobytes.Ourallocatefunctionwillthenextendtheheapasmuchasitneedstowhenitiscalled.TheallocatefunctionThisisthedoozyfunction.Let'sstartbylookingatanoutlineofthefunction: 1. Startatthebeginningoftheheap. 2. Checktoseeifwe'reattheendoftheheap. 3. Ifweareattheendoftheheap,grabthememoryweneedfromLinux,markitas"unavailable"andreturnit.IfLinuxwon'tgiveusanymore,returna0. 4. Ifthecurrentmemoryregionismarked"unavailable",gotothenextone,andgobacktostep2. 5. Ifthecurrentmemoryregionistoosmalltoholdtherequestedamountofspace,gobacktostep2. 168

PAGE 175

Chapter9.IntermediateMemoryTopics 6. Ifthememoryregionisavailableandlargeenough,markitas"unavailable"andreturnit.Now,lookbackthroughthecodewiththisinmind.Besuretoreadthecommentssoyou'llknowwhichregisterholdswhichvalue.Nowthatyou'velookedbackthroughthecode,let'sexamineitonelineatatime.Westartofflikethis:pushl%ebpmovl%esp,%ebpmovlST_MEM_SIZE%ebp,%ecxmovlheap_begin,%eaxmovlcurrent_break,%ebxThispartinitializesallofourregisters.Thersttwolinesarestandardfunctionstuff.Thenextmovepullsthesizeofthememorytoallocateoffofthestack.Thisisouronlyfunctionparameter.Afterthat,itmovesthebeginningheapaddressandtheendoftheheapintoregisters.Iamnowreadytodoprocessing.Thenextsectionismarkedalloc_loop_begin.Inthisloopwearegoingtoexaminememoryregionsuntilweeitherndanopenmemoryregionordeterminethatweneedmorememory.Ourrstinstructionschecktoseeifweneedmorememory:cmpl%ebx,%eaxjemove_break%eaxholdsthecurrentmemoryregionbeingexaminedand%ebxholdsthelocationpasttheendoftheheap.Thereforeifthenextregiontobeexaminedispasttheendoftheheap,itmeansweneedmorememorytoallocatearegionofthissize.Let'sskipdowntomove_breakandseewhathappensthere:move_break:addl$HEADER_SIZE,%ebxaddl%ecx,%ebx 169

PAGE 176

Chapter9.IntermediateMemoryTopics pushl%eaxpushl%ecxpushl%ebxmovl$SYS_BRK,%eaxint$LINUX_SYSCALLWhenwereachthispointinthecode,%ebxholdswherewewantthenextregionofmemorytobe.So,weaddourheadersizeandregionsizeto%ebx,andthat'swherewewantthesystembreaktobe.Wethenpushalltheregisterswewanttosaveonthestack,andcallthebrksystemcall.Afterthatwecheckforerrors:cmpl$0,%eaxjeerrorIftherewerenoerrorswepoptheregistersbackoffthestack,markthememoryasunavailable,recordthesizeofthememory,andmakesure%eaxpointstothestartofusablememorywhichisaftertheheader.popl%ebxpopl%ecxpopl%eaxmovl$UNAVAILABLE,HDR_AVAIL_OFFSET%eaxmovl%ecx,HDR_SIZE_OFFSET%eaxaddl$HEADER_SIZE,%eaxThenwestorethenewprogrambreakandreturnthepointertotheallocatedmemory.movl%ebx,current_breakmovl%ebp,%esppopl%ebpretTheerrorcodejustreturns0in%eax,sowewon'tdiscussit. 170

PAGE 177

Chapter9.IntermediateMemoryTopics Let'sgobacklookattherestoftheloop.Whathappensifthecurrentmemorybeinglookedatisn'tpasttheendoftheheap?Well,let'slook.movlHDR_SIZE_OFFSET%eax,%edxcmpl$UNAVAILABLE,HDR_AVAIL_OFFSET%eaxjenext_locationThisrstgrabsthesizeofthememoryregionandputsitin%edx.ThenitlooksattheavailableagtoseeifitissettoUNAVAILABLE.Ifso,thatmeansthatmemoryregionisinuse,sowe'llhavetoskipoverit.So,iftheavailableagissettoUNAVAILABLE,yougotothecodelabelednext_location.IftheavailableagissettoAVAILABLE,thenwekeepongoing.Let'ssaythatthespacewasavailable,andsowekeepgoing.Thenwechecktoseeifthisspaceisbigenoughtoholdtherequestedamountofmemory.Thesizeofthisregionisbeingheldin%edx,sowedothis:cmpl%edx,%ecxjleallocate_hereIftherequestedsizeislessthanorequaltothecurrentregion'ssize,wecanusethisblock.Itdoesn'tmatterifthecurrentregionislargerthanrequested,becausetheextraspacewilljustbeunused.So,let'sjumpdowntoallocate_hereandseewhathappens:movl$UNAVAILABLE,HDR_AVAIL_OFFSET%eaxaddl$HEADER_SIZE,%eaxmovl%ebp,%esppopl%ebpretItmarksthememoryasbeingunavailable.Thenitmovesthepointer%eaxpasttheheader,andusesitasthereturnvalueforthefunction.Remember,thepersonusingthisfunctiondoesn'tneedtoevenknowaboutourmemoryheaderrecord.Theyjustneedapointertousablememory. 171

PAGE 178

Chapter9.IntermediateMemoryTopics Okay,solet'ssaytheregionwasn'tbigenough.Whatthen?Well,wewouldthenbeatthecodelabelednext_location.Thissectionofcodeisusedanytimethatwegureoutthatthecurrentmemoryregionwon'tworkforallocatingmemory.Allitdoesisadvance%eaxtothenextpossiblememoryregion,andgoesbacktothebeginningoftheloop.Rememberthat%edxisholdingthesizeofthecurrentmemoryregion,andHEADER_SIZEisthesymbolforthesizeofthememoryregion'sheader.Sothiscodewillmoveustothenextmemoryregion:addl$HEADER_SIZE,%eaxaddl%edx,%eaxjmpalloc_loop_beginAndnowthefunctionrunsanotherloop.Wheneveryouhavealoop,youmustmakesurethatitwillalwaysend.Thebestwaytodothatistoexamineallofthepossibilities,andmakesurethatallofthemeventuallyleadtotheloopending.Inourcase,wehavethefollowingpossibilities: Wewillreachtheendoftheheap Wewillndamemoryregionthat'savailableandlargeenough WewillgotothenextlocationThersttwoitemsareconditionsthatwillcausethelooptoend.Thethirdonewillkeepitgoing.However,evenifweneverndanopenregion,wewilleventuallyreachtheendoftheheap,becauseitisanitesize.Therefore,weknowthatnomatterwhichconditionistrue,theloophastoeventuallyhitaterminatingcondition.ThedeallocatefunctionThedeallocatefunctionismucheasierthantheallocateone.That'sbecauseitdoesn'thavetodoanysearchingatall.Itcanjustmarkthecurrentmemory 172

PAGE 179

Chapter9.IntermediateMemoryTopics regionasAVAILABLE,andallocatewillnditnexttimeitiscalled.Sowehave:movlST_MEMORY_SEG%esp,%eaxsubl$HEADER_SIZE,%eaxmovl$AVAILABLE,HDR_AVAIL_OFFSET%eaxretInthisfunction,wedon'thavetosave%ebpor%espsincewe'renotchangingthem,nordowehavetorestorethemattheend.Allwe'redoingisreadingtheaddressofthememoryregionfromthestack,backinguptothebeginningoftheheader,andmarkingtheregionasavailable.Thisfunctionhasnoreturnvalue,sowedon'tcarewhatweleavein%eax.PerformanceIssuesandOtherProblemsOursimplisticmemorymanagerisnotreallyusefulforanythingmorethananacademicexercise.Thissectionlooksattheproblemswithsuchasimplisticallocator.Thebiggestproblemhereisspeed.Now,ifthereareonlyafewallocationsmade,thenspeedwon'tbeabigissue.Butthinkaboutwhathappensifyoumakeathousandallocations.Onallocationnumber1000,youhavetosearchthrough999memoryregionstondthatyouhavetorequestmorememory.Asyoucansee,that'sgettingprettyslow.Inaddition,rememberthatLinuxcankeeppagesofmemoryondiskinsteadofinmemory.So,sinceyouhavetogothrougheverypieceofmemoryyourprogram'smemory,thatmeansthatLinuxhastoloadeverypartofmemorythat'scurrentlyondisktochecktoseeifitsavailable.Youcanseehowthiscouldgetreally,reallyslow.5Thismethodissaidtoruninlineartime,whichmeansthateveryelementyouhavetomanagemakesyourprogramtake 5. Thisiswhyaddingmorememorytoyourcomputermakesitrunfaster.Themoremem-oryyourcomputerhas,thelessitputsondisk,soitdoesn'thavetoalwaysbeinterruptingyourprogramstoretreivepagesoffthedisk. 173

PAGE 180

Chapter9.IntermediateMemoryTopics longer.Aprogramthatrunsinconstanttimetakesthesameamountoftimenomatterhowmanyelementsyouaremanaging.Takethedeallocatefunction,forinstance.Itonlyruns4instructions,nomatterhowmanyelementswearemanaging,orwheretheyareinmemory.Infact,althoughourallocatefunctionisoneoftheslowestofallmemorymanagers,thedeallocatefunctionisoneofthefastest.Anotherperformanceproblemisthenumberoftimeswe'recallingthebrksystemcall.Systemcallstakealongtime.Theyaren'tlikefunctions,becausetheprocessorhastoswitchmodes.Yourprogramisn'tallowedtomapitselfmemory,buttheLinuxkernelis.So,theprocessorhastoswitchintokernelmode,thenLinuxmapsthememory,andthenswitchesbacktousermodeforyourapplicationtocontinuerunning.Thisisalsocalledacontextswitch.Contextswitchesarerelativelyslowonx86processors.Generally,youshouldavoidcallingthekernelunlessyoureallyneedto.Anotherproblemthatwehaveisthatwearen'trecordingwhereLinuxactuallysetsthebreak.PreviouslywementionedthatLinuxmightactuallysetthebreakpastwherewerequestedit.Inthisprogram,wedon'tevenlookatwhereLinuxactuallysetsthebreak-wejustassumeitsetsitwherewerequested.That'snotreallyabug,butitwillleadtounnecessarybrksystemcallswhenwealreadyhavethememorymappedin.Anotherproblemwehaveisthatifwearelookingfora5-byteregionofmemory,andtherstopenonewecometois1000bytes,wewillsimplymarkthewholethingasallocatedandreturnit.Thisleaves995bytesofunused,butallocated,memory.Itwouldbeniceinsuchsituationstobreakitapartsotheother995bytescanbeusedlater.Itwouldalsobenicetocombineconsecutivefreespaceswhenlookingforlargeallocations. 174

PAGE 181

Chapter9.IntermediateMemoryTopics UsingourAllocatorTheprogramswedointhisbookaren'tcomplicatedenoughtonecessitateamemorymanager.Therefore,wewilljustuseourmemorymanagertoallocateabufferforoneofourlereading/writingprogramsinsteadofassigningitinthe.bss.Theprogramwewilldemonstratethisonisread-records.sfrom Chapter6 .Thisprogramusesabuffernamedrecord_buffertohandleitsinput/outputneeds.Wewillsimplychangethisfrombeingabufferdenedin.bsstobeingapointertoadynamically-allocatedbufferusingourmemorymanager.Youwillneedtohavethecodefromthatprogramhandyaswewillonlybediscussingthechangesinthissection.Therstchangeweneedtomakeisinthedeclaration.Currentlyitlookslikethis:.section.bss.lcomm,record_buffer,RECORD_SIZEItwouldbeamisnomertokeepthesamename,sinceweareswitchingitfrombeinganactualbuffertobeingapointertoabuffer.Inaddition,itnowonlyneedstobeonewordbigenoughtoholdapointer.Thenewdeclarationwillstayinthe.datasectionandlooklikethis:record_buffer_ptr:.long0Ournextchangeisweneedtoinitializeourmemorymanagerimmediatelyafterwestartourprogram.Therefore,rightafterthestackissetup,thefollowingcallneedstobeadded:callallocate_initAfterthat,thememorymanagerisreadytostartservicingmemoryallocationrequests.Weneedtoallocateenoughmemorytoholdtheserecordsthatweare 175

PAGE 182

Chapter9.IntermediateMemoryTopics reading.Therefore,wewillcallallocatetoallocatethismemory,andthensavethepointeritreturnsintorecord_buffer_ptr.Likethis:pushl$RECORD_SIZEcallallocatemovl%eax,record_buffer_ptrNow,whenwemakethecalltoread_record,itisexpectingapointer.Intheoldcode,thepointerwastheimmediate-modereferencetorecord_buffer.Now,record_buffer_ptrjustholdsthepointerratherthanthebufferitself.Therefore,wemustdoadirectmodeloadtogetthevalueinrecord_buffer_ptr.Weneedtoremovethisline:pushl$record_bufferAndputthislineinitsplace:pushlrecord_buffer_ptrThenextchangecomeswhenwearetryingtondtheaddressoftherstnameeldofourrecord.Intheoldcode,itwas$RECORD_FIRSTNAME+record_buffer.However,thatonlyworksbecauseitisaconstantoffsetfromaconstantaddress.Inthenewcode,itistheoffsetofanaddressstoredinrecord_buffer_ptr.Togetthatvalue,wewillneedtomovethepointerintoaregister,andthenadd$RECORD_FIRSTNAMEtoittogetthepointer.Sowherewehavethefollowingcode:pushl$RECORD_FIRSTNAME+record_bufferWeneedtoreplaceitwiththis:movlrecord_buffer_ptr,%eaxaddl$RECORD_FIRSTNAME,%eaxpushl%eax 176

PAGE 183

Chapter9.IntermediateMemoryTopics Similarly,weneedtochangethelinethatsaysmovl$RECORD_FIRSTNAME+record_buffer,%ecxsothatitreadslikethis:movlrecord_buffer_ptr,%ecxaddl$RECORD_FIRSTNAME,%ecxFinally,onechangethatweneedtomakeistodeallocatethememoryoncewearedonewithitinthisprogramit'snotnecessary,butit'sagoodpracticeanyway.Todothat,wejustsendrecord_buffer_ptrtothedeallocatefunctionrightbeforeexitting:pushlrecord_buffer_ptrcalldeallocateNowyoucanbuildyourprogramwiththefollowingcommands:asread-records.s-oread-records.oldalloc.oread-record.oread-records.owrite-newline.ocount-chars.o-oread-recordsYoucanthenrunyourprogrambydoing./read-records.Theusesofdynamicmemoryallocationmaynotbeapparenttoyouatthispoint,butasyougofromacademicexercisestoreal-lifeprogramsyouwilluseitcontinually.MoreInformationMoreinformationonmemoryhandlinginLinuxandotheroperatingsystemscanbefoundatthefollowinglocations: 177

PAGE 184

Chapter9.IntermediateMemoryTopics MoreinformationaboutthememorylayoutofLinuxprogramscanbefoundinKonstantinBoldyshev'sdocument,"StartupstateofaLinux/i386ELFbinary",availableathttp://linuxassembly.org/startup.html Agoodoverviewofvirtualmemoryinmanydifferentsystemsisavailableathttp://cne.gmu.edu/modules/vm/ Severalin-deptharticlesonLinux'svirtualmemorysubsystemisavailableathttp://www.nongnu.org/lkdp/les.html DougLeahaswrittenupadescriptionofhispopularmemoryallocatorathttp://gee.cs.oswego.edu/dl/html/malloc.html Apaperonthe4.4BSDmemoryallocatorisavailableathttp://docs.freebsd.org/44doc/papers/malloc.htmlReviewKnowtheConcepts DescribethelayoutofmemorywhenaLinuxprogramstarts. Whatistheheap? Whatisthecurrentbreak? Whichdirectiondoesthestackgrowin? Whichdirectiondoestheheapgrowin? Whathappenswhenyouaccessunmappedmemory? Howdoestheoperatingsystempreventprocessesfromwritingovereachother'smemory? Describetheprocessthatoccursifapieceofmemoryyouareusingiscurrentlyresidingondisk? 178

PAGE 185

Chapter9.IntermediateMemoryTopics Whydoyouneedanallocator?UsetheConcepts Modifythememorymanagersothatitcallsallocate_initautomaticallyifithasn'tbeeninitialized. Modifythememorymanagersothatiftherequestedsizeofmemoryissmallerthantheregionchosen,itwillbreakuptheregionintomultipleparts.Besuretotakeintoaccountthesizeofthenewheaderrecordwhenyoudothis. Modifyoneofyourprogramsthatusesbufferstousethememorymanagertogetbuffermemoryratherthanusingthe.bss.GoingFurther Researchgarbagecollection.Whatadvantagesanddisadvantagesdoesthishaveoverthestyleofmemorymanagementusedhere? Researchreferencecounting.Whatadvantagesanddisadvantagesdoesthishaveoverthestyleofmemorymanagementusedhere? Changethenameofthefunctionstomallocandfree,andbuildthemintoasharedlibrary.UseLD_PRELOADtoforcethemtobeusedasyourmemorymanagerinsteadofthedefaultone.AddsomewritesystemcallstoSTDOUTtoverifythatyourmemorymanagerisbeingusedinsteadofthedefaultone. 179

PAGE 186

Chapter9.IntermediateMemoryTopics 180

PAGE 187

Chapter10.CountingLikeaComputerCountingCountingLikeaHumanInmanyways,computerscountjustlikehumans.So,beforewestartlearninghowcomputerscount,let'stakeadeeperlookathowwecount.Howmanyngersdoyouhave?No,it'snotatrickquestion.Humansnormallyhavetenngers.Whyisthatsignicant?Lookatournumberingsystem.Atwhatpointdoesaone-digitnumberbecomeatwo-digitnumber?That'sright,atten.Humanscountanddomathusingabasetennumberingsystem.Basetenmeansthatwegroupeverythingintens.Let'ssaywe'recountingsheep.1,2,3,4,5,6,7,8,9,10.Whydidweallofasuddennowhavetwodigits,andre-usethe1?That'sbecausewe'regroupingournumbersbyten,andwehave1groupoftensheep.Okay,let'sgotothenextnumber11.Thatmeanswehave1groupoftensheep,and1sheepleftungrouped.Sowecontinue-12,13,14,15,16,17,18,19,20.Nowwehave2groupsoften.21-2groupsoften,and1sheepungrouped.22-2groupsoften,and2sheepungrouped.So,let'ssaywekeepcounting,andgetto97,98,99,and100.Look,ithappenedagain!Whathappensat100?Wenowhavetengroupsoften.At101wehavetengroupsoften,and1ungroupedsheep.Sowecanlookatanynumberlikethis.Ifwecounted60879sheep,thatwouldmeanthatwehad6groupsoftengroupsoftengroupsoftengroupsoften,0groupsoftengroupsoftengroupsoften,8groupsoftengroupsoften,7groupsoften,and9sheepleftungrouped.So,isthereanythingsignicantaboutgroupingthingsbyten?No!It'sjustthatgroupingbytenishowwe'vealwaysdoneit,becausewehavetenngers.Wecouldhavegroupedatnineorateleveninwhichcasewewouldhavehadtomakeupanewsymbol.Theonlydifferencebetweenthedifferentgroupingsofnumbersisthatwehavetore-learnourmultiplication,addition,subtraction,and 181

PAGE 188

Chapter10.CountingLikeaComputer divisiontablesforeachgrouping.Theruleshaven'tchanged,justthewaywerepresentthem.Also,someofourtricksthatwelearneddon'talwaysapply,either.Forexample,let'ssaywegroupedbynineinsteadoften.Movingthedecimalpointonedigittotherightnolongermultipliesbyten,itnowmultipliesbynine.Inbasenine,500isonlyninetimesaslargeas50.CountingLikeaComputerThequestionis,howmanyngersdoesthecomputerhavetocountwith?Thecomputeronlyhastwongers.Sothatmeansallofthegroupsaregroupsoftwo.So,let'scountinbinary-0zero,1one,10two-onegroupoftwo,11three-onegroupoftwoandoneleftover,100four-twogroupsoftwo,101ve-twogroupsoftwoandoneleftover,110six-twogroupsoftwoandonegroupoftwo,andsoon.Inbasetwo,movingthedecimalonedigittotherightmultipliesbytwo,andmovingittotheleftdividesbytwo.Basetwoisalsoreferredtoasbinary.Thenicethingaboutbasetwoisthatthebasicmathtablesareveryshort.Inbaseten,themultiplicationtablesaretencolumnswide,andtencolumnstall.Inbasetwo,itisverysimple:Tableofbinaryaddition+|0|1--+-----+-----0|0|0--+-----+-----1|1|10Tableofbinarymultiplication 182

PAGE 189

Chapter10.CountingLikeaComputer *|0|1--+-----+-----0|0|0--+-----+-----1|0|1So,let'saddthenumbers10010101with1100101:10010101+1100101-----------11111010Now,let'smultiplythem:10010101*1100101-----------10010101000000001001010100000000000000001001010110010101---------------11101011001001 183

PAGE 190

Chapter10.CountingLikeaComputer ConversionsBetweenBinaryandDecimalLet'slearnhowtoconvertnumbersfrombinarybasetwotodecimalbaseten.Thisisactuallyarathersimpleprocess.Ifyouremember,eachdigitstandsforsomegroupingoftwo.So,wejustneedtoaddupwhateachdigitrepresents,andwewillhaveadecimalnumber.Takethebinarynumber10010101.Tondoutwhatitisindecimal,wetakeitapartlikethis:10010101|||||||||||||||Individualunits^0||||||0groupsof2^1|||||1groupof4^2||||0groupsof8^3|||1groupof16^4||0groupsof32^5|0groupsof64^61groupof128^7andthenweaddallofthepiecestogether,likethis:1*128+0*64+0*32+1*16+0*8+1*4+0*2+1*1=128+16+4+1=149So10010101inbinaryis149indecimal.Let'slookat1100101.Itcanbewrittenas1*64+1*32+0*16+0*8+1*4+0*2+1*1=64+32+4+1=101Soweseethat1100101inbinaryis101indecimal.Let'slookatonemorenumber,11101011001001.Youcanconvertittodecimalbydoing1*8192+1*4096+1*2048+0*1024+1*512+0*256 184

PAGE 191

Chapter10.CountingLikeaComputer +1*128+1*64+0*32+0*16+1*8+0*4+0*2+1*1=8192+4096+2048+512+128+64+8+1=15049Now,ifyou'vebeenpayingattention,youhavenoticedthatthenumberswejustconvertedarethesameonesweusedtomultiplywithearlier.So,let'scheckourresults:101*149=15049.Itworked!Nowlet'slookatgoingfromdecimalbacktobinary.Inordertodotheconversion,youhavetodividethenumberintogroupsoftwo.So,let'ssayyouhadthenumber17.Ifyoudivideitbytwo,youget8with1leftover.Sothatmeansthereare8groupsoftwo,and1ungrouped.Thatmeansthattherightmostdigitwillbe1.Now,wehavetherigtmostdigitguredout,and8groupsof2leftover.Now,let'sseehowmanygroupsoftwogroupsoftwowehave,bydividing8by2.Weget4,withnothingleftover.Thatmeansthatallgroupstwocanbefurtherdividedintomoregroupsoftwo.So,wehave0groupsofonlytwo.Sothenextdigittotheleftis0.So,wedivide4by2andgettwo,with0leftover,sothenextdigitis0.Then,wedivide2by2andget1,with0leftover.Sothenextdigitis0.Finally,wedivide1by2andget0with1leftover,sothenextdigittotheleftis1.Now,there'snothingleft,sowe'redone.So,thenumberwewoundupwithis10001.Previously,weconvertedtobinary11101011001001todecimal15049.Let'sdothereversetomakesurethatwediditright:15049/2=7524Remaining17524/2=3762Remaining03762/2=1881Remaining01881/2=940Remaining1940/2=470Remaining0470/2=235Remaining0235/2=117Remaining1117/2=58Remaining1 185

PAGE 192

Chapter10.CountingLikeaComputer 58/2=29Remaining029/2=14Remaining114/2=7Remaining07/2=3Remaining13/2=1Remaining11/2=0Remaining1Then,weputtheremainingnumbersbacktogether,andwehavetheoriginalnumber!Remembertherstdivisionremaindergoestothefarright,sofromthebottomupyouhave11101011001001.Eachdigitinabinarynumberiscalledabit,whichstandsforbinarydigit.Remember,computersdivideuptheirmemoryintostoragelocationscalledbytes.Eachstoragelocationonanx86processorandmostothersis8bitslong.Earlierwesaidthatabytecanholdanynumberbetween0and255.Thereasonforthisisthatthelargestnumberyoucantinto8bitsis255.Youcanseethisforyourselfifyouconvertbinary11111111intodecimal:11111111=*2^7+*2^6+*2^5+*2^4+*2^3+*2^2+*2^1+1*2^0=128+64+32+16+8+4+2+1=255Thelargestnumberthatyoucanholdin16bitsis65535.Thelargestnumberyoucanholdin32bitsis4294967295billion.Thelargestnumberyoucanholdin64bitsis18,446,744,073,709,551,615.Thelargestnumberyoucanholdin128bitsis340,282,366,920,938,463,463,374,607,431,768,211,456.Anyway,youseethepicture.Forx86processors,mostofthetimeyouwilldealwith4-bytenumbersbits,becausethat'sthesizeoftheregisters. 186

PAGE 193

Chapter10.CountingLikeaComputer Truth,Falsehood,andBinaryNumbersNowwe'veseenthatthecomputerstoreseverythingassequencesof1'sand0's.Let'slookatsomeotherusesofthis.Whatif,insteadoflookingatasequenceofbitsasanumber,weinsteadlookedatitasasetofswitches.Forexample,let'ssaytherearefourswitchesthatcontrollightinginthehouse.Wehaveaswitchforoutsidelights,aswitchforthehallwaylights,aswitchforthelivingroomlights,andaswitchforthebedroomlights.Wecouldmakealittletableshowingwhichofthesewereonandoff,likeso:OutsideHallwayLivingRoomBedroomOnOffOnOnIt'sobviousfromlookingatthisthatallofthelightsareonexceptthehallwayones.Now,insteadofusingthewords"On"and"Off",let'susethenumbers1and0.1willrepresenton,and0willrepresentoff.So,wecouldrepresentthesameinformationasOutsideHallwayLivingRoomBedroom1011Now,insteadofhavinglabelsonthelightswitches,let'ssaywejustmemorizedwhichpositionwentwithwhichswitch.Then,thesameinformationcouldberepresentedas1011oras1011Thisisjustoneofmanywaysyoucanusethecomputersstoragelocationstorepresentmorethanjustnumbers.Thecomputersmemoryjustseesnumbers,butprogrammerscanusethesenumberstorepresentanythingtheirimaginationscan 187

PAGE 194

Chapter10.CountingLikeaComputer comeupwith.Theyjustsometimeshavetobecreativewhenguringoutthebestrepresentation.Notonlycanyoudoregulararithmeticwithbinarynumbers,theyalsohaveafewoperationsoftheirown,calledbinaryorlogicaloperations.Thestandardbinaryoperationsare AND OR NOT XORBeforewelookatexamples,I'lldescribethemforyou.ANDtakestwobitsandreturnsonebit.ANDwillreturna1onlyifbothbitsare1,anda0otherwise.Forexample,1AND1is1,but1AND0is0,0AND1is0,and0AND0is0.ORtakestwobitsandreturnsonebit.Itwillreturn1ifeitheroftheoriginalbitsis1.Forexample,1OR1is1,1OR0isone,0OR1is1,but0OR0is0.NOTonlytakesonebit,andreturnsit'soppositeNOT1is0andNOT0is1.Finally,XORislikeOR,exceptitreturns0ifbothbitsare1.Computerscandotheseoperationsonwholeregistersatatime.Forexample,ifaregisterhas10100010101010010101101100101010andanotheronehas10001000010101010101010101111010,youcanrunanyoftheseoperationsonthewholeregisters.Forexample,ifweweretoANDthem,thecomputerwillrunfromtherstbittothe32ndandruntheANDoperationonthatbitinbothregisters.Inthiscase:10100010101010010101101100101010AND10001000010101010101010101111010--------------------------------10000000000000010101000100101010 188

PAGE 195

Chapter10.CountingLikeaComputer You'llseethattheresultingsetofbitsonlyhasaonewherebothnumbershadaone,andineveryotherpositionithasazero.Let'slookatwhatanORlookslike:10100010101010010101101100101010OR10001000010101010101010101111010--------------------------------10101010111111010101111101111010Inthiscase,theresultingnumberhasa1whereeithernumberhasa1inthegivenposition.Let'slookattheNOToperation:NOT10100010101010010101101100101010------------------------------------01011101010101101010010011010101Thisjustreverseseachdigit.Finally,wehaveXOR,whichislikeanOR,exceptifbothdigitsare1,itreturns0.10100010101010010101101100101010XOR10001000010101010101010101111010--------------------------------00101010111111000000111001010000ThisisthesametwonumbersusedintheORoperation,soyoucancomparehowtheywork.Also,ifyouXORanumberwithitself,youwillalwaysget0,likethis:10100010101010010101101100101010XOR10100010101010010101101100101010--------------------------------00000000000000000000000000000000Theseoperationsareusefulfortworeasons: Thecomputercandothemextremelyfast 189

PAGE 196

Chapter10.CountingLikeaComputer YoucanusethemtocomparemanytruthvaluesatthesametimeYoumaynothaveknownthatdifferentinstructionsexecuteatdifferentspeeds.It'strue,theydo.Andtheseoperationsarethefastestonmostprocessors.Forexample,yousawthatXORinganumberwithitselfproduces0.Well,theXORoperationisfasterthantheloadingoperation,somanyprogrammersuseittoloadaregisterwithzero.Forexample,thecodemovl$0,%eaxisoftenreplacedbyxorl%eax,%eaxWe'lldiscussspeedmorein Chapter12 ,butIwantyoutoseehowprogrammersoftendotrickythings,especiallywiththesebinaryoperators,tomakethingsfast.Nowlet'slookathowwecanusetheseoperatorstomanipulatetrue/falsevalues.Earlierwediscussedhowbinarynumberscanbeusedtorepresentanynumberofthings.Let'susebinarynumberstorepresentwhatthingsmyDadandIlike.First,let'slookatthethingsIlike:Food:yesHeavyMetalMusic:yesWearingDressyClothes:noFootball:yesNow,let'slookatwhatmyDadlikes:Food:yesHeavyMetalMusic:noWearingDressyClothes:yesFootball:yesNow,let'susea1tosayyeswelikesomething,anda0tosaynowedon't.Nowwehave: 190

PAGE 197

Chapter10.CountingLikeaComputer MeFood:1HeavyMetalMusic:1WearingDressyClothes:0Football:1DadFood:1HeavyMetalMusic:0WearingDressyClothes:1Football:1Now,ifwejustmemorizewhichpositioneachofthesearein,wehaveMe1101Dad1011Now,let'sseewewanttogetalistofthingsbothmyDadandIlike.YouwouldusetheANDoperation.So1101AND1011--------1001WhichtranslatestoThingswebothlikeFood:yesHeavyMetalMusic:noWearingDressyClothes:noFootball:yes 191

PAGE 198

Chapter10.CountingLikeaComputer Remember,thecomputerhasnoideawhattheonesandzeroesrepresent.That'syourjobandyourprogram'sjob.Ifyouwroteaprogramaroundthisrepresentationyourprogramwouldatsomepointexamineeachbitandhavecodetotelltheuserwhatit'sforifyouaskedacomputerwhattwopeopleagreedonanditanswered1001,itwouldn'tbeveryuseful.Anyway,let'ssaywewanttoknowthethingsthatwedisagreeon.ForthatwewoulduseXOR,becauseitwillreturn1onlyifoneortheotheris1,butnotboth.So1101XOR1011--------0110AndI'llletyoutranslatethatbackout.Thepreviousoperations:AND,OR,NOT,andXORarecalledbooleanoperatorsbecausetheywererststudiedbyGeorgeBoole.So,ifsomeonementionesbooleanoperatorsorbooleanalgebra,younowknowwhattheyaretalkingabout.Inadditiontothebooleanoperations,therearealsotwobinaryoperatorsthataren'tboolean,shiftandrotate.Shiftsandrotateseachdowhattheirnameimplies,andcandosototherightortheleft.Aleftshiftmoveseachdigitofabinarynumberonespacetotheleft,putsazerointheonesspot,andchopsoffthefurthestdigittotheleft.Aleftrotatedoesthesamething,buttakesthefurthestdigittotheleftandputsitintheonesspot.Forexample,Shiftleft10010111=00101110Rotateleft10010111=00101111Noticethatifyourotateanumberforeverydigitithasi.e.-rotatinga32-bitnumber32times,youwindupwiththesamenumberyoustartedwith.However,ifyoushiftanumberforeverydigityouhave,youwindupwith0.So,whataretheseshiftsusefulfor?Well,ifyouhavebinarynumbersrepresentingthings,youuseshiftstopeekateachindividualvalue.Let'ssay,forinstance,thatwehadmyDad'slikesstoredinaregisterbits.Itwouldlooklikethis: 192

PAGE 199

Chapter10.CountingLikeaComputer 00000000000000000000000000001011Now,aswesaidpreviously,thisdoesn'tworkasprogramoutput.So,inordertodooutput,wewouldneedtodoshiftingandmasking.Maskingistheprocessofeliminatingeverythingyoudon'twant.Inthiscase,foreveryvaluewearelookingfor,wewillshiftthenumbersothatvalueisintheonesplace,andthenmaskthatdigitsothatitisallwesee.MaskingisaccomplishedbydoinganANDwithanumberthathasthebitsweareinterestedinsetto1.Forexample,let'ssaywewantedtoprintoutwhethermyDadlikesdressyclothesornot.Thatdataisthesecondvaluefromtheright.So,wehavetoshiftthenumberright1digitsoitlookslikethis:00000000000000000000000000000101andthen,wejustwanttolookatthatdigit,sowemaskitbyANDingitwith00000000000000000000000000000001.00000000000000000000000000000101AND00000000000000000000000000000001-----------------------------------00000000000000000000000000000001Thiswillmakethevalueoftheregister1ifmyDadlikesdressyclothes,and0ifhedoesn't.Thenwecandoacomparisonto1andprinttheresults.Thecodewouldlooklikethis:#NOTE-assumethattheregister%ebxholds#myDad'spreferencesmovl%ebx,%eax#Thiscopiestheinformationinto%eaxso#wedon'tlosetheoriginaldatashrl$1,%eax#Thisistheshiftoperator.Itstands#forShiftRightLong.Thisfirstnumber#isthenumberofpositionstoshift, 193

PAGE 200

Chapter10.CountingLikeaComputer #andthesecondistheregistertoshift#Thisdoesthemaskingandl$0b00000000000000000000000000000001,%eax#Checktoseeiftheresultis1or0cmpl$0b00000000000000000000000000000001,%eaxjeyes_he_likes_dressy_clothesjmpno_he_doesnt_like_dressy_clothesAndthenwewouldhavetwolabelswhichprintedsomethingaboutwhetherornothelikesdressyclothesandthenexits.The0bnotationmeansthatwhatfollowsisabinarynumber.Inthiscaseitwasn'tneeded,because1isthesameinanynumberingsystem,butIputitthereforclarity.Wealsodidn'tneedthe31zeroes,butIputthemintomakeapointthatthenumberyouareusingis32bits.Whenanumberrepresentsasetofoptionsforafunctionorsystemcall,theindividualtrue/falseelementsarecalledags.Manysystemcallshavenumerousoptionsthatareallsetinthesameregisterusingamechanismlikewe'vedescribed.Theopensystemcall,forexample,hasasitssecondparameteralistofagstotelltheoperatingsystemhowtoopenthele.Someoftheagsinclude:O_WRONLY Thisagis0b00000000000000000000000000000001inbinary,or01inoctaloranynumbersystemforthatmatter.Thissaystoopentheleinwrite-onlymode.O_RDWR Thisagis0b00000000000000000000000000000010inbinary,or02inoctal.Thissaystoopentheleforbothreadingandwriting. 194

PAGE 201

Chapter10.CountingLikeaComputer O_CREAT Thisagis0b00000000000000000000000001000000inbinary,or0100inoctal.Itmeanstocreatetheleifitdoesn'talreadyexist.O_TRUNC Thisagis0b00000000000000000000001000000000inbinary,or01000inoctal.Itmeanstoerasethecontentsoftheleifthelealreadyexists.O_APPEND Thisagis0b00000000000000000000010000000000inbinary,or02000inoctal.Itmeanstostartwritingattheendoftheleratherthanatthebeginning.Tousetheseags,yousimplyORthemtogetherinthecombinationthatyouwant.Forexample,toopenaleinwrite-onlymode,andhaveitcreatetheleifitdoesn'texist,IwoulduseO_WRONLYandO_CREAT.OR'dtogether,Iwouldhave0101.Notethatifyoudon'tseteitherO_WRONLYorO_RDWR,thentheleisautomaticallyopenedinread-onlymodeO_RDONLY,exceptthatitisn'treallyaagsinceit'szero.Manyfunctionsandsystemcallsuseagsforoptions,asitallowsasinglewordtoholdupto32possibleoptionsifeachoptionisrepresentedbyasinglebit.TheProgramStatusRegisterWe'veseenhowbitsonaregistercanbeusedtogivetheanswersofyes/noandtrue/falsestatements.Onyourcomputer,thereisaregistercalledtheprogramstatusregister.Thisregisterholdsalotofinformationaboutwhathappensinacomputation.Forexample,haveyoueverwonderedwhatwouldhappenifyou 195

PAGE 202

Chapter10.CountingLikeaComputer addedtwonumbersandtheresultwaslargerthanwouldtinaregister?Theprogramstatusregisterhasaagcalledthecarryag.Youcantestittoseeifthelastcomputationoverowedtheregister.Thereareagsforanumberofdifferentstatuses.Infact,whenyoudoacomparecmplinstruction,theresultisstoredinthisregister.Theconditionaljumpinstructionsjge,jne,etcusetheseresultstotellwhetherornottheyshouldjump.jmp,theunconditionaljump,doesn'tcarewhatisinthestatusregister,sinceitisunconditional.Let'ssayyouneededtostoreanumberlargerthan32bits.So,let'ssaythenumberis2registerswide,or64bits.Howcouldyouhandlethis?Ifyouwantedtoaddtwo64bitnumbers,youwouldaddtheleastsignicantregistersrst.Then,ifyoudetectedancarry,youcouldadd1tothemostsignicantregister.Infact,thisisprobablythewayyoulearnedtododecimaladdition.Iftheresultinonecolumnismorethan9,yousimplycarriedthenumbertothenextmostsignicantcolumn.Ifyouadded65and37,rstyouadd7and4toget12.Youkeepthe2intherightcolumn,andcarrytheonetothenextcolumn.Thereyouadd6,3,andthe1youcarried.Thisresultsin10.So,youkeepthezerointhatcolumnandcarrytheonetothenextmostsignicantcolumn,whichisempty,soyoujustputtheonethere.Luckily,32bitsisusuallybigenoughtoholdthenumbersweuseregularly.Additionalprogramstatusregisteragsareexaminedin AppendixB .OtherNumberingSystemsWhatwehavestudiedsofaronlyappliestopositiveintegers.However,real-worldnumbersarenotalwayspositiveintegers.Negativenumbersandnumberswithdecimalsarealsoused.Floating-pointNumbersSofar,theonlynumberswe'vedealtwithareintegers-numberswithnodecimalpoint.Computershaveageneralproblemwithnumberswithdecimalpoints, 196

PAGE 203

Chapter10.CountingLikeaComputer becausecomputerscanonlystorexed-size,nitevalues.Decimalnumberscanbeanylength,includinginnitelengththinkofarepeatingdecimal,liketheresultof1/3.Thewayacomputerhandlesdecimalsisbystoringthemataxedprecisionnumberofsignicantbits.Acomputerstoresdecimalnumbersintwoparts-theexponentandthemantissa.Themantissacontainstheactualdigitsthatwillbeused,andtheexponentiswhatmagnitudethenumberis.Forexample,12345.2isstoredas1.23452*10^4.Themantissais1.23452andtheexponentis4.AllnumbersarestoredasX.XXXXX*10^XXXX.Thenumber1isstoredas1.00000*10^0.Now,themantissaandtheexponentareonlysolong,whichleadstosomeinterestingproblems.Forexample,whenacomputerstoresaninteger,ifyouadd1toit,theresultingnumberisonelarger.Thisdoesnotnecessarilyhappenwithoatingpointnumbers.Ifthenumberissufcientlybig,like5.234*10^5000,adding1toitmightnotevenregisterinthemantissaremember,bothpartsareonlysolong.Thisaffectsseveralthings,especiallyorderofoperations.Let'ssaythatIadd1to5.234*10^5000afewbillionortrilliontimes.Guesswhat-thenumberwon'tchangeatall.However,ifIaddonetoitselfenoughtimes,andthenaddittotheoriginalnumber,itmightmakeadent.Youshouldnotethatittakesmostcomputersalotlongertodooating-pointarithmeticthanitdoesintegerarithmetic.So,forprogramsthatreallyneedspeed,integersaremostlyused.NegativeNumbersHowwouldyouthinkthatnegativenumbersonacomputermightberepresented?Onethoughtmightbetousetherstdigitofanumberasthesign,so00000000000000000000000000000001wouldrepresentthenumber1,and10000000000000000000000000000001wouldrepresent-1.Thismakesalotofsense,andinfactsomeoldprocessorsworkthisway.However,ithassome 197

PAGE 204

Chapter10.CountingLikeaComputer problems.Firstofall,ittakesalotmorecircuitrytoaddandsubtractsignednumbersrepresentedthisway.Evenmoreproblematic,thisrepresentationhasaproblemwiththenumber0.Inthissystem,youcouldhavebothanegativeandapositive0.Thisleadstoalotofquestions,like"shouldnegativezerobeequaltopositivezero?",and"Whatshouldthesignofzerobeinvariouscircumstances?".Theseproblemswereovercomebyusingarepresentationofnegativenumberscalledtwo'scomplementrepresentation.Togetthenegativerepresentationofanumberintwo'scomplementform,youmustperformthefollowingsteps: 1. PerformaNOToperationonthenumber 2. AddonetotheresultingnumberSo,togetthenegativeof00000000000000000000000000000001,youwouldrstdoaNOToperation,whichgives11111111111111111111111111111110,andthenaddone,giving11111111111111111111111111111111.Togetnegativetwo,rsttake00000000000000000000000000000010.TheNOTofthatnumberis11111111111111111111111111111101.Addingonegives11111111111111111111111111111110.Withthisrepresentation,youcanaddnumbersjustasiftheywerepositive,andcomeoutwiththerightanswers.Forexample,ifyouaddoneplusnegativeoneinbinary,youwillnoticethatallofthenumbersiptozero.Also,therstdigitstillcarriesthesignbit,makingitsimpletodeterminewhetherornotthenumberispositiveornegative.Negativenumberswillalwayshavea1intheleftmostbit.Thisalsochangeswhichnumbersarevalidforagivennumberofbits.Withsignednumbers,thepossiblemagnitudeofthevaluesissplittoallowforbothpositiveandnegativenumbers.Forexample,abytecannormallyhavevaluesupto255.Asignedbyte,however,canstorevaluesfrom-128to127.Onethingtonoteaboutthetwo'scomplementrepresentationofsignednumbersisthat,unlikeunsignedquantities,ifyouincreasethenumberofbits,youcan'tjustaddzeroestotheleftofthenumber.Forexample,let'ssaywearedealingwithfour-bitquantitiesandwehadthenumber-3,1101.Ifweweretoextendthisinto 198

PAGE 205

Chapter10.CountingLikeaComputer aneight-bitregister,wecouldnotrepresentitas00001101asthiswouldrepresent13,not-3.Whenyouincreasethesizeofasignedquantityintwo'scomplementrepresentation,youhavetoperformsignextension.Signextensionmeansthatyouhavetopadtheleft-handsideofthequantitywithwhateverdigitisinthesigndigitwhenyouaddbits.So,ifweextendanegativenumberby4digits,weshouldllthenewdigitswitha1.Ifweextendapositivenumberby4digits,weshouldllthenewdigitswitha0.So,theextensionof-3fromfourtoeightbitswillyield11111101.Thex86processorhasdifferentformsofseveralinstructionsdependingonwhethertheyexpectthequantitiestheyoperateontobesignedorunsigned.Thesearelistedin AppendixB .Forexample,thex86processorhasbothasign-preservingshift-right,sarl,andashift-rightwhichdoesnotpreservethesignbit,shrl.OctalandHexadecimalNumbersThenumberingsystemsdiscussedsofarhavebeendecimalandbinary.However,twoothersareusedcommonincomputing-octalandhexadecimal.Infact,theyareprobablywrittenmoreoftenthanbinary.Octalisarepresentationthatonlyusesthenumbers0through7.Sotheoctalnumber10isactually8indecimalbecauseitisonegroupofeight.Octal121isdecimal81onegroupof64^2,twogroupsof8,andoneleftover.Whatmakesoctalniceisthatevery3binarydigitsmakeoneoctaldigitthereisnosuchgroupingofbinarydigitsintodecimal.So0is000,1is001,2is010,3is011,4is100,5is101,6is110,and7is111.PermissionsinLinuxaredoneusingoctal.ThisisbecauseLinuxpermissionsarebasedontheabilitytoread,writeandexecute.Therstbitisthereadpermission,thesecondbitisthewritepermission,andthethirdbitistheexecutepermission.So,0givesnopermissions,6givesreadandwritepermission,and5givesreadandexecutepermissions.Thesenumbersarethenusedforthe 199

PAGE 206

Chapter10.CountingLikeaComputer threedifferentsetsofpermissions-theowner,thegroup,andeveryoneelse.Thenumber0644meansreadandwritefortherstpermissionset,andread-onlyforthesecondandthirdset.Therstpermissionsetisfortheownerofthele.Thethirdpermissionsetisforthegroupownerofthele.Thelastpermissionsetisforeveryoneelse.So,0751meansthattheownerofthelecanread,write,andexecutethele,thegroupmemberscanreadandexecutethele,andeveryoneelsecanonlyexecutethele.Anyway,asyoucansee,octalisusedtogroupbitsbinarydigitsintothrees.Thewaytheassemblerknowsthatanumberisoctalisbecauseoctalnumbersareprexedwithazero.Forexample010means10inoctal,whichis8indecimal.Ifyoujustwrite10thatmeans10indecimal.Thebeginningzeroiswhatdifferentiatesthetwo.So,becarefulnottoputanyleadingzeroesinfrontofdecimalnumbers,ortheywillbeinterepretedasoctalnumbers!Hexadecimalnumbersalsocalledjust"hex"usethenumbers1-15foreachdigit.however,since10-15don'thavetheirownnumbers,hexadecimalusesthelettersathroughftorepresentthem.Forexample,theletterarepresents10,theletterbrepresents11,andsoon.10inhexadecimalis16indecimal.Inoctal,eachdigitrepresentedthreebits.Inhexadecimal,eachdigitrepresentsfourbits.Everytwodigitsisafullbyte,andeightdigitsisa32-bitword.Soyousee,itisconsiderablyeasiertowriteahexadecimalnumberthanitistowriteabinarynumber,becauseit'sonlyaquarterasmanydigits.Themostimportantnumbertorememberinhexadecimalisf,whichmeansthatallbitsareset.So,ifIwanttosetallofthebitsofaregisterto1,Icanjustdomovl$0xFFFFFFFF,%eaxWhichisconsiderablyeasierandlesserror-pronethanwritingmovl$0b11111111111111111111111111111111,%eaxNotealsothathexadecimalnumbersareprexedwith0x.So,whenwedo 200

PAGE 207

Chapter10.CountingLikeaComputer int$0x80Wearecallinginterruptnumber128groupsof16,orinterruptnumber0b00000000000000000000000010000000.Hexadecimalandoctalnumberstakesomegettingusedto,buttheyareheavilyusedincomputerprogramming.Itmightbeworthwhiletomakeupsomenumbersinhexandtrytoconvertthembackandforthtobinary,decimal,andoctal.OrderofBytesinaWordOnethingthatconfusesmanypeoplewhendealingwithbitsandbytesonalowlevelisthat,whenbytesarewrittenfromregisterstomemory,theirbytesarewrittenoutleast-signicant-portion-rst.1Whatmostpeopleexpectisthatiftheyhaveawordinaregister,say0x5d23efeethespacingissoyoucanseewherethebytesare,thebyteswillbewrittentomemoryinthatorder.However,onx86processors,thebytesareactuallywritteninreverseorder.Inmemorythebyteswouldbe0xeeef235donx86processors.Thebytesarewritteninreverseorderfromwhattheywouldappearconceptually,butthebitswithinthebytesareorderednormally.Notallprocessorsbehavethisway.Thex86processorisalittle-endianprocessor,whichmeansthatitstoresthe"littleend",orleast-signicantbyteofitswordsrst. 1. Signicanceinthiscontextisreferringtowhichdigittheyrepresent.Forexample,inthenumber294,thedigit2isthemostsignicantbecauseitrepresentsthehundredsplace,9isthenextmostsignicant,and4istheleastsignicant. 201

PAGE 208

Chapter10.CountingLikeaComputer Register-to-memorytransfersonlittle-endiansystemsOtherprocessorsarebig-endianprocessors,whichmeansthattheystorethe"bigend",ormostsignicantbyte,oftheirwordsrst,thewaywewouldnaturallyreadanumber. 202

PAGE 209

Chapter10.CountingLikeaComputer Register-to-memorytransfersonbig-endiansystemsThisdifferenceisnotnormallyaproblemalthoughithassparkedmanytechnicalcontroversiesthroughouttheyears.Becausethebytesarereversedagainornot,ifitisabig-endianprocessorwhenbeingreadbackintoaregister,theprogrammerusuallynevernoticeswhatorderthebytesarein.Thebyte-switchingmagichappensautomaticallybehindthescenesduringregister-to-memorytransfers.However,thebyteordercancauseproblemsinseveralinstances: Ifyoutrytoreadinseveralbytesatatimeusingmovlbutdealwiththemonabyte-by-bytebasisusingtheleastsignicantbytei.e.-byusing%aland/orshiftingoftheregister,thiswillbeinadifferentorderthantheyappearinmemory. 203

PAGE 210

Chapter10.CountingLikeaComputer Ifyoureadorwriteleswrittenfordifferentarchitectures,youmayhavetoaccountforwhateverordertheywritetheirbytesin. Ifyoureadorwritetonetworksockets,youmayhavetoaccountforadifferentbyteorderintheprotocol.Aslongasyouareawareoftheissue,itusuallyisn'tabigdeal.Formorein-depthlookatbyteorderissues,youshouldreadDAV'sEndianFAQathttp://www.rdrop.com/~cary/html/endian_faq.html,especiallythearticle"OnHolyWarsandaPleaforPeace"byDanielCohen.ConvertingNumbersforDisplaySofar,wehavebeenunabletodisplayanynumberstoredtotheuser,exceptbytheextremelylimittedmeansofpassingitthroughexitcodes.Inthissection,wewilldiscussconvertingpositivenumbersintostringsfordisplay.Thefunctionwillbecalledinteger2string,anditwilltaketwoparameters-anintegertoconvertandastringbufferlledwithnullcharacterszeroes.Thebufferwillbeassumedtobebigenoughtostoretheentirenumberasastring.atleast11characterslong,toincludeatrailingnullcharacter.Rememberthatthewaythatweseenumbersisinbase10.Therefore,toaccesstheindividualdecimaldigitsofanumber,weneedtobedividingby10anddisplayingtheremainderforeachdigit.Therefore,theprocesswilllooklikethis: Dividethenumberbyten Theremainderisthecurrentdigit.Convertittoacharacterandstoreit. Wearenishedifthequotientiszero. Otherwise,takethequotientandthenextlocationinthebufferandrepeattheprocess. 204

PAGE 211

Chapter10.CountingLikeaComputer Theonlyproblemisthatsincethisprocessdealswiththeone'splacerst,itwillleavethenumberbackwards.Therefore,wewillhavetonishbyreversingthecharacters.Wewilldothisbystoringthecharactersonthestackaswecomputethem.Thisway,aswepopthembackofftollinthebuffer,itwillbeinthereverseorderthatwepushedthemon.Thecodeforthefunctionshouldbeputinalecalledinteger-to-string.sandshouldbeenteredasfollows:#PURPOSE:Convertanintegernumbertoadecimalstring#fordisplay##INPUT:Abufferlargeenoughtoholdthelargest#possiblenumber#Anintegertoconvert##OUTPUT:Thebufferwillbeoverwrittenwiththe#decimalstring##Variables:##%ecxwillholdthecountofcharactersprocessed#%eaxwillholdthecurrentvalue#%ediwillholdthebase0#.equST_VALUE,8.equST_BUFFER,12.globlinteger2string.typeinteger2string,@functioninteger2string:#Normalfunctionbeginningpushl%ebpmovl%esp,%ebp 205

PAGE 212

Chapter10.CountingLikeaComputer #Currentcharactercountmovl$0,%ecx#MovethevalueintopositionmovlST_VALUE%ebp,%eax#Whenwedivideby10,the10#mustbeinaregisterormemorylocationmovl$10,%ediconversion_loop:#Divisionisactuallyperformedonthe#combined%edx:%eaxregister,sofirst#clearout%edxmovl$0,%edx#Divide%edx:%eaxwhichareimpliedby10.#Storethequotientin%eaxandtheremainder#in%edxbothofwhichareimplied.divl%edi#Quotientisintherightplace.%edxhas#theremainder,whichnowneedstobeconverted#intoanumber.So,%edxhasanumberthatis#0through9.Youcouldalsointerpretthisas#anindexontheASCIItablestartingfromthe#character'0'.Theasciicodefor'0'pluszero#isstilltheasciicodefor'0'.Theasciicode#for'0'plus1istheasciicodeforthe#character'1'.Therefore,thefollowing#instructionwillgiveusthecharacterforthe#numberstoredin%edxaddl$'0',%edx#Nowwewilltakethisvalueandpushitonthe 206

PAGE 213

Chapter10.CountingLikeaComputer #stack.Thisway,whenwearedone,wecanjust#popoffthecharactersone-by-oneandtheywill#beintherightorder.Notethatwearepushing#thewholeregister,butweonlyneedthebyte#in%dlthelastbyteofthe%edxregisterfor#thecharacter.pushl%edx#Incrementthedigitcountincl%ecx#Checktoseeif%eaxiszeroyet,gotonext#stepifso.cmpl$0,%eaxjeend_conversion_loop#%eaxalreadyhasitsnewvalue.jmpconversion_loopend_conversion_loop:#Thestringisnowonthestack,ifwepopit#offacharacteratatimewecancopyitinto#thebufferandbedone.#Getthepointertothebufferin%edxmovlST_BUFFER%ebp,%edxcopy_reversing_loop:#Wepushedawholeregister,butweonlyneed#thelastbyte.Sowearegoingtopopoffto#theentire%eaxregister,butthenonlymovethe#smallpart%alintothecharacterstring.popl%eaxmovb%al,%edx 207

PAGE 214

Chapter10.CountingLikeaComputer #Decreasing%ecxsoweknowwhenwearefinisheddecl%ecx#Increasing%edxsothatitwillbepointingto#thenextbyteincl%edx#Checktoseeifwearefinishedcmpl$0,%ecx#Ifso,jumptotheendofthefunctionjeend_copy_reversing_loop#Otherwise,repeattheloopjmpcopy_reversing_loopend_copy_reversing_loop:#Donecopying.Nowwriteanullbyteandreturnmovb$0,%edxmovl%ebp,%esppopl%ebpretToshowthisusedinafullprogram,usethefollowingcode,alongwiththecount_charsandwrite_newlinefunctionswrittenaboutinpreviouschapters.Thecodeshouldbeinalecalledconversion-program.s..include"linux.s".section.data#Thisiswhereitwillbestoredtmp_buffer:.ascii"0" 208

PAGE 215

Chapter10.CountingLikeaComputer .section.text.globl_start_start:movl%esp,%ebp#Storagefortheresultpushl$tmp_buffer#Numbertoconvertpushl$824callinteger2stringaddl$8,%esp#Getthecharactercountforoursystemcallpushl$tmp_buffercallcount_charsaddl$4,%esp#Thecountgoesin%edxforSYS_WRITEmovl%eax,%edx#Makethesystemcallmovl$SYS_WRITE,%eaxmovl$STDOUT,%ebxmovl$tmp_buffer,%ecxint$LINUX_SYSCALL#Writeacarriagereturnpushl$STDOUTcallwrite_newline#Exitmovl$SYS_EXIT,%eax 209

PAGE 216

Chapter10.CountingLikeaComputer movl$0,%ebxint$LINUX_SYSCALLTobuildtheprogram,issuethefollowingcommands:asinteger-to-string.s-ointeger-to-number.oascount-chars.s-ocount-chars.oaswrite-newline.s-owrite-newline.oasconversion-program.s-oconversion-program.oldinteger-to-number.ocount-chars.owrite-newline.oconversion-program.o-oconversion-programTorunjusttype./conversion-programandtheoutputshouldsay824.ReviewKnowtheConcepts Convertthedecimalnumber5,294tobinary. Whatnumberdoes0x0234aeffrepresent?Specifyinbinary,octal,anddecimal. Addthebinarynumbers10111001and101011. Multiplythebinarynumbers11001010110. Converttheresultsoftheprevioustwoproblemsintodecimal. DescribehowAND,OR,NOT,andXORwork. Whatismaskingfor? Whatnumberwouldyouusefortheagsoftheopensystemcallifyouwantedtoopentheleforwriting,andcreatetheleifitdoesn'texist? 210

PAGE 217

Chapter10.CountingLikeaComputer Howwouldyourepresent-55inathirty-twobitregister? Sign-extendthepreviousquantityintoa64-bitregister. Describethedifferencebetweenlittle-endianandbig-endianstorageofwordsinmemory.UsetheConcepts Gobacktopreviousprogramsthatreturnednumericresultsthroughtheexitstatuscode,andrewritethemtoprintouttheresultsinsteadusingourintegertostringconversionfunction. Modifytheinteger2stringcodetoreturnresultsinoctalratherthandecimal. Modifytheinteger2stringcodesothattheconversionbaseisaparameterratherthanhardcoded. Writeafunctioncalledis_negativethattakesasingleintegerasaparameterandreturns1iftheparameterisnegative,and0iftheparameterispositive.GoingFurther Modifytheinteger2stringcodesothattheconversionbasecanbegreaterthan10thisrequiresyoutouselettersfornumberspast9. Createafunctionthatdoesthereverseofinteger2stringcallednumber2integerwhichtakesacharacterstringandconvertsittoaregister-sizedinteger.Testitbyrunningthatintegerbackthroughtheinteger2stringfunctionanddisplayingtheresults. Writeaprogramthatstoreslikesanddislikesintoasinglemachineword,andthencomparestwosetsoflikesanddislikesforcommonalities. 211

PAGE 218

Chapter10.CountingLikeaComputer WriteaprogramthatreadsastringofcharactersfromSTDINandconvertsthemtoanumber. 212

PAGE 219

Chapter11.High-LevelLanguagesInthischapterwewillbegintolookatourrst"real-world"programminglanguage.Assemblylanguageisthelanguageusedatthemachine'slevel,butmostpeoplendcodinginassemblylanguagetoocumbersomeforeverydayuse.Manycomputerlanguageshavebeeninventedtomaketheprogrammingtaskeasier.Knowingawidevarietyoflanguagesisusefulformanyreasons,including Differentlanguagesarebasedondifferentconcepts,whichwillhelpyoutolearndifferentandbetterprogrammingmethodsandideas. Differentlanguagesaregoodfordifferenttypesofprojects. Differentcompanieshavedifferentstandardlanguages,soknowingmorelanguagesmakesyourskillsmoremarketable. Themorelanguagesyouknow,theeasieritistopickupnewones.Asaprogrammer,youwilloftenhavetopickupnewlanguages.Professionalprogrammerscanusuallypickupanewlanguagewithaboutaweeksworthofstudyandpractice.Languagesaresimplytools,andlearningtouseanewtoolshouldnotbesomethingaprogrammerinchesat.Infact,ifyoudocomputerconsultingyouwilloftenhavetolearnnewlanguagesonthespotinordertokeepyourselfemployed.Itwilloftenbeyourcustomer,notyou,whodecideswhatlanguageisused.Thischapterwillintroduceyoutoafewofthelanguagesavailabletoyou.Iencourageyoutoexploreasmanylanguagesasyouareinterestedin.Ipersonallytrytolearnanewlanguageeveryfewmonths.CompiledandInterpretedLanguagesManylanguagesarecompiledlanguages.Whenyouwriteassemblylanguage,eachinstructionyouwriteistranslatedintoexactlyonemachineinstructionforprocessing.Withcompilers,astatementcantranslateintooneorhundredsof 213

PAGE 220

Chapter11.High-LevelLanguages machineinstructions.Infact,dependingonhowadvancedyourcompileris,itmightevenrestructurepartsofyourcodetomakeitfaster.Inassemblylanguagewhatyouwriteiswhatyouget.Therearealsolanguagesthatareinterpretedlanguages.Theselanguagesrequirethattheuserrunaprogramcalledaninterpreterthatinturnrunsthegivenprogram.Theseareusuallyslowerthancompiledprograms,sincetheinterpreterhastoreadandinterpretthecodeasitgoesalong.However,inwell-madeinterpreters,thistimecanbefairlynegligible.Thereisalsoaclassofhybridlanguageswhichpartiallycompileaprogrambeforeexecutionintobyte-codes.Thisisdonebecausetheinterpretercanreadthebyte-codesmuchfasterthanitcanreadtheregularlanguage.Therearemanyreasonstochooseoneortheother.Compiledprogramsarenice,becauseyoudon'thavetoalreadyhaveaninterpreterinstalledintheuser'smachine.Youhavetohaveacompilerforthelanguage,buttheusersofyourprogramdon't.Inaninterpretedlanguage,youhavetobesurethattheuserhasaninterpreterinstalledforyourprogram,andthatthecomputerknowswhichinterpretertorunyourprogramwith.However,interpetedlanguagestendtobemoreexible,whilecompiledlanguagesaremorerigid.Languagechoiceisusuallydrivenbyavailabletoolsandsupportforprogrammingmethodsratherthanbywhetheralanguageiscompiledorinterpretted.Infactmanylanguageshaveoptionsforeitherone.High-levellanguages,whethercompiledorinterpreted,areorientedaroundyou,theprogrammer,insteadofaroundthemachine.Thisopensthemuptoawidevarietyoffeatures,whichcanincludethefollowing: Beingabletogroupmultipleoperationsintoasingleexpression Beingabletouse"bigvalues"-valuesthataremuchmoreconceptualthanthe4-bytewordsthatcomputersnormallydealwithforexample,beingabletoviewtextstringsasasinglevalueratherthanasastringofbytes. 214

PAGE 221

Chapter11.High-LevelLanguages Havingaccesstobetterowcontrolconstructsthanjustjumps. Havingacompilertochecktypesofvalueassignmentsandotherassertions. Havingmemoryhandledautomatically. Beingabletoworkinalanguagethatresemblestheproblemdomainratherthanthecomputerhardware.Sowhydoesonechooseonelanguageoveranother?Forexample,manychoosePerlbecauseithasavastlibraryoffunctionsforhandlingjustabouteveryprotocolortypeofdataontheplanet.Python,however,hasacleanersyntaxandoftenlendsitselftomorestraightforwardsolutions.It'scross-platformGUItoolsarealsoexcellent.PHPmakeswritingwebapplicationssimple.CommonLISPhasmorepowerandfeaturesthananyotherenvironmentforthosewillingtolearnit.Schemeisthemodelofsimplicityandpowercombinedtogether.Ciseasytointerfacewithotherlanguages.Eachlanguageisdifferent,andthemorelanguagesyouknowthebetterprogrammeryouwillbe.Knowingtheconceptsofdifferentlanguageswillhelpyouinallprogramming,becauseyoucanmatchtheprogramminglanguagetotheproblembetter,andyouhavealargersetoftoolstoworkwith.Evenifcertainfeaturesaren'tdirectlysupportedinthelanguageyouareusing,oftentheycanbesimulated.However,ifyoudon'thaveabroadexperiencewithlanguages,youwon'tknowofallthepossibilitiesyouhavetochoosefrom.YourFirstCProgramHereisyourrstCprogram,whichprints"Helloworld"tothescreenandexits.Typeitin,andgiveitthenameHello-World.c#include/*PURPOSE:Thisprogramismeantoshowabasic*//*Cprogram.Allitdoesisprint*/ 215

PAGE 222

Chapter11.High-LevelLanguages /*"HelloWorld!"tothescreenand*//*exit.*//*MainProgram*/intmainintargc,char**argv{/*Printourstringtostandardoutput*/puts"HelloWorld!n";/*Exitwithstatus0*/return0;}Asyoucansee,it'saprettysimpleprogram.Tocompileit,runthecommandgcc-oHelloWorldHello-World.cToruntheprogram,do./HelloWorldLet'slookathowthisprogramwasputtogether.CommentsinCarestartedwith/*andendedwith*/.Commentscanspanmultiplelines,butmanypeopleprefertostartandendcommentsonthesamelinesotheydon'tgetconfused.#includeistherstpartoftheprogram.Thisisapreprocessordirective.Ccompilingissplitintotwostages-thepreprocessorandthemaincompiler.Thisdirectivetellsthepreprocessortolookforthelestdio.handpasteitintoyourprogram.Thepreprocessorisresponsibleforputtingtogetherthetextoftheprogram.Thisincludesstickingdifferentlestogether,runningmacrosonyourprogramtext,etc.Afterthetextisputtogether,thepreprocessorisdoneandthemaincompilergoestowork. 216

PAGE 223

Chapter11.High-LevelLanguages Now,everythinginstdio.hisnowinyourprogramjustasifyoutypeditthereyourself.Theanglebracketsaroundthelenametellthecompilertolookinit'sstandardpathsforthele/usr/includeand/usr/local/include,usually.Ifitwasinquotes,like#include"stdio.h"itwouldlookinthecurrentdirectoryforthele.Anyway,stdio.hcontainsthedeclarationsforthestandardinputandoutputfunctionsandvariables.Thesedeclarationstellthecompilerwhatfunctionsareavailableforinputandoutput.Thenextfewlinesaresimplycommentsabouttheprogram.Thenthereisthelineintmainintargc,char**argv.Thisisthestartofafunction.CFunctionsaredeclaredwiththeirname,argumentsandreturntype.Thisdeclarationsaysthatthefunction'snameismain,itreturnsanintinteger-4byteslongonthex86platform,andhastwoarguments-anintcalledargcandachar**calledargv.Youdon'thavetoworryaboutwheretheargumentsarepositionedonthestack-theCcompilertakescareofthatforyou.Youalsodon'thavetoworryaboutloadingvaluesintoandoutofregistersbecausethecompilertakescareofthat,too.ThemainfunctionisaspecialfunctionintheClanguage-itisthestartofallCprogramsmuchlike_startinourassembly-languageprograms.Italwaystakestwoparameters.Therstparameteristhenumberofargumentsgiventothiscommand,andthesecondparameterisalistoftheargumentsthatweregiven.Thenextlineisafunctioncall.Inassemblylanguage,youhadtopushtheargumentsofafunctionontothestack,andthencallthefunction.Ctakescareofthiscomplexityforyou.Yousimplyhavetocallthefunctionwiththeparametersinparenthesis.Inthiscase,wecallthefunctionputs,withasingleparameter.Thisparameteristhecharacterstringwewanttoprint.Wejusthavetotypeinthestringinquotations,andthecompilertakescareofdeningstorageandmovingthepointerstothatstorageontothestackbeforecallingthefunction.Asyoucansee,it'salotlesswork.Finallyourfunctionreturnsthenumber0.Inassemblylanguage,westoredourreturnvaluein%eax,butinCwejustusethereturncommandandittakescare 217

PAGE 224

Chapter11.High-LevelLanguages ofthatforus.Thereturnvalueofthemainfunctioniswhatisusedastheexitcodefortheprogram.Asyoucansee,usinghigh-levellanguagesmakeslifemucheasier.Italsoallowsourprogramstorunonmultipleplatformsmoreeasily.Inassemblylanguage,yourprogramistiedtoboththeoperatingsystemandthehardwareplatform,whileincompiledandinterpretedlanguagesthesamecodecanusuallyrunonmultipleoperatingsystemsandhardwareplatforms.Forexample,thisprogramcanbebuiltandexecutedonx86hardwarerunningLinux,Windows,UNIX,ormostotheroperatingsystems.Inaddition,itcanalsorunonMacintoshhardwarerunninganumberofoperatingsystems.AdditionalinformationontheCprogramminglanguagecanbefoundin AppendixE .PerlPerlisaninterpretedlanguage,existingmostlyonLinuxandUNIX-basedplatforms.Itactuallyrunsonalmostallplatforms,butyounditmostoftenonLinuxandUNIX-basedones.Anyway,hereisthePerlversionoftheprogram,whichshouldbetypedintoalenamedHello-World.pl:#!/usr/bin/perlprint"Helloworld!n";SincePerlisinterpreted,youdon'tneedtocompileorlinkit.Justruninwiththefollowingcommand:perlHello-World.plAsyoucansee,thePerlversionisevenshorterthantheCversion.WithPerlyoudon'thavetodeclareanyfunctionsorprogramentrypoints.Youcanjuststart 218

PAGE 225

Chapter11.High-LevelLanguages typingcommandsandtheinterpreterwillrunthemasitcomestothem.Infactthisprogramonlyhastwolinesofcode,oneofwhichisoptional.Therst,optionallineisusedforUNIXmachinestotellwhichinterpretertousetoruntheprogram.The#!tellsthecomputerthatthisisaninterpretedprogram,andthe/usr/bin/perltellsthecomputertousetheprogram/usr/bin/perltointerprettheprogram.However,sincewerantheprogrambytypinginperlHello-World.pl,wehadalreadyspeciedthatwewereusingtheperlinterpreter.ThenextlinecallsaPerlbuiltinfunction,print.Thishasoneparameter,thestringtoprint.Theprogramdoesn'thaveanexplicitreturnstatement-itknowstoreturnsimplybecauseitrunsofftheendofthele.Italsoknowstoreturn0becausetherewerenoerrorswhileitran.Youcanseethatinterpretedlanguagesareoftenfocusedonlettingyougetworkingcodeasquicklyaspossible,withouthavingtodoalotofextralegwork.OnethingaboutPerlthatisn'tsoevidentfromthisexampleisthatPerltreatsstringsasasinglevalue.Inassemblylanguage,wehadtoprogramaccordingtothecomputer'smemoryarchitecture,whichmeantthatstringshadtobetreatedasasequenceofmultiplevalues,withapointertotherstletter.Perlpretendsthatstringscanbestoreddirectlyasvalues,andthushidesthecomplicationofmanipulatingthemforyou.Infact,oneofPerl'smainstrengthsisit'sabilityandspeedatmanipulatingtext.PythonThePythonversionoftheprogramlooksalmostexactlylikethePerlone.However,PythonisreallyaverydifferentlanguagethanPerl,evenifitdoesn'tseemsofromthistrivialexample.TypetheprogramintoalenamedHello-World.py.Theprogramfollows:#!/usr/bin/python 219

PAGE 226

Chapter11.High-LevelLanguages print"HelloWorld"Youshouldbeabletotellwhatthedifferentlinesoftheprogramdo.ReviewKnowtheConcepts Whatisthedifferencebetweenaninteprettedlanguageandacompiledlanguage? Whatreasonsmightcauseyoutoneedtolearnanewprogramminglanguage?UsetheConcepts Learnthebasicsyntaxofanewprogramminglanguage.Re-codeoneoftheprogramsinthisbookinthatlanguage. Intheprogramyouwroteinthequestionabove,whatspecicthingswereautomatedintheprogramminglanguageyouchose? Modifyyourprogramsothatitruns10,000timesinarow,bothinassemblylanguageandinyournewlanguage.Thenrunthetimecommandtoseewhichisfaster.Whichdoescomeoutahead?Whydoyouthinkthatis? Howdoestheprogramminglanguage'sinput/outputmethodsdifferfromthatoftheLinuxsystemcalls? 220

PAGE 227

Chapter11.High-LevelLanguages GoingFurther HavingseenlanguageswhichhavesuchbrevityasPerl,whydoyouthinkthisbookstartedyouwithalanguageasverboseasassemblylanguage? Howdoyouthinkhighlevellanguageshaveaffectedtheprocessofprogramming? Whydoyouthinksomanylanguagesexist? Learntwonewhighlevellanguages.Howdotheydifferfromeachother?Howaretheysimilar?Whatapproachtoproblem-solvingdoeseachtake? 221

PAGE 228

Chapter11.High-LevelLanguages 222

PAGE 229

Chapter12.OptimizationOptimizationistheprocessofmakingyourapplicationrunmoreeffectively.Youcanoptimizeformanythings-speed,memoryspaceusage,diskspaceusage,etc.Thischapter,however,focusesonspeedoptimization.WhentoOptimizeItisbettertonotoptimizeatallthantooptimizetoosoon.Whenyouoptimize,yourcodegenerallybecomeslessclear,becauseitbecomesmorecomplex.Readersofyourcodewillhavemoretroublediscoveringwhyyoudidwhatyoudidwhichwillincreasethecostofmaintenanceofyourproject.Evenwhenyouknowhowandwhyyourprogramrunsthewayitdoes,optimizedcodeishardertodebugandextend.Itslowsthedevelopmentprocessdownconsiderably,bothbecauseofthetimeittakestooptimizethecode,andthetimeittakestomodifyyouroptimizedcode.Compoundingthisproblemisthatyoudon'tevenknowbeforehandwherethespeedissuesinyourprogramwillbe.Evenexperiencedprogrammershavetroublepredictingwhichpartsoftheprogramwillbethebottleneckswhichneedoptimization,soyouwillprobablyendupwastingyourtimeoptimizingthewrongparts. theSectioncalledWheretoOptimize willdiscusshowtondthepartsofyourprogramthatneedoptimization.Whileyoudevelopyourprogram,youneedtohavethefollowingpriorities: Everythingisdocumented Everythingworksasdocumented Thecodeiswritteninanmodular,easilymodiableformDocumentationisessential,especiallywhenworkingingroups.Theproperfunctioningoftheprogramisessential.You'llnoticeapplicationspeedwasnot 223

PAGE 230

Chapter12.Optimization anywhereonthatlist.Optimizationisnotnecessaryduringearlydevelopmentforthefollowingreasons: Minorspeedproblemscanbeusuallysolvedthroughhardware,whichisoftenmuchcheaperthanaprogrammer'stime. Yourapplicationwillchangedramaticallyasyoureviseit,thereforewastingmostofyoureffortstooptimizeit.1 Speedproblemsareusuallylocalizedinafewplacesinyourcode-ndingtheseisdifcultbeforeyouhavemostoftheprogramnished.Therefore,thetimetooptimizeistowardtheendofdevelopment,whenyouhavedeterminedthatyourcorrectcodeactuallyhasperformanceproblems.Inaweb-basede-commerceprojectIwasinvolvedin,Ifocusedentirelyoncorrectness.Thiswasmuchtothedismayofmycolleagues,whowereworriedaboutthefactthateachpagetooktwelvesecondstoprocessbeforeiteverstartedloadingmostwebpagesprocessinunderasecond.However,Iwasdeterminedtomakeittherightwayrst,andputoptimizationasalastpriority.Whenthecodewasnallycorrectafter3monthsofwork,ittookonlythreedaystondandeliminatethebottlenecks,bringingtheaverageprocessingtimeunderaquarterofasecond.Byfocusingonthecorrectorder,Iwasabletonishaprojectthatwasbothcorrectandefcient.WheretoOptimizeOnceyouhavedeterminedthatyouhaveaperformanceissueyouneedtodeterminewhereinthecodetheproblemsoccur.Youcandothisbyrunningaproler.Aprolerisaprogramthatwillletyourunyourprogram,anditwilltellyouhowmuchtimeisspentineachfunction,andhowmanytimestheyarerun. 1. Manynewprojectsoftenhavearstcodebasewhichiscompletelyrewrittenasdevel-operslearnmoreabouttheproblemtheyaretryingtosolve.Anyoptimizationdoneontherstcodebaseiscompletelywasted. 224

PAGE 231

Chapter12.Optimization gprofisthestandardGNU/Linuxprolingtool,butadiscussionofusingprolersisoutsidethescopeofthistext.Afterrunningaproler,youcandeterminewhichfunctionsarecalledthemostorhavethemosttimespentinthem.Thesearetheonesyoushouldfocusyouroptimizationeffortson.Ifaprogramonlyspends1%ofitstimeinagivenfunction,thennomatterhowmuchyouspeeditupyouwillonlyachieveamaximumofa1%overallspeedimprovement.However,ifaprogramspends20%ofitstimeinagivenfunction,thenevenminorimprovementstothatfunctionsspeedwillbenoticeable.Therefore,prolinggivesyoutheinformationyouneedtomakegoodchoicesaboutwheretospendyourprogrammingtime.Inordertooptimizefunctions,youneedtounderstandinwhatwaystheyarebeingcalledandused.Themoreyouknowabouthowandwhenafunctioniscalled,thebetterpositionyouwillbeintooptimizeitappropriately.Therearetwomaincategoriesofoptimization-localoptimizationsandglobaloptimizations.Localoptimizationsconsistofoptimizationsthatareeitherhardwarespecic-suchasthefastestwaytoperformagivencomputation-orprogram-specic-suchasmakingaspecicpieceofcodeperformthebestforthemostoften-occuringcase.Globaloptimizationconsistofoptimizationswhicharestructural.Forexample,ifyouweretryingtondthebestwayforthreepeopleindifferentcitiestomeetinSt.Louis,alocaloptimizationwouldbendingabetterroadtogetthere,whileaglobaloptimizationwouldbetodecidetoteleconferenceinsteadofmeetinginperson.Globaloptimizationofteninvolvesrestructuringcodetoavoidperformanceproblems,ratherthantryingtondthebestwaythroughthem.LocalOptimizationsThefollowingaresomewell-knownmethodsofoptimizingpiecesofcode.Whenusinghighlevellanguages,someofthesemaybedoneautomaticallybyyourcompiler'soptimizer. 225

PAGE 232

Chapter12.Optimization PrecomputingCalculations Sometimesafunctionhasalimittednumberofpossibleinputsandoutputs.Infact,itmaybesofewthatyoucanactuallyprecomputeallofthepossibleanswersbeforehand,andsimplylookuptheanswerwhenthefunctioniscalled.Thistakesupsomespacesinceyouhavetostorealloftheanswers,butforsmallsetsofdatathisworksoutreallywell,especiallyifthecomputationnormallytakesalongtime.RememberingCalculationResults Thisissimilartothepreviousmethod,butinsteadofcomputingresultsbeforehand,theresultofeachcalculationrequestedisstored.Thiswaywhenthefunctionstarts,iftheresulthasbeencomputedbeforeitwillsimplyreturnthepreviousanswer,otherwiseitwilldothefullcomputationandstoretheresultforlaterlookup.Thishastheadvantageofrequiringlessstoragespacebecauseyouaren'tprecomputingallresults.Thisissometimestermedcachingormemoizing.LocalityofReference Localityofreferenceisatermforwhereinmemorythedataitemsyouareaccessingare.Withvirtualmemory,youmayaccesspagesofmemorywhicharestoredondisk.Insuchacase,theoperatingsystemhastoloadthatmemorypagefromdisk,andunloadotherstodisk.Let'ssay,forinstance,thattheoperatingsystemwillallowyoutohave20kofmemoryinphysicalmemoryandforcestherestofittobeondisk,andyourapplicationuses60kofmemory.Let'ssayyourprogramhastodo5operationsoneachpieceofdata.Ifitdoesoneoperationoneverypieceofdata,andthengoesthroughanddoesthenextoperationoneachpieceofdata,eventuallyeverypageofdatawillbeloadedandunloadedfromthedisk5times.Instead,ifyoudidall5operationsonagivendataitem,youonlyhavetoloadeachpagefromdiskonce.Whenyoubundleasmanyoperationsondatathatisphysicallyclosetoeachotherinmemory,thenyouaretakingadvantageoflocalityofreference. 226

PAGE 233

Chapter12.Optimization Inaddition,processorsusuallystoresomedataon-chipinacache.Ifyoukeepallofyouroperationswithinasmallareaofphysicalmemory,yourprogrammaybypassevenmainmemoryandonlyusethechip'sultra-fastcachememory.Thisisalldoneforyou-allyouhavetodoistotrytooperateonsmallsectionsofmemoryatatime,ratherthanbouncingallovertheplace.RegisterUsage Registersarethefastestmemorylocationsonthecomputer.Whenyouaccessmemory,theprocessorhastowaitwhileitisloadedfromthememorybus.However,registersarelocatedontheprocessoritself,soaccessisextremelyfast.Thereforemakingwiseusageofregistersisextremelyimportant.Ifyouhavefewenoughdataitemsyouareworkingwith,trytostorethemallinregisters.Inhighlevellanguages,youdonotalwayshavethisoption-thecompilerdecideswhatgoesinregistersandwhatdoesn't.InlineFunctions Functionsaregreatfromthepointofviewofprogrammanagement-theymakeiteasytobreakupyourprogramintoindependent,understandable,andreuseableparts.However,functioncallsdoinvolvetheoverheadofpushingargumentsontothestackanddoingthejumpsrememberlocalityofreference-yourcodemaybeswappedoutondiskinsteadofinmemory.Forhighlevellanguages,it'softenimpossibleforcompilerstodooptimizationsacrossfunction-callboundaries.However,somelanguagessupportinlinefunctionsorfunctionmacros.Thesefunctionslook,smell,taste,andactlikerealfunctions,exceptthecompilerhastheoptiontosimplyplugthecodeinexactlywhereitwascalled.Thismakestheprogramfaster,butitalsoincreasesthesizeofthecode.Therearealsomanyfunctions,likerecursivefunctions,whichcannotbeinlinedbecausetheycallthemselveseitherdirectlyorindirectly. 227

PAGE 234

Chapter12.Optimization OptimizedInstructions Oftentimestherearemultipleassemblylanguageinstructionswhichaccomplishthesamepurpose.Askilledassemblylanguageprogrammerknowswhichinstructionsarethefastest.However,thiscanchangefromprocessortoprocessor.Formoreinformationonthistopic,youneedtoseetheuser'smanualthatisprovidedforthespecicchipyouareusing.Asanexample,let'slookattheprocessofloadingthenumber0intoaregister.Onmostprocessors,doingamovl$0,%eaxisnotthequickestway.Thequickestwayistoexclusive-ortheregisterwithitself,xorl%eax,%eax.Thisisbecauseitonlyhastoaccesstheregister,anddoesn'thavetotransferanydata.Forusersofhigh-levellanguages,thecompilerhandlesthiskindofoptimizationsforyou.Forassembly-languageprogrammers,youneedtoknowyourprocessorwell.AddressingModes Differentaddressingmodesworkatdifferentspeeds.Thefastestaretheimmediateandregisteraddressingmodes.Directisthenextfastest,indirectisnext,andbasepointerandindexedindirectaretheslowest.Trytousethefasteraddressingmodes,whenpossible.Oneinterestingconsequenceofthisisthatwhenyouhaveastructuredpieceofmemorythatyouareaccessingusingbasepointeraddressing,therstelementcanbeaccessedthequickest.Sinceit'soffsetis0,youcanaccessitusingindirectaddressinginsteadofbasepointeraddressing,whichmakesitfaster.DataAlignment Someprocessorscanaccessdataonword-alignedmemoryboundariesi.e.-addressesdivisiblebythewordsizefasterthannon-aligneddata.So,whensettingupstructuresinmemory,itisbesttokeepitword-aligned.Somenon-x86processors,infact,cannotaccessnon-aligneddatainsomemodes.Thesearejustasmatteringofexamplesofthekindsoflocaloptimizationspossible.However,rememberthatthemaintainabilityandreadabilityofcodeis 228

PAGE 235

Chapter12.Optimization muchmoreimportantexceptunderextremecircumstances.GlobalOptimizationGlobaloptimizationhastwogoals.Therstoneistoputyourcodeinaformwhereitiseasytodolocaloptimiztions.Forexample,ifyouhavealargeprocedurethatperformsseveralslow,complexcalculations,youmightseeifyoucanbreakpartsofthatprocedureintotheirownfunctionswherethevaluescanbeprecomputedormemoized.Statelessfunctionsfunctionsthatonlyoperateontheparametersthatwerepassedtothem-i.e.noglobalsorsystemcallsaretheeasiesttypeoffunctionstooptimizeinacomputer.Themorestatelesspartsofyourprogramyouhave,themoreopportunitiesyouhavetooptimize.Inthee-commercesituationIwroteaboutabove,thecomputerhadtondalloftheassociatedpartsforspecicinventoryitems.Thisrequiredabout12databasecalls,andintheworstcasetookabout20seconds.However,thegoalofthisprogramwastobeinteractive,andalongwaitwoulddestroythatgoal.However,Iknewthattheseinventorycongurationsdonotchange.Therefore,Iconvertedthedatabasecallsintotheirownfunctions,whichwerestateless.Iwasthenabletomemoizethefunctions.Atthebeginningofeachday,thefunctionresultswereclearedincaseanyonehadchangedthem,andseveralinventoryitemswereautomaticallypreloaded.Fromthenonduringtheday,thersttimesomeoneaccessedaninventoryitem,itwouldtakethe20secondsitdidbeforehand,butafterwardsitwouldtakelessthanasecond,becausethedatabaseresultshadbeenmemoized.Globaloptimizationusuallyofteninvolvesachievingthefollowingpropertiesinyourfunctions:Parallelization Parallelizationmeansthatyouralgorithmcaneffectivelybesplitamongmultipleprocesses.Forexample,pregnancyisnotveryparallelizablebecause 229

PAGE 236

Chapter12.Optimization nomatterhowmanywomenyouhave,itstilltakesninemonths.However,buildingacarisparallelizablebecauseyoucanhaveoneworkerworkingontheenginewhileanotheroneisworkingontheinterior.Usually,applicationshavealimittohowparallelizabletheyare.Themoreparallelizableyourapplicationis,thebetteritcantakeadvantageofmultiprocessorandclusteredcomputercongurations.Statelessness Aswe'vediscussed,statelessfunctionsandprogramsarethosethatrelyentirelyonthedataexplicitlypassedtothemforfunctioning.Mostprocessesarenotentirelystateless,buttheycanbewithinlimits.Inmye-commerceexample,thefunctionwasn'tentirelystateless,butitwaswithintheconnesofasingleday.Therefore,Ioptimizeditasifitwereastatelessfunction,butmadeallowancesforchangesatnight.Twogreatbenetsresultingfromstatelessnessisthatmoststatelessfunctionsareparallelizableandoftenbenetfrommemoization.Globaloptimizationtakesquiteabitofpracticetoknowwhatworksandwhatdoesn't.Decidinghowtotackleoptimizationproblemsincodeinvolveslookingatalltheissues,andknowingthatxingsomeissuesmaycauseothers.ReviewKnowtheConcepts Atwhatlevelofimportanceisoptimizationcomparedtotheotherprioritiesinprogramming? Whatisthedifferencebetweenlocalandglobaloptimizations? Namesometypesoflocaloptimizations. 230

PAGE 237

Chapter12.Optimization Howdoyoudeterminewhatpartsofyourprogramneedoptimization? Atwhatlevelofimportanceisoptimizationcomparedtotheotherprioritiesinprogramming?WhydoyouthinkIrepeatedthatquestion?UsetheConcepts Gobackthrougheachprograminthisbookandtrytomakeoptimizationsaccordingtotheproceduresoutlinedinthischapter Pickaprogramfromthepreviousexerciseandtrytocalculatetheperformanceimpactonyourcodeunderspecicinputs.2GoingFurther Findanopen-sourceprogramthatyoundparticularlyfast.Contactoneofthedevelopersandaskaboutwhatkindsofoptimizationstheyperformedtoimprovethespeed. Findanopen-sourceprogramthatyoundparticularlyslow,andtrytoimaginethereasonsfortheslowness.Then,downloadthecodeandtrytoproleitusinggproforsimilartool.Findwherethecodeisspendingthemajorityofthetimeandtrytooptimizeit.Wasthereasonfortheslownessdifferentthanyouimagined? Hasthecompilereliminatedtheneedforlocaloptimizations?Whyorwhynot? Whatkindofproblemsmightacompilerrunintoifittriedtooptimizecodeacrossfunctioncallboundaries? 2. Sincetheseprogramsareusuallyshortenoughnottohavenoticeableperformanceprob-lems,loopingthroughtheprogramthousandsoftimeswillexaggeratethetimeittakestorunenoughtomakecalculations. 231

PAGE 238

Chapter12.Optimization 232

PAGE 239

Chapter13.MovingOnfromHereCongratulationsongettingthisfar.Youshouldnowhaveabasisforunderstandingtheissuesinvolvedinmanyareasofprogramming.Evenifyouneveruseassemblylanguageagain,youhavegainedavaluableperspectiveandmentalframeworkforunderstandingtherestofcomputerscience.Thereareessentiallythreemethodstolearntoprogram: FromtheBottomUp-Thisishowthisbookteaches.Itstartswithlow-levelprogramming,andworkstowardmoregeneralizedteaching. FromtheTopDown-Thisistheoppositedirection.Thisfocusesonwhatyouwanttodowiththecomputer,andteachesyouhowtobreakitdownmoreandmoreuntilyougettothelowlevels. FromtheMiddle-ThisischaracterizedbybookswhichteachaspecicprogramminglanguageorAPI.Thesearenotasconcernedwithconceptsastheyarewithspecics.Differentpeoplelikedifferentapproaches,butagoodprogrammertakesallofthemintoaccount.Thebottom-upapproacheshelpyouunderstandthemachineaspects,thetop-downapproacheshelpyouunderstandtheproblem-areaaspects,andthemiddleapproacheshelpyouwithpracticalquestionsandanswers.Toleaveanyoftheseaspectsoutwouldbeamistake.ComputerProgrammingisavastsubject.Asaprogrammer,youwillneedtobepreparedtobeconstantlylearningandpushingyourlimits.Thesebookswillhelpyoudothat.Theynotonlyteachtheirsubjects,butalsoteachvariouswaysandmethodsofthinking.AsAlanPerlissaid,"Alanguagethatdoesn'taffectthewayyouthinkaboutprogrammingisnotworthknowing"http://www.cs.yale.edu/homes/perlis-alan/quotes.html.Ifyouareconstantlylookingfornewandbetterwaysofdoingandthinking,youwillmakeasuccessfulprogrammer.Ifyoudonotseektoenhanceyourself,"Alittlesleep,alittleslumber,alittlefoldingofthehandstorest-andpovertywillcomeonyoulikea 233

PAGE 240

Chapter13.MovingOnfromHere banditandscarcitylikeanarmedman."Proverbs24:33-34NIV.Perhapsnotquitethatsevere,butstill,it'sbesttoalwaysbelearning.Thesebookswereselectedbecauseoftheircontentandtheamountofrespecttheyhaveinthecomputerscienceworld.Eachofthembringssomethingunique.Therearemanybookshere.Thebestwaytostartwouldbetolookthroughonlinereviewsofseveralofthebooks,andndastartingpointthatinterestsyou.FromtheBottomUpThislistisinthebestreadingorderIcouldnd.It'snotnecessarilyeasiesttohardest,butbasedonsubjectmatter. ProgrammingfromtheGroundUpbyJonathanBartlett IntroductiontoAlgorithmsbyThomasH.Cormen,CharlesE.Leiserson,andRonaldL.Rivest TheArtofComputerProgrammingbyDonaldKnuthvolumeset-volume1isthemostimportant ProgrammingLanguagesbySamuelN.Kamin ModernOperatingSystemsbyAndrewTanenbaum LinkersandLoadersbyJohnLevine ComputerOrganizationandDesign:TheHardware/SoftwareInterfacebyDavidPattersonandJohnHennessyFromtheTopDownThesebooksarearrangedfromthesimplesttothehardest.However,theycanbereadinanyorderyoufeelcomfortablewith. 234

PAGE 241

Chapter13.MovingOnfromHere HowtoDesignProgramsbyMatthiasFelleisen,RobertBruceFindler,MatthewFlatt,andShiramKrishnamurthi,availableonlineathttp://www.htdp.org/ SimplyScheme:AnIntroductiontoComputerSciencebyBrianHarveyandMatthewWright HowtoThinkLikeaComputerScientist:LearningwithPythonbyAllenDowney,JeffElkner,andChrisMeyers,availableonlineathttp://www.greenteapress.com/thinkpython/ StructureandInterpretationofComputerProgramsbyHaroldAbelsonandGeraldJaySussmanwithJulieSussman,availableonlineathttp://mitpress.mit.edu/sicp/ DesignPatternsbyErichGamma,RichardHelm,RalphJohnson,andJohnVlissides WhatnotHow:TheRulesApproachtoApplicationDevelopmentbyChrisDate TheAlgorithmDesignManualbySteveSkiena ProgrammingLanguagePragmaticsbyMichaelScott EssentialsofProgrammingLanguagesbyDanielP.Friedman,MitchellWand,andChristopherT.HaynesFromtheMiddleOutEachoftheseisthebestbookonitssubject.Ifyouneedtoknowtheselanguages,thesewilltellyouallyouneedtoknow. ProgrammingPerlbyLarryWall,TomChristiansen,andJonOrwant CommonLISP:TheLanguagebyGuyR.Steele ANSICommonLISPbyPaulGraham TheCProgrammingLanguagebyBrianW.KernighanandDennisM.Ritchie 235

PAGE 242

Chapter13.MovingOnfromHere TheWaiteGroup'sCPrimerPlusbyStephenPrata TheC++ProgrammingLanguagebyBjarneStroustrup ThinkinginJavabyBruceEckel,availableonlineathttp://www.mindview.net/Books/TIJ/ TheSchemeProgrammingLanguagebyKentDybvig LinuxAssemblyLanguageProgrammingbyBobNevelnSpecializedTopicsThesebooksarethebestbooksthatcovertheirtopic.Theyarethoroughandauthoritative.Togetabroadbaseofknowledge,youshouldreadseveraloutsideoftheareasyounormallyprogramin. PracticalProgramming-ProgrammingPearlsandMoreProgrammingPearlsbyJonLouisBentley Databases-UnderstandingRelationalDatabasesbyFabianPascal ProjectManagement-TheMythicalMan-MonthbyFredP.Brooks UNIXProgramming-TheArtofUNIXProgrammingbyEricS.Raymond,availableonlineathttp://www.catb.org/~esr/writings/taoup/ UNIXProgramming-AdvancedProgrammingintheUNIXEnvironmentbyW.RichardStevens NetworkProgramming-UNIXNetworkProgrammingvolumesbyW.RichardStevens GenericProgramming-ModernC++DesignbyAndreiAlexandrescu Compilers-TheArtofCompilerDesign:TheoryandPracticebyThomasPittmanandJamesPeters 236

PAGE 243

Chapter13.MovingOnfromHere Compilers-AdvancedCompilerDesignandImplementationbyStevenMuchnick DevelopmentProcess-Refactoring:ImprovingtheDesignofExistingCodebyMartinFowler,KentBeck,JohnBrant,WilliamOpdyke,andDonRoberts Typesetting-ComputersandTypesettingvolumesbyDonaldKnuth Cryptography-AppliedCryptographybyBruceSchneier Linux-ProfessionalLinuxProgrammingbyNeilMatthew,RichardStones,and14otherpeople LinuxKernel-LinuxDeviceDriversbyAlessandroRubiniandJonathanCorbet OpenSourceProgramming-TheCathedralandtheBazaar:MusingsonLinuxandOpenSourcebyanAccidentalRevolutionarybyEricS.Raymond ComputerArchitecture-ComputerArchitecture:AQuantitativeApproachbyDavidPattersonandJohnHennessyFurtherResourcesonAssemblyLanguageInassemblylanguage,yourbestresourcesareontheweb. http://www.linuxassembly.org/-agreatresourceforLinuxassemblylanguageprogrammers http://www.sandpile.org/-arepositoryofreferencematerialonx86,x86-64,andcompatibleprocessors http://www.x86.org/-Dr.Dobb'sJournalMicroprocessorResources http://www.drpaulcarter.com/pcasm/-Dr.PaulCarter'sPCAssemblyLanguagePage http://webster.cs.ucr.edu/-TheArtofAssemblyHomePage 237

PAGE 244

Chapter13.MovingOnfromHere http://www.intel.com/design/pentium/manuals/-Intel'smanualsfortheirprocessors http://www.janw.easynet.be/-JanWagemaker'sLinuxassemblylanguageexamples http://www.azillionmonkeys.com/qed/asm.html-PaulHsieh'sx86AssemblyPage 238

PAGE 245

AppendixA.GUIProgrammingIntroductiontoGUIProgrammingThepurposeofthisappendixisnottoteachyouhowtodoGraphicalUserInterfaces.Itissimplymeanttoshowhowwritinggraphicalapplicationsisthesameaswritingotherapplications,justusinganadditionallibrarytohandlethegraphicalparts.Asaprogrammeryouneedtogetusedtolearningnewlibraries.Mostofyourtimewillbespentpassingdatafromonelibrarytoanother.TheGNOMELibrariesTheGNOMEprojectsisoneofseveralprojectstoprovideacompletedesktoptoLinuxusers.TheGNOMEprojectincludesapaneltoholdapplicationlaunchersandmini-applicationscalledapplets,severalstandardapplicationstodothingssuchaslemanagement,sessionmanagement,andconguration,andanAPIforcreatingapplicationswhichtinwiththewaytherestofthesystemworks.OnethingtonoticeabouttheGNOMElibrariesisthattheyconstantlycreateandgiveyoupointerstolargedatastructures,butyouneverneedtoknowhowtheyarelaidoutinmemory.AllmanipulationoftheGUIdatastructuresaredoneentirelythroughfunctioncalls.Thisisacharacteristicofgoodlibrarydesign.Librarieschangefromversiontoversion,andsodoesthedatathateachdatastructureholds.Ifyouhadtoaccessandmanipulatethatdatayourself,thenwhenthelibraryisupdatedyouwouldhavetomodifyyourprogramstoworkwiththenewlibrary,oratleastrecompilethem.Whenyouaccessthedatathroughfunctions,thefunctionstakecareofknowingwhereinthestructureeachpieceofdatais.Thepointersyoureceivefromthelibraryareopaque-youdon'tneedtoknowspecicallywhatthestructuretheyarepointingtolookslike,youonlyneedtoknowthefunctionsthatwillproperlymanipulateit.Whendesigninglibraries,evenforusewithinonlyoneprogram,thisisagoodpracticetokeepinmind. 239

PAGE 246

AppendixA.GUIProgramming ThischapterwillnotgointodetailsabouthowGNOMEworks.Ifyouwouldliketoknowmore,visittheGNOMEdeveloperwebsiteathttp://developer.gnome.org/.Thissitecontainstutorials,mailinglists,APIdocumentation,andeverythingelseyouneedtostartprogrammingintheGNOMEenvironment.ASimpleGNOMEPrograminSeveralLanguagesThisprogramwillsimplyshowaWindowthathasabuttontoquittheapplication.Whenthatbuttonisclickeditwillaskyouifyouaresure,andifyouclickyesitwillclosetheapplication.Torunthisprogram,typeinthefollowingasgnome-example.s:#PURPOSE:Thisprogramismeanttobeanexample#ofwhatGUIprogramslooklikewritten#withtheGNOMElibraries##INPUT:Theusercanonlyclickonthe"Quit"#buttonorclosethewindow##OUTPUT:Theapplicationwillclose##PROCESS:Iftheuserclicksonthe"Quit"button,#theprogramwilldisplayadialogasking#iftheyaresure.IftheyclickYes,it#willclosetheapplication.Otherwise#itwillcontinuerunning#.section.data###GNOMEdefinitions-ThesewerefoundintheGNOME#headerfilesfortheClanguage#andconvertedintotheirassembly 240

PAGE 247

AppendixA.GUIProgramming #equivalents#GNOMEButtonNamesGNOME_STOCK_BUTTON_YES:.ascii"Button_Yes"GNOME_STOCK_BUTTON_NO:.ascii"Button_No"#GnomeMessageBoxTypesGNOME_MESSAGE_BOX_QUESTION:.ascii"question"#StandarddefinitionofNULL.equNULL,0#GNOMEsignaldefinitionssignal_destroy:.ascii"destroy"signal_delete_event:.ascii"delete_event"signal_clicked:.ascii"clicked"###Application-specificdefinitions#Applicationinformationapp_id:.ascii"gnome-example"app_version:.ascii"1.000"app_title:.ascii"GnomeExampleProgram"#TextforButtonsandwindowsbutton_quit_text: 241

PAGE 248

AppendixA.GUIProgramming .ascii"IWanttoQuittheGNOMEExampleProgram"quit_question:.ascii"Areyousureyouwanttoquit?".section.bss#Variablestosavethecreatedwidgetsin.equWORD_SIZE,4.lcommappPtr,WORD_SIZE.lcommbtnQuit,WORD_SIZE.section.text.globlmain.typemain,@functionmain:pushl%ebpmovl%esp,%ebp#InitializeGNOMElibrariespushl12%ebp#argvpushl8%ebp#argcpushl$app_versionpushl$app_idcallgnome_initaddl$16,%esp#recoverthestack#Createnewapplicationwindowpushl$app_title#Windowtitlepushl$app_id#ApplicationIDcallgnome_app_newaddl$8,%esp#recoverthestackmovl%eax,appPtr#savethewindowpointer 242

PAGE 249

AppendixA.GUIProgramming #Createnewbuttonpushl$button_quit_text#buttontextcallgtk_button_new_with_labeladdl$4,%esp#recoverthestackmovl%eax,btnQuit#savethebuttonpointer#MakethebuttonshowupinsidetheapplicationwindowpushlbtnQuitpushlappPtrcallgnome_app_set_contentsaddl$8,%esp#Makesthebuttonshowuponlyafterit'swindow#showsup,thoughpushlbtnQuitcallgtk_widget_showaddl$4,%esp#MakestheapplicationwindowshowuppushlappPtrcallgtk_widget_showaddl$4,%esp#HaveGNOMEcallourdelete_handlerfunction#whenevera"delete"eventoccurspushl$NULL#extradatatopasstoour#functionwedon'tuseanypushl$delete_handler#functionaddresstocallpushl$signal_delete_event#nameofthesignalpushlappPtr#widgettolistenforeventsoncallgtk_signal_connectaddl$16,%esp#recoverstack#HaveGNOMEcallourdestroy_handlerfunction#whenevera"destroy"eventoccurs 243

PAGE 250

AppendixA.GUIProgramming pushl$NULL#extradatatopasstoour#functionwedon'tuseanypushl$destroy_handler#functionaddresstocallpushl$signal_destroy#nameofthesignalpushlappPtr#widgettolistenforeventsoncallgtk_signal_connectaddl$16,%esp#recoverstack#HaveGNOMEcallourclick_handlerfunction#whenevera"click"eventoccurs.Notethat#theprevioussignalswerelisteningonthe#applicationwindow,whilethisoneisonly#listeningonthebuttonpushl$NULLpushl$click_handlerpushl$signal_clickedpushlbtnQuitcallgtk_signal_connectaddl$16,%esp#TransfercontroltoGNOME.Everythingthat#happensfromhereoutisinreactiontouser#events,whichcallsignalhandlers.Thismain#functionjustsetsupthemainwindowandconnects#signalhandlers,andthesignalhandlerstake#careoftherestcallgtk_main#Aftertheprogramisfinished,leavemovl$0,%eaxleaveret#A"destroy"eventhappenswhenthewidgetisbeing#removed.Inthiscase,whentheapplicationwindow 244

PAGE 251

AppendixA.GUIProgramming #isbeingremoved,wesimplywanttheeventloopto#quitdestroy_handler:pushl%ebpmovl%esp,%ebp#Thiscausesgtktoexitit'seventloop#assoonasitcan.callgtk_main_quitmovl$0,%eaxleaveret#A"delete"eventhappenswhentheapplicationwindow#getsclickedinthe"x"thatyounormallyuseto#closeawindowdelete_handler:movl$1,%eaxret#A"click"eventhappenswhenthewidgetgetsclickedclick_handler:pushl%ebpmovl%esp,%ebp#Createthe"Areyousure"dialogpushl$NULL#Endofbuttonspushl$GNOME_STOCK_BUTTON_NO#Button1pushl$GNOME_STOCK_BUTTON_YES#Button0pushl$GNOME_MESSAGE_BOX_QUESTION#Dialogtypepushl$quit_question#Dialogmesasgecallgnome_message_box_newaddl$16,%esp#recoverstack 245

PAGE 252

AppendixA.GUIProgramming #%eaxnowholdsthepointertothedialogwindow#SettingModalto1preventsanyotheruser#interactionwhilethedialogisbeingshownpushl$1pushl%eaxcallgtk_window_set_modalpopl%eaxaddl$4,%esp#Nowweshowthedialogpushl%eaxcallgtk_widget_showpopl%eax#Thissetsupallthenecessarysignalhandlers#inordertojustshowthedialog,closeitwhen#oneofthebuttonsisclicked,andreturnthe#numberofthebuttonthattheuserclickedon.#Thebuttonnumberisbasedontheorderthebuttons#werepushedoninthegnome_message_box_newfunctionpushl%eaxcallgnome_dialog_run_and_closeaddl$4,%esp#Button0istheYesbutton.Ifthisisthe#buttontheyclickedon,tellGNOMEtoquit#it'seventloop.Otherwise,donothingcmpl$0,%eaxjneclick_handler_endcallgtk_main_quitclick_handler_end:leave 246

PAGE 253

AppendixA.GUIProgramming retTobuildthisapplication,executethefollowingcommands:asgnome-example.s-ognome-example.ogccgnome-example.o`gnome-config--libsgnomeui`-ognome-exampleThentypein./gnome-exampletorunit.Thisprogram,likemostGUIprograms,makesheavyuseofpassingpointerstofunctionsasparameters.InthisprogramyoucreatewidgetswiththeGNOMEfunctionsandthenyousetupfunctionstobecalledwhencertaineventshappen.Thesefunctionsarecalledcallbackfunctions.Alloftheeventprocessingishandledbythefunctiongtk_main,soyoudon'thavetoworryabouthowtheeventsarebeingprocessed.Allyouhavetodoishavecallbackssetuptowaitforthem.HereisashortdescriptionofalloftheGNOMEfunctionsthatwereusedinthisprogram:gnome_init Takesthecommand-linearguments,argumentcount,applicationid,andapplicationversionandinitializestheGNOMElibraries.gnome_app_new Createsanewapplicationwindow,andreturnsapointertoit.Takestheapplicationidandthewindowtitleasarguments.gtk_button_new_with_label Createsanewbuttonandreturnsapointertoit.Takesoneargument-thetextthatisinthebutton. 247

PAGE 254

AppendixA.GUIProgramming gnome_app_set_contents Thistakesapointertothegnomeapplicationwindowandwhateverwidgetyouwantabuttoninthiscaseandmakesthewidgetbethecontentsoftheapplicationwindowgtk_widget_show Thismustbecalledoneverywidgetcreatedapplicationwindow,buttons,textentryboxes,etcinorderforthemtobevisible.However,inorderforagivenwidgettobevisible,allofit'sparentsmustbevisibleaswell.gtk_signal_connect Thisisthefunctionthatconnectswidgetsandtheirsignalhandlingcallbackfunctions.Thisfunctiontakesthewidgetpointer,thenameofthesignal,thecallbackfunction,andanextradatapointer.Afterthisfunctioniscalled,anytimethegiveneventistriggered,thecallbackwillbecalledwiththewidgetthatproducedthesignalandtheextradatapointer.Inthisapplication,wedon'tusetheextradatapointer,sowejustsetittoNULL,whichis0.gtk_main ThisfunctioncausesGNOMEtoenterintoit'smainloop.Tomakeapplicationprogrammingeasier,GNOMEhandlesthemainloopoftheprogramforus.GNOMEwillcheckforeventsandcalltheappropriatecallbackfunctionswhentheyoccur.Thisfunctionwillcontinuetoprocesseventsuntilgtk_main_quitiscalledbyasignalhandler.gtk_main_quit ThisfunctioncausesGNOMEtoexitit'smainloopattheearliestopportunity.gnome_message_box_new Thisfunctioncreatesadialogwindowcontainingaquestionandresponse 248

PAGE 255

AppendixA.GUIProgramming buttons.Ittakesasparametersthemessagetodisplay,thetypeofmessageitiswarning,question,etc,andalistofbuttonstodisplay.ThenalparametershouldbeNULLtoindicatethattherearenomorebuttonstodisplay.gtk_window_set_modal Thisfunctionmakesthegivenwindowamodalwindow.InGUIprogramming,amodalwindowisonethatpreventseventprocessinginotherwindowsuntilthatwindowisclosed.ThisisoftenusedwithDialogwindows.gnome_dialog_run_and_close Thisfunctiontakesadialogpointerthepointerreturnedbygnome_message_box_newcanbeusedhereandwillsetupalloftheappropriatesignalhandlerssothatitwillrununtilabuttonispressed.Atthattimeitwillclosethedialogandreturntoyouwhichbuttonwaspressed.Thebuttonnumberreferstotheorderinwhichthebuttonsweresetupingnome_message_box_new.ThefollowingisthesameprogramwrittenintheClanguage.Typeitinasgnome-example-c.c:/*PURPOSE:ThisprogramismeanttobeanexampleofwhatGUIprogramslooklikewrittenwiththeGNOMElibraries*/#include/*Programdefinitions*/#defineMY_APP_TITLE"GnomeExampleProgram"#defineMY_APP_ID"gnome-example"#defineMY_APP_VERSION"1.000" 249

PAGE 256

AppendixA.GUIProgramming #defineMY_BUTTON_TEXT"IWanttoQuittheExampleProgram"#defineMY_QUIT_QUESTION"Areyousureyouwanttoquit?"/*Mustdeclarefunctionsbeforetheyareused*/intdestroy_handlergpointerwindow,GdkEventAny*e,gpointerdata;intdelete_handlergpointerwindow,GdkEventAny*e,gpointerdata;intclick_handlergpointerwindow,GdkEventAny*e,gpointerdata;intmainintargc,char**argv{gpointerappPtr;/*applicationwindow*/gpointerbtnQuit;/*quitbutton*//*InitializeGNOMElibraries*/gnome_initMY_APP_ID,MY_APP_VERSION,argc,argv;/*Createnewapplicationwindow*/appPtr=gnome_app_newMY_APP_ID,MY_APP_TITLE;/*Createnewbutton*/btnQuit=gtk_button_new_with_labelMY_BUTTON_TEXT;/*Makethebuttonshowupinsidetheapplicationwindow*/gnome_app_set_contentsappPtr,btnQuit;/*Makesthebuttonshowup*/gtk_widget_showbtnQuit;/*Makestheapplicationwindowshowup*/ 250

PAGE 257

AppendixA.GUIProgramming gtk_widget_showappPtr;/*Connectthesignalhandlers*/gtk_signal_connectappPtr,"delete_event",GTK_SIGNAL_FUNCdelete_handler,NULL;gtk_signal_connectappPtr,"destroy",GTK_SIGNAL_FUNCdestroy_handler,NULL;gtk_signal_connectbtnQuit,"clicked",GTK_SIGNAL_FUNCclick_handler,NULL;/*TransfercontroltoGNOME*/gtk_main;return0;}/*Functiontoreceivethe"destroy"signal*/intdestroy_handlergpointerwindow,GdkEventAny*e,gpointerdata{/*LeaveGNOMEeventloop*/gtk_main_quit;return0;}/*Functiontoreceivethe"delete_event"signal*/intdelete_handlergpointerwindow,GdkEventAny*e,gpointerdata{return0;} 251

PAGE 258

AppendixA.GUIProgramming /*Functiontoreceivethe"clicked"signal*/intclick_handlergpointerwindow,GdkEventAny*e,gpointerdata{gpointermsgbox;intbuttonClicked;/*Createthe"Areyousure"dialog*/msgbox=gnome_message_box_newMY_QUIT_QUESTION,GNOME_MESSAGE_BOX_QUESTION,GNOME_STOCK_BUTTON_YES,GNOME_STOCK_BUTTON_NO,NULL;gtk_window_set_modalmsgbox,1;gtk_widget_showmsgbox;/*Rundialogbox*/buttonClicked=gnome_dialog_run_and_closemsgbox;/*Button0istheYesbutton.Ifthisisthebuttontheyclickedon,tellGNOMEtoquitit'seventloop.Otherwise,donothing*/ifbuttonClicked==0{gtk_main_quit;}return0;}Tocompileit,type 252

PAGE 259

AppendixA.GUIProgramming gccgnome-example-c.c`gnome-config--cflags--libsgnomeui`-ognome-example-cRunitbytyping./gnome-example-c.Finally,wehaveaversioninPython.Typeitinasgnome-example.py:#PURPOSE:Thisprogramismeanttobeanexample#ofwhatGUIprogramslooklikewritten#withtheGNOMElibraries##ImportGNOMElibrariesimportgtkimportgnome.ui####DEFINECALLBACKFUNCTIONSFIRST#####InPython,functionshavetobedefinedbefore#theyareused,sowehavetodefineourcallback#functionsfirst.defdestroy_handlerevent:gtk.mainquitreturn0defdelete_handlerwindow,event:return0defclick_handlerevent:#Createthe"Areyousure"dialogmsgbox=gnome.ui.GnomeMessageBox"Areyousureyouwanttoquit?",gnome.ui.MESSAGE_BOX_QUESTION,gnome.ui.STOCK_BUTTON_YES,gnome.ui.STOCK_BUTTON_NO 253

PAGE 260

AppendixA.GUIProgramming msgbox.set_modalmsgbox.showresult=msgbox.run_and_close#Button0istheYesbutton.Ifthisisthe#buttontheyclickedon,tellGNOMEtoquit#it'seventloop.Otherwise,donothingifresult==0:gtk.mainquitreturn0####MAINPROGRAM#####Createnewapplicationwindowmyapp=gnome.ui.GnomeApp"gnome-example","GnomeExampleProgram"#Createnewbuttonmybutton=gtk.GtkButton"IWanttoQuittheGNOMEExampleprogram"myapp.set_contentsmybutton#Makesthebuttonshowupmybutton.show#Makestheapplicationwindowshowupmyapp.show#Connectsignalhandlersmyapp.connect"delete_event",delete_handlermyapp.connect"destroy",destroy_handlermybutton.connect"clicked",click_handler 254

PAGE 261

AppendixA.GUIProgramming #TransfercontroltoGNOMEgtk.mainloopTorunittypepythongnome-example.py.GUIBuildersInthepreviousexample,youhavecreatedtheuser-interfacefortheapplicationbycallingthecreatefunctionsforeachwidgetandplacingitwhereyouwantedit.However,thiscanbequiteburdensomeformorecomplexapplications.Manyprogrammingenvironments,includingGNOME,haveprogramscalledGUIbuildersthatcanbeusedtoautomaticallycreateyourGUIforyou.Youjusthavetowritethecodeforthesignalhandlersandforinitializingyourprogram.ThemainGUIbuilderforGNOMEapplicationsiscalledGLADE.GLADEshipswithmostLinuxdistributions.ThereareGUIbuildersformostprogrammingenvironments.BorlandhasarangeoftoolsthatwillbuildGUIsquicklyandeasilyonLinuxandWin32systems.TheKDEenvironmenthasatoolcalledQTDesignerwhichhelpsyouautomaticallydeveloptheGUIfortheirsystem.Thereisabroadrangeofchoicesfordevelopinggraphicalapplications,buthopefullythisappendixgaveyouatasteofwhatGUIprogrammingislike. 255

PAGE 262

AppendixA.GUIProgramming 256

PAGE 263

AppendixB.Commonx86InstructionsReadingtheTablesThetablesofinstructionspresentedinthisappendixinclude: Theinstructioncode Theoperandsused Theagsused AbriefdescriptionofwhattheinstructiondoesIntheoperandssection,itwilllistthetypeofoperandsittakes.Ifittakesmorethanoneoperand,eachoperandwillbeseparatedbyacomma.Eachoperandwillhavealistofcodeswhichtellwhethertheoperandcanbeanimmediate-modevalueI,aregisterR,oramemoryaddressM.Forexample,themovlinstructionislistedasI/R/M,R/M.Thismeansthattherstoperandcanbeanykindofvalue,whilethesecondoperandmustbearegisterormemorylocation.Note,however,thatinx86assemblylanguageyoucannothavemorethanoneoperandbeamemorylocation.Intheagssection,itliststheagsinthe%eflagsregisteraffectedbytheinstruction.Thefollowingagsarementioned:O Overowag.Thisissettotrueifthedestinationoperandwasnotlargeenoughtoholdtheresultoftheinstruction.S Signag.Thisissettothesignofthelastresult. 257

PAGE 264

AppendixB.Commonx86Instructions Z Zeroag.Thisagissettotrueiftheresultoftheinstructioniszero.A Auxiliarycarryag.Thisagissetforcarriesandborrowsbetweenthethirdandfourthbit.Itisnotoftenused.P Parityag.Thisagissettotrueifthelowbyteofthelastresulthadanevennumberof1bits.C Carryag.Usedinarithmetictosaywhetherornottheresultshouldbecarriedovertoanadditionalbyte.Ifthecarryagisset,thatusuallymeansthatthedestinationregistercouldnotholdthefullresult.Itisuptotheprogrammertodecideonwhatactiontotakei.e.-propogatetheresulttoanotherbyte,signalanerror,orignoreitentirely.Otheragsexist,buttheyaremuchlessimportant.DataTransferInstructionsTheseinstructionsperformlittle,ifanycomputation.Insteadtheyaremostlyusedformovingdatafromoneplacetoanother.TableB-1.DataTransferInstructions Instruction Operands AffectedFlags movl I/R/M,I/R/M O/S/Z/A/C Thiscopiesawordofdatafromonelocationtoanother.movl%eax,%ebxcopiesthecontentsof%eaxto%ebx 258

PAGE 265

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags movb I/R/M,I/R/M O/S/Z/A/C Sameasmovl,butoperatesonindividualbytes. leal M,I/R/M O/S/Z/A/C Thistakesamemorylocationgiveninthestandardformat,and,insteadofloadingthecontentsofthememorylocation,loadsthecomputedaddress.Forexample,leal5%ebp,%ecx,1,%eaxloadstheaddresscomputedby5+%ebp+1*%ecxandstoresthatin%eax popl R/M O/S/Z/A/C Popsthetopofthestackintothegivenlocation.Thisisequivalenttoperformingmovl%esp,R/Mfollowedbyaddl$4,%esp.popflisavariantwhichpopsthetopofthestackintothe%eflagsregister. pushl I/R/M O/S/Z/A/C Pushesthegivenvalueontothestack.Thisistheequivalenttoperformingsubl$4,%espfollowedbymovlI/R/M,%esp.pushflisavariantwhichpushesthecurrentcontentsofthe%eflagsregisterontothetopofthestack. xchgl R/M,R/M O/S/Z/A/C Exchangethevaluesofthegivenoperands. IntegerInstructionsThesearebasiccalculatinginstructionsthatoperateonsignedorunsignedintegers.TableB-2.IntegerInstructions Instruction Operands AffectedFlags adcl I/R/M,R/M O/S/Z/A/P/C 259

PAGE 266

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags Addwithcarry.Addsthecarrybitandtherstoperandtothesecond,and,ifthereisanoverow,setsoverowandcarrytotrue.Thisisusuallyusedforoperationslargerthanamachineword.Theadditionontheleast-signicantwordwouldtakeplaceusingaddl,whileadditionstotheotherwordswouldusedtheadclinstructiontotakethecarryfromthepreviousaddintoaccount.Fortheusualcase,thisisnotused,andaddlisusedinstead. addl I/R/M,R/M O/S/Z/A/P/C Addition.Addstherstoperandtothesecond,storingtheresultinthesecond.Iftheresultislargerthanthedestinationregister,theoverowandcarrybitsaresettotrue.Thisinstructionoperatesonbothsignedandunsignedintegers. cdq O/S/Z/A/P/C Convertsthe%eaxwordintothedouble-wordconsistingof%edx:%eaxwithsignextension.Theqsigniesthatitisaquad-word.It'sactuallyadouble-word,butit'scalledaquad-wordbecauseoftheterminologyusedinthe16-bitdays.Thisisusuallyusedbeforeissuinganidivlinstruction. cmpl I/R/M,R/M O/S/Z/A/P/C Comparestwointegers.Itdoesthisbysubtractingtherstoperandfromthesecond.Itdiscardstheresults,butsetstheagsaccordingly.Usuallyusedbeforeaconditionaljump. decl R/M O/S/Z/A/P Decrementstheregisterormemorylocation.Usedecbtodecrementabyteinsteadofaword. divl R/M O/S/Z/A/P Performsunsigneddivision.Dividesthecontentsofthedouble-wordcontainedinthecombined%edx:%eaxregistersbythevalueintheregisterormemorylocationspecied.The%eaxregistercontainstheresultingquotient,andthe%edxregistercontainstheresultingremainder.Ifthequotientistoolargetotin%eax,ittriggersatype0interrupt. idivl R/M O/S/Z/A/P 260

PAGE 267

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags Performssigneddivision.Operatesjustlikedivlabove. imull R/M/I,R O/S/Z/A/P/C Performssignedmultiplicationandstorestheresultinthesecondoperand.Ifthesecondoperandisleftout,itisassumedtobe%eax,andthefullresultisstoredinthedouble-word%edx:%eax. incl R/M O/S/Z/A/P Incrementsthegivenregisterormemorylocation.Useincbtoincrementabyteinsteadofaword. mull R/M/I,R O/S/Z/A/P/C Performunsignedmultiplication.Samerulesasapplytoimull. negl R/M O/S/Z/A/P/C Negatesgivesthetwo'scomplementinversionofthegivenregisterormemorylocation. sbbl I/R/M,R/M O/S/Z/A/P/C Subtractwithborrowing.Thisisusedinthesamewaythatadcis,exceptforsubtraction.Normallyonlysublisused. subl I/R/M,R/M O/S/Z/A/P/C Subtractthetwooperands.Thissubtractstherstoperandfromthesecond,andstorestheresultinthesecondoperand.Thisinstructioncanbeusedonbothsignedandunsignednumbers. LogicInstructionsTheseinstructionsoperateonmemoryasbitsinsteadofwords.TableB-3.LogicInstructions Instruction Operands AffectedFlags 261

PAGE 268

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags andl I/R/M,R/M O/S/Z/P/C Performsalogicalandofthecontentsofthetwooperands,andstorestheresultinthesecondoperand.Setstheoverowandcarryagstofalse. notl R/M Performsalogicalnotoneachbitintheoperand.Alsoknownasaone'scomplement. orl I/R/M,R/M O/S/Z/A/P/C Performsalogicalorbetweenthetwooperands,andstorestheresultinthesecondoperand.Setstheoverowandcarryagstofalse. rcll I/%cl,R/M O/C Rotatesthegivenlocation'sbitstotheleftthenumberoftimesintherstoperand,whichiseitheranimmediate-modevalueortheregister%cl.Thecarryagisincludedintherotation,makingituse33bitsinsteadof32.Alsosetstheoverowag. rcrl I/%cl,R/M O/C Sameasabove,butrotatesright. roll I/%cl,R/M O/C Rotatebitstotheleft.Itsetstheoverowandcarryags,butdoesnotcountthecarryagaspartoftherotation.Thenumberofbitstorolliseitherspeciedinimmediatemodeoriscontainedinthe%clregister. rorl I/%cl,R/M O/C Sameasabove,butrotatesright. sall I/%cl,R/M C Arithmeticshiftleft.Thesignbitisshiftedouttothecarryag,andazerobitisplacedintheleastsignicantbit.Otherbitsaresimplyshiftedtotheleft.Thisisthesameastheregularshiftleft.Thenumberofbitstoshiftiseitherspeciedinimmediatemodeoriscontainedinthe%clregister. sarl I/%cl,R/M C 262

PAGE 269

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags Arithmeticshiftright.Theleastsignicantbitisshiftedouttothecarryag.Thesignbitisshiftedin,andkeptasthesignbit.Otherbitsaresimplyshiftedtotheright.Thenumberofbitstoshiftiseitherspeciedinimmediatemodeoriscontainedinthe%clregister. shll I/%cl,R/M C Logicalshiftleft.Thisshiftsallbitstotheleftsignbitisnottreatedspecially.Theleftmostbitispushedtothecarryag.Thenumberofbitstoshiftiseitherspeciedinimmediatemodeoriscontainedinthe%clregister. shrl I/%cl,R/M C Logicalshiftright.Thisshiftsallbitsintheregistertotherightsignbitisnottreatedspecially.Therightmostbitispushedtothecarryag.Thenumberofbitstoshiftiseitherspeciedinimmediatemodeoriscontainedinthe%clregister. testl I/R/M,R/M O/S/Z/A/P/C Doesalogicalandofbothoperandsanddiscardstheresults,butsetstheagsaccordingly. xorl I/R/M,R/M O/S/Z/A/P/C Doesanexclusiveoronthetwooperands,andstorestheresultinthesecondoperand.Setstheoverowandcarryagstofalse. FlowControlInstructionsTheseinstructionsmayaltertheowoftheprogram.TableB-4.FlowControlInstructions Instruction Operands AffectedFlags call destinationaddress O/S/Z/A/C 263

PAGE 270

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags Thispusheswhatwouldbethenextvaluefor%eipontothestack,andjumpstothedestinationaddress.Usedforfunctioncalls.Alternatively,thedestinationaddresscanbeanasteriskfollowedbyaregisterforanindirectfunctioncall.Forexample,call*%eaxwillcallthefunctionattheaddressin%eax. int I O/S/Z/A/C Causesaninterruptofthegivennumber.Thisisusuallyusedforsystemcallsandotherkernelinterfaces. Jcc destinationaddress O/S/Z/A/C 264

PAGE 271

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags Conditionalbranch.ccistheconditioncode.Jumpstothegivenaddressiftheconditioncodeistruesetfromthepreviousinstruction,probablyacomparison.Otherwise,goestothenextinstruction.Theconditioncodesare: [n]a[e]-aboveunsignedgreaterthan.Anncanbeaddedfor"not"andanecanbeaddedfor"orequalto" [n]b[e]-belowunsignedlessthan [n]e-equalto [n]z-zero [n]g[e]-greaterthansignedcomparison [n]l[e]-lessthansignedcomparison [n]c-carryagset [n]o-overowagset [p]p-parityagset [n]s-signagset ecxz-%ecxiszero jmp destinationaddress O/S/Z/A/C Anunconditionaljump.Thissimplysets%eiptothedestinationaddress.Alternatively,thedestinationaddresscanbeanasteriskfollowedbyaregisterforanindirectjump.Forexample,jmp*%eaxwilljumptotheaddressin%eax. 265

PAGE 272

AppendixB.Commonx86Instructions Instruction Operands AffectedFlags ret O/S/Z/A/C Popsavalueoffofthestackandthensets%eiptothatvalue.Usedtoreturnfromfunctioncalls. AssemblerDirectivesTheseareinstructionstotheassemblerandlinker,insteadofinstructionstotheprocessor.Theseareusedtohelptheassemblerputyourcodetogetherproperly,andmakeiteasiertouse.TableB-5.AssemblerDirectives Directive Operands .ascii QUOTEDSTRING Takesthegivenquotedstringandconvertsitintobytedata. .byte VALUES Takesacomma-separatedlistofvaluesandinsertsthemrightthereintheprogramasdata. .endr Endsarepeatingsectiondenedwith.rept. .equ LABEL,VALUE Setsthegivenlabelequivalenttothegivenvalue.Thevaluecanbeanumber,acharacter,oranconstantexpressionthatevaluatestoaanumberorcharacter.Fromthatpointon,useofthelabelwillbesubstitutedforthegivenvalue. .globl LABEL Setsthegivenlabelasglobal,meaningthatitcanbeusedfromseparately-compiledobjectles. .include FILE 266

PAGE 273

AppendixB.Commonx86Instructions Directive Operands Includesthegivenlejustasifitweretypedinrightthere. .lcomm SYMBOL,SIZE Thisisusedinthe.bsssectiontospecifystoragethatshouldbeallocatedwhentheprogramisexecuted.Denesthesymbolwiththeaddresswherethestoragewillbelocated,andmakessurethatitisthegivennumberofbyteslong. .long VALUES Takesasequenceofnumbersseparatedbycommas,andinsertsthosenumbersas4-bytewordsrightwheretheyareintheprogram. .rept COUNT Repeatseverythingbetweenthisdirectiveandthe.endrdirectivesthenumberoftimesspecied. .section SECTIONNAME Switchesthesectionthatisbeingworkedon.Commonsectionsinclude.textforcode,.datafordataembeddedintheprogramitself,and.bssforuninitializedglobaldata. .type SYMBOL,@function Tellsthelinkerthatthegivensymbolisafunction. DifferencesinOtherSyntaxesandTerminologyThesyntaxforassemblylanguageusedinthisbookisknownattheAT&Tsyntax.ItistheonesupportedbytheGNUtoolchainthatcomesstandardwitheveryLinuxdistribution.However,theofcialsyntaxforx86assemblylanguageknownastheIntelsyntaxisdifferent.Itisthesameassemblylanguageforthesameplatform,butitlooksdifferent.Someofthedifferencesinclude: InIntelsyntax,theoperandsofinstructionsareoftenreversed.Thedestinationoperandislistedbeforethesourceoperand. 267

PAGE 274

AppendixB.Commonx86Instructions InIntelsyntax,registersarenotprexedwiththepercentsign%. InIntelsyntax,adollar-sign$isnotrequiredtodoimmediate-modeaddressing.Instead,non-immediateaddressingisaccomplishedbysurroundingtheaddresswithbrackets[]. InIntelsyntax,theinstructionnamedoesnotincludethesizeofdatabeingmoved.Ifthatisambiguous,itisexplicitlystatedasBYTE,WORD,orDWORDimmediatelyaftertheinstructionname. ThewaythatmemoryaddressesarerepresentedinIntelassemblylanguageismuchdifferentshownbelow. Becausethex86processorlineoriginallystartedoutasa16-bitprocessor,mostliteratureaboutx86processorsrefertowordsas16-bitvalues,andcall32-bitvaluesdoublewords.However,weusetheterm"word"torefertothestandardregistersizeonaprocessor,whichis32bitsonanx86processor.Thesyntaxalsokeepsthisnamingconvention-DWORDstandsfor"doubleword"inIntelsyntaxandisusedforstandard-sizedregisters,whichwewouldcallsimplya"word". Intelassemblylanguagehastheabilitytoaddressmemoryasasegment/offsetpair.WedonotmentionthisbecauseLinuxdoesnotsupportsegmentedmemory,andisthereforeirrelevanttonormalLinuxprogramming.Otherdifferencesexist,buttheyaresmallincomparison.Toshowsomeofthedifferences,considerthefollowinginstruction:movl%eax,8%ebx,%edi,4InIntelsyntax,thiswouldbewrittenas:mov[8+%ebx+1*edi],eaxThememoryreferenceisabiteasiertoreadthanit'sAT&Tcounterpartbecauseitspellsoutexactlyhowtheaddresswillbecomputed.However,buttheorderofoperandsinIntelsyntaxcanbeconfusing. 268

PAGE 275

AppendixB.Commonx86Instructions WheretoGoforMoreInformationIntelhasasetofcomprehensiveguidestotheirprocessors.Theseareavailableathttp://www.intel.com/design/pentium/manuals/NotethatalloftheseusetheIntelsyntax,nottheAT&Tsyntax.ThemostimportantonesaretheirIA-32IntelArchitectureSoftwareDeveloper'sManualinitsthreevolumes:: Volume1:SystemProgrammingGuidehttp://developer.intel.com/design/pentium4/manuals/245470.htm Volume2:InstructionSetReferencehttp://developer.intel.com/design/pentium4/manuals/245471.htm Volume3:SystemProgrammingGuidehttp://developer.intel.com/design/pentium4/manuals/245472.htmInaddition,youcanndalotofinformationinthemanualfortheGNUassembler,availableonlineathttp://www.gnu.org/software/binutils/manual/gas-2.9.1/as.html.Similarly,themanualfortheGNUlinkerisavailableonlineathttp://www.gnu.org/software/binutils/manual/ld-2.9.1/ld.html. 269

PAGE 276

AppendixB.Commonx86Instructions 270

PAGE 277

AppendixC.ImportantSystemCallsThesearesomeofthemoreimportantsystemcallstousewhendealingwithLinux.Formostcases,however,itisbesttouselibraryfunctionsratherthandirectsystemcalls,becausethesystemcallsweredesignedtobeminimalisticwhilethelibraryfunctionsweredesignedtobeeasytoprogramwith.ForinformationabouttheLinuxClibrary,seethemanualathttp://www.gnu.org/software/libc/manual/Rememberthat%eaxholdsthesystemcallnumbers,andthatthereturnvaluesanderrorcodesarealsostoredin%eax.TableC-1.ImportantLinuxSystemCalls %eax Name %ebx %ecx %edx Notes 1 exit returnvalueint Exitstheprogram 3 read lede-scriptor bufferstart buffersizeint Readsintothegivenbuffer 4 write lede-scriptor bufferstart buffersizeint Writesthebuffertotheledescriptor 5 open null-terminatedlename optionlist permissionmode Opensthegivenle.Returnstheledescriptororanerrornumber. 6 close lede-scriptor Closesthegiveledescriptor 271

PAGE 278

AppendixC.ImportantSystemCalls %eax Name %ebx %ecx %edx Notes 12 chdir null-terminateddirectoryname Changesthecurrentdirectoryofyourprogram. 19 lseek lede-scriptor offset mode Repositionswhereyouareinthegivenle.Themodecalledthe"whence"shouldbe0forabsolutepositioning,and1forrelativepositioning. 20 getpid ReturnstheprocessIDofthecurrentprocess. 39 mkdir null-terminateddirectoryname permissionmode Createsthegivendirectory.Assumesalldirectoriesleadinguptoitalreadyexist. 40 rmdir null-terminateddirectoryname Removesthegivendirectory. 41 dup lede-scriptor Returnsanewledescriptorthatworksjustliketheexistingledescriptor. 42 pipe pipearray Createstwoledescriptors,wherewritingononeproducesdatatoreadontheotherandvice-versa.%ebxisapointertotwowordsofstoragetoholdtheledescriptors. 272

PAGE 279

AppendixC.ImportantSystemCalls %eax Name %ebx %ecx %edx Notes 45 brk newsystembreak Setsthesystembreaki.e.-theendofthedatasection.Ifthesystembreakis0,itsimplyreturnsthecurrentsystembreak. 54 ioctl lede-scriptor request arguments Thisisusedtosetparametersondeviceles.It'sactualusagevariesbasedonthetypeofleordeviceyourdescriptorreferences. Amorecompletelistingofsystemcalls,alongwithadditionalinformationisavailableathttp://www.lxhp.in-berlin.de/lhpsyscal.htmlYoucanalsogetmoreinformationaboutasystemcallbytypinginman2SYSCALLNAMEwhichwillreturnyoutheinformationaboutthesystemcallfromsection2oftheUNIXmanual.However,thisreferstotheusageofthesystemcallfromtheCprogramminglanguage,andmayormaynotbedirectlyhelpful.ForinformationonhowsystemcallsareimplementedonLinux,seetheLinuxKernel2.4Internalssectiononhowsystemcallsareimplementedathttp://www.faqs.org/docs/kernel_2_4/lki-2.html#ss2.11 273

PAGE 280

AppendixC.ImportantSystemCalls 274

PAGE 281

AppendixD.TableofASCIICodesTousethistable,simplyndthecharacterorescapethatyouwantthecodefor,andaddthenumberontheleftandthetop.TableD-1.TableofASCIIcodesindecimal +0 +1 +2 +3 +4 +5 +6 +7 0 NUL SOH STX ETX EOT ENQ ACK BEL 8 BS HT LF VT FF CR SO SI 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB 24 CAN EM SUB ESC FS GS RS US 32 # $ % & 40 + / 48 0 1 2 3 4 5 6 7 56 8 9 : ; < = > ? 64 @ A B C D E F G 72 H I J K L M N O 80 P Q R S T U V W 88 X Y Z [ ] ^ 96 ` a b c d e f g 104 h i j k l m n o 112 p q r s t u v w 120 x y z { | } ~ DEL ASCIIisactuallybeingphasedoutinfavorofaninternationalstandardknownasUnicode,whichallowsyoutodisplayanycharacterfromanyknownwritingsystemintheworld.Asyoumayhavenoticed,ASCIIonlyhassupportforEnglishcharacters.Unicodeismuchmorecomplicated,however,becauseitrequiresmorethanonebytetoencodeasinglecharacter.Thereareseveral 275

PAGE 282

AppendixD.TableofASCIICodes differentmethodsforencodingUnicodecharacters.ThemostcommonisUTF-8andUTF-32.UTF-8issomewhatbackwards-compatiblewithASCIIitisstoredthesameforEnglishcharacters,butexpandsintomultiplebyteforinternationalcharacters.UTF-32simplyrequiresfourbytesforeachcharacterratherthanone.WindowsusesUTF-16,whichisavariable-lengthencodingwhichrequiresatleast2bytespercharacter,soitisnotbackwards-compatiblewithASCII.Agoodtutorialoninternationalizationissues,fonts,andUnicodeisavailableinagreatArticlebyJoeSpolsky,called"TheAbsoluteMinimumEverySoftwareDeveloperAbsolutely,PositivelyMustKnowAboutUnicodeandCharacterSetsNoExcuses!",availableonlineathttp://www.joelonsoftware.com/articles/Unicode.html 276

PAGE 283

AppendixE.CIdiomsinAssemblyLanguageThisappendixisforCprogrammerslearningassemblylanguage.ItismeanttogiveageneralideaabouthowCconstructscanbeimplementedinassemblylanguage.IfStatementInC,anifstatementconsistsofthreeparts-thecondition,thetruebranch,andthefalsebranch.However,sinceassemblylanguageisnotablockstructuredlanguage,youhavetoworkalittletoimplementtheblock-likenatureofC.Forexample,lookatthefollowingCcode:ifa==b{/*TrueBranchCodeHere*/}else{/*FalseBranchCodeHere*/}/*AtThisPoint,Reconverge*/Inassemblylanguage,thiscanberenderedas:#Moveaandbintoregistersforcomparisonmovla,%eaxmovlb,%ebx#Comparecmpl%eax,%ebx#IfTrue,gototruebranchjetrue_branch 277

PAGE 284

AppendixE.CIdiomsinAssemblyLanguage false_branch:#Thislabelisunnecessary,#onlyherefordocumentation#FalseBranchCodeHere#Jumptorecovergencepointjmpreconvergetrue_branch:#TrueBranchCodeHerereconverge:#BothbranchesrecovergetothispointAsyoucansee,sinceassemblylanguageislinear,theblockshavetojumparoundeachother.Recovergenceishandledbytheprogrammer,notthesystem.Acasestatementiswrittenjustlikeasequenceofifstatements.FunctionCallAfunctioncallinassemblylanguagesimplyrequirespushingtheargumentstothefunctionontothestackinreverseorder,andissuingacallinstruction.Aftercalling,theargumentsarethenpoppedbackoffofthestack.Forexample,considertheCcode:printf"Thenumberis%d",88;Inassemblylanguage,thiswouldberenderedas:.section.datatext_string:.ascii"Thenumberis%d" 278

PAGE 285

AppendixE.CIdiomsinAssemblyLanguage .section.textpushl$88pushl$text_stringcallprintfpopl%eaxpopl%eax#%eaxisjustadummyvariable,#nothingisactuallybeingdone#withthevalue.Youcanalso#directlyre-adjust%esptothe#properlocation.VariablesandAssignmentGlobalandstaticvariablesaredeclaredusing.dataor.bssentries.Localvariablesaredeclaredbyreservingspaceonthestackatthebeginningofthefunction.Thisspaceisgivenbackattheendofthefunction.Interestingly,globalvariablesareaccesseddifferentlythanlocalvariablesinassemblylanguage.Globalvariablesareaccessedusingdirectaddressing,whilelocalvariablesareaccessedusingbasepointeraddressing.Forexample,considerthefollowingCcode:intmy_global_var;intfoo{intmy_local_var;my_local_var=1;my_global_var=2;return0;} 279

PAGE 286

AppendixE.CIdiomsinAssemblyLanguage Thiswouldberenderedinassemblylanguageas:.section.data.lcommmy_global_var,4.typefoo,@functionfoo:pushl%ebp#Saveoldbasepointermovl%esp,$ebp#makestackpointerbasepointersubl$4,%esp#Makeroomformy_local_var.equmy_local_var,-4#Cannowusemy_local_varto#findthelocalvariablemovl$1,my_local_var%ebpmovl$2,my_global_varmovl%ebp,%esp#Cleanupfunctionandreturnpopl%ebpretWhatmaynotbeobviousisthataccessingtheglobalvariabletakesfewermachinecyclesthanaccessingtheglobalvariable.However,thatmaynotmatterbecausethestackismorelikelytobeinphysicalmemoryinsteadofswapthantheglobalvariableis.AlsonotethatintheCprogramminglanguage,afterthecompilerloadsavalueintoaregister,thatvaluewilllikelystayinthatregisteruntilthatregisterisneededforsomethingelse.Itmayalsomoveregisters.Forexample,ifyouhaveavariablefoo,itmaystartonthestack,butthecompilerwilleventuallymoveitintoregistersforprocessing.Iftherearen'tmanyvariablesinuse,thevaluemaysimplystayintheregisteruntilitisneededagain.Otherwise,whenthatregisterisneededforsomethingelse,thevalue,ifit'schanged,iscopiedbacktoitscorrespondingmemorylocation.InC,youcanusethekeywordvolatiletomakesureallmodicationsandreferencestothevariablearedonetothememory 280

PAGE 287

AppendixE.CIdiomsinAssemblyLanguage locationitself,ratherthanaregistercopyofit,incaseotherprocesses,threads,orhardwaremaybemodifyingthevaluewhileyourfunctionisrunning.LoopsLoopsworkalotlikeifstatementsinassemblylanguage-theblocksareformedbyjumpingaround.InC,awhileloopconsistsofaloopbody,andatesttodeterminewhetherornotitistimetoexittheloop.Aforloopisexactlythesame,withoptionalinitializationandcounter-incrementsections.Thesecansimplybemovedaroundtomakeawhileloop.InC,awhilelooplookslikethis:whilea
PAGE 288

AppendixE.CIdiomsinAssemblyLanguage Thex86assemblylanguagehassomedirectsupportforloopingaswell.The%ecxregistercanbeusedasacounterthatendswithzero.Theloopinstructionwilldecrement%ecxandjumptoaspeciedaddressunless%ecxiszero.Forexample,ifyouwantedtoexecuteastatement100times,youwoulddothisinC:fori=0;i<100;i++{/*Doprocesshere*/}Inassemblylanguageitwouldbewrittenlikethis:loop_initialize:movl$100,%ecxloop_begin:##DoProcessHere##Decrement%ecxandloopsifnotzerolooploop_beginrest_of_program:#ContinuesontohereOnethingtonoticeisthattheloopinstructionrequiresyoutobecountingbackwardstozero.Ifyouneedtocountforwardsoruseanotherendingnumber,youshouldusetheloopformwhichdoesnotincludetheloopinstruction.Forreallytightloopsofcharacterstringoperations,thereisalsotherepinstruction,butwewillleavelearningaboutthatasanexercisetothereader.StructsStructsaresimplydescriptionsofmemoryblocks.Forexample,inCyoucansay: 282

PAGE 289

AppendixE.CIdiomsinAssemblyLanguage structperson{charfirstname[40];charlastname[40];intage;};Thisdoesn'tdoanythingbyitself,exceptgiveyouwaysofintelligentlyusing84bytesofdata.Youcandobasicallythesamethingusing.equdirectivesinassemblylanguage.Likethis:.equPERSON_SIZE,84.equPERSON_FIRSTNAME_OFFSET,0.equPERSON_LASTNAME_OFFSET,40.equPERSON_AGE_OFFSET,80Whenyoudeclareavariableofthistype,allyouaredoingisreserving84bytesofspace.So,ifyouhavethisinC:voidfoo{structpersonp;/*Dostuffhere*/}Inassemblylanguageyouwouldhave:foo:#Standardheaderbeginningpushl%ebpmovl%esp,%ebp#Reserveourlocalvariablesubl$PERSON_SIZE,%esp#Thisisthevariable'soffsetfrom%ebp 283

PAGE 290

AppendixE.CIdiomsinAssemblyLanguage .equP_VAR,0-PERSON_SIZE#DoStuffHere#Standardfunctionendingmovl%ebp,%esppopl%ebpretToaccessstructuremembers,youjusthavetousebasepointeraddressingwiththeoffsetsdenedabove.Forexample,inCyoucouldsettheperson'sagelikethis:p.age=30;Inassemblylanguageitwouldlooklikethis:movl$30,P_VAR+PERSON_AGE_OFFSET%ebpPointersPointersareveryeasy.Remember,pointersaresimplytheaddressthatavalueresidesat.Let'sstartbytakingalookatglobalvariables.Forexample:intglobal_data=30;Inassemblylanguage,thiswouldbe:.section.dataglobal_data:.long30TakingtheaddressofthisdatainC:a=&global_data; 284

PAGE 291

AppendixE.CIdiomsinAssemblyLanguage Takingtheaddressofthisdatainassemblylanguage:movl$global_data,%eaxYousee,withassemblylanguage,youarealmostalwaysaccessingmemorythroughpointers.That'swhatdirectaddressingis.Togetthepointeritself,youjusthavetogowithimmediatemodeaddressing.Localvariablesarealittlemoredifcult,butnotmuch.HereishowyoutaketheaddressofalocalvariableinC:voidfoo{inta;int*b;a=30;b=&a;*b=44;}Thesamecodeinassemblylanguage:foo:#Standardopeningpushl%ebpmovl%esp,%ebp#Reservetwowordsofmemorysubl$8,$esp.equA_VAR,-4.equB_VAR,-8#a=30 285

PAGE 292

AppendixE.CIdiomsinAssemblyLanguage movl$30,A_VAR%ebp#b=&amovl$A_VAR,B_VAR%ebpaddl%ebp,B_VAR%ebp#*b=30movlB_VAR%ebp,%eaxmovl$30,%eax#Standardclosingmovl%ebp,%esppopl%ebpretAsyoucansee,totaketheaddressofalocalvariable,theaddresshastobecomputedthesamewaythecomputercomputestheaddressesinbasepointeraddressing.Thereisaneasierway-theprocessorprovidestheinstructionleal,whichstandsfor"loadeffectiveaddress".Thisletsthecomputercomputetheaddress,andthenloaditwhereveryouwant.So,wecouldjustsay:#b=&alealA_VAR%ebp,%eaxmovl%eax,B_VAR%ebpIt'sthesamenumberoflines,butalittlecleaner.Then,tousethisvalue,yousimplyhavetomoveittoageneral-purposeregisteranduseindirectaddressing,asshownintheexampleabove.GettingGCCtoHelpOneofthenicethingsaboutGCCisit'sabilitytospitoutassemblylanguagecode.ToconvertaClanguageletoassembly,youcansimplydo: 286

PAGE 293

AppendixE.CIdiomsinAssemblyLanguage gcc-Sfile.cTheoutputwillbeinfile.s.It'snotthemostreadableoutput-mostofthevariablenameshavebeenremovedandreplacedeitherwithnumericstacklocationsorreferencestoautomatically-generatedlabels.Tostartwith,youprobablywanttoturnoffoptimizationswith-O0sothattheassemblylanguageoutputwillfollowyoursourcecodebetter.SomethingelseyoumightnoticeisthatGCCreservesmorestackspaceforlocalvariablesthanwedo,andthenAND's%esp1Thisistoincreasememoryandcacheefciencybydouble-wordaligningvariables.Finally,attheendoffunctions,weusuallydothefollowinginstructionstocleanupthestackbeforeissuingaretinstruction:movl%ebp,%esppopl%ebpHowever,GCCoutputwillusuallyjustincludetheinstructionleave.Thisinstructionissimplythecombinationoftheabovetwoinstructions.Wedonotuseleaveinthistextbecausewewanttobeclearaboutexactlywhatishappeningattheprocessorlevel.IencourageyoutotakeaCprogramyouhavewrittenandcompileittoassemblylanguageandtracethelogic.Then,addinoptimizationsandtryagain.Seehowthecompilerchosetorearrangeyourprogramtobemoreoptimized,andtrytogureoutwhyitchosethearrangementandinstructionsitdid. 1. NotethatdifferentversionsofGCCdothisdifferently. 287

PAGE 294

AppendixE.CIdiomsinAssemblyLanguage 288

PAGE 295

AppendixF.UsingtheGDBDebuggerBythetimeyoureadthisappendix,youwilllikelyhavewrittenatleastoneprogramwithanerrorinit.Inassemblylanguage,evenminorerrorsusuallyhaveresultssuchasthewholeprogramcrashingwithasegmentationfaulterror.Inmostprogramminglanguages,youcansimplyprintoutthevaluesinyourvariablesasyougoalong,andusethatoutputtondoutwhereyouwentwrong.Inassemblylanguage,callingoutputfunctionsisnotsoeasy.Therefore,toaidindeterminingthesourceoferrors,youmustuseasourcedebugger.Adebuggerisaprogramthathelpsyoundbugsbysteppingthroughtheprogramonestepatatime,lettingyouexaminememoryandregistercontentsalongtheway.Asourcedebuggerisadebuggerthatallowsyoutotiethedebuggingoperationdirectlytothesourcecodeofaprogram.Thismeansthatthedebuggerallowsyoutolookatthesourcecodeasyoutypeditin-completewithsymbols,labels,andcomments.ThedebuggerwewillbelookingatisGDB-theGNUDebugger.ThisapplicationispresentonalmostallGNU/Linuxdistributions.Itcandebugprogramsinmultipleprogramminglanguages,includingassemblylanguage.AnExampleDebuggingSessionThebestwaytoexplainhowadebuggerworksisbyusingit.Theprogramwewillbeusingthedebuggeronisthemaximumprogramusedin Chapter3 .Let'ssaythatyouenteredtheprogramperfectly,exceptthatyouleftouttheline:incl%ediWhenyouruntheprogram,itjustgoesinaninniteloop-itneverexits.Todeterminethecause,youneedtoruntheprogramunderGDB.However,todothis,youneedtohavetheassemblerincludedebugginginformationintheexecutable.Allyouneedtodotoenablethisistoaddthe--gstabsoptiontotheascommand.So,youwouldassembleitlikethis: 289

PAGE 296

AppendixF.UsingtheGDBDebugger as--gstabsmaximum.s-omaximum.oLinkingwouldbethesameasnormal."stabs"isthedebuggingformatusedbyGDB.Now,toruntheprogramunderthedebugger,youwouldtypeingdb./maximum.Besurethatthesourcelesareinthecurrentdirectory.Theoutputshouldlooksimilartothis:GNUgdbRedHatLinux.2.1-4Copyright2002FreeSoftwareFoundation,Inc.GDBisfreesoftware,coveredbytheGNUGeneralPublicLicense,andyouarewelcometochangeitand/ordistributecopiesofitundercertainconditions.Type"showcopying"toseetheconditions.ThereisabsolutelynowarrantyforGDB.Type"showwarranty"fordetails.ThisGDBwasconfiguredas"i386-redhat-linux"...gdbDependingonwhichversionofGDByouarerunning,thisoutputmayvaryslightly.Atthispoint,theprogramisloaded,butisnotrunningyet.Thedebuggeriswaitingyourcommand.Torunyourprogram,justtypeinrun.Thiswillnotreturn,becausetheprogramisrunninginaninniteloop.Tostoptheprogram,hitcontrol-c.Thescreenwillthensaythis:Startingprogram:/home/johnnyb/maximumProgramreceivedsignalSIGINT,Interrupt.start_loopatmaximum.s:3434movldata_items,%edi,4,%eaxCurrentlanguage:auto;currentlyasmgdbThistellsyouthattheprogramwasinterruptedbytheSIGINTsignalfromyourcontrol-c,andwaswithinthesectionlabelledstart_loop,andwasexecutingonline34whenitstopped.Itgivesyouthecodethatitisabouttoexecute. 290

PAGE 297

AppendixF.UsingtheGDBDebugger Dependingonexactlywhenyouhitcontrol-c,itmayhavestoppedonadifferentlineoradifferentinstructionthantheexample.Oneofthebestwaystondbugsinaprogramistofollowtheowoftheprogramtoseewhereitisbranchingincorrectly.Tofollowtheowofthisprogram,keeponenteringstepifor"stepinstruction",whichwillcausethecomputertoexecuteoneinstructionatatime.Ifyoudothisseveraltimes,youroutputwilllooksomethinglikethis:gdbstepi35cmpl%ebx,%eaxgdbstepi36jlestart_loopgdbstepi32cmpl$0,%eaxgdbstepi33jeloop_exitgdbstepi34movldata_items,%edi,4,%eaxgdbstepi35cmpl%ebx,%eaxgdbstepi36jlestart_loopgdbstep32cmpl$0,%eaxAsyoucantell,ithaslooped.Ingeneral,thisisgood,sincewewroteittoloop.However,theproblemisthatitisneverstopping.Therefore,tondoutwhattheproblemis,let'slookatthepointinourcodewhereweshouldbeexittingtheloop:cmpl$0,%eaxjeloop_exitBasically,itischeckingtoseeif%eaxhitszero.Ifso,itshouldexittheloop.Thereareseveralthingstocheckhere.Firstofall,youmayhaveleftthispieceout 291

PAGE 298

AppendixF.UsingtheGDBDebugger altogether.Itisnotuncommonforaprogrammertoforgettoincludeawaytoexitaloop.However,thisisnotthecasehere.Second,youshouldmakesurethatloop_exitactuallyisoutsidetheloop.Ifweputthelabelinthewrongplace,strangethingswouldhappen.However,again,thisisnotthecase.Neitherofthosepotentialproblemsaretheculprit.So,thenextoptionisthatperhaps%eaxhasthewrongvalue.TherearetwowaystocheckthecontentsofregisterinGDB.Therstoneisthecommandinforegister.Thiswilldisplaythecontentsofallregistersinhexadecimal.However,weareonlyinterestedin%eaxatthispoint.Tojustdisplay%eaxwecandoprint/$eaxtoprintitinhexadecimal,ordoprint/d$eaxtoprintitindecimal.NoticethatinGDB,registersareprexedwithdollarsignsratherthanpercentsigns.Yourscreenshouldhavethisonit:gdbprint/d$eax$1=3gdbThismeansthattheresultofyourrstinquiryis3.Everyinquiryyoumakewillbeassignedanumberprexedwithadollarsign.Now,ifyoulookbackintothecode,youwillndthat3istherstnumberinthelistofnumberstosearchthrough.Ifyoustepthroughtheloopafewmoretimes,youwillndthatineveryloopiteration%eaxhasthenumber3.Thisisnotwhatshouldbehappening.%eaxshouldgotothenextvalueinthelistineveryiteration.Okay,nowweknowthat%eaxisbeingloadedwiththesamevalueoverandoveragain.Let'ssearchtoseewhere%eaxisbeingloadedfrom.Thelineofcodeisthis:movldata_items,%edi,4,%eaxSo,stepuntilthislineofcodeisreadytoexecute.Now,thiscodedependsontwovalues-data_itemsand%edi.data_itemsisasymbol,andthereforeconstant.It'sagoodideatocheckyoursourcecodetomakesurethelabelisin 292

PAGE 299

AppendixF.UsingtheGDBDebugger frontoftherightdata,butinourcaseitis.Therefore,weneedtolookat%edi.So,weneedtoprintitout.Itwilllooklikethis:gdbprint/d$edi$2=0gdbThisindicatesthat%ediissettozero,whichiswhyitkeepsonloadingtherstelementofthearray.Thisshouldcauseyoutoaskyourselftwoquestions-whatisthepurposeof%edi,andhowshoulditsvaluebechanged?Toanswertherstquestion,wejustneedtolookinthecomments.%ediisholdingthecurrentindexofdata_items.Sinceoursearchisasequentialsearchthroughthelistofnumbersindata_items,itwouldmakesensethat%edishouldbeincrementedwitheveryloopiteration.Scanningthecode,thereisnocodewhichalters%ediatall.Therefore,weshouldaddalinetoincrement%ediatthebeginningofeveryloopiteration.Thishappenstobeexactlythelinewetossedoutatthebeginning.Assembling,linking,andrunningtheprogramagainwillshowthatitnowworkscorrectly.HopefullythisexerciseprovidedsomeinsightintousingGDBtohelpyounderrorsinyourprograms.BreakpointsandOtherGDBFeaturesTheprogramweenteredinthelastsectionhadaninniteloop,andcouldbeeasilystoppedusingcontrol-c.Otherprogramsmaysimplyabortornishwitherrors.Inthesecases,control-cdoesn'thelp,becausebythetimeyoupresscontrol-c,theprogramisalreadynished.Toxthis,youneedtosetbreakpoints.Abreakpointisaplaceinthesourcecodethatyouhavemarkedtoindicatetothedebuggerthatitshouldstoptheprogramwhenithitsthatpoint.Tosetbreakpointsyouhavetosetthemupbeforeyouruntheprogram.Beforeissuingtheruncommand,youcansetupbreakpointsusingthebreakcommand. 293

PAGE 300

AppendixF.UsingtheGDBDebugger Forexample,tobreakonline27,issuethecommandbreak27.Then,whentheprogramcrossesline27,itwillstoprunning,andprintoutthecurrentlineandinstruction.Youcanthenstepthroughtheprogramfromthatpointandexamineregistersandmemory.Tolookatthelinesandlinenumbersofyourprogram,youcansimplyusethecommandl.Thiswillprintoutyourprogramwithlinenumbersascreenatatime.Whendealingwithfunctions,youcanalsobreakonthefunctionnames.Forexample,inthefactorialprogramin Chapter4 ,wecouldsetabreakpointforthefactorialfunctionbytypinginbreakfactorial.Thiswillcausethedebuggertobreakimmediatelyafterthefunctioncallandthefunctionsetupitskipsthepushingof%ebpandthecopyingof%esp.Whensteppingthroughcode,youoftendon'twanttohavetostepthrougheveryinstructionofeveryfunction.Well-testedfunctionsareusuallyawasteoftimetostepthroughexceptonrareoccasion.Therefore,ifyouusethenexticommandinsteadofthestepicommand,GDBwillwaituntilcompletionofthefunctionbeforegoingon.Otherwise,withstepi,GDBwouldstepyouthrougheveryinstructionwithineverycalledfunction. WarningOneproblemthatGDBhasiswithhandlinginterrupts.OftentimesGDBwillmisstheinstructionthatimmediatelyfollowsaninterrupt.Theinstructionisactuallyexecuted,butGDBdoesn'tstepthroughit.Thisshouldnotbeaproblem-justbeawarethatitmayhappen. GDBQuick-ReferenceThisquick-referencetableiscopyright2002RobertM.Dondero,Jr.,andisusedbypermissioninthisbook.Parameterslistedinbracketsareoptional. 294

PAGE 301

AppendixF.UsingtheGDBDebugger TableF-1.CommonGDBDebuggingCommands Miscellaneous quit ExitGDB help[cmd] Printdescriptionofdebuggercommandcmd.Withoutcmd,printsalistoftopics. directory[dir1][dir2]... Adddirectoriesdir1,dir2,etc.tothelistofdirectoriessearchedforsourceles. RunningtheProgram run[arg1][arg2]... Runtheprogramwithcommandlineargumentsarg1,arg2,etc. setargsarg1[arg2]... Settheprogram'scommand-lineargumentstoarg1,arg2,etc. showargs Printtheprogram'scommand-linearguments. UsingBreakpoints infobreakpoints Printalistofallbreakpointsandtheirnumbersbreakpointnumbersareusedforotherbreakpointcommands. breaklinenum Setabreakpointatlinenumberlinenum. break*addr Setabreakpointatmemoryaddressaddr. breakfn Setabreakpointatthebeginningoffunctionfn. conditionbpnumexpr Breakatbreakpointbpnumonlyifexpressionexprisnon-zero. 295

PAGE 302

AppendixF.UsingtheGDBDebugger UsingBreakpoints command[bpnum]cmd1[cmd2]... Executecommandscmd1,cmd2,etc.wheneverbreakpointbpnumorthecurrentbreakpointishit. continue Continueexecutingtheprogram. kill Stopexecutingtheprogram. delete[bpnum1][bpnum2]... Deletebreakpointsbpnum1,bpnum2,etc.,orallbreakpointsifnonespecied. clear*addr Clearthebreakpointatmemoryaddressaddr. clear[fn] Clearthebreakpointatfunctionfn,orthecurrentbreakpoint. clearlinenum Clearthebreakpointatlinenumberlinenum. disable[bpnum1][bpnum2]... Disablebreakpointsbpnum1,bpnum2,etc.,orallbreakpointsifnonespecied. enable[bpnum1][bpnum2]... Enablebreakpointsbpnum1,bpnum2,etc.,orallbreakpointsifnonespecied. SteppingthroughtheProgram nexti "Stepover"thenextinstructiondoesn'tfollowfunctioncalls. stepi "Stepinto"thenextinstructionfollowsfunctioncalls. nish "Stepout"ofthecurrentfunction. ExaminingRegistersandMemory inforegisters Printthecontentsofallregisters. 296

PAGE 303

AppendixF.UsingtheGDBDebugger ExaminingRegistersandMemory print/f$reg Printthecontentsofregisterregusingformatf.Theformatcanbexhexadecimal,uunsigneddecimal,ooctal,aaddress,ccharacter,orfoatingpoint. x/rsfaddr Printthecontentsofmemoryaddressaddrusingrepeatcountr,sizes,andformatf.Repeatcountdefaultsto1ifnotspecied.Sizecanbebbyte,hhalfword,wword,orgdoubleword.Sizedefaultstowordifnotspecied.Formatisthesameasforprint,withtheadditionsofsstringandiinstruction. infodisplay Showsanumberedlistofexpressionssetuptodisplayautomaticallyateachbreak. display/f$reg Ateachbreak,printthecontentsofregisterregusingformatf. display/siaddr Ateachbreak,printthecontentsofmemoryaddressaddrusingsizessameoptionsasforthexcommand. display/ssaddr Ateachbreak,printthestringofsizesthatbeginsinmemoryaddressaddr. undisplaydisplaynum Removedisplaynumfromthedisplaylist. ExaminingtheCallStack where Printthecallstack. backtrace Printthecallstack. 297

PAGE 304

AppendixF.UsingtheGDBDebugger ExaminingtheCallStack frame Printthetopofthecallstack. up Movethecontexttowardthebottomofthecallstack. down Movethecontexttowardthetopofthecallstack. 298

PAGE 305

AppendixG.DocumentHistory 12/17/2002-Version0.5-InitialpostingofbookunderGNUFDL 07/18/2003-Version0.6-AddedASCIIappendix,nishedthediscussionoftheCPUintheMemorychapter,reworkedexercisesintoanewformat,correctedseveralerrors.ThankstoHaraldKorneliussenforthemanysuggestionsandtheASCIItable. 01/11/2004-Version0.7-AddedCtranslationappendix,addedthebeginningsofanappendixofx86instructions,addedthebeginningsofaGDBappendix,nishedouttheleschapter,nishedoutthecountingchapter,addedarecordschapter,createdasourceleofcommonlinuxdenitions,correctedseveralerrors,andlotsofotherxes 01/22/2004-Version0.8-FinishedGDBappendix,mostlynishedw/appendixofx86instructions,addedsectiononplanningprograms,addedlotsofreviewquestions,andgoteverythingtoacompleted,initialdraftstate. 01/29/2004-Version0.9-Lotsofedittingofallchapters.Madecodemoreconsistentandmadeexplanationsclearer.Addedsomeillustrations. 01/31/2004-Version1.0-Rewrotechapter9.Addedfullindex.Lotsofminorcorrections. 299

PAGE 306

AppendixG.DocumentHistory 300

PAGE 307

AppendixH.GNUFreeDocumentationLicense0.PREAMBLEThepurposeofthisLicenseistomakeamanual,textbook,orotherwrittendocumentfreeinthesenseoffreedom:toassureeveryonetheeffectivefreedomtocopyandredistributeit,withorwithoutmodifyingit,eithercommerciallyornoncommercially.Secondarily,thisLicensepreservesfortheauthorandpublisherawaytogetcreditfortheirwork,whilenotbeingconsideredresponsibleformodicationsmadebyothers.ThisLicenseisakindofcopyleft,whichmeansthatderivativeworksofthedocumentmustthemselvesbefreeinthesamesense.ItcomplementstheGNUGeneralPublicLicense,whichisacopyleftlicensedesignedforfreesoftware.WehavedesignedthisLicenseinordertouseitformanualsforfreesoftware,becausefreesoftwareneedsfreedocumentation:afreeprogramshouldcomewithmanualsprovidingthesamefreedomsthatthesoftwaredoes.ButthisLicenseisnotlimitedtosoftwaremanuals;itcanbeusedforanytextualwork,regardlessofsubjectmatterorwhetheritispublishedasaprintedbook.WerecommendthisLicenseprincipallyforworkswhosepurposeisinstructionorreference.1.APPLICABILITYANDDEFINITIONSThisLicenseappliestoanymanualorotherworkthatcontainsanoticeplacedbythecopyrightholdersayingitcanbedistributedunderthetermsofthisLicense.TheDocument,below,referstoanysuchmanualorwork.Anymemberofthepublicisalicensee,andisaddressedasyou.AModiedVersionoftheDocumentmeansanyworkcontainingtheDocumentoraportionofit,eithercopiedverbatim,orwithmodicationsand/ortranslatedintoanotherlanguage.ASecondarySectionisanamedappendixorafront-mattersectionoftheDocumentthatdealsexclusivelywiththerelationshipofthepublishersorauthorsoftheDocumenttotheDocument'soverallsubjectortorelatedmattersand 301

PAGE 308

AppendixH.GNUFreeDocumentationLicense containsnothingthatcouldfalldirectlywithinthatoverallsubject.Forexample,iftheDocumentisinpartatextbookofmathematics,aSecondarySectionmaynotexplainanymathematics.Therelationshipcouldbeamatterofhistoricalconnectionwiththesubjectorwithrelatedmatters,oroflegal,commercial,philosophical,ethicalorpoliticalpositionregardingthem.TheInvariantSectionsarecertainSecondarySectionswhosetitlesaredesignated,asbeingthoseofInvariantSections,inthenoticethatsaysthattheDocumentisreleasedunderthisLicense.TheCoverTextsarecertainshortpassagesoftextthatarelisted,asFront-CoverTextsorBack-CoverTexts,inthenoticethatsaysthattheDocumentisreleasedunderthisLicense.ATransparentcopyoftheDocumentmeansamachine-readablecopy,representedinaformatwhosespecicationisavailabletothegeneralpublic,whosecontentscanbeviewedandediteddirectlyandstraightforwardlywithgenerictexteditorsorforimagescomposedofpixelsgenericpaintprogramsorfordrawingssomewidelyavailabledrawingeditor,andthatissuitableforinputtotextformattersorforautomatictranslationtoavarietyofformatssuitableforinputtotextformatters.AcopymadeinanotherwiseTransparentleformatwhosemarkuphasbeendesignedtothwartordiscouragesubsequentmodicationbyreadersisnotTransparent.AcopythatisnotTransparentiscalledOpaque.ExamplesofsuitableformatsforTransparentcopiesincludeplainASCIIwithoutmarkup,Texinfoinputformat,LaTeXinputformat,SGMLorXMLusingapubliclyavailableDTD,andstandard-conformingsimpleHTMLdesignedforhumanmodication.OpaqueformatsincludePostScript,PDF,proprietaryformatsthatcanbereadandeditedonlybyproprietarywordprocessors,SGMLorXMLforwhichtheDTDand/orprocessingtoolsarenotgenerallyavailable,andthemachine-generatedHTMLproducedbysomewordprocessorsforoutputpurposesonly.TheTitlePagemeans,foraprintedbook,thetitlepageitself,plussuchfollowingpagesasareneededtohold,legibly,thematerialthisLicenserequiresto 302

PAGE 309

AppendixH.GNUFreeDocumentationLicense appearinthetitlepage.Forworksinformatswhichdonothaveanytitlepageassuch,TitlePagemeansthetextnearthemostprominentappearanceofthework'stitle,precedingthebeginningofthebodyofthetext.2.VERBATIMCOPYINGYoumaycopyanddistributetheDocumentinanymedium,eithercommerciallyornoncommercially,providedthatthisLicense,thecopyrightnotices,andthelicensenoticesayingthisLicenseappliestotheDocumentarereproducedinallcopies,andthatyouaddnootherconditionswhatsoevertothoseofthisLicense.Youmaynotusetechnicalmeasurestoobstructorcontrolthereadingorfurthercopyingofthecopiesyoumakeordistribute.However,youmayacceptcompensationinexchangeforcopies.Ifyoudistributealargeenoughnumberofcopiesyoumustalsofollowtheconditionsinsection3.Youmayalsolendcopies,underthesameconditionsstatedabove,andyoumaypubliclydisplaycopies.3.COPYINGINQUANTITYIfyoupublishprintedcopiesoftheDocumentnumberingmorethan100,andtheDocument'slicensenoticerequiresCoverTexts,youmustenclosethecopiesincoversthatcarry,clearlyandlegibly,alltheseCoverTexts:Front-CoverTextsonthefrontcover,andBack-CoverTextsonthebackcover.Bothcoversmustalsoclearlyandlegiblyidentifyyouasthepublisherofthesecopies.Thefrontcovermustpresentthefulltitlewithallwordsofthetitleequallyprominentandvisible.Youmayaddothermaterialonthecoversinaddition.Copyingwithchangeslimitedtothecovers,aslongastheypreservethetitleoftheDocumentandsatisfytheseconditions,canbetreatedasverbatimcopyinginotherrespects.Iftherequiredtextsforeithercoveraretoovoluminoustotlegibly,youshouldputtherstoneslistedasmanyastreasonablyontheactualcover,andcontinuetherestontoadjacentpages.IfyoupublishordistributeOpaquecopiesoftheDocumentnumberingmorethan100,youmusteitherincludeamachine-readableTransparentcopyalongwith 303

PAGE 310

AppendixH.GNUFreeDocumentationLicense eachOpaquecopy,orstateinorwitheachOpaquecopyapublicly-accessiblecomputer-networklocationcontainingacompleteTransparentcopyoftheDocument,freeofaddedmaterial,whichthegeneralnetwork-usingpublichasaccesstodownloadanonymouslyatnochargeusingpublic-standardnetworkprotocols.Ifyouusethelatteroption,youmusttakereasonablyprudentsteps,whenyoubegindistributionofOpaquecopiesinquantity,toensurethatthisTransparentcopywillremainthusaccessibleatthestatedlocationuntilatleastoneyearafterthelasttimeyoudistributeanOpaquecopydirectlyorthroughyouragentsorretailersofthateditiontothepublic.Itisrequested,butnotrequired,thatyoucontacttheauthorsoftheDocumentwellbeforeredistributinganylargenumberofcopies,togivethemachancetoprovideyouwithanupdatedversionoftheDocument.4.MODIFICATIONSYoumaycopyanddistributeaModiedVersionoftheDocumentundertheconditionsofsections2and3above,providedthatyoureleasetheModiedVersionunderpreciselythisLicense,withtheModiedVersionllingtheroleoftheDocument,thuslicensingdistributionandmodicationoftheModiedVersiontowhoeverpossessesacopyofit.Inaddition,youmustdothesethingsintheModiedVersion: A.UseintheTitlePageandonthecovers,ifanyatitledistinctfromthatoftheDocument,andfromthoseofpreviousversionswhichshould,iftherewereany,belistedintheHistorysectionoftheDocument.Youmayusethesametitleasapreviousversioniftheoriginalpublisherofthatversiongivespermission. B.ListontheTitlePage,asauthors,oneormorepersonsorentitiesresponsibleforauthorshipofthemodicationsintheModiedVersion,togetherwithatleastveoftheprincipalauthorsoftheDocumentallofitsprincipalauthors,ifithaslessthanve. 304

PAGE 311

AppendixH.GNUFreeDocumentationLicense C.StateontheTitlePagethenameofthepublisheroftheModiedVersion,asthepublisher. D.PreserveallthecopyrightnoticesoftheDocument. E.Addanappropriatecopyrightnoticeforyourmodicationsadjacenttotheothercopyrightnotices. F.Include,immediatelyafterthecopyrightnotices,alicensenoticegivingthepublicpermissiontousetheModiedVersionunderthetermsofthisLicense,intheformshownintheAddendumbelow. G.PreserveinthatlicensenoticethefulllistsofInvariantSectionsandrequiredCoverTextsgivenintheDocument'slicensenotice. H.IncludeanunalteredcopyofthisLicense. I.PreservethesectionentitledHistory,anditstitle,andaddtoitanitemstatingatleastthetitle,year,newauthors,andpublisheroftheModiedVersionasgivenontheTitlePage.IfthereisnosectionentitledHistoryintheDocument,createonestatingthetitle,year,authors,andpublisheroftheDocumentasgivenonitsTitlePage,thenaddanitemdescribingtheModiedVersionasstatedintheprevioussentence. J.Preservethenetworklocation,ifany,givenintheDocumentforpublicaccesstoaTransparentcopyoftheDocument,andlikewisethenetworklocationsgivenintheDocumentforpreviousversionsitwasbasedon.ThesemaybeplacedintheHistorysection.YoumayomitanetworklocationforaworkthatwaspublishedatleastfouryearsbeforetheDocumentitself,oriftheoriginalpublisheroftheversionitreferstogivespermission. K.InanysectionentitledAcknowledgementsorDedications,preservethesection'stitle,andpreserveinthesectionallthesubstanceandtoneofeachofthecontributoracknowledgementsand/ordedicationsgiventherein. L.PreservealltheInvariantSectionsoftheDocument,unalteredintheirtextandintheirtitles.Sectionnumbersortheequivalentarenotconsideredpartof 305

PAGE 312

AppendixH.GNUFreeDocumentationLicense thesectiontitles. M.DeleteanysectionentitledEndorsements.SuchasectionmaynotbeincludedintheModiedVersion. N.DonotretitleanyexistingsectionasEndorsementsortoconictintitlewithanyInvariantSection.IftheModiedVersionincludesnewfront-mattersectionsorappendicesthatqualifyasSecondarySectionsandcontainnomaterialcopiedfromtheDocument,youmayatyouroptiondesignatesomeorallofthesesectionsasinvariant.Todothis,addtheirtitlestothelistofInvariantSectionsintheModiedVersion'slicensenotice.Thesetitlesmustbedistinctfromanyothersectiontitles.YoumayaddasectionentitledEndorsements,provideditcontainsnothingbutendorsementsofyourModiedVersionbyvariousparties--forexample,statementsofpeerrevieworthatthetexthasbeenapprovedbyanorganizationastheauthoritativedenitionofastandard.YoumayaddapassageofuptovewordsasaFront-CoverText,andapassageofupto25wordsasaBack-CoverText,totheendofthelistofCoverTextsintheModiedVersion.OnlyonepassageofFront-CoverTextandoneofBack-CoverTextmaybeaddedbyorthrougharrangementsmadebyanyoneentity.IftheDocumentalreadyincludesacovertextforthesamecover,previouslyaddedbyyouorbyarrangementmadebythesameentityyouareactingonbehalfof,youmaynotaddanother;butyoumayreplacetheoldone,onexplicitpermissionfromthepreviouspublisherthataddedtheoldone.TheauthorsandpublishersoftheDocumentdonotbythisLicensegivepermissiontousetheirnamesforpublicityforortoassertorimplyendorsementofanyModiedVersion.5.COMBININGDOCUMENTSYoumaycombinetheDocumentwithotherdocumentsreleasedunderthisLicense,underthetermsdenedinsection4aboveformodiedversions,providedthatyouincludeinthecombinationalloftheInvariantSectionsofallof 306

PAGE 313

AppendixH.GNUFreeDocumentationLicense theoriginaldocuments,unmodied,andlistthemallasInvariantSectionsofyourcombinedworkinitslicensenotice.ThecombinedworkneedonlycontainonecopyofthisLicense,andmultipleidenticalInvariantSectionsmaybereplacedwithasinglecopy.IftherearemultipleInvariantSectionswiththesamenamebutdifferentcontents,makethetitleofeachsuchsectionuniquebyaddingattheendofit,inparentheses,thenameoftheoriginalauthororpublisherofthatsectionifknown,orelseauniquenumber.MakethesameadjustmenttothesectiontitlesinthelistofInvariantSectionsinthelicensenoticeofthecombinedwork.Inthecombination,youmustcombineanysectionsentitledHistoryinthevariousoriginaldocuments,formingonesectionentitledHistory;likewisecombineanysectionsentitledAcknowledgements,andanysectionsentitledDedications.YoumustdeleteallsectionsentitledEndorsements.6.COLLECTIONSOFDOCUMENTSYoumaymakeacollectionconsistingoftheDocumentandotherdocumentsreleasedunderthisLicense,andreplacetheindividualcopiesofthisLicenseinthevariousdocumentswithasinglecopythatisincludedinthecollection,providedthatyoufollowtherulesofthisLicenseforverbatimcopyingofeachofthedocumentsinallotherrespects.Youmayextractasingledocumentfromsuchacollection,anddispbibuteitindividuallyunderthisLicense,providedyouinsertacopyofthisLicenseintotheextracteddocument,andfollowthisLicenseinallotherrespectsregardingverbatimcopyingofthatdocument.7.AGGREGATIONWITHINDEPENDENTWORKSAcompilationoftheDocumentoritsderivativeswithotherseparateandindependentdocumentsorworks,inoronavolumeofastorageordistributionmedium,doesnotasawholecountasaModiedVersionoftheDocument,providednocompilationcopyrightisclaimedforthecompilation.Suchacompilationiscalledanaggregate,andthisLicensedoesnotapplytotheother 307

PAGE 314

AppendixH.GNUFreeDocumentationLicense self-containedworksthuscompiledwiththeDocument,onaccountoftheirbeingthuscompiled,iftheyarenotthemselvesderivativeworksoftheDocument.IftheCoverTextrequirementofsection3isapplicabletothesecopiesoftheDocument,theniftheDocumentislessthanonequarteroftheentireaggregate,theDocument'sCoverTextsmaybeplacedoncoversthatsurroundonlytheDocumentwithintheaggregate.Otherwisetheymustappearoncoversaroundthewholeaggregate.8.TRANSLATIONTranslationisconsideredakindofmodication,soyoumaydistributetranslationsoftheDocumentunderthetermsofsection4.ReplacingInvariantSectionswithtranslationsrequiresspecialpermissionfromtheircopyrightholders,butyoumayincludetranslationsofsomeorallInvariantSectionsinadditiontotheoriginalversionsoftheseInvariantSections.YoumayincludeatranslationofthisLicenseprovidedthatyoualsoincludetheoriginalEnglishversionofthisLicense.IncaseofadisagreementbetweenthetranslationandtheoriginalEnglishversionofthisLicense,theoriginalEnglishversionwillprevail.9.TERMINATIONYoumaynotcopy,modify,sublicense,ordistributetheDocumentexceptasexpresslyprovidedforunderthisLicense.Anyotherattempttocopy,modify,sublicenseordistributetheDocumentisvoid,andwillautomaticallyterminateyourrightsunderthisLicense.However,partieswhohavereceivedcopies,orrights,fromyouunderthisLicensewillnothavetheirlicensesterminatedsolongassuchpartiesremaininfullcompliance.10.FUTUREREVISIONSOFTHISLICENSETheFreeSoftwareFoundationmaypublishnew,revisedversionsoftheGNUFreeDocumentationLicensefromtimetotime.Suchnewversionswillbesimilarinspirittothepresentversion,butmaydifferindetailtoaddressnewproblemsorconcerns.Seehttp://www.gnu.org/copyleft/.EachversionoftheLicenseisgivenadistinguishingversionnumber.Ifthe 308

PAGE 315

AppendixH.GNUFreeDocumentationLicense DocumentspeciesthataparticularnumberedversionofthisLicenseoranylaterversionappliestoit,youhavetheoptionoffollowingthetermsandconditionseitherofthatspeciedversionorofanylaterversionthathasbeenpublishednotasadraftbytheFreeSoftwareFoundation.IftheDocumentdoesnotspecifyaversionnumberofthisLicense,youmaychooseanyversioneverpublishednotasadraftbytheFreeSoftwareFoundation.AddendumTousethisLicenseinadocumentyouhavewritten,includeacopyoftheLicenseinthedocumentandputthefollowingcopyrightandlicensenoticesjustafterthetitlepage:CopyrightYEARYOURNAME.Permissionisgrantedtocopy,distributeand/ormodifythisdocumentunderthetermsoftheGNUFreeDocumentationLicense,Version1.1oranylaterversionpublishedbytheFreeSoftwareFoundation;withtheInvariantSectionsbeingLISTTHEIRTITLES,withtheFront-CoverTextsbeingLIST,andwiththeBack-CoverTextsbeingLIST.AcopyofthelicenseisincludedinthesectionentitledGNUFreeDocumentationLicense.IfyouhavenoInvariantSections,writewithnoInvariantSectionsinsteadofsayingwhichonesareinvariant.IfyouhavenoFront-CoverTexts,writenoFront-CoverTextsinsteadofFront-CoverTextsbeingLIST;likewiseforBack-CoverTexts.Ifyourdocumentcontainsnontrivialexamplesofprogramcode,werecommendreleasingtheseexamplesinparallelunderyourchoiceoffreesoftwarelicense,suchastheGNUGeneralPublicLicense,topermittheiruseinfreesoftware. 309

PAGE 316

AppendixH.GNUFreeDocumentationLicense 310

PAGE 317

AppendixI.PersonalDedicationTherearesomanypeopleIcouldthank.IwillnameherebutafewofthepeoplewhohavebroughtmetowhereIamtoday.Themanyfamilymembers,SundaySchoolteachers,youthpastors,schoolteachers,friends,andotherrelationshipsthatGodhasbroughtintomylifetoleadme,helpme,andteachmearetoomanytocount.Thisbookisdedicatedtoyouall.Therearesomepeople,however,thatIwouldliketothankspecically.Firstofall,IwanttothankthemembersoftheVineyardChristianFellowshipChurchinChampaign,Illinoisforeverythingthatyouhavedonetohelpmeandmyfamilyinourtimesofcrisis.It'sbeenalongtimesinceI'veseenorheardfromanyofyou,butIthinkaboutyoualways.Youhavebeensuchablessingtome,mywife,andDaniel,andIcouldneverthankyouenoughforshowingusChrist'slovewhenweneededitmost.IthankGodeverytimeIthinkofyou-IthankHimforbringingyoualltousinourdeepesttimesofneed.EvenoutinthemiddleofIllinoiswithnofriendsoffamily,GodshowedthatHewasstillwatchingafterus.ThankyouforbeingHishandsonEarth.Specically,I'dliketothankJoeandRhonda,PamandDell,andHerschelandVicki.Thereweremany,manyothers,too-somanypeoplehelpedusthatitwouldbeimpossibletolistthemall.Ialsowanttothankmyparents,whogavemetheexampleofperserveranceandstrengthinhardtimes.Yourexamplehashelpedmebeagoodfathertomychildren,andagoodhusbandtomywife.Ialsowanttothankmywife,whoevenfromwhenwerststarteddatingencouragedmetoseekGodineverything.Thankyouforyoursupportinwritingthisbook,andmoreimportantly,foryoursupportinbeingobedienttoGod.IalsowanttothankstheLittleLightHouseschool.Myentirefamilyiscontinuallyblessedbythehelpyougivetoourson.IalsowanttothankJoeandD.A.Thankyoufortakingachanceonmeinministry.BeingabletobeapartofGod'sministryagainhashelpedmeinsomanyways. 311

PAGE 318

AppendixI.PersonalDedication YouallhavegivenmethestrengthIneededtowritethisbookoverthelastfewyears.Withoutyoursupport,Iwouldhavebeentoooverwhelmedbypersonalcrisestoeventhinkaboutanythingmorethangettingthroughaday,muchlessputtingthisbooktogether.Youhaveallbeenagreatblessingtome,andIwillkeepyouinmyprayersalways. 312

PAGE 319

Index$?, 22 28 %ah, 44 %al, 44 %ax, 44 %eax, 25 41 57 68 75 91 115 118 125 135 259 271 %ebp, 25 55 62 173 294 %ebx, 25 41 68 75 91 271 %ecx, 25 75 91 263 271 282 %edi, 25 %edx, 25 75 91 259 271 %eags, 25 39 257 258 %eip, 25 54 %esi, 25 %esp, 25 53 149 173 287 294 *, 135 -dynamic-linker, 132 ..., 135 ./, 21 .ascii, 34 266 .bss, 77 149 .byte, 34 77 266 .data, 77 103 149 .endr, 100 266 .equ, 80 111 266 283 .globl, 24 35 104 266 .include, 104 266 .int, 34 .lcomm, 78 266 .long, 33 266 .rept, 100 266 .section, 23 149 266 .text, 23 77 149 .type, 62 69 266 /etc/ld.so.conf, 134 143 /lib, 140 142 /lib/ld-linux.so.2, 134 /usr/include, 217 /usr/lib, 140 142 /usr/local/include, 217 0x80, 27 80 @functions, 62 34 n, 34 t, 35 _start, 23 35 ABI, 58 adcl, 259 addl, 24 259 address, 11 111 284 addressingmodes, 15 41 228 alignedmemory, 228 AND, 188 andl, 261 ApplicationBinaryInterface, 58 argv, 92 arithmeticandlogicunit, 9 array, 139 as, 21 ASCII, 11 89 assemble, 20 assembler, 24 111 313

PAGE 320

assemblerdirectives, 23 AssemblyLanguage, 6 20 213 assert, 120 AT&Tsyntax, 267 auxiliarycarryag, 258 backtrace, 294 basecase, 64 basepointer, 57 basepointeraddressingmode, 16 43 54 69 96 228 279 284 basepointerregister, 55 baseten, 184 basetwo, 182 184 big-endian, 202 binary, 184 199 binarydigit, 186 binarynumber, 194 binaryoperations, 186 binaryoperators, 192 bits, 186 187 201 blockstructuredlanguage, 277 booleanalgebra, 192 booleanoperators, 192 branchprediction, 10 break, 293 294 breakpoints, 293 brk, 155 167 271 buffer, 88 buffers, 76 bytes, 11 44 186 198 200 201 Clanguagecallingconvention, 54 Cprogramminglanguage, 52 135 215 273 277 cache, 227 cachehierarchies, 10 caching, 226 call, 52 54 62 67 263 278 callingconvention, 58 callingconventions, 52 58 callinginterface, 135 carryag, 196 258 casestatement, 278 cdq, 259 char, 135 chdir, 271 clear, 294 close, 76 271 cmpl, 39 196 259 command, 294 command-line, 79 91 comments, 22 compilers, 213 computerarchitecture, 7 computermemory, 11 condition, 277 294 conditionaljump, 30 196 constants, 88 96 104 111 contextswitch, 174 continue, 294 coprocessors, 10 cornercases, 119 CPU, 7 9 currentbreak, 150 166 314

PAGE 321

databus, 9 datasection, 23 33 database, 95 decimal, 184 199 decl, 63 70 259 delete, 294 destinationoperand, 43 digit, 184 directaddressingmode, 15 26 42 78 139 228 279 directory, 294 disable, 294 display, 294 divl, 259 DLLs, 130 documentation, 223 double, 139 down, 294 drivers, 121 dup, 271 dynamiclinker, 132 134 dynamiclinking, 133 dynamicmemoryallocation, 156 dynamic-linklibraries, 130 dynamically-linked, 133 echo, 22 28 edgecases, 119 effectiveaddress, 286 ELF, 143 enable, 294 errorchecking, 126 errorcode, 121 errorconditions, 117 118 errormessages, 121 122 exit, 26 133 271 exitstatuscode, 22 27 35 exponent, 197 false, 190 falsebranch, 277 fclose, 141 fetch-executecycle, 9 fgets, 141 elds, 95 ledescriptors, 75 78 les, 75 79 nish, 294 ags, 194 257 oat, 138 owcontrol, 30 39 215 fopen, 141 fprintf, 141 fputs, 141 frame, 294 functioncall, 59 126 278 functionparameters, 55 89 functions, 49 54 64 129 135 194 GCC, 4 286 GDB, 290 general-purposeregisters, 9 25 getpid, 271 globaloptimizations, 225 229 globalvariables, 51 57 279 GNOME, 239 GNU/Linux, 3 315

PAGE 322

gprof, 225 GUI, 247 GUIbuilder, 255 heap, 156 166 help, 294 hexadecimal, 27 199 high-levellanguages, 6 218 idivl, 25 259 ifstatement, 277 immediatemodeaddressing, 15 26 43 56 111 139 228 285 imull, 24 63 259 incl, 40 70 259 index, 36 indexregister, 15 37 42 indexedaddressingmode, 15 37 42 indexedindirectaddressingmode, 90 228 indirectaddressingmode, 15 42 54 286 inniteloop, 31 info, 294 infodisplay, 294 inforegister, 292 inforegisters, 294 inlinefunctions, 227 instruction, 126 instructiondecoder, 9 instructionpointer, 13 54 int, 27 135 201 263 Intelsyntax, 267 interpreter, 214 interrupts, 27 294 ioctl, 271 Jcc, 263 jmp, 62 196 263 kernel, 4 kernelmode, 174 kill, 294 Knoppix, 4 labels, 24 33 50 80 Larry-Boy, 27 ld, 21 ldd, 134 LD_LIBRARY_PATH, 134 143 leal, 258 286 leave, 287 link, 20 linker, 21 104 Linux, 4 27 little-endian, 201 localoptimizations, 225 localvariables, 51 55 63 64 279 285 localityofreference, 226 logicaloperations, 188 long, 138 longlong, 138 loop, 38 282 291 loops, 31 281 lseek, 271 MachineLanguage, 5 macros, 227 mantissa, 197 mapping, 152 masking, 193 316

PAGE 323

memoizing, 226 memory, 7 201 memoryaddress, 41 memorymanager, 156 memorypages, 154 microcodetranslation, 10 mkdir, 271 movb, 44 258 movl, 24 36 258 mull, 259 multiplier, 15 37 42 negativenumbers, 197 negl, 259 newline, 34 nexti, 294 294 NOT, 188 198 notl, 261 null, 100 nullcharacter, 91 135 nullcharacters, 104 105 objdump, 140 objectle, 21 octal, 91 199 offset, 16 offsets, 96 one'scomplement, 261 open, 75 91 194 271 operands, 24 optimization, 223 OR, 188 orl, 261 out-of-orderexecution, 10 overowag, 257 O_APPEND, 195 O_CREAT, 195 O_RDWR, 194 O_TRUNC, 195 O_WRONLY, 194 pad, 104 padding, 100 pages, 154 parallelization, 229 parameter, 69 parameters, 26 49 50 54 67 135 parityag, 258 Perl, 218 permissions, 75 91 199 persistance, 75 persistent, 95 physicaladdress, 152 physicalmemory, 152 227 280 pipe, 271 pipelining, 10 pipes, 79 pointer, 53 63 pointers, 12 14 284 popl, 53 258 precision, 197 preprocessor, 216 primitivefunctions, 50 primitives, 50 print, 292 294 printf, 133 135 proler, 224 317

PAGE 324

programcounter, 9 programstatusregister, 195 programming, 1 prototype, 135 prototypes, 138 pseudo-operations, 23 pushl, 53 67 258 Python, 219 QTDesigner, 255 quit, 294 rcll, 261 rcrl, 261 read, 76 92 271 records, 95 recoverypoints, 122 recursive, 64 register, 63 190 203 registeraddressingmode, 15 43 registers, 10 12 27 36 58 186 201 227 regularles, 79 rep, 282 residentsetsize, 155 ret, 52 57 263 287 returnaddress, 51 54 89 returnvalue, 52 58 68 returnvalues, 69 rmdir, 271 robust, 117 117 119 roll, 261 rorl, 261 rotate, 192 run, 290 294 sall, 261 sarl, 199 261 sbbl, 259 set, 294 sharedlibraries, 129 132 sharedlibrary, 142 sharedobjects, 130 shift, 192 shifting, 193 shll, 261 short, 138 show, 294 shrl, 199 261 SIGINT, 290 sign, 197 signextension, 199 signag, 257 signed, 199 signednumbers, 198 skeletoncode, 129 sourcecode, 20 sourcele, 21 sourceoperand, 43 specialles, 79 specialregister, 55 special-purposeregister, 13 special-purposeregisters, 10 25 stack, 53 stackframe, 55 65 stackmemory, 53 stackpointer, 55 318

PAGE 325

stackregister, 53 standarderror, 79 standardinput, 78 standardoutput, 79 statelessfunctions, 229 staticvariables, 51 57 279 statically-linked, 133 statuscode, 28 118 statusregister, 39 STDERR, 79 STDIN, 78 stdio.h, 216 STDOUT, 79 stepi, 291 294 294 strcmp, 141 strdup, 141 strlen, 105 141 struct, 139 structs, 282 structureddata, 95 stub, 121 subl, 24 259 superscalarprocessors, 10 swapdeath, 155 swapping, 154 switches, 187 symbol, 23 50 symbols, 133 systembreak, 150 systemcall, 26 41 126 194 systemcalls, 26 50 273 tab, 35 testing, 118 testl, 261 textsection, 23 true, 190 truebranch, 277 two'scomplement, 198 259 typedef, 140 unconditionaljump, 30 196 undisplay, 294 UNIXmanual, 273 unsigned, 139 199 unstructureddata, 95 up, 294 usermode, 174 variables, 35 virtualaddress, 152 virtualmemory, 152 155 volatile, 280 VonNeumannarchitecture, 7 9 where, 294 whileloop, 281 Win32, 255 word, 12 44 200 write, 76 93 271 x, 294 xchgl, 258 XOR, 188 xorl, 261 zeroag, 258 319