Pentium™ Processor: Optimization Tools
()
About this ebook
Related to Pentium™ Processor
Related ebooks
PowerPC Microprocessor Common Hardware Reference Platform: A System Architecture Rating: 4 out of 5 stars4/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5PIC Microcontrollers: An Introduction to Microelectronics Rating: 5 out of 5 stars5/5IP Addressing and Subnetting INC IPV6: Including IPv6 Rating: 0 out of 5 stars0 ratingsPIC Projects for Non-Programmers Rating: 4 out of 5 stars4/5Recent Developments in Artificial Intelligence and Communication Technologies Rating: 0 out of 5 stars0 ratingsSchaum's Outline of Principles of Computer Science Rating: 0 out of 5 stars0 ratingsAutomotive Scan Tool PID Diagnostics Rating: 5 out of 5 stars5/5C# Deconstructed: Discover how C# works on the .NET Framework Rating: 0 out of 5 stars0 ratingsAccelerated Computing with HIP Rating: 4 out of 5 stars4/5System on Chip Interfaces for Low Power Design Rating: 0 out of 5 stars0 ratingsModern C Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library Rating: 0 out of 5 stars0 ratingsPower and Performance: Software Analysis and Optimization Rating: 0 out of 5 stars0 ratingsInternet Economics: Models, Mechanisms and Management Rating: 0 out of 5 stars0 ratingsFast and Effective Embedded Systems Design: Applying the ARM mbed Rating: 5 out of 5 stars5/5Programming 8-bit PIC Microcontrollers in C: with Interactive Hardware Simulation Rating: 3 out of 5 stars3/5Configuration Management for Senior Managers: Essential Product Configuration and Lifecycle Management for Manufacturing Rating: 0 out of 5 stars0 ratingsIntroduction to Machine Learning with Python Rating: 0 out of 5 stars0 ratingsSoftware Engineering for Embedded Systems: Methods, Practical Techniques, and Applications Rating: 3 out of 5 stars3/5Database Benchmarking and Stress Testing: An Evidence-Based Approach to Decisions on Architecture and Technology Rating: 0 out of 5 stars0 ratingsMicrosoft System Center 2012 Endpoint Protection Cookbook Rating: 0 out of 5 stars0 ratingsUsing HPC for Computational Fluid Dynamics: A Guide to High Performance Computing for CFD Engineers Rating: 0 out of 5 stars0 ratingsTesting and Tuning Market Trading Systems: Algorithms in C++ Rating: 3 out of 5 stars3/5Engineering Documentation Control / Configuration Management Standards Manual Rating: 0 out of 5 stars0 ratingsProgramming and Customizing the PIC Microcontroller Rating: 0 out of 5 stars0 ratingsPC-based Instrumentation and Control Rating: 3 out of 5 stars3/5Designing SCADA Application Software: A Practical Approach Rating: 0 out of 5 stars0 ratingsMake Your Own PCBs with EAGLE: From Schematic Designs to Finished Boards Rating: 5 out of 5 stars5/5Learn RStudio IDE: Quick, Effective, and Productive Data Science Rating: 0 out of 5 stars0 ratingsASP.NET Core 1.0 High Performance Rating: 0 out of 5 stars0 ratings
Computers For You
Deep Search: How to Explore the Internet More Effectively Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 5 out of 5 stars5/5How to Create Cpn Numbers the Right way: A Step by Step Guide to Creating cpn Numbers Legally Rating: 4 out of 5 stars4/5Network+ Study Guide & Practice Exams Rating: 4 out of 5 stars4/5Procreate for Beginners: Introduction to Procreate for Drawing and Illustrating on the iPad Rating: 0 out of 5 stars0 ratingsThe ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 0 out of 5 stars0 ratings101 Awesome Builds: Minecraft® Secrets from the World's Greatest Crafters Rating: 4 out of 5 stars4/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Ultimate Guide to Mastering Command Blocks!: Minecraft Keys to Unlocking Secret Commands Rating: 5 out of 5 stars5/5AP Computer Science Principles Premium, 2024: 6 Practice Tests + Comprehensive Review + Online Practice Rating: 0 out of 5 stars0 ratingsCompTIA Security+ Practice Questions Rating: 2 out of 5 stars2/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5CompTIA IT Fundamentals (ITF+) Study Guide: Exam FC0-U61 Rating: 0 out of 5 stars0 ratingsChildhood Unplugged: Practical Advice to Get Kids Off Screens and Find Balance Rating: 0 out of 5 stars0 ratingsChatGPT Ultimate User Guide - How to Make Money Online Faster and More Precise Using AI Technology Rating: 0 out of 5 stars0 ratingsPractical Lock Picking: A Physical Penetration Tester's Training Guide Rating: 5 out of 5 stars5/5Elon Musk Rating: 4 out of 5 stars4/5Dark Aeon: Transhumanism and the War Against Humanity Rating: 5 out of 5 stars5/5The Professional Voiceover Handbook: Voiceover training, #1 Rating: 5 out of 5 stars5/5Master Builder Roblox: The Essential Guide Rating: 4 out of 5 stars4/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5
Reviews for Pentium™ Processor
0 ratings0 reviews
Book preview
Pentium™ Processor - Michael L. Schmit
Pentium™ Processor
Optimization Tools
MICHAEL L. SCHMIT
Quantasm Corporation, Cupertino, California
Boston San Diego New York London Sydney Tokyo Toronto
Table of Contents
Cover image
Title page
Inside Front Cover
Copyright
Dedication
Introduction
I: Review and Historical Context
Chapter 1: Number Systems
Publisher Summary
HEX
SIGNED NUMBERS
NUMERIC OVERFLOW
DATA SIZES
LITTLE ENDIAN VS. BIG ENDIAN
Chapter 2: What Is Assembly?
Publisher Summary
INTRODUCTION
Tools and Terminology
WHAT ARE COMPILERS, INTERPRETERS AND ASSEMBLERS?
Chapter 3: The 8086 Family History and Architecture
Publisher Summary
COMPATIBILITY LESSONS
MATH CO-PROCESSORS
THE 80286
32-BIT 80386
RISCY 80486
THE 80586
THE COMPETITION
THE P6
II: 80×86 Family Background
Chapter 4: 8086 Architecture and Instruction Set
Publisher Summary
8088 Architecture
The 8088 Instruction Set
Shifts and Rotates
Program Control And Branching
Flag Manipulations
Multiply and Divide
BCD Instructions
String Instructions
Interrupts
Miscellaneous Instructions
Flag Summary
Chapter 5: Writing Beginning Programs
Publisher Summary
Chapter 6: Assembly Tools
Publisher Summary
Chapter 7: The Instruction Set Evolves: The 186 to the 386
Publisher Summary
The 80286
The 80386
III: Introduction to Pentium and Tools
Chapter 8: The 80486 and Pentium
Publisher Summary
Pentium
Summary
Chapter 9: Superscalar Programming
Publisher Summary
Dual Integer Pipelines
Branch Prediction Logic
Optimized Cycle Times
Chapter 10: Integer and Floating-Point Pipeline Operation
Publisher Summary
Pentium Floating-Point Pipeline
Chapter 11: Using the Pentium Optimizer Program
Publisher Summary
HOW IT WORKS
ADDRESS GENERATION INTERLOCKS
Chapter 12: Timing with a Software Timer
Publisher Summary
ICE
BUILT-IN PENTIUM TIMER
SOFTWARE TIMER
TIMER SOFTWARE FUNCTION REFERENCE
Percent Speed Changes
IV: Superscalar Pentium Programming
Chapter 13: Optimization Warm-ups
Publisher Summary
STRING INSTRUCTION OPTIMIZATIONS
Chapter 14: String Search and Translate
Publisher Summary
STRING SEARCH
String Translations
Atomic Programming
Case-Independent String Searching
CASE-INDEPENDENT STRING SCAN
CASE-INDEPENDENT STRING COMPARE
CONCLUSIONS
Chapter 15: Checksums and Extended Precision Addition
Publisher Summary
STEP 1
STEP 2
STEP 3
STEP 4
STEP 5
STEP 6
COMING COMPLETELY UNDONE
SUMMARY
FALSE STEPS
Extended Precision Addition
V: Advanced Topics
Chapter 16: Floating-Point Math
Publisher Summary
FPU BASICS
FPU MATRIX OPTIMIZATIONS
WHICH ARRAY DECLARATION IS BEST?
OPTIMIZING WITH ASSEMBLY
Chapter 17: Interfacing to C
Publisher Summary
INLINE ASSEMBLY
INLINE ASSEMBLY EXAMPLE
LINKING SEPARATE MODULES
CALLING CONVENTIONS
FULL C-TO-ASSEMBLY TEMPLATES
EXAMPLES OF CALLING ASSEMBLY ROUTINES FROM C
USING THE EXTENDED PROC DIRECTIVE
FASTCALL
FASTCALL REGISTERS
TIMING C CODE
Chapter 18: Protected-Mode Programming
Publisher Summary
INTRODUCTION TO PROTECTED MODE
DPMI, DOS PROTECTED-MODE INTERFACE
PROTECTED-MODE SEGMENTS
CONVERTING CODE TO PROTECTED MODE
MIXED 16-BIT AND 32-BIT PROTECTED-MODE PROGRAMMING
FULL SEGMENT DEFINITIONS
PROTECTED MODE TIMING
32-BIT PROTECTED-MODE CODE TEMPLATE
LARGE DATA SEGMENTS
TIMING 32-BIT CODE
Cloaking Developers Toolkit
Chapter 19: Final Notes and Optimizations
Publisher Summary
SPEED VS. CODE SIZE
LEA, THE MULTI-PURPOSE INSTRUCTION
CODE AND DATA ALIGNMENT
LOCAL STACK VARIABLES
MEASURING AND CORRECTING THE DATA MISALIGNMENT PENALTY
CODE ALIGNMENT
Further Reading
Where We’ve Been
VI: Power PC vs. Pentium
Chapter 20: PowerPC vs. Pentium
Publisher Summary
WHAT IS RISC?
WHAT IS CISC?
WHAT IS RISC, REALLY?
WHICH IS BETTER, RISC OR CISC?
IS THE PENTIUM RISC OR CISC?
SUPERSCALAR PROCESSORS
SUPERSCALAR TECHNIQUES AND TERMINOLOGY
WHAT IS IN THE POWERPC?
IS THE POWERPC LESS EXPENSIVE?
FUTURE PROCESSOR DESIGNS
Appendices
Appendix A: Instruction Set Reference
Appendix B: Optimization Cross-Reference by Instruction
Appendix C: Optimization Guidelines by CPU
Appendix D: Simple Instructions for Pentium Pairing
Appendix E: Instruction Pairing Rules for Pentium
Appendix F: Single-Byte Instructions
Appendix G: Quick Reference for Important Instruction Timings
Appendix H: Undocumented Pentium Registers
Appendix I: DEBUG32 Command Summary
Appendix J: Improving Performance
Appendix K: Glossary of Terms
Appendix L: Products Mentioned
Index
Inside Back Cover
Inside Front Cover
LIMITED WARRANTY AND DISCLAIMER OF LIABILITY
ACADEMIC PRESS, INC. (AP
) AND ANYONE ELSE WHO HAS BEEN INVOLVED IN THE CREATION OR PRODUCTION OF THE ACCOMPANYING CODE (THE PRODUCT
) CANNOT AND DO NOT WARRANT THE PERFORMANCE OR RESULTS THAT MAY BE OBTAINED BY USING THE PRODUCT. THE PRODUCT IS SOLD AS IS
WITHOUT WARRANTY OF ANY KIND (EXCEPT AS HEREAFTER DESCRIBED), EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF PERFORMANCE OR ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE. AP WARRANTS ONLY THAT THE MAGNETIC DISKETTE(S) ON WHICH THE CODE IS RECORDED IS FREE FROM DEFECTS IN MATERIAL AND FAULTY WORKMANSHIP UNDER THE NORMAL USE AND SERVICE FOR A PERIOD OF NINETY (90) DAYS FROM THE DATE THE PRODUCT IS DELIVERED. THE PURCHASER’S SOLE AND EXCLUSIVE REMEDY IN THE EVENT OF A DEFECT IS EXPRESSLY LIMITED TO EITHER REPLACEMENT OF THE DISKETTE(S) OR REFUND OF THE PURCHASE PRICE, AT AP’S SOLE DISCRETION.
IN NO EVENT, WHETHER AS A RESULT OF BREACH OF CONTRACT, WARRANTY OR TORT (INCLUDING NEGLIGENCE), WILL AP OR ANYONE WHO HAS BEEN INVOLVED IN THE CREATION OR PRODUCTION OF THE PRODUCT BE LIABLE TO PURCHASER FOR ANY DAMAGES, INCLUDING ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PRODUCT OR ANY MODIFICATIONS THEREOF, OR DUE TO THE CONTENTS OF THE CODE, EVEN IF AP HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
Any request for replacement of a defective diskette must be postage prepaid and must be accompanied by the original defective diskette, your mailing address and telephone number, and proof of date of purchase and purchase price. Send such requests, stating the nature of the problem, to Academic Press Customer Service, 6277 Sea Harbor Drive, Orlando, FL 32887, 1-800-321-5068. APP shall have no obligation to refund the purchase price or to replace a diskette based on claims of defects in the nature or operation of the Product.
Some states do not allow limitation on how long an implied warranty lasts, nor exclusions or limitations of incidental or consequential damage, so the above limitations and exclusions may not apply to you. This Warranty gives you specific legal rights, and you may also have other rights which vary from jurisdiction to jurisdiction.
THE RE-EXPORT OF UNITED STATES ORIGIN SOFTWARE IS SUBJECT TO THE UNITED STATES LAWS UNDER THE EXPORT ADMINISTRATION ACT OF 1969 AS AMENDED. ANY FURTHER SALE OF THE PRODUCT SHALL BE IN COMPLIANCE WITH THE UNITED STATES DEPARTMENT OF COMMERCE ADMINISTRATION REGULATIONS. COMPLIANCE WITH SUCH REGULATIONS IS YOUR RESPONSIBILITY AND NOT THE RESPONSIBILITY OF AP.
Copyright
Copyright © 1995 by Academic Press, Inc.
All rights reserved.
No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopy, recording, or any information storage and retrieval system, without permission in writing from the publisher.
All brand names and product names mentioned in this book are trademarks or registered trademarks of their respective companies.
AP PROFESSIONAL
955 Massachusetts Avenue, Cambridge, MA 02139
An Imprint of ACADEMIC PRESS, INC.
A Division of HARCOURT BRACE & COMPANY
United Kingdom Edition published by
ACADEMIC PRESS LIMITED
24-28 Oval Road, London NW1 7DX
Library of Congress Cataloging-in-Publication Data
Schmit, Michael L.
Pentium processor optimization tools / Michael L. Schmit.
p. cm.
Includes bibliographical references and index.
ISBN 0-12-627230-1 (acid-free paper)
1. Pentium (Microprocessor) I. Title.
QA76.8.P46S36 1995 94-32054
005.265--dc20 CIP
Printed in the United States of America
94 95 96 97 98 IP 9 8 7 6 5 4 3 2 1
Dedication
This book is dedicated to my wife, Phyllis, and my daughter, Stephanie.
Introduction
WHO IS THIS BOOK FOR?
This book is for programmers, not just any programmer, mind you, but for programmers who desire to learn advanced program optimization techniques for the Intel 80×86 family of chips, including the Pentium.
The first few chapters review the basics of 80×86 assembly language programming but an intermediate understanding of assembly or C programming is extremely helpful for the later chapters.
WHAT IS ON THE DISK?
The disk includes all the code listings in the book as well as a version of Quantasm’s PentOpt Program (a Pentium Optimizer) and DEBUG32 (a 32-bit, protected mode, DPMI debugger). Most of the code will work with Microsoft’s MASM 5.1 or above (MASM 6.0 or above preferred) or Borland’s TASM (any version). A few of the Pentium code examples require MASM 6.11, the first Microsoft assembler to support the Pentium. (You can bypass this requirement by writing macros to generate the proper opcodes.)
WHY LEARN ASSEMBLY LANGUAGE FOR THE PENTIUM?
As the software world continues to turn toward object oriented and high level languages with more and more complex operating systems, so why should you learn all the details of assembly language for the Pentium. There is one simple answer: speed. There are other reasons why you might want to program in assembly language, but the overriding reason, I think, is speed. If it is not, then you can still program for the 8088 in assembly language. The Pentium contains two instruction pipelines, both somewhat equivalent to a 386 or 486 CPU. Keeping both of these pipelines busy is mostly what programming for the Pentium is all about.
If you program mostly in a higher level language, such as C/C++, knowing assembly language will help you understand the language, its quirks and its performance capabilities on the PC. In this book you’ll see how easy it is to add small amounts of in-line assembly that result in large performance gains.
There are a number of tasks that are simply more conveniently done with assembly language. These tasks include writing portions of operating systems, system utilities, device drivers, V×D’s and the like. Many of these programs also will benefit from the performance gains possible when optimizing for the Pentium.
Finally, you may want to learn and use assembly language because it is enjoyable and/or a challenge. If these reasons fit you, then you won’t be disappointed with the Pentium. Being the first CPU in the 80×86 family with more than one pipeline provides for many new and interesting challenges that were never an issue on previous 80×86 CPUs.
HOW TO PROCEED
Programmers who are new to assembly language (or new to assembly language on the Intel 80×86 family of CPUs) should start with Chapter 1 or 2. If you are comfortable with binary and hexadecimal numbers, you can skip Chapter 1.
Programmers who are knowledgeable about 80×86 assembly language should skip or skim Chapters 1 through 5.
Expert assembly programmers should start with Chapter 8 or 9, depending on any previous experience with the Pentium. Most of the material in the advanced topics (chapters 16–19.
The chapters are arranged in sections, as follows:
ACKNOWLEDGMENTS
Writing a book such as this for an advanced technical subject is not an easy task. It would not have been possible without the help of a number of people. Those who helped with technical information are: Frank van Gilluwe, Dave Horn, Rob Larson, Terje Mathisen and Stuart McCarley. Harlan Stockman (hwstock@sandia.gov) provided much of the code and the benchmarks for the FPU chapter. Thanks to Shapeware Corp. for their Visio drawing package that I used to produce the figures in this book. And special thanks to my long-time friend Larry Conrad who kept my writing closer to the English language than a programming language.
—Mike Schmit
I
Review and Historical Context
Outline
Chapter 1: Number Systems
Chapter 2: What Is Assembly?
Chapter 3: The 8086 Family History and Architecture
CHAPTER 1
Number Systems
Publisher Summary
This chapter reviews binary, hexadecimal, and decimal number systems. Decimal numbers are used for money, time, measurements, and even television channels. Everything is based on decimal except the internals of computers and other electronic devices. The binary number system is used internally in every computer. Binary or base two has two digits, 0 and 1. Decimal or base 10 has 10 digits, 0 through 9. Computers use binary because the electronic circuits can have only two states, on or off. Different devices may use different physical properties; a magnetic disk may store binary digits as magnetized or not magnetized or as north or south but the effect is the same—on or off. Decimal numbers are formed by combining a number of digits.
It’S a poor sort of memory that only works backwards,
the Queen remarked.
—Lewis Carroll from Alice’s Adventures in Wonderland
In this chapter we’ll review binary, hexadecimal and decimal number systems. If you have a working knowledge of binary and hexadecimal, then skip to Chapter 2. If you’ve never programmed a computer or used a higher-level language such as C, BASIC or Pascal, you may be familiar only with the concept of decimal (base 10) numbers. We all grew up with decimal numbers—for money, time, measurements and even television channels. Everything is based on decimal—except the internals of computers and other electronic devices. Decimal is easy for us because we grew up with it. And, of course, we have 10 fingers.
The binary number system is used internally in every computer. Binary, or base two, has two digits, 0 and 1. Decimal, or base 10, has 10 digits, 0 through 9. Computers use binary because the electronic circuits can have only two states, on
or off.
Different devices may use different physical properties (a magnetic disk may store binary digits as magnetized or not magnetized or as north or south) but the effect is the same–on or off.
To become familiar with binary, we’ll start by looking at whole integer decimal numbers (i.e., 0, 1, 2, 3 …). We form decimal numbers by combining a number of digits. Each digit has two factors that are multiplied together. The first factor is the digit (0 through 9).
Figure 1.1 Example Decimal Number
The second factor varies based on its position within the whole number. The far right digit has a multiplier of 1. The next digit to the left has a multiplier of 10, the next 100, and so on. Moving a digits multiplier left increases its value by a factor of 10, making the numbers base 10.
For example in the number 3406, we say that the number is:
Of course, we already knew the value of 3406 was 3406. The real issue is how to convert numbers from one base to another. This same process can be performed for numbers in any base. Also notice the multiplier is the base raised to the power of the position. In the example above the 4 is multiplied by 100 and the 100 is 10² (10 raised to the power of 2).
Each digit in a binary number is called a bit, which is a binary digit. So when we have a binary number, say with 4 bits, the value of each of bit is progressively larger. The first bit (bit 0) has a value of 1, or 2⁰. The next bit has a value of 2, or 2¹. The next bit has a value of 4, or 2². The last bit has a value of 8, or 2³. Each bit has a value that is two times the value of the previous bit. In decimal, each digit has a value of 10 times the value of the previous digit to its right.
A byte is a binary number that contains 8 bits. If all the bits in a byte are 1, (the largest possible number), the base 10 value of the byte would be 255. So a byte can have a value from 0 to 255.
Figure 1.2 Example Binary Number
In the following example we’ll convert the binary number to base 10.
Addition in binary is very easy because there are only four possible combinations of numbers to add. By contrast, in base 10 there are 100 combinations. The following is a list of all possible single bit additions.
To add two numbers in binary, we follow the same procedure as when adding in decimal. We’ll add 01101001 to 00010001:
HEX
Hexadecimal (usually shortened to hex
) is a number system based on 16 digits (base 16). Because there are only 10 symbols (0–9) for digits, this poses a small problem when working with number systems requiring more than 10 symbols. There could be many solutions to this problem. One would be to use the first 16 letters of the alphabet. Another would be to make up an entirely new set of symbols. However, the convention in general use is to use the digits 0 to 9 and then use the letters A to F for the values 10 to 15.
Here we’ll convert a hex number to decimal and to binary. The hex number is 3A21h.
Converting a hex number to binary is a completely different and much easier process. Each hex digit is just another representation for a combination of four binary digits (or bits). The reason that hex is commonly used is because of the fact that it is just a combination of four bits. Think of hex as a shorthand
for binary. Table 1.1 is the conversion table.
Table 1.1
Decimal, Hex and Binary Equivalents
So, to convert to binary, each hex digit is converted in sequence. Converting 3A21h goes as follows:
For easier reading, most examples in this book will use hex and decimal. Hex numbers are usually followed by the letter H for clarity. Memory addresses are always in hex and may be shown in the segment:offset format (4 hex digits, followed by a colon and then followed by 4 more hex digits). Later chapters will discuss the meaning of the segment:offset format.
In chapter 3 we’ll discuss the use of an assembler
to translate the code that you write into a machine readable format. Assemblers usually require hex numbers to begin with a decimal digit. This is because variable names and labels in programs usually start with an alphabetic character. Therefore, if a hex number starts with A—F, it must be preceded with a zero.
SIGNED NUMBERS
We’ve seen how a byte can store any number from 0 to 255. However, we may wish to use negative numbers at some point. The same byte that can store numbers from 0 to 255 could also store signed numbers from −128 to 127. One way to think about this is to decide that the larger values (128 to 255) will be used for negative numbers: 255 is used for −1, 254 is used for −2 and so on. This may seem awkward or be confusing, so let’s use an example.
Say we have a car with a five-digit odometer that goes from 0 miles to 99,999 miles (no tenths). Let’S also assume that our car can go in reverse and the odometer will go backwards. If we start with a new car with an odometer reading of 00,000 and go backwards for one mile, it won’t read −1, it will be 99,999. If we go backwards another mile it will read 99,998. If we go forward for 3 miles it will read 00,001. Clearly, 99,999 is the same as −1, 99,998 is the same as −2 and so on, when we have a five-digit decimal counter.
Now lets switch to binary. Lets install an eight-bit binary odometer. Start at 00000000 and go backward 1 mile. The reading will be 11111111 (or 255 in decimal). Go back another mile and it will read 11111110 (or 254 in decimal). Go forward for 3 miles and it will read 00000001 (or 1 in decimal).
The only remaining difficulty is determining if a number is signed or unsigned. How do you tell them apart? The answer is that you can’t. In some high-level languages we declare certain variables to have a particular data type (i.e., integer, unsigned integer, etc.), and the compiler or interpreter keeps track of how to handle each variable. In assembly language the programmer must keep track. There are different instructions to handle signed and unsigned numbers, as we shall see in later chapters.
NUMERIC OVERFLOW
Even if you are only dealing with positive integers, you can run into problems. Suppose you have a byte variable (range 0–255) with a value of 255 and we add 1 to get 256. Wrong. Since a byte has a maximum value of 255, we must take this into account. This is the same as having a car with 99,999 miles on it and going one more mile. You can’t fool anyone into thinking that you have a new car. When we see 00,000 on the odometer we immediately know that the car has 100,000 miles on it by looking at the rust and paint. We know that 100,000 miles have been logically subtracted from the real mileage because the odometer only goes up to 99,999. Note that the 100,000 value is the maximum odometer reading (99,999) plus one. Lets get back to adding 255 plus 1. Since the computer is only going to retain eight binary digits (255) we know that the real value will be off by 255 plus 1 or 256. So the computer will have a result of only 0. Since the computer registers have no rust or paint to clue them in that something is amiss, we need to have some other indication of overflow. Computers have a set of flags (or condition codes) that are set when various conditions occur, such as a numeric overflow. The carry flag
is set when the result of adding two bytes is greater than 255. we’ll discuss this more later on.
Lets say that we are dealing with signed bytes (−128 to 127). Start with 125 and add 10. We get 135 and it is stored as 135. But 135 is the same as −121. So all we have to do is check to see if we got a negative result and then we know that an overflow occurred. Wrong again. Actually, if we knew that we were adding two positive signed integers then a negative result would indicace an overflow. But what if we were adding two negative numbers? Here we expect a negative result. And what about adding a negative and positive number? As it turns out the computer has an overflow flag
that takes all this into account. When adding two positive signed numbers or adding two negative signed numbers the overflow flag is set if the result has a different sign. When adding numbers with different signs there can be no overflow. (In a similar manner, the overflow flag is set when subtracting two numbers with different signs and an overflow occurs.) we’ll discuss all the various flags in Chapter 4.
DATA SIZES
Depending on the purpose of a variable you may choose to have a small range of values or a larger range of values. In this section we’ll discuss the various data sizes available on the 8088. we’ll cover them in detail in the next few chapters.
A byte is the smallest addressable unit of storage in the computer memory. Each byte has its own address and may be read or written to individually by the CPU. If you want to read just one bit, you must read an entire byte or more into a register. (we’ll discuss registers in Chapter 4.) If you want to write just one bit, you must read an entire byte, modify the bit, then write the entire byte back to memory. Each half of a byte (4 bits) is called a nibble. Data is sometimes arranged with one item per nibble, but must still be read and written one byte at a time.
Bytes can represent:
A word is 16 bits, or two bytes. You can read or write words to or from memory as well as individual bytes. Words can be used for:
A dword is 32 bits, two words or four bytes. There are four common data types stored in dword formats:
When we get to writing programs and declaring data we’ll see that DB declares a byte, DW declares a word and DD declares a dword.
LITTLE ENDIAN VS. BIG ENDIAN
There is sometimes some confusion over the format in which values larger than 8 bits are stored in memory. On the 80×86 (i.e., 8088/86, 80286, etc.) computers a word is normally stored with the least significant byte in the lowest