write great code - volume ii - thinking low-level writing high-level

642 265 0
write great code - volume ii - thinking low-level writing high-level

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PRAISE FOR WRITE GREAT CODE, VOLUME 1: UNDERSTANDING THE MACHINE “If you are programming without benefit of formal train- ing, or if you lack the aegis of a mentor, Randall Hyde’s Write Great Code series should rouse your interest.” —UnixReview.com No prior knowledge of assembly language required! In the beginning, most software was written in assembly, the CPU’s low-level language, in order to achieve acceptable performance on relatively slow hardware. Early programmers were sparing in their use of high-level language code, knowing that a high-level language com- piler would generate crummy low-level machine code for their software. Today, however, many programmers write in high-level languages like C, C++, Pascal, Java, or BASIC. The result is often sloppy, inefficient code. Write Great Code, Volume 2 helps you avoid this common problem and learn to write well-structured code. In this second volume of the Write Great Code series, you’ll learn: • How to analyze the output of a compiler to verify that your code does, indeed, generate good machine code • The types of machine code statements that compilers typically generate for common control structures, so you can choose the best statements when writing HLL code • Just enough x86 and PowerPC assembly language to read compiler output • How compilers convert various constant and variable objects into machine data, and how to use these objects to write faster and shorter programs You don’t need to give up the productivity and portability of high-level languages in order to produce more efficient software. With an understanding of how compilers work, you’ll be able to write source code that they can translate into elegant machine code. That understanding starts right here, with Write Great Code: Thinking Low-Level, Writing High-Level. About the author Randall Hyde is the author of The Art of Assembly Language, one of the most highly recommended resources on assembly, and Write Great Code, Volume 1 (both No Starch Press). He is also the co-author of The Waite Group’s MASM 6.0 Bible. He has written for Dr. Dobb’s Journal and Byte, as well as professional and academic journals. Get better re s u lts from your source code Get better re s u lts from your source code www.nostarch.com “I lay flat.” This book uses RepKover—a durable binding that won’t snap shut. THE FI N E ST IN GEE K E N TE RT AIN M E NT ™ SHELVE IN: PROGRAMMING $44.95 ($58.95 CDN) 6 8 9 1 4 5 7 0 6 5 8 1 5 4 4 9 5 9 7 81 5 9 3 2 7 0 65 0 ISBN: 1-59327-065-8 H YD E W R I TE GR E AT CODE W R I TE GR E AT CODE V O L U M E 2 : T H I N K I N G L O W - L E V E L , W R I T I N G H I G H - L E V E L R a n d a l l H y d e W R I T E G R E A T C ODE W R I T E G R E A T C ODE V O L U M E 2 : T H I N K I N G L O W - L E V E L , W R I T I N G H I G H - L E V E L 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.” — UNIX REVIEW “The book explains in detail what most programmers take for granted.” COMPUTER SHOPPER (UK) “Great fun to read.” — VSJ MAGAZINE “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, AFTER GIVING IT A 5 OUT OF 5 STARS RATING “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.” — PRACTICAL APPLICATIONS “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.” — BAY AREA LARGE INSTALLATION SYSTEM ADMINISTRATORS (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; info@nostarch.com; 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 BRIEF CONTENTS 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 Class Data Types 341 vi Brief Contents 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 the 80x86 and PowerPC CPU Families 581 Online Appendices 589 Index 591 CONTENTS IN DETAIL 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 viii Contents in Detail 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 [...]... of great code: Great code uses the CPU efficiently (that is, the code is fast) Great code uses memory efficiently (that is, the code is small) Great code uses system resources efficiently Great code is easy to read and maintain Great code follows a consistent set of style guidelines 8 C h a pt er 1 Great code uses an explicit design that follows established software engineering conventions Great code. .. machine code 1.5 Writing High-Level One problem with thinking in low-level terms while writing high-level code is that it is almost as much work to write HLL code in this fashion as it is to write assembly code This negates many of the familiar benefits of writing programs in HLLs such as faster development time, better readability, easier maintenance, and so on If you’re sacrificing the benefits of writing. .. http://en.widipedia.org/wiki/Software_optimization In t rod uc ti on xix 1 THINKING LOW-LEVEL, WRITING HIGH-LEVEL “If you want to write the best high-level language code, learn assembly language.” —Common programming advice This book doesn’t teach anything revolutionary It describes a time-tested, well-proven approach to writing great code to make sure you understand how the code you write will actually execute on a real machine... consider the low-level execution of your high-level programs How compilers generate machine code from high-level language (HLL) statements How compilers represent various data types using low-level, primitive, data types How to write your HLL code to help the compiler produce better machine code How to take advantage of a compiler’s optimization facilities How to “think” in assembly language (low-level terms)... on the quality of the code they expect the compiler to produce 6 C h a pt er 1 By thinking in low-level terms when writing HLL code, a programmer can help an optimizing compiler approach the code quality level achieved by hand-optimized assembly language code Sadly, the converse is usually True as well: if a programmer does not consider the low-level ramifications of his HLL code, the compiler will... programmers writing code in high-level languages like C can come close if they write their HLL code in a manner that allows the compiler to easily translate the program into efficient machine code So, the real question is “How do I write my HLL code so that the compiler can translate it most efficiently?” Well, answering that question is the subject of this book But the short answer is “Think in assembly; write. .. some efficiency that it wouldn’t otherwise have Once the code is written, you won’t have to constantly think about it in low-level terms during the maintenance and enhancement phases of the software life cycle Therefore, thinking in low-level terms during the initial software development stage will retain the advantages of both low-level and high-level coding (efficiency plus ease of maintenance) without... of code written by younger programmers who’ve never really mastered this material, they might even write it off This book (and Volume 1 of this series) attempts to fill the gaps in the education of the current generation of programmers, so they can write quality code, too This particular volume of the Write Great Code series will teach you the following concepts: Why it’s important to consider the low-level. .. possible while others may feel that great code is written quickly Still others may feel that great code is created on time and under budget And you can think of additional characteristics So what is great code? Here is a reasonable definition: 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... implementing an algorithm as source code This book will concentrate on some of the efficiency aspects of writing great code Although efficiency might not always be the primary goal of a software development effort, most people will generally agree that inefficient code is not great code This does not suggest that code isn’t great if it isn’t as efficient as possible However, code that is grossly inefficient . ADMINISTRATORS (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. 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. sloppy, inefficient code. Write Great Code, Volume 2 helps you avoid this common problem and learn to write well-structured code. In this second volume of the Write Great Code series, you’ll

Ngày đăng: 29/04/2014, 14:56

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan