Transcription of Jonathan Bartlett - cs.princeton.edu
1 Programming from the Ground UpJonathan BartlettEdited byDominick Bruno, from the Ground Upby Jonathan BartlettEdited by Dominick Bruno, 2003 by Jonathan BartlettPermission is granted to copy, distribute and/or modify this document under the terms of the GNU Free DocumentationLicense, Version or any later version published by the Free Software Foundation; with no Invariant Sections, with noFront-Cover Texts, and with no Back-Cover Texts. A copy of the license is included inAppendix H. In addition, you aregranted full rights to use the code examples for any purpose without even having to credit the trademarks are property of their respective book can be purchased at book is not a reference book, it is an introductory book.
2 It is therefore not suitable by itself to learn how toprofessionally program in x86 assembly language, as some details have been left out to make the learning processsmoother. The point of the book is to help the student understand how assembly language and computer programmingworks, not to be a reference to the subject. Reference information about a particular processor can be obtained bycontacting the company which makes receive a copy of this book in electronic form, please visit the website site contains the instructions for downloading a transparent copy of this book as defined by the GNU FreeDocumentation of Contents1. to Computer of Computer Accessing Your First in the of an Assembly Language the a Maximum All About with Functions Functions using the C Calling Function Dealing with UNIX File and Special Files in a Reading and Writing Simple the Developing Robust Does the Time Go?
3 87 Some Tips for Developing Robust Errors Our Program More Sharing Functions with Code a Shared Shared Libraries Information about a Shared Intermediate Memory a Computer Views Memory Layout of a Linux Memory Address is a More Simple Memory our Counting Like a , Falsehood, and Binary Program Status Numbering and Hexadecimal of Bytes in a Numbers for High-Level and Interpreted First C to to Moving On from the Bottom the Top the Middle Resources on Assembly GUI Common x86 Important System Table of ASCII C Idioms in Assembly Using the GDB Document GNU Free Documentation Personal 1. IntroductionWelcome to ProgrammingI love programming.
4 I enjoy the challenge to not only make a working program, but to do so withstyle. Programming is like poetry. It conveys a message, not only to the computer, but to thosewho modify and use your program. With a program, you build your own world with your ownrules. You create your world according to your conception of both the problem and the programmers create worlds with programs that are clear and succinct, much like apoem or of the greatest programmers, Donald Knuth, describes programming not as telling acomputer how to do something, but telling a person how they would instruct a computer to dosomething. The point is that programs are meant to be read by people, not just computers. Yourprograms will be modified and updated by others long after you move on to other projects.
5 Thus,programming is not as much about communicating to a computer as it is communicating to thosewho come after you. A programmer is a problem-solver, a poet, and an instructor all at goal is to solve the problem at hand, doing so with balance and taste, and teach yoursolution to future programmers. I hope that this book can teach at least some of the poetry andmagic that makes computing introductory books on programming frustrate me to no end. At the end of them you can stillask "how does the computer really work?" and not have a good answer. They tend to pass overtopics that are difficult even though they are important. I will take you through the difficult issuesbecause that is the only way to move on to masterful programming.
6 My goal is to take you fromknowing nothing about programming to understanding how to think, write, and learn like aprogrammer. You won t know everything, but you will have a background for how everything fitstogether. At the end of this book, you should be able to do the following: Understand how a program works and interacts with other programs Read other people s programs and learn how they work Learn new programming languages quickly Learn advanced concepts in computer science quicklyI will not teach you everything. Computer science is a massive field, especially when youcombine the theory with the practice of computer programming. However, I will attempt to getyou started on the foundations so you can easily go wherever you want is somewhat of a chicken and egg problem in teaching programming, especially assemblylanguage.
7 There is a lot to learn - it s almost too much to learn almost at once, but each piece1 Chapter 1. Introductiondepends on all the others. Therefore, you must be patient with yourself and the computer whilelearning to program. If you don t understand something the first time, reread it. If you still don tunderstand it, it is sometimes best to take it by faith and come back to it later. Often after moreexposure to programming the ideas will make more sense. Don t get discouraged. It s a longclimb, but very the end of each chapter are three sets of review exercises. The first set is more or lessregurgitation - they check to see if can you give back what you learned in the chapter. The secondset contains application questions - they check to see if you can apply what you learned to solveproblems.
8 The final set is to see if you are capable of broadening your horizons. Some of thesequestions may not be answerable until later in the book, but they give you some things to thinkabout. Other questions require some research into outside sources to discover the answer. Stillothers require you to simply analyze your options and explain a best solution. Many of thequestions don t have right or wrong answers, but that doesn t mean they are the issues involved in programming, learning how to research answers, and learninghow to look ahead are all a major part of a programmer s you have problems that you just can t get past, there is a mailing list for this book wherereaders can discuss and get help with what they are reading.
9 The address This mailing list is open for any type of question ordiscussion along the lines of this book. You can subscribe to this list by going ToolsThis book teaches assembly language for x86 processors and the GNU/Linux operating we will be giving all of the examples using the GNU/Linux standard GCC tool set. Ifyou are not familiar with GNU/Linux and the GCC tool set, they will be described shortly. If youare new to Linux, you should check out the guide available at Iintend to show you is more about programming in general than using a specific tool set on aspecific platform, but standardizing on one makes the task much new to Linux should also try to get involved in their local GNU/Linux User s s Group members are usually very helpful for new people, and will help you fromeverything from installing Linux to learning to use it most efficiently.
10 A listing of GNU/LinuxUser s Groups is available at of these programs have been tested using Red Hat Linux , and should work with any is quite a large document. You certainly don t need to know everything to get started with this simply need to know how to navigate from the command line and how to use an editor likepico,emacs,orvi(or others).2 Chapter 1. IntroductionGNU/Linux distribution, will not work with non-Linux operating systems such asBSD or other systems. However, all of theskillslearned in this book should be easily transferableto any other you do not have access to a GNU/Linux machine, you can look for a hosting provider whooffers a Linuxshell account, which is a command-line only interface to a Linux machine.