THE INTEL MICROPROCESSORS 8086/8088, 80186/80188, 80286, 80386, 80486, Pentium, Pentium Pro Processor, Pentium II, Pentium III, Pentium 4, and Core2 with 64-Bit Extensions Architecture, Programming, and Interfacing Eighth Edition BARRY B. BREY Upper Saddle River, New Jersey Columbus, Ohio Library of Congress Cataloging in Publication Data Brey, Barry B. The Intel microprocessors 8086/8088, 80186/80188, 80286, 80386, 80486, Pentium, Pentium Pro processor, Pentium II, Pentium III, Pentium 4, and Core2 with 64-bit extensions: architecture, programming, and interfacing / Barry B. Brey—8th ed. p. cm. Includes index. ISBN 0-13-502645-8 1. Intel 80xxx series microprocessors. 2. Pentium (Microprocessor) 3. Computer interfaces. I. Title. QA76.8.I292B75 2009 004.165—dc22 2008009338 Editor in Chief:Vernon Anthony Acquisitions Editor:Wyatt Morris Editorial Assistant:Christopher Reed Production Coordination:GGS Book Services Project Manager:Jessica Sykes Operations Specialist:Laura Weaver Design Coordinator:Mike Fruhbeis Cover Designer:Ilze Lemesis Cover image: iStockphoto Director of Marketing:David Gesell Marketing Manager:Jimmy Stephens Marketing Assistant:Les Roberts This book was set in Times by GGS Book Services. It was printed and bound by Hamilton Printing. The cover was printed by Phoenix Color Corp. Copyright © 2009, 2006, 2003, 2000, 1997, 1994, 1991, 1987 by Pearson Education, Inc., Upper Saddle River, New Jersey 07458. Pearson Prentice Hall. All rights reserved. Printed in the United States of America. This publication is protected by Copyright and per- mission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department. Pearson Prentice Hall™is a trademark of Pearson Education, Inc. Pearson®is a registered trademark of Pearson plc Prentice Hall®is a registered trademark of Pearson Education, Inc. Pearson Education Ltd., London Pearson Education Australia Pty. Limited Pearson Education Singapore Pte. Ltd. Pearson Education North Asia Ltd., Hong Kong Pearson Education Canada, Inc. Pearson Educación de Mexico, S.A. de C.V. Pearson Education—Japan Pearson Education Malaysia Pte. Ltd. 10 9 8 7 6 5 4 3 2 1 ISBN–13: 978–0–13–502645–8 ISBN–10: 0–13–502645–8 This text is dedicated to my progenies, Brenda (the programmer) and Gary (the veterinarian technician), and to my constant four-legged companions: Romy, Sassy, Sir Elton, Eye Envy, and Baby Hooter. iii This page intentionally left blank PREFACE This practical reference text is written for students who require a thorough knowledge of pro- gramming and interfacing of the Intel family of microprocessors. Today, anyone functioning or striving to function in a field of study that uses computers must understand assembly language programming, a version of C language, and interfacing. Intel microprocessors have gained wide, and at times exclusive, application in many areas of electronics, communications, and control systems, particularly in desktop computer systems. A major addition to this eighth edition explains how to interface C/C++ using Visual C++ Express, which is a free download from Microsoft, with assembly language for both the older DOS and the Windows environments. Many applications include Visual C++ as a basis for learning assembly language using the inline assembler. Updated sections that detail new events in the fields of microprocessors and micro- processor interfacing have been added. ORGANIZATION AND COVERAGE To cultivate a comprehensive approach to learning, each chapter begins with a set of objectives that briefly define its content. Chapters contain many programming applications and examples that illustrate the main topics. Each chapter ends with a numerical summary, which doubles as a study guide, and reviews the information just presented. Questions and problems are provided for reinforcement and practice, including research paper suggestions. This text contains many example programs using the Microsoft Macro Assembler program and the inline assembler in the Visual C++ environment, which provide a learning opportunity to program the Intel family of microprocessors. Operation of the programming environment includes the linker, library, macros, DOS function, BIOS functions, and Visual C/C++ program development. The inline assembler (C/C++) is illustrated for both the 16- and 32-bit program- ming environments of various versions of Visual C++. The text is written to use Visual C++ Express 2005 or 2008 as a development environment, but any version of Visual Studio can also be used with almost no change. This text also provides a thorough description of family members, memory systems, and various I/O systems that include disk memory, ADC and DAC, 16550 UART, PIAs, timers, key- board/display controllers, arithmetic coprocessors, and video display systems. Also discussed are v vi PREFACE the personal computer system buses (AGP, ISA, PCI, PCI Express, USB, serial ports, and parallel port). Through these systems, a practical approach to microprocessor interfacing can be learned. APPROACH Because the Intel family of microprocessors is quite diverse, this text initially concentrates on real mode programming, which is compatible with all versions of the Intel family of micro- processors. Instructions for each family member, which include the 80386, 80486, Pentium, Pentium Pro, Pentium II, Pentium III, and Pentium 4 processors, are compared and contrasted with those for the 8086/8088 microprocessors. This entire series of microprocessors is very sim- ilar, which allows more advanced versions and their instructions to be learned with the basic 8086/8088. Please note that the 8086/8088 are still used in embedded systems along with their updated counterparts, the 80186/80188 and 80386EX embedded microprocessor. This text also explains the programming and operation of the numeric coprocessor, MMX extension, and the SIMD extension, which function in a system to provide access to floating- point calculations that are important in control systems, video graphics, and computer-aided design (CAD) applications. The numeric coprocessor allows a program to access complex arithmetic operations that are otherwise difficult to achieve with normal microprocessor pro- gramming. The MMX and SIMD instructions allow both integer and floating-point data to be manipulated in parallel at very high speed. This text also describes the pin-outs and function of the 8086–80486 and all versions of the Pentium microprocessor. First, interfacing is explained using the 8086/8088 with some of the more common peripheral components. After explaining the basics, a more advanced emphasis is placed on the 80186/80188, 80386, 80486, and Pentium through Pentium 4 microprocessors. Coverage of the 80286, because of its similarity to the 8086 and 80386, is minimized so the 80386, 80486, and Pentium versions can be covered in complete detail. Through this approach, the operation of the microprocessor and programming with the advanced family members, along with interfacing all family members, provides a working and practical background of the Intel family of microprocessors. Upon completing a course using this text, you will be able to: 1. Develop software to control an application interface microprocessor. Generally, the software developed will also function on all versions of the microprocessor. This software also includes DOS-based and Windows-based applications. The main emphasis is on developing inline assembly and C++ mixed language programs in the Windows environment. 2. Program using MFC controls, handlers, and functions to use the keyboard, video display system, and disk memory in assembly language and C++. 3. Develop software that uses macro sequences, procedures, conditional assembly, and flow control assembler directives that are linked to a Visual C++ program. 4. Develop software for code conversions using lookup tables and algorithms. 5. Program the numeric coprocessor to solve complex equations. 6. Develop software for the MMX and SIMD extensions. 7. Explain the differences between the family members and highlight the features of each member. 8. Describe and use real and protected mode operation of the microprocessor. 9. Interface memory and I/O systems to the microprocessor. 10. Provide a detailed and comprehensive comparison of all family members and their software and hardware interfaces. 11. Explain the function of the real-time operating system in an embedded application. 12. Explain the operation of disk and video systems. 13. Interface small systems to the ISA, PCI, serial ports, parallel port, and USB bus in a personal computer system. PREFACE vii CONTENT OVERVIEW Chapter 1introduces the Intel family of microprocessors with an emphasis on the microprocessor- based computer system: its history, operation, and the methods used to store data in a microprocessor-based system. Number systems and conversions are also included. Chapter 2 explores the programming model of the microprocessor and system architecture. Both real and protected mode operations are explained. Once an understanding of the basic machine is grasped, Chapters 3 through 6 explain how each instruction functions with the Intel family of microprocessors. As instructions are explained, simple applications are presented to illustrate the operation of the instructions and develop basic programming concepts. Chapter 7introduces the use of Visual C/C++ Express with the inline assembler and sepa- rate assembly language programming modules. It also explains how to configure Visual C++ Express for use with assembly language applications. After the basis for programming is developed, Chapter 8 provides applications using the Visual C++ Express with the inline assembler program. These applications include programming using the keyboard and mouse through message handlers in the Windows environment. Disk filesare explained using the File class, as well as keyboard and video operations on a personal computer system through Windows. This chapter provides the tools required to develop virtually any program on a personal computer system through the Windows environment. Chapter 9introduces the 8086/8088 family as a basis for learning basic memory and I/O interfacing, which follow in later chapters. This chapter shows the buffered system as well as the system timing. Chapter 10explains memory interface using both integrated decoders and programmable logic devices using VHDL. The 8-, 16-, 32-, and 64-bit memory systems are provided so the 8086–80486 and the Pentium through Pentium 4 microprocessors can be interfaced to memory. Chapter 11 provides a detailed look at basic I/O interfacing, including PIAs, timers, the 16550 UART, and ADC/DAC. It also describes the interface of both DC and stepper motors. Once these basic I/O components and their interface to the microprocessor are understood, Chapters 12 and 13 provide detail on advanced I/O techniques that include interrupts and direct memory access (DMA). Applications include a printer interface, real-time clock, disk memory, and video systems. Chapter 14details the operation and programming for the 8087–Pentium 4 family of arith- metic coprocessors, as well as MMX and SIMD instructions. Today few applications function efficiently without the power of the arithmetic coprocessor. Remember that all Intel micro- processors since the 80486 contain a coprocessor; since the Pentium, an MMX unit; and since the Pentium II, an SIMD unit. Chapter 15shows how to interface small systems to the personal computer through the use of the parallel port, serial ports, and the ISA, and PCI bus interfaces. Chapters 16 and 17 cover the advanced 80186/80188–80486 microprocessors and explore their differences with the 8086/8088, as well as their enhancements and features. Cache memory, interleaved memory, and burst memory are described with the 80386 and 80486 microproces- sors. Chapter 16also covers real-time operating systems (RTOS), and Chapter 17also describes memory management and memory paging. Chapter 18details the Pentium and Pentium Pro microprocessors. These microprocessors are based upon the original 8086/8088. Chapter 19introduces the Pentium II, Pentium III, Pentium 4, and Core2 microprocessors. It covers some of the new features, package styles, and the instructions that are added to the orig- inal instruction set. Appendices are included to enhance the text. Appendix A provides an abbreviated listing of the DOS INT 21H function calls because the use of DOS has waned. It also details the use of viii PREFACE the assembler program and the Windows Visual C++ interface. A complete listing of all 8086–Pentium 4 and Core2 instructions, including many example instructions and machine cod- ing in hexadecimal as well as clock timing information, is found in Appendix B. Appendix C provides a compact list of all the instructions that change the flag bits. Answers for the even- numbered questions and problems are provided in Appendix D. To access supplementary materials online, instructors need to request an instructor access code. Go to www.pearsonhighered.com/irc, where you can register for an instructor access code. Within 48 hours after registering, you will receive a confirming e-mail, including an instructor access code. Once you have received your code, go to the site and log on for full instructions on downloading the materials you wish to use. Acknowledgments I greatly appreciate the feedback from the following reviewers: James K. Archibald, Brigham Young University William H. Murray III, Broome Community College. STAY IN TOUCH We can stay in touch through the Internet. My Internet site contains information about all of my textbooks and many important links that are specific to the personal computer, microprocessors, hardware, and software. Also available is a weekly lesson that details many of the aspects of the personal computer. Of particular interest is the “Technical Section,” which presents many notes on topics that are not covered in this text. Please feel free to contact me at [email protected] you need any type of assistance. I usually answer all of my e-mail within 24 hours. My website is http://members.ee.net/brey