GGeett bbeetttteerr rreessuullttss ffrroomm yyoouurr ssoouurrccee ccooddee VWW WW RR II TT EE GG RR EE AATT O L U MRR E CC OO DD EE 2 PRAISE FOR WRITE GREAT CODE, VOLUME 1: UNDERSTANDING THE MACHINE • The types of machine code statements that compilers :II “If you are programming without benefit of formal train- typically generate for common control structures, so you THTT ing, or if you lack the aegis of a mentor, Randall Hyde’s can choose the best statements when writing HLL code I Write Great Code series should rouse your interest.” NEE K • Just enough x86 and PowerPC assembly language to —UnixReview.com I read compiler output N GGG No prior knowledge of • How compilers convert various constant and L assembly language required! O variable objects into machine data, and how to use WRR these objects to write faster and shorter programs - In the beginning, most software was written in assembly, L the CPU’s low-level language, in order to achieve You don’t need to give up the productivity and EVEE V O L U M E 2 : T H I N K I N G L O W - L E V E L , acceptable performance on relatively slow hardware. portability of high-level languages in order to produce EL Early programmers were sparing in their use of high-level more efficient software. With an understanding of how ,AA W R I T I N G H I G H - L E V E L W language code, knowing that a high-level language com- compilers work, you’ll be able to write source code RTT piler would generate crummy low-level machine code for that they can translate into elegant machine code. That I T their software. Today, however, many programmers write understanding starts right here, with Write Great Code: I NCC in high-level languages like C, C++, Pascal, Java, or Thinking Low-Level, Writing High-Level. G BASIC. The result is often sloppy, inefficient code. Write H Great Code, Volume 2 helps you avoid this common About the author IOO G problem and learn to write well-structured code. H Randall Hyde is the author of The Art of Assembly -DD L In this second volume of the Write Great Code series, Language, one of the most highly recommended E you’ll learn: resources on assembly, and Write Great Code, Volume VEEE 1 (both No Starch Press). He is also the co-author of L • How to analyze the output of a compiler to verify that The Waite Group’s MASM 6.0 Bible. He has written your code does, indeed, generate good machine code for Dr. Dobb’s Journal and Byte, as well as professional and academic journals. H Y D E $44.95 ($58.95 CDN) wTHwE wF.InNoEsStTa rIcNh G.cEoEmK ENTERTAINMENT™ ISBN: 1-59327-065-8 54495 SHELVE IN:OGRAMMING PR Randall Hyde “I lay flat.” 9 781593 270650 6 89145 70658 1 This book uses RepKover—a durable binding that won’t snap shut. PRAISE FOR WRITE GREAT CODE, VOLUME 1: UNDERSTANDING THE MACHINE “If you are programming without benefit of formal training, or if you lack the aegis of a mentor, Randall Hyde’s Write Great Code series should rouse your interest . . . The first five chapters and the Boolean Logic chapter are worth the price of the book.” —UNIXREVIEW “The book explains in detail what most programmers take for granted.” --COMPUTERSHOPPER (UK) “Great fun to read.” —VSJMAGAZINE “Not for the novice, this book details the innermost workings of the machine at a very complex level. Programmers who are interested in working at that level will the find the book most helpful.” —SECURITYITWORLD.COM “It fills in the blanks nicely and really could be part of a Computer Science degree required reading set. . . . Once this book is read, you will have a greater understanding and appreciation for code that is written efficiently—and you may just know enough to do that yourself." —MACCOMPANION,AFTERGIVINGITA 5 OUTOF 5 STARSRATING “Write Great Code: Understanding the Machine should be on the required reading list for anyone who wants to develop terrific code in any language without having to learn assembly language.” —WEBSERVERTALK “Hyde is taking on a topic that continues to lie at the core of all development, the foundations of computer architecture.” —PRACTICALAPPLICATIONS “Isn’t your typical ‘teach yourself to program’ book. . . . It’s relevant to all languages, and all levels of programming experience. . . . Run, don’t walk, to buy and read this book.” —BAYAREALARGEINSTALLATIONSYSTEMADMINISTRATORS (BAYLISA) WRITE GREAT CODE Volume 2: Thinking Low- Level, Writing High-Level by Randall Hyde San Francisco WRITE GREAT CODE, Vol. 2: Thinking Low-Level, Writing High-Level. Copyright © 2006 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. Printed on recycled paper in the United States of America 1 2 3 4 5 6 7 8 9 10 – 09 08 07 06 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: Elizabeth Campbell Cover and Interior Design: Octopod Studios Developmental Editor: Jim Compton Technical Reviewer: Benjamin David Lunt Copyeditor: Kathy Grider-Carlyle 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; [email protected]; 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 Cataloging-in-Publication Data (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 B R I E F C O N T E N T S Acknowledgments..........................................................................................................xv Introduction.................................................................................................................xvii Chapter 1: Thinking Low-Level, Writing High-Level ..............................................................1 Chapter 2: Shouldn’t You Learn Assembly Language? .......................................................11 Chapter 3: 80x86 Assembly for the HLL Programmer ........................................................21 Chapter 4: PowerPC Assembly for the HLL Programmer ....................................................47 Chapter 5: Compiler Operation and Code Generation .....................................................61 Chapter 6: Tools for Analyzing Compiler Output ............................................................115 Chapter 7: Constants and High-Level Languages ............................................................165 Chapter 8: Variables in a High-Level Language .............................................................189 Chapter 9: Array Data Types .......................................................................................241 Chapter 10: String Data Types ....................................................................................281 Chapter 11: Pointer Data Types ...................................................................................315 Chapter 12: Record, Union, and ClassData Types ........................................................341 Chapter 13: Arithmetic and Logical Expressions .............................................................385 Chapter 14: Control Structures and Programmatic Decisions ...........................................439 Chapter 15: Iterative Control Structures .........................................................................489 Chapter 16: Functions and Procedures ..........................................................................521 Engineering Software .................................................................................................579 Appendix: A Brief Comparison of the80x86 and PowerPC CPU Families .........................581 Online Appendices.....................................................................................................589 Index.........................................................................................................................591 vi Brief Contents C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS xv INTRODUCTION xvii 1 THINKING LOW-LEVEL, WRITING HIGH-LEVEL 1 1.1 Misconceptions About Compiler Quality..............................................................2 1.2 Why Learning Assembly Language Is Still a Good Idea.........................................2 1.3 Why Learning Assembly Language Isn’t Absolutely Necessary................................3 1.4 Thinking Low-Level.............................................................................................3 1.4.1 Compilers Are Only as Good as the Source Code You Feed Them..........4 1.4.2 Helping the Compiler Produce Better Machine Code.............................4 1.4.3 How to Think in Assembly While Writing HLL Code..............................5 1.5 Writing High-Level............................................................................................7 1.6 Assumptions.....................................................................................................7 1.7 Language-Neutral Approach..............................................................................8 1.8 Characteristics of Great Code............................................................................8 1.9 The Environment for This Text..............................................................................9 1.10 For More Information.....................................................................................10 2 SHOULDN’T YOU LEARN ASSEMBLY LANGUAGE? 11 2.1 Roadblocks to Learning Assembly Language.......................................................12 2.2 Write Great Code, Volume 2, to the Rescue.......................................................12 2.3 High-Level Assemblers to the Rescue..................................................................13 2.4 The High-Level Assembler (HLA)........................................................................14 2.5 Thinking High-Level, Writing Low-Level...............................................................15 2.6 The Assembly Programming Paradigm (Thinking Low-Level)..................................16 2.7 The Art of Assembly Language and Other Resources...........................................18 3 80X86 ASSEMBLY FOR THE HLL PROGRAMMER 21 3.1 Learning One Assembly Language Is Good, Learning More Is Better.....................22 3.2 80x86 Assembly Syntaxes...............................................................................22 3.3 Basic 80x86 Architecture.................................................................................23 3.3.1 Registers........................................................................................23 3.3.2 80x86 General-Purpose Registers.....................................................24 3.3.3 The 80x86 EFLAGS Register.............................................................25 3.4 Literal Constants..............................................................................................26 3.4.1 Binary Literal Constants....................................................................26 3.4.2 Decimal Literal Constants.................................................................27 3.4.3 Hexadecimal Literal Constants..........................................................27 3.4.4 Character and String Literal Constants...............................................28 3.4.5 Floating-Point Literal Constants..........................................................29 3.5 Manifest (Symbolic) Constants in Assembly Language..........................................30 3.5.1 Manifest Constants in HLA...............................................................30 3.5.2 Manifest Constants in Gas...............................................................30 3.5.3 Manifest Constants in MASM and TASM...........................................31 3.6 80x86 Addressing Modes...............................................................................31 3.6.1 80x86 Register Addressing Modes...................................................31 3.6.2 Immediate Addressing Mode............................................................32 3.6.3 Displacement-Only Memory Addressing Mode...................................33 3.6.4 Register Indirect Addressing Mode....................................................35 3.6.5 Indexed Addressing Mode...............................................................36 3.6.6 Scaled-Indexed Addressing Modes....................................................38 3.7 Declaring Data in Assembly Language..............................................................39 3.7.1 Data Declarations in HLA.................................................................40 3.7.2 Data Declarations in MASM and TASM.............................................41 3.7.3 Data Declarations in Gas.................................................................41 3.8 Specifying Operand Sizes in Assembly Language...............................................44 3.8.1 Type Coercion in HLA.....................................................................44 3.8.2 Type Coercion in MASM and TASM.................................................45 3.8.3 Type Coercion in Gas.....................................................................45 3.9 The Minimal 80x86 Instruction Set....................................................................46 3.10 For More Information.....................................................................................46 4 POWERPC ASSEMBLY FOR THE HLL PROGRAMMER 47 4.1 Learning One Assembly Language Is Good; More Is Better..................................48 4.2 Assembly Syntaxes..........................................................................................48 4.3 Basic PowerPC Architecture..............................................................................49 4.3.1 General-Purpose Integer Registers.....................................................49 4.3.2 General-Purpose Floating-Point Registers............................................49 4.3.3 User-Mode-Accessible Special-Purpose Registers.................................49 4.4 Literal Constants..............................................................................................52 4.4.1 Binary Literal Constants....................................................................52 4.4.2 Decimal Literal Constants.................................................................53 4.4.3 Hexadecimal Literal Constants..........................................................53 4.4.4 Character and String Literal Constants...............................................53 4.4.5 Floating-Point Literal Constants..........................................................53 4.5 Manifest (Symbolic) Constants in Assembly Language..........................................54 4.6 PowerPC Addressing Modes............................................................................54 4.6.1 PowerPC Register Access.................................................................54 4.6.2 The Immediate Addressing Mode......................................................54 4.6.3 PowerPC Memory Addressing Modes................................................55 4.7 Declaring Data in Assembly Language..............................................................56 4.8 Specifying Operand Sizes in Assembly Language...............................................59 4.9 The Minimal Instruction Set...............................................................................59 4.10 For More Information.....................................................................................59 viii Contents in Detail