Set aside some money and buy this book, or get a friend to buy it and get it from them while still in the store. When you get home, read it TWICE so that you master what is in these pages. Then read it again.
As a high-level-language programmer, if you want to know whats really going on with your programs, you need to spend a little time learning assembly languageand you wont find an easier introduction.
This is a good book. A very, very good book. Frankly, Im blown away at the quality of writing.
WRITE GREAT CODE
VOLUME 2
2ND EDITION
Thinking Low-Level, Writing High-Level
by Randall Hyde
San Francisco
WRITE GREAT CODE, Volume 2: Thinking Low-Level, Writing High-Level, 2nd Edition.
Copyright 2020 by 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.
ISBN-10: 1-718-50038-6
ISBN-13: 978-1-71850-038-9
Publisher: William Pollock
Executive Editor: Barbara Yien
Production Editor: Rachel Monaghan
Developmental Editor: Athabasca Witschi
Project Editor: Dapinder Dosanjh
Cover and Interior Design: Octopod Studios
Technical Reviewer: Anthony Tribelli
Copyeditor: Rachel Monaghan
Compositor: Danielle Foster
Proofreader: James Fraleigh
Illustrator: David Van Ness
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;
www.nostarch.com
The Library of Congress issued the following Cataloging-in-Publication Data for the first edition of Volume 1:
Hyde, Randall.
Write great code : understanding the machine / Randall Hyde.
p. cm.
ISBN 1-59327-003-8
1. Computer programming. 2. Computer architecture. I. Title.
QA76.6.H94 2004
005.1--dc22
2003017502
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.
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.
About the Author
Randall Hyde is the author of The Art of Assembly Language and Write Great Code, Volumes 1, 2, and 3 (all from No Starch Press), as well as Using 6502 Assembly Language and P-Source (Datamost). He is also the coauthor of Microsoft Macro Assembler 6.0 Bible (The Waite Group). Over the past 40 years, Hyde has worked as an embedded software/hardware engineer developing instrumentation for nuclear reactors, traffic control systems, and other consumer electronics devices. He has also taught computer science at California State Polytechnic University, Pomona, and at the University of California, Riverside. His website is www.randallhyde.com/.
About the Technical Reviewer
Tony Tribelli has more than 35 years of experience in software development, including work on embedded device kernels and molecular modeling. He developed video games for 10 years at Blizzard Entertainment. He is currently a software development consultant and privately develops applications utilizing computer vision.
BRIEF CONTENTS
CONTENTS IN DETAIL
1
THINKING LOW-LEVEL, WRITING HIGH-LEVEL
2
SHOULDNT YOU LEARN ASSEMBLY LANGUAGE?
3
80X86 ASSEMBLY FOR THE HLL PROGRAMMER
4
COMPILER OPERATION AND CODE GENERATION
5
TOOLS FOR ANALYZING COMPILER OUTPUT
6
CONSTANTS AND HIGH-LEVEL LANGUAGES
7
VARIABLES IN A HIGH-LEVEL LANGUAGE
8
ARRAY DATA TYPES
9
POINTER DATA TYPES
10
STRING DATA TYPES
11
RECORD, UNION, AND CLASS DATA TYPES
12
ARITHMETIC AND LOGICAL EXPRESSIONS
13
CONTROL STRUCTURES AND PROGRAMMATIC DECISIONS
14
ITERATIVE CONTROL STRUCTURES
15
FUNCTIONS AND PROCEDURES
ACKNOWLEDGMENTS
Many people have read and reread every word, symbol, and punctuation mark in this book in order to produce a better result. Kudos to the following people for their careful work on the second edition: development editor Athabasca Witschi, copyeditor/production editor Rachel Monaghan, and proofreader James Fraleigh.
I would like to take the opportunity to graciously thank Anthony Tribelli, a longtime friend, who went well beyond the call of duty when doing a technical review of this book. He pulled every line of code out of this book (including snippets) and compiled and ran it to make sure it worked properly. His suggestions and opinions throughout the technical review process have dramatically improved the quality of this work.
Of course, I would also like to thank all the countless readers over the years whove emailed suggestions and corrections, many of which have found their way into this second edition.
Thanks to all of you,
Randall Hyde
INTRODUCTION
What do we mean by great code? Different programmers will have different opinions. Therefore, it is impossible to provide an all-encompassing definition that will satisfy everyone. Here is the definition this book will use:
Great code is software that is written using a consistent and prioritized set of good software characteristics. In particular, great code follows a set of rules that guide the decisions a programmer makes when implementing an algorithm as source code.
However, as I noted in Write Great Code, Volume 1: Understanding the Machine (hereafter, WGC1), there are some attributes of great code that nearly everyone can agree on. Specifically, great code: