San Francisco
Copyright 2004 Randall Hyde.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
1 2 3 4 5 6 7 8 9 10 - 07 06 05 04
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners . Rather than use a trademark symbol with every occurrence of a trademarked name , we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Publisher: William Pollock
Managing Editor: Karol Jurado
Cover and Interior Design: Octopod Studios
Developmental Editor: Hillel Heinstein
Technical Reviewer: Mark de Wever
Copyeditor: Andy Carroll
Compositor: Riley Hoffman
Proofreader: Stephanie Provines
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
555 De Haro Street, Suite 250, San Francisco, CA 94107
phone: 415-863-9900; fax: 415-863-9950; info @nostarch.com; http://www.nostarch.com
The information in this book is distributed on an 'As Is' basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
Library of Congress Cataloguing-in-Publication Data
Hyde, Randall.
Write great code : understanding the machine / Randall Hyde.
p. cm.
1. Computer programming. 2. Computer architecture. I. Title.
QA76.6.H94 2004
005.1--dc22
2003017502
Acknowledgments
A book such as the one you are now holding is rarely the work of one person, even if only one name appears on the cover. Producing this book has been a team effort, and I would like to take this opportunity to acknowledge the other individuals who have contributed greatly to its quality.
Mary Philips, a wonderful friend who helped proofread several of the earlier chapters.
Bill Pollock, who read and offered suggestions for Chapters 1 through 6.
Karol Jurado, my editor, who shepherded this project from conception toproduction.
Hillel Heinstein, the developmental editor, who kept this book on the right track and helped clean up the writing.
Andy Carroll, the copyeditor, who also helped improve my writing.
Mark de Wever, the technical reviewer, who caught a large number of little typos and technical problems to help ensure the accuracy of the material.
Riley Hoffman, who handled the page layout chores and helped ensure that the book (including the listings) was readable.
Stephanie Provines, whose proofreading caught several typographical and layout errors.
Leigh Sacks, who has done a great job of marketing this book and my earlier book, The Art of Assembly Language .
And of course, all the great people at No Starch Press who've been supportive of this project from the very beginning.
Last, but not least, I would like to thank my wife, Mandy, who allowed meto get away with not spending as much time working around the house asI should have, so that I could get this book out the door.
Thanks to all of you,
Randall Hyde
Thinking Low-Level, Writing High-Level
The goal of this volume, Understanding the Machine , was to get you thinking at the level of the machine. Of course, one way to force yourself to write code at the machine level is to write your applications in assembly language. When you've got to write code statement by statement in assembly language, you're going to have a pretty good idea of the cost associated with every statement.
Unfortunately, using assembly language isn't a realistic solution for most applications. The disadvantages of assembly language have been well publicized (and blown out of proportion) over the past several decades, so most people are quite familiar with the drawbacks, real or imagined. Assembly just isn't an option for most people.
Though writing code in assembly language forces you to think down at the machine level, writing code in a high-level language does not force you to think at a high level of abstraction. There is nothing preventing you from thinking in low-level terms while writing high-level code. The goal of this book was to provide you with the background knowledge you need to do exactly that - think in low-level terms while writing high-level code. By learning how the computer represents data, you've learned how high-level language data types translate to the machine level. By learning how the CPU executes machine instructions, you've learned the costs of various operations in your high-level language applications. By learning about memory performance, you've learned how to organize your high-level language variables and other data to maximize cache and memory access. There's only one piece missing from this puzzle: 'Exactly how does a particular compiler map high-level language statements to the machine level?' Unfortunately, that topic is sufficiently large that it deserves an entire book on its own. And that's the purpose of the next volume in the Write Great Code series: Thinking Low-Level, Writing High-Level .
Write Great Code: Thinking Low-Level, Writing High-Level will pick up right where this volume leaves off. Thinking Low-Level, Writing High-Level will teach you how each statement in a typical high-level language maps to machine code, how you can choose between two or more high-level sequences to produce the best possible machine code, and how to analyze that machine code to determine its quality and the quality of the high-level code that produced it. And while doing all of this, it will give you a greater appreciation of how compilers do their job and how you can assist them in doing their job better.
Congratulations on your progress thus far towards knowing how to write great code. See you in volume 2.