Assembly Language Stepby-Step: Programming with DOS and Linux, Second Edition ISBN:0471375233 by Jeff Duntemann John Wiley & Sons © 2000 (613 pages) A “Lost World” journey into 16-bit assembler programming concepts and techniques Table of Contents Assembly Language Step-by-Step— Programming with DOS and Linux, Second Edition Foreword "Why Would You Want to Introduction Do That?" Another Pleasant Valley Saturday Understanding Chapter 1 What Computers Really Do Alien Bases Getting Your Chapter 2 - Arms around Binary and Hexadecimal Lifting the Hood Chapter 3 - Discovering What Computers Actually Are The Right to Assemble The Process of Making Chapter 4 Assembly Language Programs NASM-IDE: A Place to Stand Give me a lever Chapter 5 - long enough, and a place to stand, and I will move the Earth An Uneasy Alliance The x86 CPU and Its Chapter 6 Segmented Memory System Following Your Instructions Meeting Chapter 7 Machine Instructions up Close and Personal Our Object All Sublime Chapter 8 - Creating Programs that Work Dividing and Conquering Using Procedures and Chapter 9 Macros to Battle Complexity Bits, Flags, Branches, and Tables Easing into Chapter 10 Mainstream Assembly Programming Stringing Them Up Chapter 11 - Those Amazing String Chapter 12 Chapter 13 Conclusion Appendix A Appendix B Appendix C Appendix D Instructions The Programmer's View of Linux Tools and Skills - to Help You Write Assembly Code under a True 32-Bit OS Coding for Linux Applying What You've - Learned to a True Protected Mode Operating System Not the End, But Only the Beginning Partial 8086/8088 - Instruction Set Reference Segment Register - Assumptions for Real Mode Segmented Model Web URLs for Assembly Programmers Segment Register Assumptions - What's on the CD-ROM? Appendix E Index List of Figures List of Tables Back Cover The bestselling guide to assembly language now updated and expanded to include coverage of Linux This new edition of the bestselling guide to assembly programming now covers DOS and Linux! The Second Edition begins with a highly accessible overview of the internal operations of the Intel-based PC and systematically covers all the steps involved in writing, testing, and debugging assembly programs Expert author Jeff Duntemann then presents working example programs for both the DOS and Linux operating systems using the popular free assembler NASM He also inlcudes valuable infomation on how to use procedures and macros, plus rare explanations of assembly-level coding for Linux, all of which combine to offer a comprehensive look at the complexitites of assembly programming for Intel processors Providing you with the foundation to create executable assembly language programs, this book: Explains how to use NASM-IDE, a simple program editor and assembly-oriented development environment Details the most used elements of the 86-family instruction set Teaches about DEBUG, the single most useful tool you have as an assembly language programmer Examines the operations that machine instructions force the CPU to perform Discusses the process of memory addressing Covers coding for Linux About the Author Jeff Duntemann is the Editor-in-Chief of Visual Developer magazine, former editor of Turbo Technix and PC Techniques, the "Structured Programming" columnist for Dr Dobb’s Journal, and has written and edited more than twenty programming books Assembly Language Step-by-Step— Programming with DOS and Linux, Second Edition Jeff Duntemann Wiley Computer Publishing John Wiley & Sons, Inc NEW YORK • CHICHESTER • WEINHEIM • BRISBANE • SINGAPORE • TORONTO Publisher: Robert Ipsen Editor: Cary Sullivan Managing Editor: Micheline Frederick Text Design & Composition: North Market Street Graphics Designations used by companies to distinguish their products are often claimed as trademarks In all instances where John Wiley & Sons, Inc., is aware of a claim, the product names appear in initial capital or ALL CAPITAL LETTERS Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration Copyright © 2000 by Jeff Duntemann All rights reserved Published by John Wiley & Sons, Inc Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4744 Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 605 Third Avenue, New York, NY 10158-0012, (212) 850-6011, fax (212) 850-6008, E-Mail: This publication is designed to provide accurate and authoritative information in regard to the subject matter covered It is sold with the understanding that the publisher is not engaged in professional services If professional advice or other expert assistance is required, the services of a competent professional person should be sought Library of Congress Cataloging-in-Publication Data: Duntemann, Jeff Assembly language step-by-step : programming with DOS and Linux / Jeff Duntemann.—2nd ed p cm Rev ed of: Assembly language, © 1992 ISBN 0-471-37523-3 (paper/CD-ROM : alk paper) Assembler language (Computer program language)I Duntemann, Jeff Assembly language.II Title QA76.73.A8 D88 2000 005.265—dc21 00-020611 10 9 8 7 6 5 4 3 2 1 To the eternal memory of Kathleen M Duntemann, Godmother 1920–1999 who gave me books when all I could do was put teeth marks on them There are no words for how much I owe you! Acknowledgments First of all, to the authors of the software that I am honored to provide by their permission on this book's CD-ROM: Robert Anderton, author of NASM-IDE: www.inglenook.co.uk/nasmide/ Anthony Williams, author of ALINK: http://alink.home.dhs.org/ And, of course, the NASM team: Julian "Jules" Hall, Simon Tatham, H Peter Anvin, John Fine, Kendall Bennet, Gary Clark, and Andrew Crabtree: www.web-sites.co.uk/nasm/ Because of their generosity, there is "nothing else to buy." Everything you need to program in assembly is right here on the CD-ROM for this book Abundant thanks also go to Bill Schindler, for teaching me the ways of the C library, and Michael Abrash, who has always has been the rock upon whom my knowledge of assembly language itself has been anchored Finally, to Carol, as always, for the sacramental friendship that has challenged me, shaped me, and redeemed me every day of our 30 years together and 23 years as spouses Foreword Time passes It was exactly 10 years ago this summer, back in July 1989, when I turned in the manuscript of a book called Assembly Language from Square One The book was well received, but its publisher went belly-up only a few months after its introduction That may have been a blessing, because the book was too short, had a few more errors in it than it should have had, and was printed on horrible cheap paper that ripped with almost no provocation and is now turning sickly yellow So, I leapt on the chance to do the book over and publish it with a real publisher, the most venerable John Wiley & Sons, who (as their T-shirts say) has been totally awesome since 1809 It was thoroughly rewritten and became a new book with a new title, and went on the shelves in September of 1992 Time passes, but in a world where the life of a computer book may well be eight months or less, Wiley kept the first edition of Assembly Language Step-by-Step in print for eight years, from 1992 to 2000 In that time it has probably sold more copies than any other single assembly language book, and I've received hundreds of letters of advice, corrections, suggestions, and simple, "Hey, this is cool!" compliments Thanks to you all for taking the time to write It means a lot to me It's unclear how long this second edition will remain in print, but as long as people keep buying it (and telling me it's been useful to them), I suspect that either this edition or one to follow will remain available Time passes And before we get into the book proper, there's something else I wanted to relate On July 8, 1999, my sister Gretchen Duntemann Roper found that Kathleen Duntemann had died peacefully in her sleep in Chicago, almost 10 years to the day since I had completed Assembly Language from Square One, which was also dedicated to her She kept both books on her coffee table and would show them to anyone who came to visit, even though she never had a computer and probably never understood what assembly language was She was my aunt and godmother, my father's sole sibling, who sang my ABCs to me and demanded that I be admitted to Adler Planetarium in Chicago when I was six, even though the rules at that time demanded that children be seven to attend the sky show "Name the planets for the nice man," she told me, and I did, and when I had gone through all the planets I started in on the constellations I got in, because she believed in me And she was there through every other major milestone in my life: First Communion, Confirmation, wedding, my father's illness and death, years and years of Christmases and Thanksgivings and birthdays, always with treats for the dog and stories to tell, with a quick Irish wit and a generous heart-and truly I cannot and will not ever forget her I say this only because so many of you are considerably younger than I, and may forget in the fever of young life: Time passes, and so do the people who believe in us, and urge us through the walls as we hit them so that we may arrive at midlife with something to show for it Fathers and mothers, grandparents, aunts and uncles can add immeasurably to our lives, and often do, even when we're too busy to notice Cherish them while you have them, because cherishing them after they're gone is a lonely business indeed In the meantime, having been talking about assembly language in one book or another for 10 years, I've decided to make it 20 As long as there will be PCs, there will be assembly language Stay tuned The year 2009 will be here before you know it Chapter 6: An Uneasy Alliance The x86 CPU and Its Segmented Memory System Figure 6.1: The 8080 memory model Figure 6.2: The 8080 memory model inside an 8086 memory system Figure 6.3: Seeing a megabyte through 64K blinders Figure 6.4: Memory addresses versus segment addresses Figure 6.5: Segments and offsets Figure 6.6: Extending the 16-bit general-purpose registers Figure 6.7: 8-bit, 16-bit, and 32-bit registers Figure 6.8: The real mode flat model Figure 6.9: The real mode segmented model Figure 6.10: The protected mode flat model Figure 6.11: The PC's video refresh buffer Chapter 7: Following Your Instructions Meeting Machine Instructions up Close and Personal Figure 7.1: How memory data is addressed Chapter 8: Our Object All Sublime Creating Programs that Work Figure 8.1: The big picture of the real mode stack Figure 8.2: How the stack works Figure 8.3: The interrupt vector table Figure 8.4: Riding the interrupt vector into DOS Figure 8.5: Returning home from an interrupt Chapter 9: Dividing and Conquering Using Procedures and Macros to Battle Complexity Figure 9.1: Calling a procedure and returning Figure 9.2: Connecting globals and externals Figure 9.3: How macros work Chapter 10: Bits, Flags, Branches, and Tables Easing into Mainstream Assembly Programming Figure 10.1: Bit numbering Figure 10.2: The anatomy of an AND instruction Figure 10.3: Using XOR to zero a register Figure 10.4: Using a lookup table Figure 10.5: Interrupt 11H configuration information Chapter 11: Stringing Them Up Those Amazing String Instructions Figure 11.1: Unpacked BCD digits Chapter 12: The Programmer's View of Linux Tools and Skills to Help You Write Assembly Code under a True 32-Bit OS Figure 12.1: How gcc builds Linux executables Figure 12.2: The structure of a Linux assembly language program Chapter 13: Coding for Linux Applying What You've Learned to a True Protected Mode Operating System Figure 13.1: A stack frame Figure 13.2: Protected mode memory addressing Figure 13.3: Linux command-line arguments List of Tables Chapter 2: Alien Bases Getting Your Arms around Binary and Hexadecimal Table 2.1: Counting in Martian, Base Fooby Table 2.2: Powers of Fooby Table 2.3: Counting in Octal, Base 8 Table 2.4: Octal Columns as Powers of Eight Table 2.5: Counting in Hexadecimal, Base 16 Table 2.6: Hexadecimal Columns as Powers of 16 Table 2.7: Binary Columns as Powers of 2 Chapter 6: An Uneasy Alliance The x86 CPU and Its Segmented Memory System Table 6.1: Collective Terms for Memory Chapter 7: Following Your Instructions Meeting Machine Instructions up Close and Personal Table 7.1: MOV and Its Operands Table 7.2: Segment Override Prefixes Table 7.3: Rogue MOV Instructions Table 7.4: DEBUG's Flag State Symbols Chapter 10: Bits, Flags, Branches, and Tables Easing into Mainstream Assembly Programming Table 10.1: The AND Truth Table for Formal Logic Table 10.2: The AND Truth Table for Assembly Language Table 10.3: The OR Truth Table for Assembly Language Table 10.4: The XOR Truth Table for Assembly Language Table 10.5: The NOT Truth Table for Assembly Language Table 10.6: Arithmetic Jump Mnemonics and Their Synonyms Table 10.7: Arithmetic Tests Useful After a CMP Instruction Table 10.8: Legal PC Display Adapter/Monitor Combinations Chapter 12: The Programmer's View of Linux Tools and Skills to Help You Write Assembly Code under a True 32-Bit OS Table 12.1: Format Codes for gdb's Print and x Commands Table 12.2: Unit Size Codes for gdb's x Command Chapter 13: Coding for Linux Applying What You've Learned to a True Protected Mode Operating System Table 13.1: Protected Mode Memory-Addressing Schemes Table 13.2: Common printf Formatting Codes Table 13.3: The Values Contained in the tm Structure Table 13.4: File Access Codes for Use with fopen ... Dobb’s Journal, and has written and edited more than twenty programming books Assembly Language Step- by -Step Programming with DOS and Linux, Second Edition Jeff Duntemann Wiley Computer Publishing John Wiley & Sons, Inc... Duntemann, Jeff Assembly language step- by -step : programming with DOS and Linux / Jeff Duntemann. 2nd ed p cm Rev ed of: Assembly language, © 1992 ISBN 0-471-37523-3 (paper/CD-ROM : alk paper) Assembler language (Computer program language) I... now updated and expanded to include coverage of Linux This new edition of the bestselling guide to assembly programming now covers DOS and Linux! The Second Edition begins with a highly accessible overview of the