You are on page 1of 1019
INTERNATIONAL EDITION THe 80x86 IBM PC AND COMPATIBLE COMPUTERS (Votumes | & Il) ASSEMBLY LANGUAGE, DESIGN, AND INTERFACING retro) Janice Gillispie Mazidi ow a ec please go to hitp://www.prenhall.com/mazidi THe 80x86 IBM PC AND Compatisie COMPUTERS (Votumes | & II) ASSEMBLY LANGUAGE, DESIGN, AND INTERFACING Muhammad Ali Mazi Janice Gillispie Mazi Praised by experts for its clarity and topical breadth, this visually appealing text provides con eosy-to-understand, systematic approach to teaching the fundamentals of 80x86 ‘assembly language programming and PC architecture. It offers readers a fun, hands-on learning experience and reinforces concepts with numerous examples and review questions. It uses the Debug utility to show what action the instruction performs, and then provides a sample program to show its application, The text delves into architecture, supporting chips, buse: characteristics, and more. The fourth edition: terfacing techniques, system programming, hard disk See ee eC ee ce * Combines assembly and C programming, and covers C programming in Ce ee eens * Introduces the 80x86 instructions with examples of how they are used. * Ensures a basic understanding of binary and hex numbering systems. Dee ee te ae ee ae Capea a Reece ec ee Sa eee eee ae ea eee ae cece er Cee es This so special international edition of an established tile widely used by colleges and universities throughout the word. rele ent ret eee ag the benefit of students outside the United States and Canada. \f you purchased this book within the United States or Conado you should be aware that it has been wrongfully imported without the approval ofthe Publisher or the Author. Pearson International Edition eran Education Not for Sale in the U.S.A w1V75-8 OL . or Canada THE 80x86 IBM PC AND COMPATIBLE COMPUTERS VOLUMESI & II Assembly Language, Design, and Interfacing Fourth Edition THE 80x86 IBM PC AND COMPATIBLE COMPUTERS VOLUMES I & II Assembly Language, Design, and Interfacing Fourth Edition Muhammad Ali Mazidi Janice Gillispie Mazidi Pearson Education International If you purchased this book within the United States or Canada you should be aware that it has been. ‘wrongfully imported without the approval ofthe Publisher or the Author. Editor in Chief: Stephen Helba Assistant Vice President and Publisher: Charles E. Stewart, Jr. Production Editor: Alexandrina Benedicto Wolf Design Coordinator: Diane Emsberger Cover Designer: Jeff Vanik Cover image: Digital Images Production Manager: Matthew Ottenweller Marketing Manager: Ben Leonard ‘This book was set in Times Roman by Janice Mazidi. It was printed and bound by Courier/Kendaliville. The cover was printed by Phoenix Color Corp. Pearson Education LTD. Pearson Education Australia PTY, Limited Pearson Education Singapore, Pe. Ltd Pearson Education North Asia Ltd Pearson Education Canada, Ltd Pearson Educacign de Mexico, S.A. de CN. Pearson Education - Japan Pearson Education Malaysia, Pte, Ltd Pearson Education, Upper Saddle River, New Jersey Earlier edition © 1995 by Muhammad Ali Mazidi and Janice Gillispie Mazidi Copyright © 2003, 2000, 1998 by Pearson Education, Inc., Upper Saddle River, New Jersey 07458. Alli rights reserved. Printed in the United States of America. This publication is protected by Copyright and permission 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. dele Se cauinte, een 1098765432 ISBN 0-13-121975-8 Regard man as a mine rich in gems of inestimable value. Education can, alone, cause it to reveal its treasures, and enable mankind to benefit therefrom. Baha'u'llah DEDICATIONS, This book is dedicated to the memory of Muhammad Ali's parents, who raised 10 children and persevered through more than 50 years of hardship together with dignity and faith. We feel especially blessed to have the support, love, and encouragement of Janice's parents whose kindness, wisdom, and sense of humor have been the bond that has welded us into a family. In addition, we must also mention our two most important collaborations: our sons Robert Nabil and Michael Jamal who have taught us the meaning, of love and patience. ‘We would also like to honor the memory of a dear friend, Kamran Lotfi CONTENTS AT A GLANCE Assembly Language Programming on the IBM PG, PS, and Compatibles INTRODUCTION TO COMPUTING, 1 THE 80x86 MICROPROCESSOR, 18 ASSEMBLY LANGUAGE PROGRAMMING, 49 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS, 82 BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C, 121 MACROS AND THE MOUSE, 150 SIGNED NUMBERS, STRINGS, AND TABLES, 173 MODULES: MODULAR AND C PROGRAMMING, 193 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES, 220 eNoaroENac Design and Interfacing of the IBM PC, PS, and Compatibles 9 8088, 80286 MICROPROCESSOR AND ISA BUS, 235 10 MEMORY AND MEMORY INTERFACING, 265 11 VO AND THE 8265; ISA BUS INTERFACING, 309 12 INTERFACING TO THE PC: LCD, MOTOR, ADC, AND SENSOR, 351 13 8253/54 TIMER AND MUSIC, 386 14 INTERRUPTS AND THE 8259 CHIP, 410 15 DIRECT MEMORY ACCESSING; THE 8237 DMA CHIP, 447 16 VIDEO AND VIDEO ADAPTERS, 477 17 SERIAL DATA COMMUNICATION AND THE 16450/8250/51 CHIPS, 508 18 KEYBOARD AND PRINTER INTERFACING, 541 19 FLOPPY DISKS, HARD DISKS, AND FILES, 570 20 THE 80x87 MATH COPROCESSOR, 600 21 386 MICROPROCESSOR: REAL VERSUS PROTECTED MODE, 631 22 HIGH-SPEED MEMORY INTERFACING AND CACHE, 659 23 486, PENTIUM, PENTIUM PRO AND MMX, 690 24 MS DOS STRUCTURE, TSR, AND DEVICE DRIVERS, 724 25 MS DOS MEMORY MANAGEMENT, 740, 26 IC TECHNOLOGY AND SYSTEM DESIGN, 759 27 ISA, PCI, AND USB BUSES, 784 28 PROGRAMMING DOS, BIOS, & HARDWARE WITH C/C++, 808 APPENDICES DEBUG PROGRAMMING, 825 80x86 INSTRUCTIONS AND TIMING, 847 ASSEMBLER DIRECTIVES AND NAMING RULES, 883 DOS INTERRUPT 21H AND 33H LISTING, 898 BIOS INTERRUPTS, 924 ASCII CODES, 940 VO ADDRESS MAPS, 941 IBM PC/PS BIOS DATA AREA, 952 DATA SHEETS, 959 —zo7mo00ow> CONTENTS PREFACE TO VOLUMES I AND If CHAPTER 0: INTRODUCTION TO COMPUTING — 1 SECTION 0.1: NUMBERING AND CODING SYSTEMS 2 Decimal and binary number systems 2 Converting from decimal to binary 2 Converting from binary to decimal 2 Hexadecimal system © 3 Converting between binary and hex 4 Converting from decimal tohex 4 Converting from hex to decimal 4 Counting in base 10,2, and 16 6 Addition of binary and hex numbers 6 2's complement = 6 ‘Addition and subtraction of hex numbers 7 ‘Addition of hex numbers 7 Subtraction of hex numbers 7 ASCI code 8 SECTION 0.2: INSIDE THE COMPUTER 9 Some important terminology 9 Internal organization of computers 9 More about the data bus 10 More about the address bus 10 CPU and its relation to RAM and ROM I Inside CPUs 1 Internal working of computers 12 SECTION 0.3; BRIEF HISTORY OF THE CPU 13 ClSCys.RISC 14 CHAPTER 1: THE 80x86 MICROPROCESSOR — 18 SECTION 1.1: BRIEF HISTORY OF THE 80x86 FAMILY 19. Evolution from 8080/8085 to 8086 19 Evolution from 8086 to 8088 19 Success of the 8088 19 Other microprocessors: the 80286, 80386, and 80486 19 SECTION 1.2: INSIDE THE 8088/8086 21 Pipelining 21 Registers 22 SECTION 13: INTRODUCTION TO ASSEMBLY PROGRAMMING Assembly language programming 24 MOV instruction 24 ADD instruction 25 SECTION 1.4: INTRODUCTION TO PROGRAM SEGMENTS Origin and definition of the segment 27 Logical address and physical address 27 Code segment 27 Logical address vs. physical address in the code segment Data segment 29 Logical address and physical address in the data segment Little endian convention 31 Extra segment (ES) 32 Memory map of the IBM PC 32 More about RAM 32 VideoRAM 33 More about ROM 33 Function of BIOS ROM 33 SECTION 1.5: MORE ABOUT SEGMENTS IN THE 80x86 ‘What isa stack, and why is it needed? 33, How stacks are accessed 34 Pushing onto the stack 34 Popping the stack 34 Logical address vs. physical address for the stack 35 A few more words about segments in the 80x86 36 Overlapping 36 Flag register. 37 Bits of the flag register 38 Flag register and ADD instruction 38 Use of the zero flag for looping 40 SECTION 1.6: 80x86 ADDRESSING MODES 41. Register addressing mode 41 Immediate addressing mode 41 rect addressing mode 42. Register indirect addressing mode 42. Based relative addressing mode 43 Indexed relative addressing mode 43 Based indexed addressing mode 44 Segment overrides 44 26 28 30 33 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING — 49 SECTION 2.1: DIRECTIVES AND A SAMPLE PROGRAM — 50 ‘Segments of a program 50. Stack segment definition ST Data segment definition SI Code segment definition 52 SECTION 2.2: ASSEMBLE, LINK, AND RUN A PROGRAM = 54 asm and obj files 55 Istfile 35 PAGE and TITLE directives 56 erffile 56 LINKing the program 57 map file 57 SECTION 2.3: MORE SAMPLE PROGRAMS — 57 Analysis of Program 2-1 58 Various approaches to Program 2-1 60 Analysis of Program 2-2 62 Analysis of Program 2-3 62 ‘Stack segment definition revisited 62 SECTION 2.4: CONTROL TRANSFER INSTRUCTIONS 64 FAR andNEAR 64 Conditional jumps 64 Short jumps 64 Unconditional jumps 66 CALL statements 66 Assembly language subroutines 67 Rules for names in Assembly language 67 SECTION 2.5: DATA TYPES AND DATA DEFINITION 69 ‘80x86 data types 69 Assembler data directives 69 ORG (origin) 69 DB (define byte) 69 DUP (duplicate) 70 DW (define word) 70 EQU (equate) 71 DD (define doubleword) 71 DQ (define quadword) 72 DT (define ten bytes) 72 SECTION 2.6: SIMPLIFIED SEGMENT DEFINITION 73 Memory model 4 Segment definition 74 xi SECTION 2.7: EXEVS.COMFILES 76 Why COM files? 76 Converting from EXEtoCOM = 77 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS 82 SECTION 3.1; UNSIGNED ADDITION AND SUBTRACTION 83 83 Analysis of Program 3-la 84 CASE 2: Addition of multiword numbers 85 Analysis of Program 3-286 Subtraction of unsigned numbers 87 ‘SBB (subtract borrow) 88 SECTION 3.2: UNSIGNED MULTIPLICATION AND DIVISION 88 Multiplication of unsigned numbers 88 Division of unsigned numbers 90 SECTION 33: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS 93 AND 93 OR 93 XOR 94 SHIFT 95 COMPARE of unsigned numbers 96 IBM BIOS method of converting from lowercase to uppercase 99 BIOS examples of logic instructions 100 SECTION 3.4 BCD AND ASCII OPERANDS AND INSTRUCTIONS. 101 BCD number system 101 Unpacked BCD 102 Packed BCD 102 ASCII numbers 102 ASCII to BCD conversion 102 ASCII to unpacked BCD conversion 102 ASCII to packed BCD conversion 103 Packed BCD to ASCII conversion 104 BCD addition and subtraction 104 BCD addition and correction 104 DAA 105 Summary of DAA action 105 BCD subtraction and correction 105 Summary of DAS action 107 ASCH addition and subtraction 109 Unpacked BCD multiplication and division 110 AAM 110 AAD 110 SECTION 3.5: ROTATE INSTRUCTIONS II Rotating the bits of an operand right and left 111 ROR rotate right ML ROL rotate left 2 RCR rotate right through carry 113 RCL rotate left through cary 113. SECTION 3.6: BITWISE OPERATION IN THE C LANGUAGE 114 Bitwise operators inC 114 Bitwise shift operators in 115 Packed BCD-to-ASCII conversion in C116 Testing bits inC 116 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C 121 SECTION 4.1: BIOS INT 10H PROGRAMMING 122 Monitor screen in text mode 122 Clearing the screen using INT 10H function 06H 123 INT 10H function 02: setting the cursor to a specific location 123 INT 10H function 03: get current cursor position 124 Changing the video mode 124 Attribute byte in monochrome monitors 125 Attribute byte in CGA text mode 125 Graphics: pixel resolution and color 127 INT 10H and pixel programming ‘128 Drawing horizontal or vertical lines in graphics mode 128 Changing the background color 129 SECTION 4.2: DOS INTERRUPT 21H 130 INT 211 option 09: outputting a string to the monitor 130 INT 21H option 02: outputting a character to the monitor 130 INT 21H option 01: inputting a character, with echo 130 INT 21H option OAH: inputting a string from the keyboard 131 Inputting more than the buffer size 132 Use of carriage return and line feed 134 INT 21H option 07: keyboard input without echo 135 Using the LABEL directive to define a string buffer 136 SECTION 4.3: INT 16H KEYBOARD PROGRAMMING — 139 Checking akey press 139 Which key is pressed? 139 SECTION 4.4: INTERRUPT PROGRAMMING WITH C14] Programming BIOS interrupts with C/C++ 141 Programming INT 21H DOS functions calls with C/C++ 143 Accessing segment registers. 144 Accessing the carry flag in int86 and intdos functions 144 ing C with Assembly and checking ZF 145 C function kbhit vs. INT 16H keyboard input 146 xiii CHAPTER 5: MACROS AND THE MOUSE 150 SECTION 5.1: WHAT IS A MACRO AND HOW IS IT USED? 151 MACRO definition 151 Comments in a macro 152, Analysis of Program 5-1 154 LOCAL directive and its use in macros 155, INCLUDE directive 158 SECTION 5.2: MOUSE PROGRAMMING WITH INT 33H. 161 INT33H 161 Detecting the presence ofa mouse 161 Some mouse terminology 162 Displaying and hiding the mouse cursor 162 Video resolution vs. mouse resolution in text mode 163 Video resolution vs. mouse resolution in graphics mode 163 Getting the current mouse cursor position (AX=03) 163 Setting the mouse pointer position (AX=04) 166 Getting mouse button press information (AX=05) 166 Monitoring and displaying the button press count program 167 Getting mouse button release information (AX=06) 168 Setting horizontal boundary for mouse pointer (AX=07) 168 Setting vertical boundary for mouse pointer (AX=08) 168 Setting an exclusion area for the mouse pointer (AX=10) 169 Getting mouse driver information (version) (AX=24H) 169 CHAPTER 6: SIGNED NUMBERS. STRINGS, AND TABLES 173 SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 174 Concept of signed numbers in computers 174 174 174 174 Word-sized signed numbers 175 Overflow problem in signed number operations 176 When the overflow flag is set in 8-bit operations 176 Overflow flag in 16-bit operations 177 Avoiding erroneous results in signed number operations 178 IDIV (Signed number division) 179 IMUL (Signed number multiplication) 180 Arithmetic shift 182 SAR (shift arithmetic right) 182 SAL (shift arithmetic lefi) and SHL (shifi left) 182 Signed number comparison 182 xiv SECTION 6.2: STRING AND TABLE OPERATIONS 184 Use of SI and DI, DS and ES in string instructions 185 Byte and word operands in string instructions 185 DF, the direction flag 185 REP prefix 186 STOS and LODS instructions 186 Testing memory using STOSB and LODSB_——187 The REPZ and REPNZ prefixes 187 SCAS (scan string) 189 Replacing the scanned character 189 XLAT instruction and look-up tables 190. Code conversion using XLAT 190 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING —_ 193 SECTION 7.1: WRITING AND LINKING MODULES 194 ‘Why modules? 194 Writing modules 194 EXTRN directive 194 PUBLIC directive 194 END directive in modules 195 Linking modules together into one executable unit 196 SEGMENT directive 198 Complete stack segment definition Complete data and code segment defi Analysis of Program 7-2 link map 200 Modular programming and the new segment definition 201 198 SECTION 7.2: SOME VERY USEFUL MODULES 203 Binary (hex)-to-ASCII conversion 203 ASCH (decimal)-to-binary (hex) conversion 204 Binary-to-ASCII module 205 ASCIl-to-binary module 207 Calling module 207 ‘SECTION 7.3: PASSING PARAMETERS AMONG MODULES 208 Passing parameters via registers 208 Passing parameters via memory 208 Passing parameters via the stack 208 SECTION 7.4: COMBINING ASSEMBLY LANGUAGE AND C 210 Whyc? 210 Inserting 80x86 assembly code into C programs 211 programs that call Assembly procedures. 212 C calling convention 213 How parameters are retumedtoC 214 ‘New assemblers and linking withC 215 Passing array addresses from C to the stack 216 Linking assembly language routines with 217 xvi CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES 220 SECTION 8.1: 80386/80486 MACHINES IN REAL MODE 221 General registers are pointers in 386/486 222 386/486 maximum memory range in real mode: IM 224 Accessing 32-bit registers with commonly used assemblers 224 Little endian revisited 226 ‘SECTION 8.2: SOME SIMPLE 386/486 PROGRAMS —_ 226 Adding 16-bit words using 32-bit registers 226 Adding multiword data in 386/486 machines 228 Multiplying a 32-bit operand by a 16-bit operand 229 32-bit by 16-bit multiplication using 8086/286 registers 229 SECTION 8.3: 80x86 PERFORMANCE COMPARISON 231. Running an 8086 program across the 80x86 family 231 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS 235 SECTION 9.1: 8088 MICROPROCESSOR 236 Microprocessor buses 236 Data bus in 8088 236 Address bus in 8088 238 8088 control bus 238 of 8088 239 Other 8088 pins 240 SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS 242 8288 bus controller 242 Input signals 242 Output signals 243 8284 clock generator 244 Input pins 244 Output signals 245 SECTION 9.3: 8-BIT SECTION OF ISA BUS 248 Abit of bus history 246 Local bus vs. system bus 247 Address bus 247 Data bus 248 Control bus 249 One bus, two masters 249 AEN signal generation 249 Control of the bus by DMA 250 Bus boosting 250 &-bit section of the ISA bus 250 SECTION 9.4: 80286 MICROPROCESSOR 251 Pin descriptions 252 SECTION 9.5: 16-BIT ISA BUS 255 Exploring ISA bus signals 255 Address bus 256 Data bus 256 Memory and 1/0 control signals 256 Other contro! signals 258 ODD and EVEN bytes and BHE 259 A20 gate and the case of high memory area (HMA) CHAPTER 10: MEMORY AND MEMORY INTERFACING. 260 265 ‘SECTION 10.1: SEMICONDUCTOR MEMORY FUNDAMENTALS 266 Memory capacity 266 Memory organizat Speed 267 ROM (read-only memory) 267 PROM (programmable ROM) or OTP ROM 268 EPROM (erasable programmable ROM) 268 EEPROM (electrically erasable programmable ROM} Flash memory 270 Mask ROM — 271 RAM (random access memory) 271 SRAM (static RAM) 271 DRAM (dynamic RAM) 273 Packaging issue in DRAM 273 DRAM, SRAM and ROM organizations 275 NV-RAM (nonvolatile RAM) 276 266 SECTION 10.2: MEMORY ADDRESS DECODING — 276 Simple logic gate as address decoder 278 Using the 748138 as decoder 279 SECTION 10.3: IBM PC MEMORY MAP 280 Conventional memory: 640K of RAM 281 BIOS data area 282 Video display RAM (VDR) map 282 ROM address and cold boot on the PC. 283 SECTION 10.4: DATA INTEGRITY IN RAM AND ROM Checksum byte 284 Checksum program 286 Use of parity bit in DRAM error detection 286 DRAM memory banks 286 Parity bit generator/checker in the IBM PC 288 748280 parity bit generator and checker 288 ) 269 284 xvii SECTION 10.5: 16-BIT MEMORY INTERFACING 289 ODD and EVEN banks 289 Memory cycle time and inserting wait states 291 Accessing EVEN and ODD words 292 Bus bandwidth 293 SECTION 10.6: ISA BUS MEMORY INTERFACING 295 Address bus signals 295 Memory control signals 295 ISA bus timing for memory 299 8-bit memory timing for ISA bus 299 ROM duplicate and x86 PC memory map 301 Shadow RAM 302 DIMM and SIMM memory modules 302 CHAPTER 11: /O AND THE 8255; ISA BUS INTERFACING — 309 SECTION 11.1: 8088 INPUT/OUTPUT INSTRUCTIONS 310 &bit data ports 310 How to use I/O instructions 3u SECTION 10.2: /O ADDRESS DECODING AND DESIGN 312 Using the 74L$373 in an output port design 312 IN port design using the 74LS244 312 Memory map VO. 314 SECTION 11.3: /O ADDRESS MAP OF X86 PCS 316 ‘Absolute vs. linear select address decoding 316 Prototype addresses 300 - 31FH in the x86 PC 316 Use of simple logic gates as address decoders. 316 Use of 74LS 138 as decoder 318 IBM PC I/O address decoder 318 ‘Use of the 8255 in the IBM PC/XT 341. Port 61H and time delay generation 319 SECTION 11.4: 8255 PPI CHIP 320 Mode selection of the 82554 321 SECTION 11.5: PC INTERFACE TRAINER AND BUS EXTENDER 325 PC I/O Bus Extender 325 Buffering 300 - 31F address range 326 Installing the PC Bus Extender and booting the PC 327 Failure to boot 327 PC Interface Trainer 327 Design of the PC Trainer 328 ‘The role of HI and H2 328 Connecting the Module Trainer to the PC and testing 328 Testing the 8255 port 329 Testing Port A 330 xviii SECTION 11.6: YO PROGRAMMING WITH C/C++ AND VB 332 Visual C/C++ I/O programming, 332 Visual C+* output example 332 Visual C+ input example 332 VO programming in Turbo CIC++ 334 VO programming in Linux C/C++ 335 Linux C/C++ program with VO functions 335, SECTION 11.7: 8-BIT AND 16-BIT /O TIMING IN ISA BUS 338 S-bit and 16-bit I/O in ISA bus 338 VO signals of the ISA bus 339 B-bit timing and operation in ISA bus 341 16-bit VO operation and timing in ISA bus 342 16-bit data ports instruction 342 16-bit VO timing and operation via ISA bus 342 V/O bus bandwidth for ISA 343 Interfacing 8-bit peripherals to a 16-bit data bus 344 CHAPTER 12: INTERFACING TO THE PC: LCD, MOTOR, ADC, AND SENSOR 351 SECTION 12.1: INTERFACING AN LCD TO THE PC 352 LCD operation 352 LcD descriptions 352 Sending commands to LCDs 353 Sending data tothe LCD 355 Checking LCD busy flag 356 LCD cursor position 357 LCD programming in Visual C/C++ 358 LCD timing and data sheet 358 SECTION 12.2: INTERFACING A STEPPER MOTOR TO A PC 362 Stepper motors 362 Stepangle 363 Stepper motor connection and programming 364 Steps per second and RPM relation 365 ‘The four-step sequence and number of teeth on rotor 365 Motor speed 366 Holding torque 366 Wave drive 4-step sequence 367 SECTION 123: INTERFACING DAC TO APC 368 Digital-to-analog (DAC) converter 368 MC1408 DAC (or DAC 808) 369 Converting IOUT to voltage in 1408 DAC 369 Generating a sine wave 369 xix SECTION 12.4; INTERFACING ADC AND SENSORS TO THE PC_ 373 ADC devices 373 ADC 804 chip 373 Selecting an input channel 376 ADCO0848 connection to $255 377 Interfacing a temperature sensor toaPC 378 LM34 and LM35 temperature sensors 378 Signal conditioning and interfacing the LM35 toaPC 379 CHAPTER 13: 8253/54 TIMER AND MUSIC 386 ‘SECTION 13.1: 8253/54 TIMER DESCRIPTION AND INITIALIZATION 387 Initialization of the 8253/54 388 Control word 388 SECTION 13.2: IBM PC 8253/54 TIMER CONNECTIONS AND PROGRAMMING 391 Using counter0 392 Using counter | _393 Using counter 2 393 Use of timer 2 by the speaker 394 ‘Turning on the speaker via PBO and PBI of port 61H 394 Time delay for 80x86 PCs 394 Creating time delays in 8088/86-based computers 395 Time delays in 80x86 IBM PC for 286 and higher processors 395 SECTION 13.3: GENERATING MUSIC ON THE IBM PC 397 Playing "Happy Birthday” on the PC 399 SECTION 13.4: SHAPE of 8253/54 OUTPUTS 401 OUTO pulse shape in IBM BIOS 401 OUT! pulse shape in IBM BIOS 402 ‘OUT? pulse shape in JBM BIOS 402 8253/54 modes of operation 402 Testing the 8255/54 timer of the PC Interface Trainer 407 CHAPTER 14: INTERRUPTS AND THE 8259 CHIP 410 SECTION 14.1: 8088/86 INTERRUPTS 411 Interrupt service routine (ISR) 411 Difference between INT and CALL instructions 412 Categories of interrupts 413 Hardware interrupts 413 Software interrupts 413 Interrupts and the flag register 414 Processing interrupts 414 Functions associated with INT 00 toINT04 415 ‘SECTION 142: IBM PC AND DOS ASSIGNMENT OF INTERRUPTS 417 Examining the interrupt vector table of your PC 417 Analyzing an IBM BIOS interrupt service routine 419 INT 12H: checking the size of RAM on the IBM PC 419 ‘SECTION 143:8259 PROGRAMMABLE INTERRUPT CONTROLLER 420 8259 control words and ports 421 Masking and prioritization of IRO- IR7 interrupts 426 OCW (operation command word) 426 OCWI (operation command word 1) 427 OCW? (operation command word 2) 427 Importance of the EOI (end of interrupt) command 429 OCW3 (operation command word 3) 429 SECTION 14.4: USE OF THE 8259 CHIP IN THE IBM PC/XT 430 Interfacing the 8259 to the 8088 in IBM PC/XT computers 430 Initialization words of the 8259 in the IBM PC/XT 431 Sequences of hardware interrupts with the 8259 432 Sources of hardware interrupts in the IBM PC/XT 433 ‘Sources of NMI in the IBM PC. 433 ‘SECTION 145: INTERRUPTS ON 80286 AND HIGHER 80x86 PCs 436 IBM PC AT hardware interrupts 436 8259 in master mode 436 8259 in slave mode 437 AT-type computers interrupt assignment 438 Case of missing IRQs on the AT expansion slot 438 80x86 microprocessor generated interrupts (exceptions) 439 Interrupt priority 441 More about edge- and level-triggered interrupts 441 Interrupt sharing in the x86 PC 442 CHAPTER 15: DIRECT MEMORY ACCESSING; THE 8237 DMA CHIP 447 SECTION 15.1: CONCEPT OF DMA 448. SECTION 15.2: 8237 DMA CHIP PROGRAMMING 450 8237s internal control registers. 453 Command register 453 Status register 454 Mode register 456 Single mask register 457 All mask register 457 Master clearitemporary register 458 Clear mask register 459 SECTION 15.3: 8237 DMA INTERFACING IN THE IBM PC/XT 459 8237 and 8088 connections in the IBM PC 459 Channel assignment of the 8237 in the IBM PC/XT 463, DMA page register 463 DMA data transfer rate of the PC/XT 464, xxi SECTION 15.4: REFRESHING DRAM USING CHANNEL 0 OF THE 8237 465 Refreshing DRAM with the 8237 467 Refreshing in the IBM PC/XT 467 DMA cycle of channel 0 467 ‘SECTION 15.5: DMA IN 80x86-BASED PC AT-TYPE COMPUTERS 468 8237 DMA #1 468 8237DMA #2 469 Points to be noted regarding 16-bit DMA channels 470 DMA channel priority 471 1/0 cycle recovery time 471 DMA transfer rate 472 CHAPTER 16: DEO AND VIDEO ADAPTERS 477 SECTION 16.1: PRINCIPLES OF MONITORS AND VIDEO ADAPTERS 478 How to judge a monitor 478 Dot pitch 480 Dot pitch and monitor size 480 Phosphorous materials 480 Color monitors 481 Analog and digital monitors 481 Video display RAM and video controller 481 Character box 482 SECTION 16.2: VIDEO ADAPTERS AND TEXT MODE PROGRAMMING 484 CGA (color graphies adapter) 484 Video RAM inCGA 484 Attribute byte in CGA text mode 485 MDA (monochrome display adapter) 486 Video RAM inMDA 486 Attribute byte in IBMMDA 487 EGA (enhanced graphics adapter) 487 EGA video memory and attribute 487 MCGA (multicolor graphics array) 488 VGA (video graphics array) 489 Video memory and attributes in VGA 489 Super VGA (SVGA) and other video adapters 491 ‘SECTION 163: TEXT MODE PROGRAMMING USING INT 10H. 491 Finding the current video mode 491 Changing the video mode 491 Setting the cursor position (AH=02) 493, Getting the current cursor position (AH=03) 493 Scrolling the window up to clear the sereen (AH=06) 493 Writing a character in teletype mode (AH=0E) 494 Writing a string in teletype mode (AH=13H) 495 Character generator ROM 495 How characters are displayed in text mode 497 Character definition table in VGA 498 ‘Changing the cursor shape using INT 10H 498 SECTION 16.4: GRAPHICS AND GRAPHICS PROGRAMMING 501 Graphics: pixel resolution, color, and video memory SOI The case of CGA SOI The case of EGA 502 Video memory size and color relation for EGA $02 ‘The case of VGA 502 Video memory size and color relation for VGA 503 The case of SVGA graphics 503 INT 10H and pixel programming 504 Drawing horizontal or vertical lines in graphics mode 504 CHAPTER 17: SERIAL DATA COMMUNICATION AND THE 16450/8250/51 CHIPS 508 SECTION 17.1: BASICS OF SERIAL COMMUNICATION — 509 Half. and full-duplex transmission 510 Asynchronous serial communication and data framing 511 Start and stop bits 511 Data transfer rate 512 RS232 and other serial /O standards 513 RS232 pins, 513 Other serial VO interface standards S14 Data communication classification 514 Examining the RS232 handshaking signals 514 SECTION 17.2: ACCESSING IBM PC COM PORTS USING DOS AND BIOS 516 IBMPCCOM ports 516 Using the DOS MODE command 517 Data COM programming using BIOS INT 14H 520 SECTION 17.3: INTERFACING THE NS8250/16450 UART IN THE IBMPC 522 8250 pin descriptions 522 ‘The 8250 registers 524 Limitation of the 8250/16450 UART and 16550 530 SECTION 17.4: INTEL 8251 USART AND SYNCHRONOUS: COMMUNICATION 531 Intel's 8251 USART chip 531 ‘Synchronous serial data communication 531 ‘SDLC (serial data link control) 535 Cyclic redundancy checks 535 CHAPTER 18: KEYBOARD AND PRINTER INTERFACING 541 SECTION 18.1: INTERFACING THE KEYBOARD TO THE CPU 542 ‘Scanning and identifying the key 542 Grounding rows and reading the columns 543 SECTION 18.2: PC KEYBOARD INTERFACING AND PROGRAMMING 546 Make and break 546 IBM PC scan codes 546 BIOS INT 16H keyboard programming 549 Hardware INT 09 role in the IBM PC keyboard 551 Keyboard overrun 552 Keyboard buffer in BIOS data area 552 BIOS keyboard buffer 553 Tail pointer 553 Head pointer 553 PC keyboard technology 553 SECTION 18.3: PRINTER AND PRINTER INTERFACING IN THE IBM PC 554 Centronics printer interface pins 554 Data lines and grounds 556 Printer status signals 556 Printer control signals 556 IBM PC printe1 rfacing S57 Programming the IBM PC printer with BIOS INT 17H 559 ‘What is printer time-out? 560 ASCII control characters 560 Inner working of BIOS INT 17H for printing a character 561 SECTION 18.4: BIDIRECTIONAL DATA BUS IN PARALLEL PORTS 562 SPP 562 PS/2 562 How to detect a PS/2-type bidirectional data bus 563 EPP 563 ECP 563 Using an LPT port for output 564 LCD connection to the parallel port 564 Stepper motor connection to the parallel port 564 Data input buffering 566 BIOS data area and LPT 1/0 address 566 CHAPTER 19: FLOPPY DISKS, HARD DISKS, AND FILES 570 SECTION 19.1: FLOPPY DISK ORGANIZATION 571 Capacity of the floppy disk 572 Formatting disks 572 Disk organization $72 xxiv Looking into the boot record 573 Directory 577 Bootable and nonbootable disks 579 FAT (file allocation table) 580 How to calculate sector locations of the FAT and the directory. 582 SECTION 19.2: HARD DISKS 583 Hard disk capacity and organization $83 Partitioning 585 Hard disk layout 585 Hard disk boot record 585 Hard disk FAT 585 ‘Clusters 585 Hard disk directory 585 Speed of the hard disk 585 Data encoding techniques in the hard disk 586 Interfacing standards in the hard disk 588 Interleaving 591 Low- and high-level formatting 592 Parking the head 592 Disk caching 592 Disk reliability 592 SECTION 19.3: DISK FILE PROGRAMMING 593 File handle and error code 593 CHAPTER 20: THE 80x87 MATH COPROCESSOR 600 SECTION 20.1: MATH COPROCESSOR AND IEEE FLOATING- POINT STANDARDS 601 IEEE floating point standard 601 IEEE single-precision floating-point numbers 602 IEEE double-precision floating-point numbers 602 Other data formats of the 8087 604 SECTION 20.2: 80x87 INSTRUCTIONS AND PROGRAMMING 605 ‘Assembling and running 80x87 programs on the IBM PC_ 605 Verifying the Solution for Examples 20-1 to 20-4 605 80x87 registers 607 Trig functions 612 Integer numbers 614 SECTION 20.3: 8087 HARDWARE CONNECTIONS IN THE IBM PCIXT 616 8087 and 8088 connection in the IBM PC/XT 616 How the 8088 and 8087 work together in the IBM PC/XT 618 xKv SECTION 20.4: 80x87 INSTRUCTIONS AND TIMING 620 Real transfers 620 Integer transfers 621 Packed decimal transfers 621 Addition 621 Subtraction 621 Reversed subtraction 622 Multiplication 622 Division 622 Reversed division 622 Other arithmetic instructions 622 Compare instructions 623 Transcendental instructions 623 Constant instructions 624 Processor control instructions 625 CHAPTER 21: 386 MICROPROCESSOR: REAL vs. PROTECTED MODE 631 SECTION 21.1: 80386 IN REAL MODE 632 ‘What happened to the 80186/188? 632 0186/88 instructions 632 80286 Microprocessor 634 Major changes in the 80386 634 80386 Real mode programming 635 32-bit registers 635 Which end goes first? 636 General registers as pointers 636 Sealed index addressing mode 637 ‘Some new 386 instructions 639 MOVSX and MOVZX instructions 639 Bit scan instructions 640 SECTION 21.2: 80386: A HARDWARE VIEW 641 ‘Overview of pin functions of the 80386 642 Bus bandwidth in the 386 645, Data misalignment in the 386 646 UO address space in the 386 646 SECTION 21.3: 80386 PROTECTED MODE 647 Protection mechanism in the 386 647 Virtual memory 647 Segmentation and descriptor table 648 Local and global descriptor tables 651 64 Terabtyes of virtual memory 651 Paging 652 Going from a linear address to a physical address 653 ‘The bigger the TLB, the better 654 Virtual 8086 mode 654 CHAPTER 22: HIGH-SPEED MEMORY INTERFACING AND CACHE 659 SECTION 22.1: MEMORY CYCLE TIME OF THE 80X86 660 Introducing wait states into the memory cycle 660 SECTION 22.2: PAGE, STATIC COLUMN, AND NIBBLE MODE DRAMS 662 Memory access time vs. memory cycle time 662 Types of DRAM 662 DRAM (standard mode) 663 DRAM interfacing using the interleaving method 663 Interleaved drawback 665 Page mode DRAM 667 Static column mode 669 Nibble mode 669 Timing comparison of DRAM modes 671 SECTION 22.3: CACHE MEMORY 672 Cache organization 673 Fully associative cache 673 Direct-mapped cache 674 Set associative 676 Updating main memory 678 Write-through 678 Write-back (copy-back) 678 Cache coherency 679 Cache replacement policy 679 Cache fill block size 679 SECTION 22.4: EDO, SDRAM, AND RAMBUS MEMORIES 680. EDO DRAM: origin and operation 680 SDRAM (synchronous DRAM) 682 Synchronous DRAM and burst mode 682 SDRAM and interleaving 683 Rambus DRAM 683 Overview of Rambus technology 683 Rambus protocol for block transfer 684 CHAPTER 23: 486, PENTIUM, PENTIUM PRO AND MMX 690 SECTION 23.1: THE 80486 MICROPROCESSOR 691 Enhancements of the 486 691 CLK in the 80486 694 High memory area (HMA) and the 80486 695 386, 486 Performance comparison 695 More about pipelining 695 SECTION 23.2: INTEL'S PENTIUM — 697 Features of the Pentium 699 Intel's overdrive technology 703 SECTION 23.3: RISC ARCHITECTURE 704 Features of RISC 704 ‘Comparison of sample program for RISC and CISC 707 IBM/Motorola RISC 709 SECTION 23.4: PENTIUM PRO PROCESSOR 710 Pentium Pro: internal architecture 710 Pentium Pro is both superpipelined and superscalar 711 What is out-of-order execution? 71 Branch prediction 714 Bus frequency vs, internal frequency in Pentium Pro 714 "TION 23.5: MMX TECHNOLOGY 715 DSP and multimedia 715 Register aliasing by MMX 715 Data types in MMX 716 ‘SECTION 23.6: PROCESSOR IDENTIPICATION IN INTEL X86. 717 Program to identify the CPU 717 CPUID instruction and MMX technology 718 CHAPTER 24: MS DOS STRUCTURE, TSR, AND DEVICE DRIVERS 724 SECTION 24.1: MS DOS STRUCTURE 725 DOS genealogy 725 From cold boot to DOS prompt 725 DOS standard device names 728 More about CONFIG.SYS and how it is used 728 What is AUTOEXEC.BAT and how is it used? 729 Types of DOS commands 730 SECTION 24.2: TSR AND DEVICE DRIVERS 731 Executing but not abandoning the program 731 How to make a program resident 731 Invoking the TSR 732 Hooking into hardware interrupts 732 Replacing the CS:1P values in the interrupt vector table 732 Writing a simple TSR 732 TSR with hotkeys 734 Hooking into timer clock INT 08 735 DOS is not reentrant 736 Device drivers 736 Device driver categories 737 CHAPTER 25: MS DOS MEMORY MANAGEMENT 740 SECTION 25.1: 80x86 PC MEMORY TERMINOLOGY AND CONCEPTS 741 Conventional memory. 741 Upper memory area 741 IBM standard using ROM space in the upper memory area 742 Expanded memory 743 Extended memory 746 High memory area (HMA) 746 Shadow RAM 748 DOS MEM command 748 SECTION 25.2: DOS MEMORY MANAGEMENT AND LOADING HIGH 749 Loading high into HMA 749 Finding holes in the upper memory area 750 EMM386.EXE options and switches 751 Loading high TSR and device driver into upper memory area. 754 Emulating expanded memory and using UMB in 386/486/Pentium PC 755 How expanded memory is accessed 756 CHAPTER 26: IC TECHNOLOGY AND SYSTEM DESIGN CONSIDERATIONS 759 SECTION 26.1: OVERVIEW OF IC TECHNOLOGY 760 MOS vs. bipolar transistors 760 Overview of logic families 761 ‘The case of inverters 761 CMOS inverter 762 Input, output characteristics of some logic families 762 tory of logic fami 763 Recent advances in logic families 764 Evolution of IC technology in Intets 80x86 microprocessors 765 SECTION 26.2: IC INTERFACING AND SYSTEM DESIGN CONSIDERATIONS — 766 IC fan-out 766 Capacitance derating 768 Power dissipation considerations 770 Dynamic and static currents 771 Power-down option and Intel's SL series 771 Ground bounce 771 Filtering the transient currents Bulk decoupling capacitor 774 Crosstalk 774 ‘Transmission line ringing 775 decoupling capacitors 774 SECTION 26.3: DATA INTEGRITY AND ERROR DETECTION INDRAM 776 Soft error and hard error 776 Mean time between failure (MTBF) and FIT for DRAM 777 Error detection and correction 778 ECL and gallium arsenide (GaAs) chips 780 xxix CHAPTER 27: ISA, EISA, MCA, LOCAL, AND PCI BUS 784 SECTION 27.1: ISA, EISA, AND IBM MICRO CHANNEL 785 Master and slave 785 Bus arbitration 785 Bus protocol 785 Bus bandwidth 786 ISA buses 786 36-pin part of the ISA bus 789 Limitations of the ISA bus 791 IBM Micro Channel Architecture (MCA) 793 Major characteristics of MCA. 794 EISA bus 795 EISA slot numbering 797 Bus performance comparison 798 SECTION 27.2: VL BUS AND PCI LOCAL BUSES 799 Definition and merits of local bus 799 VL bus (VESA local bus) characteristics 801 PCI local bus 801 PCI local bus characteristics 801 Plug and play feature 804 PCI connector 804 PCI performance 804 CHAPTER 28: PROGRAMMING DOS, BIOS, HARDWARE WITH C/C++ 808 SECTION 28.1: BIOS & DOS INTERRUPT PROGRAMMING WITHC 809 Programming BIOS interrupts with C/C++ 809 Finding the conventional memory size with INT 12H 811 INT 16H and keyboard access 812 Programming INT 21H DOS function calls with C/C++ 812 Accessing segment registers 812 Accessing the carry flag in int86 and intdos functions 814 SECTION 28.2: PROGRAMMING PC HARDWARE WITH CIC++ 815 Accessing 80x86 SEGMENT:OFFSET memory addresses 815 Accessing BIOS data area with C815 Programming input/output ports with C/C++ 816, Revisiting playing music 816 Accessing parallel printer's (LPT!) data bus with 816 Finding memory above IMB: the extended memory size 819 Programming the CMOS RAM real-time clock (RTC) 820 Accessing the CMOS RAM bytes 820 Programming CMOS RAM with C/C++ 822 APPENDIX A: DEBUG PROGRAMMING. 825 APPENDIX B: 80x86 INSTRUCTIONS AND TIMING 847 APPENDIX C: ASSEMBLER DIRECTIVES AND NAMING RULES 883 APPENDIX D: DOS INTERRUPT 21H AND 33H LISTING 898 APPENDIX E: BIOS INTERRUPTS = 924. APPENDIX F: ASCIICODES 940 APPENDIX G: /O ADDRESS MAPS 941 APPENDIX H: IBM PC/PS BIOS DATA AREA 952 APPENDIX I: DATA SHEETS 959 REFERENCES — 967 INDEX 969 xxi PREFACE TO VOLUMES | AND il Purpose ‘This combined volume is intended for use in college-level courses in which both Assembly language programming and 80x86 PC interfacing are dis- cussed. It not only builds the foundation of Assembly language programming, but also provides a comprehensive treatment of 80x86 PC design and interfacing for students in engineering and computer science disciplines. This volume is intend- ed for those who wish to gain an in-depth understanding of the internal working of the IBM PC, PS, and 80x86 compatible computers. It builds a foundation for the design and interfacing of microprocessor-based systems using the real-world example of the 80x86 IBM PC. In addition, it can also be used by practicing tech- nicians, hardware engineers, computer scientists, and hobbyists who want to do PC interfacing and data acquisition. Prerequisites Readers should have a minimal familiarity with the IBM PC and the DOS operating system in addition to having had an introductory digital course. Knowledge of other programming languages would be helpful, but is not neces- sary. Although a vast majority of current PCs use 386, 486, or Pentium micro- processors, their design is based on the IBM PC/AT, an 80286 microprocessor system introduced in 1984, A good portion of PC/AT features, hence its limita- tions, are based on the original IBM PC, an 8088 microprocessor system, intro- duced in 1981. In other words, one cannot expect to understand fully the arc tectural philosophy of the 80x86 PC and its expansion slot signals unless the 80286 PC/AT and its subset, the IBM PC/XT, are first understood. For this rea- son, we describe the 8088 and 80286 microprocessors in Chapter 9. Contents of Volume | ‘A systematic, step-by-step approach has been used in covering various aspects of Assembly language programming. Many examples and sample pro- grams are given to clarify concepts and provide students an opportunity to fearn by doing. Review questions are provided at the end of each section to reinforce the main points of the section. We feel that one of the functions of a textbook is to familiarize the student with terminology used in technical literature and in industry, so we have followed that guideline in this text. ‘Chapter 0 covers concepts in number systems (binary, decimal, and hex) ‘and computer architecture, Most students will have learned these concepts in pre- vious courses, but Chapter 0 provides a quick overview for those students who have not leamed these concepts, or who may need to refresh their memory. Chapter | provides a brief history of the evolution of x86 microproces- sors and an overview of the internal workings of the 8086 as a basis of all x86 processors. Chapter 1 should be used in conjunction with Appendix A (a tutorial introduction to DEBUG) so that the student can experiment with concepts being learned on the PC. The order of topics in Appendix A has been designed to cor- respond to the order of topics presented in Chapter 1. Thus, the student can begin programming with DEBUG without having to learn how to use an assembler. Chapter 2 explains the use of assemblers to create programs. Although the programs in the book were developed and tested with Microsoft's MASM assembler, any Intel-compatible assembler such as Borland’s TASM may be used. xxxili xxi Chapter 3 introduces the bulk of the logic and arithmetic instructions for unsigned numbers, plus bitwise operations in C. Chapter 4 introduces DOS and BIOS interrupts. Programs in Assembly and C allow the student to get input from the keyboard and send output to the monitor. In addition, interrupt programming in C is described, as well as how to put Assembly language code in C programs. Chapter 5 describes how to use macros to develop Assembly language programs in a more time-efficient and structured manner. We also cover INT 33H mouse function calls and mouse programming, Chapter 6 covers arithmetic and logic instructions for signed numbers as, well as string processing instructions. Chapter 7 discusses modular programming and how to develop larger Assembly language programs by breaking them into smaller modules to be coded ‘and tested separately. In addition, linking Assembly language modules with C programs is thoroughly explained. Chapter 8 introduces some 32-bit concepts of 80386 and 80486 pro- gramming. Although this book emphasizes 16-bit programming, the 386/486 is introduced to help the student appreciate the power of 32-bit CPUs. Several pro- grams are run across the 80x86 family to show the dramatic improvement in clock eycles with the newer CPUs. Contents of Volume II Chapter 9 describes the 8088 and 286 microprocessors and supporting. chips in detail and shows how they are used in the original IBM PC/XT/AT. In addition, the origin and function of the address, data, and control signals of the ISA expansion slot are described. Chapter 10 provides an introduction to various types of RAM and ROM memories, their interfacing to the microprocessor, the memory map of the 80x86 PC, the timing issue in interfacing memory to the ISA bus, and the checksum byte and parity bit techniques of ensuring data integrity in RAM and ROM. Chapter 11 is dedicated to the interfacing of VO ports, the use of IN and OUT instructions in the 80x86, and interfacing and programming of the 8255 pro- grammable peripheral chip. We describe /O programming in several languages, as well. Chapter 12 covers the PC Interface Trainer and Bus Extender, which are used to interface PCs to devices for data acquisition such as LCDs, stepper motors, ADC, DAC, and sensors. Chapter 13 discusses the use of the 8253/54 timer chip in the 80x86 PC, as well as how to generate music and time delays. Chapter 14 is dedicated to the explanation of hardware and software inter- rupts, the use of the 8259 interrupt controller, the origin and assignment of IRQ signals on the expansion slots of the ISA bus, and exception interrupts in 80x86 microprocessors. Chapter 15 is dedicated to direct memory access (DMA) concepts, the use. of the 8237 DMA chip in the 80x86 PC, and DMA channels and associated sig- nals on the ISA bus. Chapter 16 covers the basics of video monitors and various video modes ‘and adapters of the PC, in addition to the memory requirements of various video boards in graphics mode. Chapter 17 discusses serial communication principles, the interfacing and programming of National Semiconductor's 8250/16450/16550 UART chip, Intel's 8251 USART chip, and verifying data integrity using the CRC method. Chapter 18 covers the interfacing and programming of the keyboard in the 80x86 PC, in addition to printer port interfacing and programming. In addi- tion, a discussion of various types of parallel ports such as EPP and ECP is includ- ed. Chapter 19 discusses both floppy and hard disk storage organization and terminology. We also show how to write Assembly language programs to access files using INT 21H DOS function calls Chapter 20 cxamines the 80x87 math coprocessor, its programming and interfacing, and IEEE single and double precision floating point data types. Chapter 21 explores the programming and hardware of the 386 micro- processor, contrasts and explains real and protected modes, and discusses the implementation of virtual memory. Chapter 22 is dedicated to the interfacing of high-speed memories and describes various types of DRAM, including EDO and SDRAM, and examines cache memory and various cache organizations and terminology in detail. In Chapter 23 we describe the main features of the 486, Pentium and Pentium Pro and compare these microprocessors with the RISC processors. Chapter 23 also provides a discussion of MMX technology and how to write pro- grams to detect which CPU a PC has. ‘Chapter 24 describes the MS DOS structure and the role of CONFIG.SYS and batch files in the 80x86 PC, the writing of TSR (terminate and stay resident) programs and device drivers. ‘Chapter 25 explains 80x86 PC memory terminology, such as convention al memory, expanded memory, upper memory block, high memory area, as well as MS DOS memory management. Chapter 26 provides an overview of the IC technology including the recent advances in the IC fabrication, describes IC interfacing and system design issues, and covers error detection and correction. Chapter 27 is dedicated to the discussion of the various types of PC buses, such as ISA, EISA, USB, their performance comparisons, the local bus and fea- tures of the PCI local bus. In Chapter 28 we show how to use C language to access DOS function calls, BIOS interrupts, memory, input/output ports, and CMOS RAM of the 80x86. Appendices The appendices have been designed to provide all reference material required for the topics covered in this combined volume so that no additional ref- erences should be necessary. ‘Appendix A provides a tutorial introduction to DEBUG. Appendix B pro- vides a listing of Intel's 8086 instruction set along with clock cycles for 80x86 microprocessors. Appendix C describes assembler directives with examples of their use. Appendix D lists some commonly used DOS 21H function calls and INT 33H mouse functions. Appendix E lists the function calls for various BIOS interrupts. Appendix F provides a table of ASCH codes. Appendix G lists the VO map of 80x86-based ISA computers. Appendix H provides a description of the BIOS data area. Appendix I contains data sheets for various IC chips. Lab Manual ‘The lab manual for this series is available on the following web site: www.microdigitaled.com Xxxv Acknowledgments This book is the result of the dedication, work and love of many individ- uals. Our sincere and heartfelt appreciation goes out to all of them. First, we must inal reviewers who provided valuable suggestions and encourage- ment: Mr. William H. Shannon of the University of Maryland, Mr. Howard W. ‘Atwell of Fullerton College, Mr. David G. Delker of Kansas State University, Mr. Michael Chen of Duchess Community College, Mr. Yusuf Motiwala of Prairie View A&M University, and Mr. Donaid '. Coston of ITT Technical Institute. We were truly amazed by the depth and breadth of their knowledge of microproces- sor-based system design in general and 80x86 PC architecture in particular. We sincerely appreciate their comments and suggestions. ‘Thanks also must go to the many students whose comments have helped shape this book, especially Daniel Woods, Sam Oparah, Herbert Sendeki, Greg, Boyle, Philip Fitzer, Adnan Hindi, Kent Keeter, Mark Ford, Shannon Looper, Mitch Johnson, Carol Killelea, Michael Madden, Douglas McAlister, David Simmons, Dwight Brown, Clifton Snyder, Phillip Boatright, Wilfrid Lowe, ‘obert Schabel, John Berry, Clyde Knight, Robert Jones (all of DeVry Institute of Technolgy), Lynnette Garetz (Heald College), Peter Woof (Southem Sydney Institute, Lidcombe College of Tafe), M. Soleimanzadeh, Mark Lessley, Snehal Amin, Travis Erek, Gary Hudson, Nathan Noel, Dan Bent, and Frank Fortman. A. word must also be said of our colleagues, especially the fate Mr. Allan Escher, whose encouragement set the making of this series into motion. For the last 25 years, his dedication and love of microprocessor education were a source of inspiration to many. A special thanks goes to Mr. James Vignali for his enthu- siasm in discussing the internal intricacies of the 80x86 PC and his readiness to keep current with the ever-changing world of the PC. In addition, we offer our appreciation for the dedicated professionals at Prentice Hall, Many thanks to Charles Stewart for his continued support and guid- ance of this series. Finally, we would like to sincerely thank the following professors from some outstanding enginecring schools whose enthusiasm for the book, sugges- tions, and kind words have been encouraging to us and made us think we are on the right track: Dr. Michael Chwialkowski (Electrical Engineering Dept., University of Texas at Arlington), Dr. Roger $. Walker (Computer Science Engineering Dept., University of Texas at Arlington), Dr. Behbood Zoghi (Electronics Engineering Technology, Texas A&M University). ABOUT THE AUTHORS Muhammad Ali Mazidi holds Master's degrees from both Southern Methodist University and the University of Texas at Dallas, and currently is a.b.. Ph.D. in the Electrical Engineering Department of Southern Methodi University. He is a co-founder and chief researcher of Microprocessor Education Group, a company dedicated to bringing knowledge of microprocessors to the widest possible audience. He also teaches microprocessor-based system design at DeVry Institute of Technology in Dallas, Texas. Janice Gillispie Mazidi has a Master of Science degree in Computer Science from the University of North Texas. After several years experience as a software engineer in Dallas, she co-founded Microprocessor Education Group, where she is the chief technical writer, production manager, and is responsible for software development and testing. ‘The Mazidis have been married since 1985 and have two sons, Robert Nabil and Michael Jamal. ‘The authors can be contacted at the following address if you have any comments, suggestions, or if you find any errors, Microprocessor Education Group P.O. Box 381970 Duncanville, TX 75138 email: mazidi@mail.dal.devry.edu or: profimazidi@yahoo.com The web site www.microdigitaled.com provides much support for this book. xxxvil CHAPTER 0 INTRODUCTION TO COMPUTING SECTION 0.1: To understand the software and hardware of the computer, one must first master some very basic concepts underlying computer design. In this chapter (hich in the tradition of digital computers can be called Chapter 0), the fundamen- tals of numbering and coding systems are presented. ‘Then an introduction to the workings of the inside of the computer is given. Finally, in the last section we give a brief history of CPU architecture, Although some readers may have an adequate background in many of the topics of this chapter, itis recommended that the material be scanned, however briefly NUMBERING AND CODING SYSTEMS, Whereas human beings use base 10 (decimal) arithmetie, computers use the base 2 (binary) system. In this section we explain how to convert from the decimal system to the binary system, and vice versa, The convenient representation of binary numbers called hexadecimal also is covered. Finally, the binary format of the alphanumeric code, called ASCI, is explored. Decimal and binary number systems Although there has been speculation that the origin of the base 10 system is the fact that human beings have 10 fingers, there is absolutely no speculation about the reason behind the use of the binary system in computers. The binary system is used in computers because 1 and 0 represent the two voltage levels of on and off. Whereas in base 10 there are 10 distinct symbols, 0, 1, 2, .., 9, in base 2 there are only two, Oand I, with which to generate numbers. Base 10 contains digits, O through 9; binary contains digits 0.and 1 only. These two binary digits, 0 and 1, are commonly referred to as bits. Converting from decimal to binary One method of converting from decimal to binary is to divide the decimal number by 2 repeatedly, keeping track of the remainders. This process continues until the quotient becomes zero. The remainders are then written in reverse order to obtain the binary number. This is demonstrated in Example 0-1 Example 0-1 Convert 259 to binary. Solution: Quotient Remainder 25/2 2 1 LSB (least significant bit) 122 6 0 62 = 3 0 32 1 1 12 0 1 MSB (most significant bit) ‘Therefore, 2549 = 11001, Converting from binary to decimal To convert from binary to decimal, itis important to understand the concept of weight associated with each digit position. First, as an analogy, recall the weight of numbers in the base 10 system: CHAPTER 0; INTRODUCTION TO COMPUTING 34068345 = 3x10 = 3 axigt = 80 oe = 600 x10? = 0000 axio# = 40000 x10® = 700000 740683 By the same token, each digit position in a number in base 2 has a weight associated with it: 110103, Decimal Binary a2? sa. = 1 1 ox2! x2 = 9 oo x2? = 14 4 100 0x2? = x8 0 000 1x24 = 1x16 16 10000 12> 1x32 32 10009 53 1 Knowing the weight of each bit in a binary number makes it simple to add them together to get its decimal equivalent, as shown in Example 0-2. Example 0-2 Ea ‘Convert 110013 to decimal. Solution: Weight: 16H eae 1 Digits: 1 1 0 0 1 Sum: 16+ 8+ OF OF 15254 Knowing the weight associated with each binary bit position allows one to convert a decimal number to binary directly instead of going through the process of repeated division. This is shown in Example 0-3. Example 0-3 a Use the concept of weight to convert 39,9 to binary. Solution: Weight: 32,16 1 0 324 0+ Therefore, 399 = 1001113, con f S Hexadecimal system Base 16, the hexadecimal system as it is called in computer literature, is used as a convenient representation of binary numbers. For example, it is much ‘easier for a human being to represent a string of Os and 1s such as 100010010110 as its hexadecimal equivalent of 896H. The binary system has 2 digits, 0 and 1. The base 10 system has 10 digits, 0 through 9. The hexadecimal (base 16) system must have 16 digits. In base 16, the first 10 digits, 0 to 9, are the same as in decimal, and for the remaining six digits, the letters A, B,C, D, E, and F are used. Table 0-1 shows the equivalent binary, decimal, and hexadecimal representations for 0 to 15. SECTION 0.1: NUMBERING AND CODING SYSTEMS 3 al, Binary, and Hex Converting between binary and hex Table 0-1: Decimal, Bina To represent a binary number Decimal___Binary _. Hexadecimal asits equivalent hexadecimal number, start from the right and group 4 bits at a time, replacing each 4-bit binary number with its hex equivalent shown in Table 0-1. To convert from hex to binary, each hex digit is replaced with its 4-bit binary equivalent. Converting between binary and hex is shown in Examples 0-4 and 0-5. Converting from decimal to hex Converting from decimal to hex could be approached in two ways: 1. Convert to binary first and then con- vert to hex. Experimenting with this method is left to the reader. 2, Convert directly from decimal to hex by the method of repeated division, Keeping track of the remainders. Ex- ample 0-6 demonstrates this method ‘of converting decimal to hex. Converting from hex to decimal Conversion from hex to deci- ‘mal can also be approached in two ways: 1. Convert from hex to binary and then to decimal, 2. Convert directly ftom hex to decimal by summing the weight of all digits. Example 0-7 demonstrates the second method of converting from hex to decimal, Example 0-4 Represent binary 100111110101 in hex. Solution: First the number is grouped into sets of 4 bits: 1001 1111 0101 Then each group of 4 bits is replaced with its hex equivalent: 1001 1111-0101 9 P 5 Therefore, 100111110101, = 9FS hexadecimal Example 0-5 Convert hex 29B to binary, Solution: 2 9 8B = 0010 10011011 Dropping the leading zeros gives 1010011011 4 CHAPTER 0: INTRODUCTION TO COMPUTING Example 0-6 a (a) Convert 4549 to hex, Solution: Quotient Remainder 45116 = 2 13 (hex D) (least significant digit) 26 = 0 2 (most significant digit) Therefore, 4549 = 2D6 (b) Convert decimal 629 to hexadecimal. Solution: Quotient Remainder 629/16 39S (least significant digit) 39/16 2H al 26 = 0 2 (most significant digit) Therefore, 6299 = 275, (©) Convert 1714 base 10 to hex. Solution: Quotient Remainder 1714/16 = (107 2_—_—(least significant digit) 107/16 = 6 1 (hex BY 616 = 0 6 (most significant digit) Therefore, 17149 = 6B216 Example 0-7 ‘Convert the following hexadecimal numbers to decimal (@) 6B2ig Solution: 6B2 hexadecimal = p16 = 2x1 = 2 aixigl = apaée = 176 x16? = 6x256 = 1536 1714 Therefore, 6B2j¢ = 171440 (b) 9F2D)6 Solution: 9F2D hexadecimal = 13x16° 13x - 13 2x62 mae = 32 isxi62 = 15256 3840 x16? = 9x4096 = 36864 40749 Therefore, 9F2Dj5= 40749 jg. SECTION 0.1: NUMBERING AND CODING SYSTEMS 5 Decimal Binary Hex Counting in bases 10, 2, and 16 4 0 00000 0 ‘To show the relationship between 1 00001 L all three bases, in Figure 0-1 we show the 2 00010 2 sequence of numbers from 0 to 31 in deci- 3 00011 3 mal, along with the equivalent binary and 4 00100 4 hex numbers. Notice in each base that 2 oold1 S when one more is added to the highest 6 pgiie | digit, that digit becomes zero and a 1 is 3 eae 3 3 carried to the next-highest digit position. 7 $toee : For example, in decimal, 9 + | = 0 with a 10 01010 in carry to the next-highest position, In bi- i 01021 B nary, 1 +1 = 0 with a carry; similarly, in 12 01100 c hex, F + 1 = 0 with a carry. 13 01101 D 14 01110 E ‘Table 0-2: Binary Addition te aD00e i (A+B Carry Sum ie oe Fr 0+0 0 0 19 10021 13 a 20 40100 4 O+1 0 oop 3 7 1 3 101 15 16110 16 ptt+o. 0 1 23 10111 i7 aH 24 11000 18 — 25 41001 19 Addition of binary and hex numbers | 35 fagra ae The addition of binary numbersis, i ates + a very straightforward process. Table 0-2 30 ilo iB shows the addition of two bits. The dis- 31 in iE cussion of subtraction of binary numbers is bypassed since all computers use the addition process to implement subtrac- tion, Although computers have adder cit Figure 0-1. Counting in 3 Basos cuitry, there is no separate circuitry for subtractors. Instead, adders are used in conjunction with 2 complement circuitry to perform subtraction. In other words, to implement "x — y", the computer takes the 2's complement of y and adds it tox. The concept of 2's complement is reviewed next, but the process of subtraction of two binary numbers using 2's complement is shown in detail in Chapter 3, Example 0-8 shows the addition of binary numbers, Example 0-8 ‘Add the following binary numbers. “heck against their decimal equivalents. Solution Binary Decimal 1101 1001 + 10110 101100 2's complement To get the 2’s complement of a binary number, invert all the bits and then add | to the result. Inverting the bits is simply a matter of changing all Os to 1s and. 1s to 0s. This is called the 1 complement. See Example 0-9. CHAPTER 0: INTRODUCTION TO COMPUTIN Example “Take the 2’s complement of 10011101 Solution: 10011101 binary number 01100010 ‘1's complement + 1 01100011 2's complement Addition and subtraction of hex numbers In studying issues related to software and hardware of computers, itis often necessary to add or subtract hex numbers. Mastery of these techniques is essential Hex addition and subtraction are discussed separately below. Addition of hex numbers This section describes the process of adding hex numbers. Starting with the least significant digits, the digits are added together. If the result is less than 16, ‘write that digit as the sum for that position. If it is greater than 16, subtract 16 from it to get the digit and carry | to the next digit. ‘The best way to explain this is by ‘example, as shown in Example 0-10. Example 0-10 Perform hex addition: 23D9 + 94BE. Solution: 23D9 + S4BE B897 LSD: 9+14 1+13411 1+ 344 MSD: 2+9=B 23.23 - 16=7 with a carry to next digit 25.25 — 16 =9 witha carry to next digit 8 ‘Subtraction of hex numbers. In subtracting two hex numbers, if the second digit is greater than the first, borrow 16 from the preceding digit. See Example 0-11 Example 0-11 Perform hex subtraction: SOF - 2B8, Solution: SOF 288 267 LSD: 8 from 15 11 from 25 (9 + 16)= 14, which is E MSD: — 2 from4 (5-1) =2 SECTION 0,1: NUMBERING AND CODING SYSTEMS 7 ASCII code. The discussion so far has revolved around the representation of number systems. Since all information in the computer must be represented by Os and 1s, binary pattems must be assigned to letters and other characters. In the 1960s a standard representation called ASCIT (American Standard Code for Information Interchange) was established. The ASCII (pronounced "ask-E") code assigns binary patterns for numbers 0 to 9, all the letters of the English alphabet, both uppercase (capital) and lowercase, and many control codes and punctuation marks. The great advantage of this system is that itis used by most computers, so that information can be shared among computers. The ASCII system uses a total of bits to represent each code. For example, 100 0001 is assigned to the uppercase letter "A" and 110 0001 is for the lowercase "a". Often, a zero is placed in the most significant bit position to make it an 8-bit code. Figure 0-2 shows selected ASCII codes. A complete list of ASCII codes is given in Appendix F. The use of ASCII is not only standard for keyboards used in the United States and many other countries but also provides a standard for printing and displaying characters by output devices such as printers and monitors, The patiern of ASCII codes was designed to allow for easy manipulation of ASCII data. For example, digits 0 through 9 are represented by ASCII codes 30 through 39. This enables a program to easily convert ASCII to decimal by masking off the "3" in the upper nibble. As another example, notice in the codes listed below that there is a relationship between the uppercase and lowercase letters. ‘Namely, uppercase letters are represented by ASCII codes 41 through 5A while lowercase letters are represented by ASCII codes 61 through 7A. Looking at the binary code, the only bit that is different between uppercase "A" and lowercase "a" is bit 5. Therefore conversion between uppercase and low- cercase is as simple as changing bit 5 of the ASCII code. Hex ‘Symbol Hex ‘Symbol 41 A 61 a 42 B 62 b 43 c 63 c 44 D 64 d 45 E 65 e 48 F 66 f 47 G o7 9 48 Hq 68 h 49 1 69 i 4a J 6A i 4B K 6B k 4c L 6c 1 4D M 6D m 4E N 6E n ar ° oF ° 50 P 70 P 51 Q 7 q 52 R 72 r 53 s 73 5 54 T 74 t 55 u 75 u 56 v 76 v 87 w 7 w 38 x 78 x 59 Y 79 y BA Zz 7A z Figure 0-2. Alphanumeric ASCII Codes 8 CHAPTER 0: INTRODUCTION TO COMPUTING en easene SECTION 0.2: Review Questions Why do computers use the binary number system instead of the decimal system? Convert 3470 to binary and hex. Convert 1101012 to hex and decimal. Perform binary addition: 101100 + 101. Convert 101100) to its 2°s complement representation. Add 36BH + FOH Subtract 36BH ~ F6H. Write "80x86 CPUs" in its ASCH code (in hex form). INSIDE THE COMPUTER In this section we provide an introduction to the organization and internal working of computers. The model used is generic, but the concepts discussed are applicable to all computers, including the IBM PC, PS/2, and compatibles. Before ‘embarking on this subject, it will be helpful to review definitions of some of the ‘most widely used terminology in computer literature, such as K, mega, giga, byte, ROM, RAM, and so on. ‘Some important terminology ‘One of the most important features of a computer is how much memory it has. Next we review terms used to describe amounts of memory in IBM PCs and compatibles. Recall from the discussion above that a bit is a binary digit that can have the value 0 or 1. A byte is defined as 8 bits. A nibble is half a byte, or 4 bits ‘Aword is two bytes, or 16 bits. The following display is intended to show the relative size of these units. Of course, they could all be composed of any combination of zeros and ones, Bit ° Nibble 0000 Byte 0000 0000 word 0000 0000 0900 9000 A kilobyte is 2!° bytes, which is 1024 bytes, The abbreviation K is often used. For example, some floppy disks hold 356K bytes of data. A megabyte, or meg as some call it, is 22° bytes, That is a little over | million bytes; it is exactly 1,048,576. Moving rapidly up the scale in size, a gigabyte is 2°° bytes (over 1 billion), and a terabyte is 2*" bytes (over | trillion). As an example of how some ofthese terms are used, suppose that a given computer has 16 megabytes of memory. That would be 16 x 22°, or 24x 229, which is 24. Therefore 16 megabytes is 24 bytes. ‘Two types of memory commonly used in microcomputers are RAM, which stands for random access memory (sometimes called read/write memory), and ROM, which stands for read-only memory. RAM is used by the computer for temporary storage of programs that itis running. That data is lost when the computer is tumed off. For this reason, RAM is sometimes called volatile memory. ROM contains programs and information essential to operation of the computer. The information in ROM is permanent, cannot be changed by the user, and is not lost when the power is turned off. Therefore, itis called nonvolatile memory. Internal organization of computers The internal working of every computer can be broken down into three parts: CPU (central processing unit), memory , and J/O (inpuvloutput) devices (see Figure 0-3). The function of the CPU is to execute (process) information stored in memory. The function of /O devices such as the keyboard and video monitor is to provide a means of communicating with the CPU. The CPU is connected to memory SECTION 0.2: INSIDE THE COMPUTER and VO through strips of wire called a bus. The bus inside a computer carries information from place to place just asa street bus carries people from place to place In every computer there are three types of buses: address bus, data bus, and conivol bus. For a device (memory or 1/0) to be recognized by the CPU, it must be assigned an address. ‘The address assigned to a given device must be unique: no two devices are allowed to have the same address. The CPU puts the address (of course, in binary) on the address bus, and the decoding circuitry finds the device Then the CPU uses the data bus either to get data from that device or to send data to it. The control buses are used to provide read or write signals to the device to indicate ifthe CPU is asking for information or sending it information. Of the three buses, the address bus and data bus determine the capability of a given CPU. Address bus pO eee et Memory Peripherals. cPU (monitor, (RAM, ROM) printer, etc.) py Data bus Figure 0-3. Inside the Computer More about the data bus Since data buses are used to carry information in and out of a CPU, the more data buses available, the better the CPU. If one thinks of data buses as highway lanes, itis clear that more lanes provide a better pathway between the CPU and its external devices (such as printers, RAM, ROM, etc.; see Figure 0-4). By the same token, that inetease in the number of lanes increases the cost of construction, More data buses mean a more expensive CPU and computer. The average size of data buses in CPUs varies between 8 and 64. Early computers such as Apple 2 used an 8-bit data bus, while supercomputers such as Cray use a 64-bit data bus. Data buses ate bidirectional, since the CPU must use them either to receive or to send data. The processing power of a computer is related to the size of its buses, since an 8-bit bus can send out I byte a time, but a 16-bit bus can send out 2 bytes at a time, which is twice as fast More about the address bus Since the address bus is used to identify the devices and memory connected to the CPU, the more address buses available, the larger the number of devices that can be addressed. In other words, the number of address buses fora CPU determines the number of locations with which it can communicate. The number of locations is always equal to 2%, where x is the number of address lines, regardless of the size of the data bus. For example, a CPU with 16 address lines can provide a total of 65,536 (2!6) or 64K bytes of addressable memory. Each location can have a maximum of 1 byte of data. This is due to the fact that all general-purpose ‘microprocessor CPUs are what is called byte addressable. As another example, the IBM PC AT uses a CPU with 24 address lines and 16 data lines. In this case the total accessible memory is 16 megabytes (224 = 16 megabytes). In this example there would be 2”4 locations, and since each location is one byte, there would be 16 megabytes of memory. The address bus is a unidirectional bus, which means that the CPU uses the address bus only to send out addresses. To summarize: The total number of memory locations addressable by a given CPU is always equal to 2° where x is the number of address bits, regardless of the size of the data bus. 10 CHAPTER 0: INTRODUCTION TO COMPUTING Address bus Printer Control bus Figure 0-4. intemal Organization of Computers: CPU and its relation to RAM and ROM For the CPU to process information, the data must be stored in RAM ot ROM. The function of ROM in computers is to provide information that is fixed and permanent. This is information such as tables for character patterns to be displayed on the video monitor, or programs that are essential to the working of the ‘computer, such as programs for testing and finding the total amount of RAM installed on the system, or programs to display information on the video monitor. In contrast, RAM is used to store information that is not permanent and can change with time, such as various versions of the operating system and application packages. such as word processing or tax calculation packages. These programs are loaded into RAM to be processed by the CPU. The CPU cannot get the information from. the disk directly since the disk is to0 slow. In other words, the CPU gets the information to be processed, first from RAM (or ROM). Only ifit is not there does the CPU seek it from a mass storage device such as a disk, and then it transfers the information to RAM. For this reason, RAM and ROM are sometimes referred to as primary memory and disks are called secondary memory. Figure 0-4 shows a block diagram of the internal organization of the PC. Inside CPUs A program stored in memory provides instructions to the CPU to perform an action. The action can simply be adding data such as payroll data or controlling ‘a machine such as a robot. It is the function of the CPU to fetch these instructions from memory and execute them. To perform the actions of fetch and execute, all CPUs are equipped with resources such as the following: 1. Foremost among the resources at the disposal of the CPU are a number of registers, ‘The CPU uses registers to store information temporarily. The information could be two values to be processed, or the address of the value needed to be fetched from memory. Registers inside the CPU can be 8-bit, 16-bit, 32-bit, or even 64-bit registers, depending on the CPU. In general, the more and bigger the registers, the better the CPU. The disadvantage of more and bigger registers is the increased cost of such a CPU. 2. The CPU also has what is called the ALU (arithmetic/logic unit). The ALU section of the CPU is responsible for performing arithmetic functions such as ada, subtract, multiply, and divide, and logic funetions such as AND, OR, and NOT. 3. Every CPU has what is called a program counter. The function of the program counter is to point to the address of the next instruction to be executed. As each instruction is executed, the program counter is incremented to point to the address of the next instruction to be executed. Itis the contents of the program counter that are placed on the address bus to find and fetch the desired instruction, In the IBM PC, the program counter is a register called IP, or the instruction pointer. SECTION 0.2: INSIDE THE COMPUTER 12 4, The function of the instruction decoder is to interpret the instruction fetched into the CPU. One can think of the instruction decoder as a kind of dictionary, storing the meaning of each instruction and what steps the CPU should take upon receiving a given instruction. Just as a dictionary requires more pages the more words it defines,a CPU capable of understanding more instructions requires more transistors to design. Internal working of computers To demonstrate some of the concepts discussed above, a step-by-step analysis of the process a CPU would go through to add three numbers is given next. Assume that an imaginary CPU has registers called A, B, C, and D. It has an 8-bit address bus. ‘Therefore, the CPU can access memory from addresses 0000 to FFFFH (for a total of 10000H locations). The action to be performed by the CPU is to put hexadecimal value 21 into register A, and then add to register A values 42H and 12H. Assume that the code for the CPU to move a value to register A is 1011 0000 (BOH) and the code for adding a value to register ‘Ais 0000 0100 (04H). The necessary steps and code to perform them are as follows. Action, a Code Data Move value 21H into register A OH 21H ‘Add value 42H to register A ost 42H ‘Add value 12H to register A ost 12H If the program to perform the actions listed above is stored in memory locations starting at 1400H, the following would represent the contents for each memory address location: Memory address Contents of memory address 1400 (60) the code for moving a value to register A 1401 (21) the value to be moved 1402 (04) the code for adding a value to register A 1403 (42) the value to be added 1404 (04) the code for adding a value to register A 1405 (12) the value to be added 1408 (F4) the code for halt ‘The actions performed by the CPU to run the program above would be as follows: 1. The CPU's program counter can have a value between 0000 and FFFFH. The program counter must be set to the valuc 1400H, indicating the address of the frst instruction code to be executed. After the program counter has been loaded with the address of the first instruction, the CPU is ready to execute. 2. The CPU puts 14001 on the address bus and sends it out. The memory circuitry finds the location while the CPU activates the READ signal, indicating to memory that it wants the byte at location 1400H. This causes the contents of memory location 1400H, which is BO, to be put on the data bus and brought into the CPU. 3. The CPU decodes the instruction BO with the help of its instruction decoder dictionary. When it finds the definition for that instruction it knows it must bring into register A of the CPU the byte in the next memory location. Therefore, it ‘commands its controller circuitry to do exactly that. When it brings in value 21H from memory location 1401, it makes sure that the doors of all registers are closed except register A. Therefore, when value 21H comes into the CPU itwill go directly into register A. After completing one instruction, the program counter points to the address of the next instruction to be executed, which in this case is 1402H, Address 1402 is sent out on the address bus to fetch the next instruction. 4, From memory location 1402H it fetches code 0411. After decoding, the CPU knows that it mustadd to the contents of register A the byte sitting atthe next address (1403), ‘After it brings the value (in this case 42H) into the CPU, it provides the contents of CHAPTER 0: INTRODUCTION TO COMPUTING nape 9, 10. on 12. 13, SECTION 0.3: register A along with this value to the ALU to perform the addition. It then takes the result of the addition from the ALU’s output and puts it in register A. Meanwhile the program counter becomes 1404, the address of the next instruction. Address 1404H is put on the address bus and the code is fetched into the CPU, decoded, and executed. This code isagain adding a value to register A. The program counter is updated to 1406H. Finally, the contents of address 1406 arc fetched in and executed. This HALT instruction tells the CPU to stop incrementing the program counter and asking for the next instruction. In the absence of the HALT, the CPU would continue updating the program counter and fetching instructions. ‘Now suppose that address 1403H contained value 04 instead of 42H. How would the CPU distinguish between data 04 to be added and code 04? Remember that code 04 for this CPU means move the next value into register A. Therefore, the CPU will not try to decode the next value. It simply moves the contents of the following memory location into register A, regardless of its value. Review Questions How many bytes is 24 kilobytes? What does "RAM" stand for? How is it used in computer systems? ‘What does "ROM" stand for? How is it used in computer systems? Why is RAM called volatile memory? List the three major components of a computer system, What does "CPU" stand for? Explain its function in a computer, List the three types of buses found in computer systems and state briefly the pur- pose of each type of bus. State which of the following is unidirectional and which is bidirectional, (a) data bus () address bus If an address bus for a given computer has 16 lines, then what is the maximum amount of memory it can access? What does "ALU" stand for?) What is its purpose? How are registers used in computer systems? What is the purpose of the program counter? What is the purpose of the instruction decoder? BRIEF HISTORY OF THE CPU In the 1940s, CPUs were designed using vacuum tubes. The vacuum tube was bulky and consumed a lot of electricity. For example, the first large-scale digital computer, ENIAC, consumed 130,000 watts of power and occupied 1500 square feet, The invention of transistors changed all of that. In the 1950s, transistors replaced vacuum tubes in the design of computers. Then in 1959, the first IC (integrated circuit) was invented. This set into motion what many people believe is the second industrial revolution. In the 1960s the use of IC chips in the design of CPU boards became common. It was not until the 1970s that the entire CPU was put on a single IC chip. The first working CPU on a chip was invented by Intel in 1971. This CPU was called a microprocessor. The first microprocessor, the 4004, had a 4-bit data bus and was made of 2300 transistors. It was designed primarily for the hand-held calculator but soon came to be used in applications such as traffic-light controllers. The advances in IC fabrication made during the 1970s made it possible to design microprocessors with an 8-bit data bus and a 16-bit address bus. By the late 1970s, the Intel 8080/85 was one of the most widely used microproces- sors, appearing in everything fiom microwave ovens to homemade computers. Meanwhile, many other companies joined in the race for faster and better micro- processors. Notable among them was Motorola with its 6800 and 68000 microproc- essors. Apple's Macintosh computers use the 68000 series microprocessors. Figure 0-5 shows a block diagram of the internal structure of a CPU. SECTION 0.3: BRIEF HISTORY OF THE CPU 13 Address bus Program counter Instruction register Instruction decoder, timing, |-—Gonte! mu and control Flags u Data os bus Register A [| Register B isin Register C buses [-— Register D Figure 0-6. Internal Block Diagram of a CPU CISC vs. RISC Until the early 1980s, all CPUs, whether single-chip or whole-board, followed the C/SC (complex instruction set computer) design philosophy, CISC refers to CPUs with hundreds of instructions designed for every possible situation. To design CPUs with so many instructions consumed not only hundreds of thou- sands of transistors, but also made the design very complicated, time-consuming, and expensive. In’the early 1980s, a new CPU design philosophy called RISC (reduced instruction set computer) was developed. The proponents of RISC argued that no one was using all the instructions etched into the brain of CISC-type CPUs. Why not streamline the instructions by simplifying and reducing them from hun- dreds to around 40 or so and use all the transistors that are saved to enhance the power of the CPU? Although the RISC concept had been explored by computer scientists at IBM as carly as the 1970s, the first working single-chip RISC micro- processor was implemented by a group of researchers at the University of California at Berkeley in 1980. Today the RISC design philosophy is no longer an experiment limited to research laboratories. Since the late 1980s, many companies designing new CPUs (either single-chip or whole-board) have used the RISC philosophy. It appears that eventually the only CISC microprocessors remaining in use will be members of the 80x86 family (8086, 8088, 80286, 80386, 80486, 80586, etc.) and the 680x0 family (68000, 68010, 68020, 68030, 68040, 68050, etc.). The 80x86 will be kept alive by the huge base of IBM PC, PS, and compatible computers, and the Apple Macintosh is prolonging the life of 680x0 microprocessors. Review Questions 1, What is 2 microprocessor? Describe briefly how advances in technology have affected the size, cost, and avail- ability of computer systems. Explain the major difference between CISC and RISC computers. CHAPTER 0: INTRODUCTION TO COMPUTING SUMMARY PROBLEMS eon aw The binary number system represents all numbers with a combination of the two binary digits, 0 and 1. The use of binary systems is necessary in digital ‘computers because only two states can be represented: on or off. Any binary number canbe coded directly into its hexadecimal equivalent for the convenience of humans, Converting from binary/hex to decimal, and vice versa, isa straightforward process that becomes easy with practice. The ASCII code is binary code used to represent alphanumeric data intemally in the computer. It is frequently used in peripheral devices for input and/or output. The major components of any computer system are the CPU, memory, and VO devices. "Memory" refers to temporary or permanent storage of data. In most systems, memory can be accessed as bytes or words. The terms kilobyte, megabyte, gigabyte, and terabyte are used to refer to large numbers of bytes. There are two main types of memory in computer systems: RAM and ROM. RAM (random access memory) is used for temporary storage of programs and data. ROM (read-only memory) is used for permanent storage of programs and data that the computer system must have in order to function. All components of the computer system are under the control of the CPU. Peripheral devices such as 1/O (input/output) devices allow the CPU to communicate with humans or other computer systems. There are three types of buses in computers: address, control, and data. Control buses arc used. by the CPU to direct other devices. The address bus is used by the CPU to locate a device ora memory location. Data buses are used to send information back and forth between the CPU and other devices. As changes in technology were incorporated into the design of computers, their cost and size were reduced dramatically. The earliest computers were as large as an average home and were available only to a select group of scientists. The invention of transistors and subsequent advances in their design have made the ‘computer commonly available. As the limits of hardware innovation have been approached, computer designers are looking at new design techniques, such as RISC architecture, to enhance computer performance. Convert the following decimal numbers to binary. (a) 12 (b) 123 (c) 63 (d) 128 (e) 1000 Convert the following binary numbers to decimal. (a) 100100 (b) 1000001 (c) 11101 (@ 1010 (©) 00100010 Convert the values in Problem 2 to hexadecimal Convert the following hex numbers to binary and decimal. (@)2B9H—(b) FSH (e) 912H (28H (©) FFFFH Convert the values in Problem I to hex. Find the 2's complement of the following binary numbers. (a) 1001010 (b) 111001 (e) 10000010 (A) 111110001 ‘Add the following hex values. (a) 2CH+3FH (6) F34H+5D6H (c) 20000H+12FFH (d) FFFFH+2222H Perform hex subtraction for the following. (a) 24FH-129H (6) FESH-SCCH (c) 2FFFFH-FFFFFH (d) 9FF2SH-4DD99H Show the ASCH codes for numbers 0, 1, 2,3, .. 9 in both hex and binary. Show the ASCII code (in hex) for the following string "US.A. isa country” CRLF "in North America” CRLF CRis carriage return LF is line feed SUMMARY 15 UL. 12, 1B. 14, 15, 16. Answer the following: (a) How many nibbles are 16 bits? (b) How many bytes are 32 bits? (©) Ifa word is defined as 16 bits, how many words is a 64-bit data item? (@) What is the exact value (in decimal) of 1 meg? (e) How many K is 1 meg? (®) What is the exact value (in decimal) of giga? (g) How many K is 1 giga? (h) How many meg is | giga? (@)_ Ifa given computer has a total of 8 megabytes of memory, how many bytes (in decimal) is this? How many kilobytes is this? A given mass storage device such as a hard disk can store 2 gigabytes of informa- tion. Assuming that each page of text has 25 rows and each row has 80 columns of ASCII characters (each character = | byte), approximately how many pages of information can this disk store? Ina given byte-addressable computer, memory locations 10000H to 9FFFFH are available for user programs. The first location is 10000H and the last location is SFFFFH. Calculate the following: (a) The total number of bytes available (in decimal) (b) The total number of kilobytes (in decimal) A given computer has a 32-bit data bus. What is the largest number that can be carried into the CPU at a time? Below are listed several computers with their data bus widths. For each com- puter, list the maximum value that can be brought into the CPU at a time (in both hex and decimal). (a) Apple 2 with an 8-bit data bus (b) IBM PS/2 with a 16-bit data bus (c) IBM PS/2 model 80 with a 32-bit data bus (@) CRAY supercomputer with a 64-bit data bus Find the total amount of memory, in the units requested, for each of the following CPUs, given the size of the address buses. (a) 16-bit address bus (in K) (b) 24-bit address bus (in meg) (©) 32-bit address bus (in megabytes and gigabytes) (d) 48-bit address bus (in megabytes, gigabytes and terabytes) Regarding the data bus and address bus, which is unidirectional and which is bi- directional? .. Which register of the CPU holds the address of the instruction to be fetched? . Which section of the CPU is responsible for performing addition? ). Which type of CPU (CISC or RISC) has the greater variety of instructions? ANSWERS TO REVIEW QUESTIONS ‘SECTION 0.1: NUMBERING AND CODING SYSTEMS. Computers use the binary system because each bit can have one of two voltage levels: on and off, 3hyo= 1000102 = 2246 $101012 = 3546 = 5319 a110004 010100 461 215: 38 30 78 38 26 20 43 50 65 73 16 CHAPTER 0: INTRODUCTION TO COMPUTING ‘SECTION 0.2: INSIDE THE COMPUTER 1. 24.876 2. random access memory; itis used for temporary storage of programs that the CPU is running, such as, the operating system, word processing programs. etc. 3, read-only memory; itis used for permanent programs such as those that control the keyboard, etc 4; the contents of RAM are lost when the computer is powered off 5. the CPU, memory, and I/O devices 6. central processing unit it can be considered the “brain” of the computer, it executes the programs and ‘controls all other devices in the computer 7. the address bus carries the location (adéress) needed by the CPU; the data bus carries information in and out of the CPU; the control bus is used by the CPU fo send signals controlling I/O devices &. (a) bidirectional (6) unidirectional 9, 64K, o- 65,536 bytes 10. arithmeticiogic unit; it performs all arithmetic end logic operations 11. for temporary storage of information 12. itholds the address of the next instruction to be executed 13. ittells the CPU what steps to perform for each instruction SECTION 0.3: BRIEF HISTORY OF THE CPU 1. aCPUona single chip 2. The transition from vacuum tubes to transistors to ICs reduced the size and cost of camputers and therefore made them more widely available. 3. CISC computers use many instructions whereas RISC computers use a small set of instructions. ANSWERS TO REVIEW QUESTIONS 7 CHAPTER 1 THE 80x86 MICROPROCESSOR 18 CHAPTER L: THE 80x86 MICROPROCESSOR This chapter begins with a history of the evolution of Intel's family of microprocessors. The second section is an overview of the intemal workings of 80x86 microprocessors. An introduction to 80x86 Assembly language programming is given in the third section, The fourth and fifth sections cover segments of Assembly language programs and how physical addresses are generated. Finally, the last section describes in detail the addressing modes of the 80x86, SECTION 1.1: BRIEF HISTORY OF THE 80x86 FAMILY In this section we trace the evolution of Intel’s family of microprocessors from the late 1970s, when the personal computer had not yet found widespread acceptance, to the powerful microcomputers widely in use today. Evolution from 8080/8085 to 8086 In 1978, Intel Corporation introduced a 16-bit microprocessor called the 8086. This processor was a major improvement over the previous generation 8080/8085 series Intel microprocessors in several ways. First, the 8086's capacity of 1 megabyte of memory exceeded the 8080/8085's capability of handling a maximum of 64K bytes of memory. Second, the 8080/8085 was an 8-bit system, meaning that the microprocessor could work on only & bits of data at a time. Data larger than 8 bits had to be broken into 8-bit pieces to be processed by the CPU. In contrast, the 8086 is a 16-bit microprocessor. Third, the 8086 was a pipelined processor, as opposed to the nonpipelined 8080/8085. In a system with pipelining, the data and address buses are busy transferring data while the CPU is processing information, thereby increasing the effective processing power of the microproces- sor. Although pipelining was a common feature of mini- and mainframe computers, Intel was a pioneer in putting pipelining on a single-chip microprocessor. Pipelining is discussed further in Section 1.2, Evolution from 8086 to 8088 The 8086 is a microprocessor with a 16-bit data bus internally and exter- nally, meaning that all registers are 16 bits wide and there is a 16-bit data bus to transfer data in and out of the CPU. Although the introduction of the 8086 marked a great advancement over the previous generation of microprocessors, there was still some resistance in using the 16-bit extemal data bus since at that time all peripherals were designed around an 8-bit microprocessor. In addition, a printed circuit board with a 16-bit data bus was much more expensive, Therefore, Intel came out with the 8088 version. Itis identical to the 8086 as far as programming is concerned, but externally it has an §-bit data bus instead of a 16-bit bus. It has, the same memory capacity, 1 megabyte. Success of the 8088 In 1981, Intel’s fortunes changed forever when IBM picked up the 8088 as their microprocessor of choice in designing the IBM PC. The 8088-based IBM PC ‘was an enormous success, largely because IBM and Microsoft (the developer of the MS-DOS operating system) made it an open system, meaning that all documentation and specifications of the hardware and software of the PC were made public. This, ‘made it possible for many other vendors to clone the hardware successfully and thus, spawned a major growth in both hardware and software designs based on the IBM PC. Thisisin contrast with the Apple computer, which wasa closed system, blocking, any attempt at cloning by other manufacturers, both domestically and overseas. Other microprocessors: the 80286, 80386, and 80486 With a major victory behind Intel and a need from PC users for a more powerful microprocessor, Intel introduced the 80286 in 1982, Its features included 16-bit intemal and external data buses; 24 address lines, which give 16 megabytes of memory (27 = 16 megabytes); and most significantly, virtual memory. The 1.1: BRIEF HISTORY OF THE 80x86 FAMILY 80286 can operate in one of two modes: real mode or protected mode. Real mode is simply a faster 8088/8086 with the same maximum of 1 megabyte of memory. Protected mode allows for 16M of memory but is also capable of protecting the operating system and programs from accidental or deliberate destruction by a user, a feature that is absent in the single-user 8088/8086. Virtual memory is a way of fooling the microprocessor into thinking that it has access to an almost unlimited amount of memory by swapping data between disk storage and RAM. IBM picked up the 80286 for the design of the IBM PC AT, and the clone makers followed IBM's, lead. With users demanding even more powerful systems, in 1985 Intel intro- » Explain the difference between Assembly language instructions and pseudo-instruetions ‘Identify the segments of an Assembly language program ‘Code simple Assembly language instructions Assemble, link, and run a simple Assembly language program ‘Code control transfer instructions such as conditional and uncondi- ‘tional jumps and call instructions » Code Assembly language data directives for binary, hex, decimal, or ASCH data » Write an Assembly language program using either the full segment defi- nition or the simplified segment definition > Explain the difference between COM and EXE files and list the advan- tages of each vee y ‘This chapter is an introduction to Assembly language programming with the 80x86. First the basic form of a program is explained, followed by the steps required to edit, assemble, link, and run a program. Next, control transfer instruc tions such as jump and call are discussed and data types and data directives in 80x86 Assombly language are explained. Then the full segment definition is dis- cussed, Finally, the differences between "exe" and "com" files are explained, ‘The programs in this chapter and following ones can be assembled and run on any IBM PC, PS and compatible computer with an 8088/86 or higher microprocessor SECTION 2.1: DIRECTIVES AND A SAMPLE PROGRAM 23 In this section we explain the components of a simple Assembly language program to be assembled by the assembler. A given Assembly language program (sce Figure 2-1) is a series of statements, or lines, which are either Assembly lan- guage instructions such as ADD and MOV, or statements called directives. Directives (also called pseudo-instructions) give directions to the assembler about how it should translate the Assembly language instructions into machine code An Assembly language instruction consists of four fields: [label:] mnemonic foperands} [:comment) Brackets indicate that the field is optional. Do not type in the brackets ‘The label field allows the program to refer to a line of code by name, The label ficld cannot exceed 31 characters. Labels for directives do not need to end with a colon. A label must end with a colon when it refers to an opcode generating instruction; the colon indicates to the assembler that this refers to code within this ‘code segment. Appendix C, Section 2 gives more information about labels. ‘The Assembly language mnemonic (instruction) and operand(s) fields together perform the real work of the program and accomplish the tasks for which the pro gram was written. In Assembly language statements such as ADD AL,BL MOV AX8764 ADD and MOV are the mnemonic opcodes and "AL,BL" and "AX,6764" are the operands. Instead of a mnemonic and operand, these two fields could contain assembler pseudo-instructions, or directives. They are used by the assembler to organize the program as well as other output files. Directives do not generate any machine code and are used only by the assembler as opposed to instructions, which are translated into machine code for the CPU to execute. In Figure 2-1 the ‘commands DB, END, and ENDP are examples of directives. The comment field begins with a ;". Comments may be at the end of a line or on a line by themselves. The assembler ignores comments, but they are indispen- sable to programmers, Comments are optional, but arc highly recommended to make it easier for someone to read and understand the program. Model definition ‘The first statement in Figure 2-1 after the comments is the MODEL direc- tive. This directive selects the size of the memory model. Among the options for the memory model are SMALL, MEDIUM, COMPACT, and LARGE, -MODEL SMALL shis directive defines the model as small CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING SMALL is one of the most widely used memory models for Assembly language programs and is sufficient for the programs in this book. The small model uses a maximum of 64K bytes of memory for code and another 64K bytes for data. The other models are defined as follows: MODEL MEDIUM sthe data must fit into 64K bytes ibut the code can exceed 64K bytes of memory MODEL COMPACT _ ithe data can exceed 64K bytes ibut the code cannot exceed 64K bytes -MODEL LARGE both data and code can exceed 64K but no single set of data should exceed 64K -MODEL HUGE iboth code and data can exceed 64K ‘data items (such as arrays) can exceed 64K -MODEL TINY jused with COM files in which data and code must fit into 64K bytes Notice in the above list that MEDIUM and COMPACT are opposites. Also note that the TINY model cannot be used with the simplified segment defi- nition deseribed in this section. Segment definition As mentioned in Chapter 1, the 80x86 CPU has four segment registers: CS (code segment), DS (data segment), SS (stack segment), and ES (extra seg- ment). Every line of an Assembly language program must correspand to one of these segments. The simplified segment definition format uses three simple direc- tives: "CODE", "DATA", and "STACK", which correspond to the CS, DS, and SS registers, respectively. There is another segment definition style called the full segment definition, which is described in Section 2.6, ‘Segments of a program Although one can write an Assembly language program that uses only one segment, normally a program consists of at least three segments: the stack segment, the data segment, and the code segment. STACK marks the beginning of the stack segment ‘DATA {marks the beginning of the data segment ‘CODE “marks the beginning of the code segment Assembly language statements are grouped into segments in order to be recognized by the assembler and consequently by the CPU. The stack segment defines storage for the stack, the data segment defines the data that the program will use, and the code segment contains the Assembly language instructions. In Chapter I we gave an overview of how these segments were stored in memory. In the following pages we describe the stack, data, and code segments as they are defined in Assembly language programming, Stack segment ‘The following directive reserves 64 bytes of memory for the stack: STACK 64 Data segment ‘The data segment in the program of Figure 2-1 defines three data items: DATAI, DATA2, and SUM. Each is defined as DB (define byte). The DB direc tive is used by the assembler to allocate memory in byte-sized chunks. Memory SECTION 2.1: DIRECTIVES AND A SAMPLE PROGRAM 51 can be allocated in different sizes, such as 2 bytes, which has the directive DW (Gefine word). More of these pscudo-instructions are discussed in detail in Section 2. The data items defined in the data segment will be accessed in the code seg- ‘ment by their labels. DATA and DATA? arc given initial values in the data sec. tion. SUM is not given an initial value, but storage is set aside for it DATAt DaTaz ‘SUM MAIN MAIN ‘THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM iNOTE: USING SIMPLIFIED SEGMENT DEFINITION MODEL SMALL STACK 64 DATA DB 52H DB 20H OB? -CODE PROC FAR ‘this is the program entry point MOV AX.@DATA load the data segment address, MOV DS,AX sassign value to DS MOV AL,DATA1 get the first operand MOV BL,DATA2 _ get the second operand ADD ALBL add the operands MOV SUMAL :store the result in location SUM MOV AH.CH ;set up to retum to DOS INT 21H 5 ENDP END MAIN «this is the program exit point Figure 2-1. Simple Assembly Language Program Code segment definition ‘The last segment of the program in Figure 2-1 is the code segment, The first line of the segment after the CODE directive is the PROC directive. A pro. cedure is a group of instructions designed to accomplish a specific function. A code segment may consist of only one procedure, but usually is organized into several small procedures in order to make the program more structured, Every Procedure must have a name defined by the PROC directive, followed by the assembly language instructions and closed by the ENDP directive, The PROC and ENDP statements must have the same label. The PROC directive may have the option FAR or NEAR. The operating system that controls the computer must be directed to the beginning of the program in order to execute it. DOS requires that the entry point to the user program be a FAR procedure, From then on, either FAR or NEAR can be used. The differences between a FAR and a NEAR procedure, as well as where and why each is used, are explained later in this chapter. For ow, just remember that in order to run a program, FAR must be used at the pro- gram entry point. A good question to ask at this point is: What value is actually assigned to the CS, DS, and SS registers for exccution of the program? ‘The DOS operating system must pass control to the program so that it may execute, but before it does that it assigns values for the segment registers, The operating system must do this because it knows how much memory is installed in the computer, how much of it 1s used by the system, and how much is available. In the IBM PC, the operating system first finds out how many kilobytes of RAM memory are installed, allo= cates some for its own use, and then allows the user program to use the portions that it needs. Various DOS versions require different amounts of memory, and since the user program must be able to run across different versions, one cannot 52 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING MAIN MAIN |THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM ; USING SIMPLIFIED SEGMENT DEFINITION tell DOS to give the program a specific area of memory, say from 25FFF to 289E2. Therefore, itis the job of DOS to assign exact values for the segment reg- isters. When the program begins executing, of the three segment registers, only CS and SS have the proper values. The DS value (and ES, if used) must be ini- tialized by the program. This is done as follows: MOV AX,@DATA —_—-:DATA refers to the start of the data segment MOV DS.AX Remember from Chapter 1 that no segment register can be loaded direct ly. That is the reason the two lines of code above are needed, You cannot code "MOV DS,@DATA". Afier these housekeeping chores, the Assembly language program instructions can be written to perform the desired tasks. In Figure 2-1, the pro- gram loads AL and BL with DATA! and DATA2, respectively, ADDs them together, and stores the result in SUM. MOV AL,DATA1 MOV BL,DATAZ ADD AL,BL MOV SUMAL ‘The two last instructions in the shell are: MOV AH.4CH INT (21H ‘Their purpose is to return control to the operating system. The last two lines end the procedure and the program, respectively. Note that the label for NDP (MAIN) matches the label for PROC. The END pseudo-instruction ends the entire program by indicating to DOS that the entry point MAIN has ended. For this reason the labels for the entry point and END must match. Figure 2-2 shows a sample shell of an Assembly language program. When writing your first few programs, it is handy to keep a copy of this shell on your disk and simply fill it in with the instructions and data for your program MODEL SMALL (STACK 64 DATA jplace data definitions here ‘cove PROC FAR ‘his is the program entry point MOV AX,@DATA jload the data segment address MOV DSAX sassign value to DS splace code here Mov AHACH :set up to INT 21H ‘return to DOS NDP END MAIN is the program exit point Figure 2-2. Shell of an Assembly Language Program SECTION 2.1: DIRECTIVES AND A SAMPLE PROGRAM 53 Review Questions 1. What is the purpose of pseudo-instructions? 2 are translated by the assembler into machine code, whereas are not 3. Write an Assembly language program with the following characteristies: (@) A data item named HIGH_DAT, which contains 95 (b) Instructions that move HIGH_DAT to registers AH, BH, and DL (©) A program entry point named START 4. Find the errors in the following: MODEL ENORMOUS STACK .CODE DATA MAIN PROC FAR MOV AX,DATA MoV DS,@DATA MOV AL.34H ADD ALAFH MOV DATAt,AL START ENDP END SECTION 2.2: ASSEMBLE, LINK, AND RUN A PROGRAM Now that the basic form of an Assembly language program has been given, the next question is: How is it created and assembled? The three steps to ‘eeate an executable Assembly language program are outlined as follows: Step Tnput Program Output 1. Edit the program Keyboard | editor myfile.asm 2. Assemble the program ile.asm | MASM or TASM | myfile.obj 3. Link the program myfile.obj | LINK or TLINK | myfile.exe ‘The MASM and LINK pro- ‘grams are the assembler and linker programs for Microsoft's MASM EDITOR assembler. If you are using another PROGRAM assembler, such as Borland's Tiyfile.asm TASM, consult the manual for the procedure to assemble and link a ¥ program, Many excellent editors or ‘ASSEMBLER word processors are available that PROGRAM can be used to create and/or edit the program. The editor must be able to produce an ASCII file. Although filenames follow the usual DOS conventions, the source file must end in ".asm" for the assembler used in this book. This “asm” source file is assembled by an assembler, such as Microsoft's MASM, or Borland’s TASM. The assembler will produce an object file and a list file, along with other mmyttest J] | Le mytie.crt myfile.obj te other obj files LINKER PROGRAM Le mie map myfile.exe Figure 2-3. Steps to Create a Program (CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING files that may be useful to the programmer. The extension for the object file must be *.obj”. This object file is input to the LINK program, which produces the exe- cutable program that ends in "exe". The " exe" file can be executed by the micro- processor. Before feeding the " obj" file into LINK, all syntax errors produced by the assembler must be corrected. Of course, fixing these errors will not guarantee that the program will work as intended since the program may contain conceptu- al errors. Figure 2-3 shows the steps in producing an executable file Figure 2-4 shows how an executable program is created by following the steps outlined above, and then run under DEBUG. The portions in bold indicate what the user would type in to perform these steps. Figure 2-4 assumes that the MASM, LINK, and DEBUG programs are on drive C and the Assembly language program is on drive A. The drives used will vary depending on how the system is set up (C>MASM A:MYFILE.ASM Microsoft (R) Macro Assembler Version 5.10 |Copyright (C) Microsoft Corp 1981, 1988. Al rights reserved, ‘Object filename {A:MYFILE.OBJ]: A: Source listing [NUL.LST}:A:MYFILE.LST \Cross-reference [NUL.CRF]: 47962 + 413345 Bytes symbol space free © Waring Errors O Severe Errors C>LINK A:MYFILE.OBJ Microsoft (R) Overlay Linker Version 3.64 ‘Copyright (C) Microsoft Corp 1983-1988. All rights reserved. Run File (A:MYFILE.EXE|:A: List File [NULMAP!: Libraries [LIB): LINK : warning L4021: no stack segment (C>DEBUG A:MYFILE.EXE -UC8:0 1 1064:0000 B86610 MOV AX,1066 -D 1066:0 F 1066:0000 52 29 00 00 00 00 00 00-00 00 00 00 00 00 00 00 R). -G Program terminated normally -D 1066:0 F 1066:0000 52 29 7B 00 00 00 00 00-00 00 00 00 00 00 00 00 RX. -Q Cs Figure 2-4. Creating and Running the .exe File Note: The parts you type in are printed in bold SECTION 2.2: ASSEMBLE, LINK, AND RUN A PROGRAM. 55 .asm and .obj files ‘The ".asm" file (the source file) is the file crated with a word processor ‘or line editor. The MASM (or other) assembler converts the .asm file's Assembly language instructions into machine language (the " obj" object file). In addition to creating the object program, MASM also creates the ".Ist” list file. lst file The "Ist" file, which is optional, is very useful to the programmer because it lists all the opcodes and offset addresses as well as errors that MASM detected. MASM assumes that the list file is not wanted (NUL.LST indicates no list), To get a list file, type in a filename after the prompt, This file can be dis- played on the monitor or sent to the printer. The programmer uses it to help debug the program. It is only after fixing all the errors indicated in the " Ist" file that the obj" file can be input to the LINK program to create the executable program. ‘One way to look at the list file is to use the following command at the DOS level. This command will print myfile.Ist to the monitor, one sceen at atime. Cotype myfile ist | more Another way to look at the list file is to bring it into a word processor. ‘Then you can read it or print it. There are two assembler directives that can be used to make the "Ist" file more readable: PAGE and TITLE. PAGE and TITLE directives ‘The format of the PAGE directive is PAGE [lines] {columns} and its function isto tell the printer how the list should be printed. In the default mode, meaning that the PAGE directive is coded with no numbers coming after it, the output will have 66 lines per page with a maximum of 80 characters per line. In this book, programs will change the default settings to 60 and 132 as follows: PAGE 60,132 ‘The range for number of lines is 10 to 255 and for columns is 60 to 132. When the list is printed and it is more than one page, the assembler can be instructed to print the title of the program on top of each page. What comes after the TITLE pseudo-instruction is up to the programmer, but it is common practice to put the name of the program as stored on the disk immediately after the TITLE pscudo-instruction and then a brief description of the function of the program ‘The text after the TITLE pscudo-instruction cannot be more than 60 ASCII char- acters. crf file MASM produces another optional file, the cross-reference, which has the extension "crf". It provides an alphabetical list of all symbols and labels used in the program as well as the program line numbers in which they are referenced. This can be a great help in large programs with many data segments and code segments CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING LINKing the program ‘The assembler (MASM) creates the opcodes, operands, and offset addresses under the ".0bj" file. It is the LINK program that produces the ready- to-mun version of a program that has the ".exe" (EXEcutable) extension. The LINK program sets up the file so that it can be loaded by DOS and executed. In Figure 2-4 we used DEBUG to execute the program in Figure 2-1 and analyze the result. In the program in Figure 2-1, three data items are defined in the data segment. Before running the program, one could look at the data in the data segment by dumping the contents of DS:offset as shown in Figure 2-4. Now ‘what is the value for the DS register? This can vary from PC to PC and from DOS to DOS. For this reason it is important to look at the value in "MOV AX,xxxx" as was shown and use that number. The result of the program can be verified after it is run as shown in Figure 2-4. When the program is working successfully, it can be nin at the DOS level. To execute myfile.exe, simply type in C>A:myfile However, since this program produces no output, there would be no way to verify the results. When the program name is typed in at the DOS level, as shown above, DOS loads the program in memory. This is sometimes referred to ‘as mapping, which means that the program is mapped into the physical memory of the PC. map file ‘When there are many segments for code or data, there is a need to see ‘where each is located and how many bytes are used by each. This is provided by the map file. This file, which is optional, gives the name of each segment, where it starts, where it stops, and its size in bytes. In Chapter 7 the importance of the map will be seen when many separate subroutines (modules) are assembled sep- arately and then linked together. Review Questions 1. (@) The input file to the MASM assembler program has the extension (b) The input file to the LINK program has the extension 2. Select all the file types from the second column that are the output of the pro- gram in the first column Editor (a) .obj (b) asm Assembler (6) exe (d) Ist : MORE SAMPLE PROGRAMS, Linker (6) rf (A) map ‘Now that some familiarity with Assembly language programming in the IBM PC has been achieved, in this section we look at more example programs in order to allow the reader to master the basic features of Assembly programming. ‘The following pages show Program 2-1 and the list file generated when the pro- ‘gram was assembled. After the program was assembled and linked, DEBUG was used to dump the code segment to see what value is assigned to the DS register. Precisely where DOS loads a program into RAM depends on many factors, including the amount of RAM on the system and the version of DOS used, Therefore, remember that the value you get could be different for "MOV AX,cou" as well as for CS in the program examples. Do not attempt to modify the segment register contents to conform to those in the examples, or your system may crash! SECTION 2.3: MORE SAMPLE PROGRAMS 57 Welt, run, and analyze a prograr that adds 5 bytes of data and saves the result, The data should be the following hex numbers: 25, 12, 15, IF, and 2B. PAGE 60,132 TITLE PROG2-1_ (EXE) PURPOSE: ADDS 5 BYTES OF DATA MODEL SMALL (STACK 64 DATA DATALIN DB 25H,12H.1SH,1FH,2BH ‘SUM DB 2 CODE MAIN PROC FAR MOV" AXQDATA MOV DS) MOV CX,05 sset up loop counter CX=5 MOV BX,OFFSET DATA_IN !set up data pointer BX MOV ALO. initialize AL. AGAIN. ADD ALEX] add next data item to AL INC Bx Imake BX point to next data item DEC cx decrement loop counter NZ AGAIN jump if loop counter not zero MOV SUMAL ‘load result into sum MOV AH,4CH ‘set up return INT 21H sretumn to DOS. MAIN ENOP END MAIN ‘After the program was assembled and linked, it was run using DEBUG: ‘C>debug prog2-t.exe -u.cs:0 19 1067:0000 B86610 MOV AX,1066 1067:0003 BEDS MOV DS\AX 1067:0005 890500 MOV CX,0005 1967:0008 BB0000 «MOV BX.0000 1087:000D 0207 ADD ALIBX] 1087:000F 43 INC. 1067:0010 49 DEC Cx 1987:0013 az0s00 MOV ans} AL 4067:0016 B44 MOV AH,4( 1067:0018 CD21 INT 21 -d 106601 1066:0000 25 12 15 1F 2B 00 00 00-00 00 00 00 00 00 00 00 %...+. a Program terminated normally -d 1086:0f u 1066:0000 25 12 15 1F 2B 96 00 00-00 00 00 00 00 00 00 00 %...+.. a co Program 2-1 Analysis of Program 2-1 ‘The DEBUG program is explained thoroughly in Appendix A. The com- mands used in running Program 2-1 were (1) u, to unassemble the code from ¢s:0 for 19 bytes; (2) d, to dump the contents of memory from 1066:0 for the next F bytes; and (3) g, to go, that is, run the program. Notice in Program 2-1 that when the program was run in DEBUG, the contents of the data segment memory were dumped before and after execution of the program to verify that the program worked as planned. Normally, it is not nec essary to unassemble this much code, but it was done here because in later sec- 58 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING Turbo Assembler Version 3.0 06/25/99 120532 Paget tesi21.asm 1 0000 MODEL SMALL 2 0000 ‘STACK 64 4 0000 “DATA 5 0000 2512151F 2B DATAIN DB 25H,12H,15H,1FH,28H § 0005 77 SUM OB ? 8 0006 —__—_s00E 8 0000, MAIN PROC FAR 10 0000 B8 0000s MoV Beapara 11 0003 8E D8 MOV DSy 12 0005 B9 0005 MOV set up loop counter CX=5 13 0008 BB 0000r MoV ‘FSET DATA_IN jset up data 14 000B BO 00 MOV initialize AL . 15 000D 0207 AGAIN: ADD AL [BX] jadd next data item to AL 16 OOOF 43 INC Bx imake BX point to next 17 0010 49 DEC CX decrement loop counter 18 0011 75 FA JNZ AGAIN jump if counter not zero 49 0013 A2 0005r MOV SUMAL load resut into sum 20 0016 B4 4c MOV = AH.4CH «set up return 21 0018 CD21 INT 21H jreturn to DOS 22 001A MAIN: ENDP 23 END MAIN ‘Turbo Assembler Version 3.0 06/25/99 12:05:32 Page 2 ‘Symbol Table ‘Symbol Name Type Value 72DATE Text "06/25/99" 27FILENAME Text “test21 " 27 TIME Text "12:05:32" 22VERSION Number 0300 @32BIT Text 0. CODE Text = _TEXT JCODESIZE Text 0 CPU Text 0101H IJCURSEG Text TEXT DATA Tea GROUP @DATASIZE Tet 0 @FILENAME Text TEST21 INTERFACE Text 00H JMODEL Text 2 STACK Text ~DGROUP )WORDSIZE Text 2 \GAIN Near _TEXT:000D DATA_IN Byte DBGROUP:0000 MAIN Far TEXT:0000 SUM Byte BGROUP:0005 ‘Groups & Segments Bit Size Align Combine Class DGROUP Group STACK 16 0040 Para Stack STACK DATA 16 0006 Word Public DATA |_TEXT 16 001A Word Public CODE List File for Program 2-1 MORE SAMPLE PROGRAMS. tions of the chapter we examine the jump instruction in this program. Also notice that the first 5 bytes dumped above arc the data items defined in the data segment of the program and the sixth item is the sum of those five items, so it appears that the program worked correctly (25H + 12H + 15H + [FH + 2BH = 96H), Program 2-1 is explained below, instruction by instruction, MOV CX,05" will load the value 05 into the CX register. This register is used by the program as a counter for iteration (looping). "MOV BX,OFFSET DATA_IN" will load into BX the offset address assigned to DATA. The assembler starts at offset 0000 and uses memory for the data and then assigns the next available offset memory for SUM (in this case, 0008). "ADD AL,{BX}" adds the contents of the memory location pointed at by the register BX to AL. Note that (BX) is a pointer to a memory location “INC BX" simply increments the pointer by adding 1 to register BX. This will cause BX to point to the next data item, that is, the next byte "DEC CX" will decrement (subtract I from) the CX counter and will set the zero flag high if CX becomes zero "INZ AGAIN" will jump back to the label AGAIN as long as the zero flag is indicating that CX is not zero. "JNZ AGAIN" will not jump (that is, exe- ution will resume with the next instruction after the INZ instruction) only after the zero flag has been set high by the "DEC CX" instruction (that is, CX becomes zero). When CX becomes zero, this means that the loop is completed and all five numbers have been added to AL. Various approaches to Program 2-1 ‘There are many ways in which any program may be written. The method shown for Program 2-1 defined one field of data and used pointer [BX] to access data elements. In the method used below, a name is assigned to each data item that will be accessed in the program. Variations of Program 2-1 are shown below to clarify the use of addressing modes in the context of a real program and also to show that the 80x86 can use any general-purpose register to do arithmetic and logic operations. In earlicr-generation CPUs, the accumulator had to be the des- tination of all arithmetic and logic operations, but in the 80x86 this is not the case. Since the purpose of these examples is to show different ways of accessing ‘operands, itis left to the reader to run and analyze the programs. ;from the data segment: DATA1 DB 25H DATA2 DB 12H DATA3 DB 15H DATA4 DB 1FH DATAS. DB 2BH SUM DB ? :from the code segment: MOV AL,DATA1 ;MOVE DATA1 INTO AL ADD AL,DATA2 — :ADD DATA2 TO AL ADD AL,DATA3 ADD AL,DATA4 ADD AL,DATAS MOV SUMAL —;SAVE ALIN SUM There is quite a difference between these two methods of writing the same program. While in the first one the register indirect addressing mode was used to access the data, in the second method the dircet addressing mode was used. (CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING Write and run a program that adds four words of data and saves te result. The values will be 234DH, IDE6H, 3BC7H, and 566AH. Use DEBUG to verify the sum is D364. TITLE PROG2.2 (EXE) PURPOSE: ADDS 4 WORDS OF DATA PAGE 60,132 .MODEL SMALL (STACK 64 “DATA DATAIN DW__234DH, 1DE6H,38C7H,566AH ORG 40H SUM ow. 7? CODE MAIN PROC FAR, MOV” AX.@DATA MOV DS\AX MOV CXi04 set up loop counter CX=4 MOV DLOFFSET DATA_IN ‘set up data pointer Di MOV BX,00 jinitialize BX ADD_LP: = ADD BX/[DN) add contents pointed at by [DI] to BX INC DI jincrement Dl twice INC DI 4to point to next word DEC cx decrement loop counter JINZ ADD_LP jump if oop counter not zero MOV SIOFFSET SUM —_load pointer for sum MoV [Si.BX {store in data segment MOV AH.4CH ‘set up return INT 21H srelum to DOS MAIN ENDP END MAIN ‘After the program was assembled and linked, it was run using DEBUG: (Codebug a:prog2-2.exe 1068 10 MOV AX,1066 -D 1066:0 1F 41066:0000 4D 23 E6 1D C7 3B 6A 56-00 00 00 00 0 1966:0010 00 60 00 00:00 00 00 00-00 00 00 00 00 Program terminated normally -D 1066:0 1F 1066:0000 4D 23 E6 1D C7 3B 6A 56-00 00 00 00 00 00 00 00 M#f.GiV. 1966:0010 €4 D3 00 00 00 00 00 00-00 00 00 00 00 a0 00 00 S.. & Program 2-2 Analysis of Program 2-2 First notice that the 16-bit data (a word) is stored with the low-order byte first. For example, "234D" as defined in the data segment is stored as "4D23", meaning that the lower address, 0000, has the least significant byte, 4D, and the higher address, 0001, has the most significant byte, 23. This is shown in the DEBUG display of the data segment. Similarly, the sum, D364, is stored as 64D3. As discussed in Chapter 1, this method of low byte to low address and high byte to high address operand assignment is referred to in computer literature as "little endian." Second, note that the address pointer is incremented twice, since the operand being accessed is a word (two bytes). The program could have used “ADD DI1,2" instead of using "INC DI" twice. When storing the result of word addition, “MOV SI,OFFSET SUM" was used to load the pointer (in this case 0010, as defined by ORG 0010H) for the memory allocated for the label SUM, SECTION 2.3: MORE SAMPLE PROGRAMS 61 and then "MOV [SI].BX" was used to move the contents of register BX to mem= ‘ory locations with offsets 0010 and 0011. Again, as was done previously, it could have been coded simply as "MOV SUM,BX", using the direct addressing mode. Program 2-2 uses the ORG directive. In previous programs where ORG was not used, the assembler would start at offset 0000 and ust memory for each data item, The ORG directive can be used to set the offset addresses for data items. Although the programmer cannot assign exact physical addresses, one is allowed to assign offset addresses. The ORG directive in Program 2-2 caused SUM to be stored at DS:0010, as can be seen by looking at the DEBUG display of the data segment. ‘Write and run a program that transfers 6 bytes of data from memory locations with offset of 0010H to memory loca- tions with offset of 0028H. TITLE PROG2-3 (EXE) PURPOSE: TRANSFERS 6 BYTES OF DATA PAGE 60,132 MODEL SMALL STACK 64 ‘DATA ORG 10H DATALIN, DB 25H,4FH.85H, 1FH.26H,0C4H ORG 28H ‘COPY 0B 6 DUP?) CODE MAIN PROC FAR MOV" AXQDATA MOV Mov SVOPESET DATAIN. :S1 points to data to be copied MOV DILOFFSETCOPY DI points to copy of data MOV CX.06H ‘Joop counter=6 MOV_LOOP: MOV” AL'SI] move the next byte from DATA area to AL MOV [DiLAL move the next byte lo COPY area INC SI {increment DATA pointer INC DI ‘increment COPY pointer DEC cx ‘decrement LOOP counter JNZ_ MOV_LOOP jump if loop counter not zero MOV AHATH iset up to retum INT 21H ‘return to DOS. MAIN ENOP END MAIN ‘After the program was assembled and linked, it was run using DEBUG: C>debug prog2-3.exe -u 05:01 1069:0000 BB6610 MOV AX,1056 -d 1066:0 2F 4066:0000 00 00 00 G0 00 00 00 00-00 00 00 00 00 00 00 00... 4068:0010 25 4F 85 1F 28 C4 00 00-00 00 00 00 00 00 00 00 %6..+D. 4066:0020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 .. a Program terminated normally -0 1066:0 2f 4086:0000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 . 1068:0010 25 4F 85 1F 2B C4 00 00-00 00 00 00 00 00 00 00 %O..*0.... 1066:0020 00 00 00 00 00 00 00 00-25 4F 85 1F 2B C4 00 00 %O..+D. aq ce Program 2-3 62 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING Analysis of Program 2-3 ‘The DEBUG example shows the data segment being dumped before the program was run and after to verify that the data was copied and that the program ran successfully. Notice that C4 was coded in the data segments as 0C4. This is required by the assembler to indicate that C is a hex number and not a letter. This is required if the first digit is a hex digit A through F This program uses two registers, SI and DI, as pointers to the data items being manipulated. The first is used as a pointer to the data item to be copied and the second as a pointer to the location the data item is to be copied to. With each iteration of the loop, both data pointers are incremented to point to the next byte Stack segment definition revisited One of the primary functions of the DOS operating system is to determine the total amount of RAM installed on the PC and then manage it properly. DOS uses the portion it needs for the operating system and allocates the rest. Since memory requirements vary for different DOS versions, a program cannot dictate the exact physical memory location for the stack or any segment. Since memory management is the responsibility of DOS, it will map Assembly programs into the memory of the PC with the help of LINK. ‘Although in the DOS environment a program can have multiple code seg- ‘ments and data segments, itis strongly recommended that it have only one stack segment, to prevent RAM fragmentation by the stack. Itis the function of LINK to combine all different code and data segments to create a single executable pro- gram with a single stack, which is the stack of the system. Various options for seg- ment definition are discussed in Chapter 7 and many of these concepts are explained there. Review Questions 1. What is the purpose of the INC instruction? 2. What is the purpose of the DEC instruction? 3. In Program 2-1, why does the label AGAIN have a colon after it, whereas the label MAIN does not? 4, State the difference between the following two instructions: MOV BX,DATA1 MOV BX,OFFSET DATA1 5. State the difference between the following two instructions: ADD AX,BX ADD AX,BX} SECTION 2.4: CONTROL TRANSFER INSTRUCTIONS In the sequence of instructions to be executed, it is often necessary to transfer program control to a different location. There are many instructions in the 80x86 to achieve this. This section covers the control transfer instructions available in the 8086 Assembly language. Before that, however, it is necessary to explain the concept of FAR and NEAR as it applies to jump and call instructions. FAR and NEAR If control is transferred to. a memory location within the current code seg- ment, it is NEAR. This is sometimes called intrasegment (within segment). If control is transferred outside the current code segment, it isa FAR or intersegment (between segments) jump. Since the CS:IP registers always point to the address of the next instruction to be executed, they must be updated when a control trans- SECTION 2.4; CONTROL TRANSFER INSTRUCTIONS, 63 fer instruction is executed. In a NEAR jump, the IP is updated and CS remains the same, since control is still inside the current code segment. In a FAR jump, because control is passing outside the current code segment, both CS and IP have to be updated to the new values. In other words, in any control transfer instruc- tion such as jump or call, the IP must be changed, but only in the FAR case is the CS changed, too. Conditional jumps Conditional jumps, summarized in Table 2-1, have mnemonics such as INZ (jump not zero) and JC (jump if carry). In the conditional jump, control is, transferred to a new location if a certain condition is met, The flag register is the fone that indicates the current condition. For example, with "INZ label”, the processor looks at the zero flag to see if itis raised. If not, the CPU starts to fetch and execute instructions from the address of the label. If ZF = 1, it will not jump but will execute the next instruction below the INZ. Table 2-1: 8086 Conditional Jump Instructions Mnemonic “Jump IF...” TAJINBE above/not below nor 2670 JAEJINB, above or equal/not below IB/INAE below/not above nor equal IBEJINA | (CF or ZF) below or equal/not above IC CF=1 carry JEDZ ZF=1 equal/zero IG/NLE | (SF xor OF) or ZF) =0 | greater/not less nor equal IGE/INL__| (SF xor OF) = 0 greater or equal/not less IL/INGE | (SF xor OR)=1 less/not greater nor equal ILE/ING __| (SF xor OF) or ZF)=1 _| less or equal/not greater INC Ci not carry not equal/not zero not overflow not parity/parity odd not sign overflow parity/parity equal sign “Above” end “below” refer to the relationship of two unsigned values; “greater” and “less” refer to the relationship of two signed values, (Reprinted by permission of Intel Corporation, Copyright Intel Corp. 1989) Short jumps All conditional jumps are short jumps. In a short jump, the address of the target must be within - 128 to +127 bytes of the IP. In other words, the condition- al jump is a two-byte instruction: one byte is the opcode of the J condition and the second byte is a value between 00 and FF. An offset range of 00 to FF gives 256 possible addresses; these are split between backward jumps (to -128) and forward jumps (to +127), In a jump backward, the second byte is the 2's complement of the dis- placement value. To calculate the target address, the second byte is added to the CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING IP of the instruction after the jump. To understand this, look at the unassembled code of Program 2-1 for the instruction NZ AGAIN, repeated below. 1067:0000 B86610 MOV AX,1066 1067:0003 8ED8 MOV DS,AX 1067:0005 B90500 MOV CX,0005 1067:0008 BB0000 ©» MOV BX,0000 1087:000D 0207 ADD AL|BX] 1067:000F 43 INC BX DEC cx 1067:0011 75FA JNZ — 00D 1067:0013 A20500 MOV [0005],AL 1087:0016 B44c MOV AH,4C 1067:0018 CD21 INT 24 The instruction "JNZ AGAIN" was assembled as "JNZ 000D", and 00D is the address of the instruction with the label AGAIN. The instruction *JNZ 00D" has the opcode 75 and the target address FA, which is located at offset addresses 0011 and 0012. This is followed by "MOV SUM,AL", which is locat- ed beginning at offSct address 0013. The IP value of MOV, 0013, is added to FA to calculate the address of labet AGAIN (0013 + FA = 000D) and the carry is, dropped. In reality, FA is the 2's complement of -6, meaning that the address of the target is ~6 bytes from the IP of the next instruction Similarly, the target address for a forward jump is calculated by adding the IP of the following instruction to the operand. In that case the displacement value is positive, as shown next. Below is a portion of a list file showing the opcodes for several conditional jumps. 0005 8A 47 02 AGAIN MOV ALJBX}+2 0008 3c 6t CMP AL61H 000A 72.06 JB NEXT 000 3c 7A CMP AL,7AH OO0E 7702 JA NEXT 0010 24 DF AND AL,ODFH 0012 88.04 NEXT: MOV [SILAL In the program above, "JB NEXT" has the opcode 72 and the target address 06 and is located at IP = 000A and 000B. The jump will be 6 bytes from. the next instruction, which is IP = 000C. Adding gives us 0OOCH + 0006H = 0012H, which is the exact address of the NEXT label. Look also at "JA NEXT", which has 77 and 02 for the opcode and displacement, respectively. The IP of the following instruction, 0010, is added to 02 to get 0012, the address of the target location. It must be emphasized that regardless of whether the jump is forward or backward, for conditional jumps the address of the target address can never be more than -128 to + 127 bytes away from the IP associated with the instruction following the jump (- for the backward jump and + for the forward jump). If any attempt is made to violate this rule, the assembler will generate a "relative jump out of range” message. These conditional jumps are sometimes referred to as SHORT jumps. Unconditional jumps “IMP label" is an unconditional jump in which control is transferred unconditionally to the target location label. The unconditional jump can take the following forms: SECTION 2.4: CONTROL TRANSFER INSTRUCTIONS 65 1. SHORT JUMP, which is specified by the format "JMP SHORT label". This is a jump in which the address of the target location is within 128 to +127 bytes of memory relative to the address of the current IP. In this case, the opcode is EB and the operand is | byte in the range 00 to FF. The operand byte is added to the current IP to calculate the target address. Ifthe jump is backward, the operand is, in 2's complement. This is exactly like the J condition case. Coding the directive “short” makes the jump more efficient in that it will be assembled into a 2-byte instruction instead of a 3-byte instruction. 2, NEAR JUMP, which is the default, has the format "IMP label”, This is a near jump (within the current code segment) and has the opcode E9, The target address can be any of the addressing modes of direct, register, register indirect, ot memory indirect: (a) Direct JUMP is exactly like the short jump explained catlier, except that the target address can be anywhere in the segment within the range +32767 to 32768 of the current IP. () Register indirect JUMP; the target address is in a register. For example, in “JMP BX", IP takes the value BX. (c) Memory indirect JMP; the target address is the contents of two memory locations pointed at by the register. Example: "IMP [DI]" will replace the IP with the contents of memory locations pointed at by DI and DI+1 3. FAR JUMP which has the format *JMP FAR PTR label", This is a jump out of the current code segment, meaning that not only the IP but also the CS is replaced with new values. CALL statements Another control transfer instruction is the CALL instruction, which is used to call a procedure. CALLs to procedures are used to perform tasks that need to be performed frequently. This makes a program more structured. The target address could be in the current segment, in which case it will be a NEAR call or ‘outside the current CS segment, which is a FAR call, To make sure that afler exe~ ‘cution of the called subroutine the microprocessor knows where to come back, the microprocessor automatically saves the address of the instruction following the call on the stack. It must be noted that in the NEAR call only the IP is saved on the stack, and in a FAR call both CS and IP are saved. When a subroutine is called, control is transferred to that subroutine and the processor saves the IP (and CS in the case of a FAR call) and begins to fetch instructions from the new loca- tion, After finishing execution of the subroutine, for control to be transferred back to the caller, the last instruction in the called subroutine must be RET (return). In the same way that the assembler generates different opcode for FAR and NEAR calls, the opcode for the RET instruction in the case of NEAR and FAR is differ- ‘ent, as well. For NEAR calls, the IP is restored; for FAR calls, both CS and IP are restored. This will ensure that control is given back to the caller. As an example, assume that SP = FFFEH and the following code is a portion of the program unassembled in DEBUG: 12B0:0200 BB1295 MOV Bx,9512 12B0:0203 E8FA0O CALL 0300 1280:0206 B82F14 MOV AX,142F Since the CALL instruction is a NEAR call, meaning that it is in the same code segment (different IP, same CS), only IP is saved on the stack. In this case, the IP address of the instruction after the call is saved on the stack as shown in Figure 2-5. That IP will be 0206, which belongs to the "MOV AX,142F" instruc- tion. CHAPTER SSEMBLY LANGUAGE PROGRAMMING ‘The last instruction of the called subroutine must be a RET instruction which directs the CPU to POP the top 2 bytes of the stack into the IP and resume executing at offset address 0206, For this reason, the number of PUSH and POP instructions (which alter the SP) must match. In other words, for every PUSH there must be a POP. 12B0:0300 53 PUSH BX 1280:0301 1280:0309 5B POP Bx 12B0:030A C3 RET J_xa_} Assembly language subroutines +E In Assembly language programming it | FFFC is common to have one main program and many | per, 3 subroutines to be called from the main program. | FFFD | 02 This allows you to make each subroutine intoa | pRFE separate module. Each module can be tested sep arately and then brought together, as will be shown in Chapter 7. The main program is the entry point from DOS and is FAR, as explained earlier, but the subroutines called within the main program can be FAR or NEAR. Remember that NEAR routines are in the same code segment, while FAR routines are outside the current code segment. If there is no specific mention of FAR after the directive PROC, it defaults to NEAR, as shown in Figure 2-6. From now on, all code segments will be writen in that format. Rules for names in Assembly language Figure 2-5. IP in the Stack By choosing label names that are meaningful, a programmer can make a program much easier to read and maintain, There are several rules that names ‘must follow. First, each label name must be unique. The names used for labels in ‘Assembly language programming consist of alphabetic letters in both upper and lower case, the digits 0 through 9, and the special characters question mark (?), period (), at (@), underline (.), and dollar sign (S). The first character of the name must be an alphabetic character or special character. It cannot be a digit. ‘The period can only be used as the first character, but this is not recommended since later versions of MASM have several reserved words that begin with a peri- ‘od, Names may be up to 31 characters long. A list of reserved words is given at the end of Appendix C. Review Questions 1. Ifcontrol is transferred outside the current code segment, is it NEAR or FAR? 2. Ifaconditional jump is not taken, what is the next instruction to be executed? 3. In calculating the target address to jump to, a displacement is added to the contents of re : 4. What is the advantage in coding the operator "SHORT" in an unconditional jump? 5. ‘A(n) jump is within -128 to +127 bytes of the current IP. A(n) jump is within the current code segment, whereas a(n) jump is outside the current code segment. How does the CPU know where to retum to after executing a RET? Describe briefly the function of the RET instruction. State why the following label names are invalid, (@)GETDATA (b) NUM (¢) TEST-DATA (4) RET ene SECTION 2.4: CONTROL TRANSFER INSTRUCTIONS 67 MAIN MAIN ‘SUBR1 SUBR1 SUBR2 SUBR2 SUBRS SUBR3 CODE PROC FAR, STHIS IS THE ENTRY POINT FOR DOS MOV" AX@DATA MOV DS, CALL SUBRI CALL SUBR2 CALL SUBR3 MOV AH.4CH INT 21H ENDP PROG RET ENOP PROC. RET ENDP PROC, RET ENDP END MAIN STHIS IS THE EXIT POINT Figure 2-6. Shell of Assembly Language Subroutines SECTION 2.5: DATA TYPES AND DATA DEFINITION The assembler supports all the various data types of the 80x86 micro- processor by providing data directives that define the data types and set aside memory for them. In this section we study these directives and how they are used to represent different data types of the 80x86. The application of these directives becomes clearer in the context of examples in subsequent chapters. 80x86 data types ‘The 8088/86 microprocessor supports many data types, but none are longer than 16 bits wide since the size of the registers is 16 bits. I is the job of the programmer to break down data larger than 16 bits (0000 to FFFFH, or 0 to 65535 in decimal) to be processed by the CPU. Many of these programs are shown in Chapter 3. The data types used by the 8088/86 can be 8-bit or 16-bit, positive or negative. If a number is less than 8 bits wide, it still must be coded as an 8-bit register with the higher digits as zero. Similarly, ifthe number is less than 16 bits wide it must use all 16 bits, with the rest being 0s. For example, the num- ber 5 is only 3 bits wide (101) in binary, but the 8088/86 will accept it as 05 or "0000 0101" in binary. The number 514 is "10 0000 0010" in binary, but the 8088/86 will accept it as 0000 0010 0000 0010" in binary. The discussion of signed numbers is postponed unt later chapters since their representation and application are unique Assembler data directives Alll the assemblers designed for the 80x86 (8088, 8086, 80188, 80186, 80286, 80386, 80386SX, 80486, and Pentium) microprocessors have standard- ized the directives for data representation. The following are some of the data CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING directives used by the 80x86 microprocessor and supported by all software and hardware vendors of IBM PCs and compatibles, ORG (origin) ORG is used to indicate the beginning of the offset address. The number that comes after ORG can be either in hex or in decimal. If the number is not fol- lowed by H, itis decimal and the assembler will convert it to hex. Although the ORG directive is used extensively in this book in the data segment to separate fields of data to make it more readable for the student, it can also be used for the offset of the code segment (IP). DB (define byte) ‘The DB directive is one of the most widely used data directives in the assembler. It allows allocation of memory in byte-sized chunks. This is indeed the smallest allocation unit permitted. DB can be used to define numbers in dec- imal, binary, hex, and ASCII. For decimal, the D after the decimal number is optional, but using B (binary) and H (hexadecimal) for the others is required. Regardless of which one is used, the assembler will convert them into hex. To indicate ASCH, simply place the string in single quotation marks (like this’). The assembler will assign the ASCII code for the numbers or characters automatical- ly. DB is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all ASCII data definitions. Following are some DB examples: DATA DB 25 ;DECIMAL DATA2 DB 10001001B_ ;BINARY DATAS DB 12H iHEX ORG 0010H DATAS DB '2501" ‘SCI NUMBERS. ORG 0018H DATAS DB? T ASIDE A BYTE ORG 0020H DATAS DB ‘My name is Joe’ ASCII CHARACTERS 0000 19 DATAI DB 25, ;DECIMAL 0001 89 DATA2 DB 100010018 BINARY 002 12 DATA3 DB 12H iHEX 0010 ORG 0010H 0010 3235 39.31 DATA4 DB ‘259° :ASCII NUMBERS 0018 ORG 0018H 0018 00 DATAS DB 7. :SET ASIDE A BYTE 0020 ORG 0020H 0020 4D 79206E616D —DATAS DB ‘My name is Joe’ ;ASCIl CHARACTERS. 85 20 69 75 20 4A 6 List File for DB Examples Either single or double quotes can be used around ASCII strings. Thi can be useful for strings which should contain a single quote such as "O'Leary' DUP (duplicate) DUP is used to duplicate a given number of characters. This can avoid a lot of typing. For example, contrast the following two methods of filling six memory locations with FFH: DATA TYPES AND DATA DEFINITION 69 ORG 0030H DATA7 DB OFFH.OFFH,OFFH.OFFH,OFFH,OFFH ;FILL 6 BYTES WITH FF ORG 38H DATAS DB 6 DUP(OFFH) FILL 6 BYTES WITH FF ; the following reserves 32 bytes of memory with no initial value given ‘ORG 40H DATA9 DB 32DUP(?)__;SET ASIDE 32 BYTES ;DUP can be used inside another DUP jithe following fils 10 bytes with 99 DATAI0 DB _5 DUP (2 DUP (99)) ;FILL 10 BYTES WITH 99 (0030 ‘ORG 00304. 0030 FFFFFFFFFFFF © DATA7 DB OFFHOFFH,OFFH,OFFHOFFH,OFFH ; 8 BYTES = FF 0038 0038 0006 0040 9040 0920 { }0060 ‘0060 0005{ ORG 38H fe DATAB DB 6 DUP(OFFH) FILL 6 BYTES WITH FF ! ORG 40H DATA9 DB 32DUP(?) (SET ASIDE 32 BYTES 1 ‘ORG 60H DATAIODB 5 DUP (2. DUP (99) FILL 10 BYTES WITH 99 0002 54 List File for DUP Examples DW (define word) DWis used to allocate memory 2 bytes (one word) ata time, DW is used widely in the 8088/8086 and 80286 microprocessors since the registers are 16 bits wide. The following are some examples of DW: ORG 70H DATAI1 DW 954 ;DECIMAL DATA12 DW 1001010101008 ~—;BINARY DATAIS DW 253FH sHEX ORG 78H DATAI4 DW 9,2,7,0CH,00100000B,5,HI' ;MISC. DATA DATAIS DW 8 DUP (?) :SET ASIDE 8 WORDS 0070 ORG 70H 0070 038A DATAIT OW 954 ;DECIMAL 0072 0954 DATAI2 DW 1001010101008 BINARY. 0074 253F DATAIS DW 253FH SHEX 0078 ORG © 78H 0078 0009 0002 0007 000C | DATA14 DW 9,2,7,0CH,00100000B,5,'HI' ;MISC. DATA 0020 0005 4849 0086 00081 | pATAts OW 8 DUP () ;SET ASIDE 8 WORDS ist File for DW Examples EQU (equate) This is used to define a constant without occupying a memory location, EQU does not set aside storage for a data item but associates a constant value with ‘a data label so that when the label appears in the program, its constant value will be substituted for the labe!. EQU can also be used outside the data segment, even in the middle of a code segment. Using EQU for the counter constant in the imme- diate addressing mode: 70 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING COUNT EQU 25 When executing the instructions "MOV CX,COUNT”, the register CX will be loaded with the value 25. This is in contrast to using DB: COUNT DB 25 ‘When executing the same instruction "MOV CX,COUNT™ it will be in the direct addressing mode. Now what is the real advantage of EQU? First, note that EQU can also be used in the data segment: COUNT EQU 25 COUNTER! DB COUNT COUNTER2 DB COUNT Assume that there is a constant (a fixed value) used in many different places in the data and code segments. By the use of EQU, one can change it once and the assembler will change all of them, rather than making the programmer try to find every location and correct it, DD (define doubleword) ‘The DD directive is used to allocate memory locations that are 4 bytes (two words) in size. Again, the data can be in decimal, binary, or hex. In any case the data is converted to hex and placed in memory locations according to the rule of low byte to low address and high byte to high address. DD examples are ‘ORG OOAGH DATAI6 DD 1023 {DECIMAL DATAI7 DD 100010010110010111008 BINARY DATAI8 DD 5C2A57F2H jHEX DATAI9 DD 23H,34789H.65533 ‘O0A0 00A0 000003FF O0A4 0008965C OOA8 SC2A57F2 OAC 00000023 00034789 DATAI9 DD 23H,34789H,65533 OO0OFFFD ‘ORG OOAGH DATAIS DD 1023 DATAI7 OD 100010010110010111008 DATAI8 DD 5C2AS7F2H List File for DD Examples DQ (define quadword) DQ is used to allocate memory 8 bytes (four words) in size. This can be used to represent any variable up to 64 bits wide: ORG 00COH DATA20 DQ 4523C2H == -sHEX DATA21 DQ ‘HI {ASCII CHARACTERS, DATA22 DQ ? :NOTHING ‘o0co ‘ORG 00COH 00CO ©223450000000000 DATA20 DQ 4523C2H —;HEX 008 4948000000000000 DATA21 DQ ‘HI {ASCII CHARACTERS 000 coDd000000000000 DATA22 DQ ? INOTHING List File for DQ Examples ——— SECTION 2.5: DATA TYPES AND DATA DEFINITION nm DT (define ten bytes) DT is used for memory allocation of packed BCD numbers, The applica- tion of DT will be seen in the multibyte addition of BCD numbers in Chapter 3, For now, observe how they are located in memory. Notice that the "H" after the data is not needed. This allocates 10 bytes, but a maximum of 18 digits can be entered. ‘ORG O0EOH DATA23 DT 867943569829 {BCD DATA24 DT 7? ;NOTHING ‘O0EO O0E0 2e9856437086000000 DATAZS DT 887943569629 DEA 990900000000000000 DATA2S DT 7 ‘ORG OOEOH List File for DT Examples DT can also be used to allocate 10-byte integers by using the "D" option DEC DT 65535d 1e assembler will convert the decimal snumber to hex and store it Figure 2-7 shows the memory dump of the data section, including all the examples in this section. It is essential to understand the way operands are stored in memory. Looking at the memory dump shows that all of the data directives use the little endian format for storing data, meaning that the least significant byte is located in the memory location of the lower address and the most significant byte resides in the memory location of the higher address. For example, look at the case of "DATA20 DQ 4523C2", residing in memory starting at offset OOCOH. C2, the least significant byte, is in location 00CO, with 23 in 0OC1, and 45, the most significant byte, in 00C2. It must also be noted that for ASCII data, only the DB directive can be used to define data of any length, and the use of DD, DQ, or DT directives for ASCII strings of more than 2 bytes gives an assembly error. When DB is used for ASCII numbers, notice how it places them backwards in memory. For example, see "DATA4 DB '2591" at origin 10H: 32, ASCII for 2, is in memory location 10H; 35, ASCII for 5, is in 11H; and so on, =D 1066:0 100 1066: 0000 1066: 0030 1066:0040 1066: 0060 1066:0070 1066: 0080 1066:0090 1066:00D0 1066: 0080 19 32 4D FF 00 63 BA 20 00 FF 89 2 00 29 89 12 00 00 00 00 00-00 00 00 00 00 00 00 00 ......seee 35 39 31 00 00 00 90-00 00 00 00 00 00 00 00 258i: 79 20 GE 61 6D 65 20-69 73 20 4A 6F 65 00 00 My name is Joe. FE FF FF FF PF 00 00-FF 00 00 09 00 00 G0 00-00 00 40 00 00 00 00 a0 *. 63 63 63 63 63 63 63-63 63 00 00 00 00 00 00 ccceccccce 03 54 09 3F 25 00 00-03 00 02 00 07 00 0c 00 00 05 00 4F 48.00 00-00 00 00 00 00 00 00 00 90 00 00 00 00 00 00-00 G0 00 00 00 00 00 00 Seda deel 03 00 00 SC 9608 00-F2 57 2A SC 23 00 00 00 1...\...Em\E 47 03 00 ED FF°O0 00-00 00 00 00 00 00 00 00 Biz......IH.. 23 45 00 00 00 00 00-49 48 00 00 00 90 00 00 90 99 00 00 00 00 00-00 00 00 00 00 00 00 00 sierriiiiiseeeee 98 56 43 79 86 00 00-00 00 00 09 00 00 00 00 S.veyé...222I11! Figure 2-7. DEBUG Dump of Data Segment nn CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING Review Questions 1. The directive is always used for ASCII strings longer than 2 bytes. 2. How many bytes are defined by the following? DATA DB 6 DUP (4 DUP (OFFH)) 3. Do the following two data segment definitions result in the same storage in bytes at offset 10H and ITH? If not, explain why. ORG 10H ORG 10H DATA DB 72 DATA’ DW 7204H DATA_2 DB 04H 4, The DD directive is used to allocate memory locations that are _ bytes in length, The DQ directive is used to allocate memory locations that are bytes in length, 5. State briefly the purpose of the ORG directive 6. What is the advantage in using the EQU directive to define a constant value? 7. How many bytes are set aside by each of the following directives? (a) ASC_DATA DB ‘1234 (b) HEX_DATA DW 1234H 8. Does the little endian storage convention apply to the storage of ASCII data? SECTION 2.6: FULL SEGMENT DEFINITION ‘The way that segments have been defined in the programs above is a newer definition referred to as simple segment definition. It is supported by Microsoft's MASM 5.0 and higher, Borland's TASM version | and higher, and many other compatible assemblers. The older, more traditional definition is called the full segment definition. Although the simplified segment definition is much easier to understand and use, especially for beginners, it is essential to master full segment definition since many older programs use it Segment definition The "SEGMENT" and "ENDS" directives indicate to the assembler the beginning and ending of a segment and have the following format: label SEGMENT [options] :place the statements belonging to this segment here label ENDS The label, or name, must follow naming conventions (see the end of Section 2.4) and must be unique. The [options] field gives important information to the assembler for organizing the segment, but is not required. The ENDS label must be the same label as in the SEGMENT directive. In the full segment defini- tion, the "MODEL" directive is not used. Further, the directives "STACK", "DATA", and ".CODE" are replaced by SEGMENT and ENDS directives that surround each segment, Figure 2-8 shows the full segment definition and simpli- fied format, side by side. This is followed by Programs 2-2 and 2-3, rewritten using the full segment definition. Stack segment definition ‘The stack segment shown below contains the line: "DB 64 DUP (?)" to reserve 64 bytes of memory for the stack. The following three lines in full seg- ‘ment definition are comparable to "STACK 64” in simple definition STSEG SEGMENT _;the "SEGMENT" directive begins the segment DB 64 DUP (?) ;this segment contains only one line STSEG ENDS ithe "ENDS" segment ends the segment SECTION 2.6: FULL SEGMENT DEFINITION 73 {FULL SEGMENT DEFINITION ;— stack segment — amet SEGMENT DB 64 DUP (7) namet ENDS j— data segment — name2 SEGMENT idata definitions are placed here ntame2 ENDS j-—— code segment — name3 SEGMENT MAIN PROC FAR ASSUME .. MOV AX,name2 MOV DS,AX MAIN. ENDP name3 ENDS END MAIN {SIMPLIFIED FORMAT MODEL SMALL STACK 64 _ DATA :data definitions are placed here MAIN CODE PROC FAR MOV AX,@DATA MOV DS,AX ENDP END MAIN MAIN Figure 2-8. Full versus Simplified Segment Definition TITLE PURPOSE: ADDS 4 WORDS OF DATA PAGE 60,132 STSEG ‘SEGMENT DB 32 DUP (2) STSEG ENDS DTSEG ‘SEGMENT DATAIN DW 234DH, 1DE6H,3BC7H,566AH ORG 10H ‘SUM ow ? DTSEG ENDS CDsEG SEGMENT MAIN PROC FAR ASSUME CS:CDSEG,DS:DTSEG,SS:STSEG MOV AX.DTSEG. MOV DS,AX MOV Cx.04 set up loop counter CX=4 MOV DILOFFSET DATA_IN set up data pointer Di MOV BX.00 sinitialize BX ADD_LP: ADD -—BX,[DI] add contents pointed at by [Di] to BX INC DI ‘increment Di twice INC OI 310 point to next word DEC cx lecrement loop counter JNZ ADD_LP :jump if loop counter not zero MOV SIOFFSET SUM load pointer for sum MOV [SI]BX istore in data segment MOV AH.4CH iset up return INT 21H iretum to DOS MAIN ENDP CDSEG ENDS END MAIN Program 2-2, rewritten with full segment definition 74 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING TLE PAGE STSEG STSEG DTSEs DATA_IN COPY DTSEG CDSEG MAIN MAIN COSEG PURPOSE: TRANSFERS 6 BYTES OF DATA 60.132 ‘SEGMENT oB 32 DUP (2) ENDS ‘SEGMENT ORG 10H 0B 25H,4FH,85H, 1FH,.2BH,0C4H ORG 28H 0B 6 DUP(?) ENDS SEGMENT PROC FAR ASSUME CS:CDSEG,DS:DTSEG,SS:STSEG MOV AX,DTSEG MOV DS,AX MOV SIOFFSET DATA_IN ;SI points to data to be copied MOV DILOFFSETCOPY _;Di points to copy of data MOV CX.06H sloop counter = 6 MOV AL{SI] :move the next byte from DATA area to AL MOV [DI]AL move the next byte to COPY area INC SI ‘increment DATA pointer INC DI ‘increment COPY pointer DEC cx decrement LOOP counter JNZ_ MOV_LOOP :jump if loop counter not zero MOV AH.ACH sset up to return INT 21H sreturn to DOS. ENDP ENDS END MAIN Program 2-3, rewritten with full segment definition Data segment definition In full segment definition, the SEGMENT dircetive names the data seg- ment and must appear before the data. The ENDS segment marks the end of the data segment DTSEG SEGMENT __:the SEGMENT directive begins the segment define your data here DTSEG ENDS ithe ENDS segment ends the segment Code segment definition “The code segment also begins with a SEGMENT directive and ends with ‘a matching ENDS directive CDSSEG = SEGMENT _ ithe SEGMENT directive begins the segment your code is here CDSEG ENDS the ENDS segment ends the segment In full segment definition, immediately after the PROC directive is the ASSUME directive, which associates segment registers with specific segments by SECTION 2.6: FULL SEGMENT DEFINTION 5 assuming that the segment register is equal to the segment labels used in the pro- gram, If an extra segment had been used, ES would also be included in the ASSUME statement. The ASSUME statement is needed because a given Assembly language program can have several code segments, one or two or three ‘or more data segments and more than one stack segment, but only one of each can be addressed by the CPU at agiven time since there is only one of each of the seg- ‘ment registers available inside the CPU. Therefore, ASSUME tells the assembler which of the segments defined by the SEGMENT directives should be used. It also helps the assembler to calculate the offset addresses from the beginning of that segment. For example, in "MOV AL,[BX]" the BX register is the offsct of the data segment. Upon transfer of control from DOS to the program, of the three segment registers, only CS and SS have the proper values. The DS value (and ES, if used) must be initialized by the program. This is done as follows in full segment defi- nition: MOV AX,DTSEG. MOV DS,AX :DTSEG is the label for the data segment SECTION 2.7: EXE VS, COM FILES All program cxamples so far were designed to be assembled and linked into EXE files. This section looks at the COM file, which like the EXE file con- tains the executable machine code and can be run at the DOS level. At the end of this section, the process of conversion from one file to the other is shown. Why COM files? ‘There are occasions where, due to a limited amount of memory, one needs to have very compact code. This is the time when the COM file is useful. The fact that the EXE file can be of any size is one of the main reasons that EXE files are used so widely. On the other hand, COM files are used because of their compact- ness since they cannot be greater than 64K bytes. The reason for the 64K-byte limit is that the COM file must fit into a single segment, and since in the 80x86 the size of a segment is 64K bytes, the COM file cannot be larger than 64K, To limit the size of the file to 64K bytes requires defining the data inside the code segment and also using an area (the end area) of the code segment for the stack. (One of the distinguishing features of the COM file program is the fact that in con- trast to the EXE file, it has no separate data segment definition. One ean summa- rize the differences between COM and EXE files as shown in Table 2-2. Table 2-2: EXE vs. COM File Format lunlimited size Istack segment is defined data segment is defined code, data defined at any offset address larger file (takes more memory) COM File maximum size 64K bytes no stack segment definition data segment defined in code segment code and data begin at offset 0100H smaller file (takes less memory) Another reason for the difference in the size of the EXE and COM files is the fact that the COM file does not have a header block. The header block, which occupies 512 bytes of memory, precedes every EXE file and contains information such as size, address location in memory, and stack address of the EXE module, 76 CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING Program 2-4, written in COM format, adds two words of data and saves the result. This format is very similar to many programs written on the 8080/85 microprocessors, the generation before the 8088/86. This format of first having the code and then the data takes longer to assemble; therefore, it is strongly rec~ ‘ommended to put the data first and then the code, but the program must bypass the data area by the use of a JUMP instruction, as shown in Program 2-5. TITLE PROG2-4 COM PROGRAM TO ADD TWO WORDS PAGE 60,132 |CODsG ‘SEGMENT ORG 100H ASSUME CS:CODSG.DS:CODSG.ES:CODSG t——THIS IS THE CODE AREA PROGCODE PROC NEAR MOV AX,DATAT —_EXE2BIN A:PROGIA:PROG1.COM Notice that there is no extension of EXE for PROGI since it is assumed that one is converting an EXE file. Keep in mind that for a program to be con- verted into a COM file, it must be in the format shown in Programs 2-4 and 2-5, SUMMARY ‘An Assembly language program is composed of a series of statements that are either instructions or pseudo-instructions, also called directives. Instructions are translated by the assembler into machine code. Pscudo-instruc- tions are not translated into machine code: They direct the assembler in how to translate the instructions into machine code. The statements of an Assembly lan- ‘guage program are grouped into segments, Other pseudo-instructions, often called data directives, are used to define the data in the data segment. Data can be allo cated in units ranging in size from byte, word, doubleword, and quadword to 10 bytes at atime. The data can be in binary, hex, decimal, or ASCII. The flow of a program proceeds sequentially, from instruction to instruc- tion, unless a control transfer instruction is executed. The various types of con- trol transfer instructions in Assembly language include conditional and uncondi- tional jumps, and call instructions. PROBLEMS Rewrite Program 2-3 to transfer one word at a time instead of one byte. List the steps in getting a ready-to-run program. ‘Which program produces the "exe" file? Which program produces the "obj" file? ‘True or false: The "Ist" file is produced by the assembler regardless of whether or not the programmer wants it. ‘The source program file must have the ".asm" extension in some assemblers. such as MASM. Is this true for the assembler you are using? Circle one: ‘The linking process comes (after, before) assembling. In some applications it is common practice to save all registers at the beginning of a subroutine. Assume that SP = 1288H before a subroutine CALL, Show the contents of the stack pointer and the exact memory contents of the stack after PUSH for the following: a ween 1132:0450 CALL PROC1 1132:0453 INC BX PROC1 = PROC PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI PUSH DI PUSHF Procit — ENDP. 9. To restore the original information inside the CPU at the end of a CALL toa subroutine, the sequence of POP instructions must follow a certain order. ‘Write the sequence of POP instructions that will restore the information in Problem 8. At each point, show the contents of the SP. 8 CHAPTER 2; ASSEMBLY LANGUAGE PROGRAMMING 10. When a CALL is executed, how does the CPU know where to return? II. Ina FAR CALL, and are saved on the stack, whereas in a NEAR CALL, ‘is saved on the stack. 12. Compare the number of bytes of stack taken due to NEAR and FAR CALLs. 13, Find the contents of the stack and stack pointer after execution of the CALL instruction shown next cS cP (2450:673A CALL SUM (2450:673D DEC AH ‘SUM is a near procedure. Assume the value $$:1296 right before the execution of CALL. 14, The following is a section of BIOS of the IBM PC which is described in detail in Chapter 3. All the jumps below are short jumps, meaning that the labels are in the range -128 to +127. IP Code E06C 733F JNC ERROR1 0727139 JNO ERROR1 E0sc sED8 C8: MOV DS.AX EQA7 EBES JMP CB E0AG F4 ERROR1: HLT ‘Verify the address calculations of: (a) INC ERROR! (b) JNO ERRORI (c) IMP C8 15, Find the precise offset location of each ASCII character or data in the following: ‘ORG 20H DATAt DB 1-800-555-1234" ORG 40H DATA2 DB ‘Name: John Jones’ 60H DATAS DB '5956342" 70H DaTas DW 2560H,10000000001108 ORG 80H DATAS DD 25697F6EH DATA? DQ SE7BA21C99F2H ORG 30H DATAB DT —_439997924999828, DATAS DB 6 DUP (0EEH) 16. The following program contains some errors. Fix the errors and make the program run correctly. Verify it through the DEBUG program. This program adds four words and saves the result. TITLE PROBLEM (EXE) PROBLEM 16 PROGRAM PAGE 60,132 MODEL SMALL ‘STACK 32 DATA DATA DW — 234DH,DEGH,3BC7H,566AH_ ORG 10H suM DW ? ‘CODE START: PROC FAR MOV" AX,DATA MOV DS/AX MOV CX\04 {SET UP LOOP COUNTER CX=4 PROBLEMS 9 Mov Bx.0 INITIALIZE BX TO ZERO MOV DILOFFSET DATA. ;SET UP DATA POINTER BX LOOP1: ADD BX,(Dl] |ADD CONTENTS POINTED AT BY [DI] TO BX inc Ol INCREMENT DI JNZ LOOP1 JUMP IF COUNTER NOT ZERO MOV SILOFFSET RESULT ;LOAD POINTER FOR RESULT MOV [SI].BX STORE THE SUM MOV AH.4CH INT 21H START ENDP, END STRT ANSWERS TO REVIEW QUESTIONS SECTION 2.1: DIRECTIVES AND A SAMPLE PROGRAM 1, Pseudo-instructions direct the assembler as to how to assemble the program, 2. Instructions, pseudo-instructions or directives, 3 MODEL SMALL ‘STACK 64 ‘DATA HIGH_DAT 0B 95 CODE START PROC FAR MOV AX,@DATA MOV DS.AX. MOV AH-HIGH_DAT MOV BH.AH MOV DL'BH MOV AH,4CH INT 21H ‘START ENOP. END START 4, (1) there is no ENORMOUS model (2) ENOP label does not match label for PROC directive (@) CODE and .DATA directives need to be switched (4) "MOV AX,DATA" should be "MOV AX,@DATA" (6) "MOV DS,@DATA” should be "MOV DS,AX* (6) END must have the entry point label "MAIN" SECTION 2.2: ASSEMBLE, LINK, AND RUN A PROGRAM 1. (@) MASM must have the *.asm" file as input (©) LINK must have the *.obj" fle as input 2. Editor outputs : (b) .asm Assembler outputs: (a) .0bj, (0) .st, and (e) .crf files Linker outputs: () .exe and (9) map files SECTION 2.3: MORE SAMPLE PROGRAMS, 1. increments the operand, that is, it causes 1 to be added to the operand 2. decrements the operand, that is, it causes 1 to be subtracted from the ‘operand 3. a colon is required after labels referring to instructions; colons are not placed after labels for directives 4, the first moves the contents of the word beginning at offset DATA, the second moves the offset address of DATA 5. the first adds the contents of BX to AX, the second adds the contents of the memory location at offset BX. CHAPTER 2: ASSEMBLY LANGUAGE PROGRAMMING SECTION 2.4: CONTROL TRANSFER INSTRUCTIONS far 2. the instruction right below the jump 3.1P the machine code for the instruction will take up 1 less byte short, near, far the contents of CS and IP were stored on the stack when the call was executed 7. itrestores the contents of CS:IP and returns control to the instruction immediately following the CALL 8. (a) GET.DATA, invalid because *" is only allowed as the first character (b) 1_NUM, because the first character cannot be a number (©) TEST-DATA, because *-" is not allowed (@ RET, is a reserved word oons ‘SECTION 2.5: DATA TYPES AND DATA DEFINITION 1. DB 2. 24 3. no because of the litle endian storage conventions, which will cause the word *7204H'" to be stored with the lower byte (04) at offset 10H and the upper byte at offset 11H; DB allocates each byte as itis defined 448 5. it is used to assign the offset address 6. ifthe value is to be changed later, it can be changed in one place instead of at every occurrence 7. @4 2 8. no ANSWERS TO REVIEW QUESTIONS 81 CHAPTER 3 ARITHMETIC AND LOGIC INSTRUCTIONS AND PROGRAMS Perforin bitwise manipulation using the C language 82 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS. In this chapter, most of the arithmetic and logic instructions are discussed and program examples are given to illustrate the application of these instructions. Unsigned numbers are used in this discussion of arithmetic and logic instructions. Signed numbers are discussed separately in Chapter 6. Unsigned numbers are defined as data in which all the bits are used to represent data and no bits are set aside for the positive or negative sign. This means that the operand can be between 00 and FFH (0 to 255 decimal) for 8-bit data and between 0000 and FFFFH (0 to 65535 decimal) for 16-bit data. The last section of the chapter describes bitwise operations in the C language. SECTION 3.1: UNSIGNED ADDITION AND SUBTRACTION Addition of unsigned numbers The form of the ADD instruction is ‘ADD destination,source ;dest. operand = dest. operand + source operand The instructions ADD and ADC are used to add two operands. The desti- nation operand can be a register or in memory. The source operand can be a register, inmemory, or immediate. Remember that memory-to-memory operations are nevet allowed in 80x86 Assembly language. The instruction could change any of the ZF, SE, AF, CF, or PF bits ofthe flag register, depending on the operands involved, The effect of the ADD instruction on the overflow flag is discussed in Chapter 6 since itis used in signed number operations. Look at Example 3-1 ‘Show how the flag register is affected by MOV AL,OFSH ADD ALOBH Solution: FSH 4111 0101 +9BH + 0000 1011 00H ‘00000000 After the addition, the AL register (destination) contains 00 and the flags are as follows: CF = { since there is a carry out from D7 SF =0 the status of D7 of the result PF =1 the number of Is is zero (zero is an even number) AF = 1 there is a carry ftom D3 to D4 ZF =1 the result of the action is zero (for the 8 bits) In discussing addition, the following two cases will be examined: 1, Addition of individual byte and word data 2. Addition of multibyte data CASE 1: Addition of individual byte and word data In Chapter 2 there was a program that added 5 bytes of data, The total sum was purposely kept less than FFH, the maximum value an 8-bit register can hold. To calculate the total sum of any number of operands, the carry flag should be checked after the addition of each operand. Program 3-1a uses AH to accumulate carries as the operands are added to AL. SECTION 3.1: UNSIGNED ADDITION AND SUBTRACTION 83 ‘Write a program to calculate the total sum of 5 bytes of data. Each byte represents the daily wages of a worker. This person does not make more than $255 (FFH) a day. The decimal data is as follows: 125, 235, 197, 91, and 48. TITLE — PROG3-1A (EXE) ADDING 5 BYTES: PAGE _ 60,132 MODEL SMALL ‘STACK 64 DATA COUNT EQU 05 DATA OB 125,235,197,91,48 ORG 0008H suUM OW? ‘CODE MAIN PROC FAR MOV” AX@DATA MOV DS.AX MOV CX.COUNT _:CXis the loop counter MOV SLOFFSET DATA.SIs the dais pointer MOV AX! “AG hold the sum BACK ADD AL{SII ‘add the next byte to AL JNG OVER iifno carry, continue INC AH ‘else accumulate carry in AH OVER: INC SI increment data pointer DEC Cx ‘decrement loop counter NZ BACK ‘not finished, go add next byte MOV SUM,AX {store sum. MOV AH.ACH INT tH :90 back to DOS MAIN ENDP END MAIN Program 3-La Analysis of Program 3-ta ‘These numbers are converted to hex by the assembler as follows: 125 = 7DH, 235 = OEBH, 197 = 0CSH, 91 = SBH, 48 = 30H. Three iterations of the loop are shown below. ‘The tracing of the program is let to the reader as an exercise. 1, Inthe first iteration of the loop, 7DH is added to AL with CF =0 and AH = 00.CX = 04 and ZF = 0, 2. In the second iteration of the loop, EBH is added to AL, which results in AL = 68H and CF = |. Since a carry occurred, AH is incremented. CX = 03 and ZF = 0 3. In the third iteration, CSH is added to AL, which makes AL = 2DH. Again a carry occurred, so AH is incremented again. CX = 02 and ZF = 0. ‘This process continues until CX = 00 and the zero flag becomes 1, which will cause JNZ to fall through. Then the result will be saved in the word-sized memory set aside in the data segment. Although this program works correctly, due to pipelining itis strongly recommended that the following lines of the program be replaced: Replace these lines With these lines BACK: ADD AL(SI BACK: ADD LISI] UNG OVER ADC AH,00 jadd 1 to AH if CF=1 INC AH INC SI OVER: INC SI CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS ‘The "ADC AH,00" instruction in reality means add 00 + AH + CF and place the result in AH, This is much more efficient since the instruction "JNC OVER” has to empty the queue of pipelined instructions and fetch the instructions from the OVER target every time the carry is zer0 (CF = 0). ‘The addition of many word operands works the same way, Register AX (or CX or DX or BX) could be used as the accumulator and BX (or any general-purpose 16-bit register) for keeping the carries, Program 3-Ib is the same as Program 3-1a, rewritten for word addition. Write a program to calculate the total sum of five words of data. Each data value represents the yearly wages of a worker. This person does not make more than $65,555 (FFFFH) a year. The decimal data is as follows: 27345, 28521, 29533, 30105, and 32375, TITLE — PROGS-1B (EXE) ADDING S WORDS, PAGE 60,132 MODEL SMALL STACK 64 : DATA COUNT EQU 05 DATA = DW_—27345,28521,29533,30105,32575 ORG —0010H SUM = DW 2 DUP(?) : CODE MAIN PROC FAR MOV AX.@DATA Mov DS.AK MOV CX,COUNT _ ;Cxis the loop counter MOV — SLOFFSET DATA‘SIis the data pointer Mov AX00 TAX will hold the sum Mov BXAX BX will hold the carries BACK: ADD AXIS!) [add the next word to AX ‘ADC BXO ‘add camry to BX INC St rement data pointer twice INC SI point to next word DEC cx fecrement loop counter ANZ BACK not finished, continue adding Mov SUMAX ore the sum MOV SUMS2,8x store the carries MOV AH.ACH Int 21H :90 back to DOS MAIN ENOP END MAIN Program 3-1b CASE 2: Addition of multiword numbers Assume a program is needed that will add the total U. S, budget for the last 100 years or the mass of all the planets in the solar system. In cases like this, the numbers being added could be up to 8 bytes wide or more. Since registers are only 16 bits wide (2 bytes), it is the job of the programmer to write the code to break down these large numbers into smaller chunks. to be processed by the CPU. If a 16-bit register is used and the operand is 8 bytes wide, that would take a total of four iterations. However, if an 8-bit register is used, the same operands would require ccight iterations. This obviously takes more time for the CPU. This is one reason to have wide registers in the design of the CPU. Large and powerful computers such as the CRAY have registers of 64 bits wide and larger. SECTION 3.1: UNSIGNED ADDITION AND SUBTRACTION 85 86 Write a program that adds the following two multiword numbers and saves the result: DATAI = 548FB9963CE7H and DATA? = 3FCD4FA23B8DH. TITLE PROG3-2 (EXE) MULTIWORD ADDITION PAGE _ 60,132 MODEL SMALL STACKE4 DATA DATA: DQ" S4eFa9969CE7H ORG 0010 DATA2 DQ. SECD4FAZIBRDH ORG 9020H oaras DQ? ‘CODE MAIN PROC FAR MOV = AX.@DATA MOV DS,AX cue clear cary before fist addition MOV SLOFFSETOATA: —_—_“Slig porter for operant MOV DLOFESETDATA2 "Dl's pointer for operand MOV BXOFFSETDATAS “EX i poiterfor the sum Mov CXO4 {CK is he loop counter BACK MOV AXIS! “move the fst operand to AX ADC AXIO} add the second operand to AX. MOV (exax «store the sum inc SI ‘pont to next word of operand? INC SI INC BI point to next word of operand2 INC BI INC BK ‘point to next word of sum INC BX LOOP BACK iif not finished, continue adding MOV AH,4CH INT 21H go back to DOS MAIN ENOP END MAIN Program 3-2 Analysis of Program 3-2 In writing this program, the first thing to be decided was the directive used for coding the data in the data segment. DQ was chosen since it can represent data as large as 8 bytes wide. The question is: Which add instruction should be used? In the addition of multibyte (or multiword) numbers, the ADC instruction is always used since the carry must be added to the next-higher byte (or word) in the next iteration, Before executing ADC, the carry flag must be cleared (CF =0) so that in the first iteration, the carry would not be added. Clearing the carry flag isachieved by the CLC (clear carry) instruction. Three pointers have been used: SI for DATA1, DI for DATA2, and BX for DATA3 where the result is saved. There is a new instruction in that program, "LOOP XXXX", which replaces the often used "DEC CX" and "INZ_XXXX". In other words: ‘When the "LOOP xxxx" is executed, CX is decremented automatically, and if CX is not 0, the microprocessor will jump to target address xxxx. If CX is 0, the next instruction (the one below "LOOP s00xx") is executed, CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS ‘Subtraction of unsigned numbers SUB dest,source dest = dest - source In subtraction, the 80x86 microprocessors (indeed, almost all modern CPUs) use the 2's complement method. Although every CPU contains adder circuitry, it would be too cumbersome (and take too many transistors) to design separate subtractor circuitry. For this reason, the 80x86 uses internal adder circuitry to perform the subtraction command, Assuming that the 80x86 is executing simple subtract instructions, one can summarize the steps of the hardware of the CPU in executing the SUB instruction for unsigned numbers, as follows. 1. Take the 2's complement of the subtrahend (source operand), 2. Add it to the minuend (destination operand). 3. Invert the carry. ‘These three steps are performed for every SUB instruction by the internal hardware of the 80x86 CPU regardiess ofthe source and destination of the operands as long as the addressing mode is supported. It is after these three steps that the result is obtained and the flags are set. Example 3-2 illustrates the three steps. Example 3-2 Show the steps involved in the followin MOV ALF Toad AL“3FH MOV BH,23H load BH=23H SUB ALBH ‘Subtract BH from AL. Place result in AL. Solution: AL SE 0011 1111 oot 111 -BH 23 - 00100011 #1101 1101 (2's complement) ic ToO0T1100 —_CF=0 (step 3) The flags would be set as follows: CF = 0, ZF = 0, AF = 0, PF = 0, and SF = 0, The programmer must look at the carry flag (not the sign flag) to determine if the result is positive or negative. After the execution of SUB, if CF = 0, the result is positive; if CF = 1, the result is negative and the destination has the 2’s complement of the result, Normally, the result is left in 2's complement, but the NOT and INC instructions can be used to change it, The NOT instruction performs the 1’s complement of the operand; then the operand is incremented to get the 2's complement. See Example 3-3. Example 3-3 ‘Analyze the following program: ‘fom the data segment DATAI DB 4CH DATA2 DB 6EH DATAS 0B ? :from the code segment: ‘MOV OH.DATA ‘Joad DH with DATA value (4CH) SUB OHDATAZ t DATA2 (6E) from DH (4CH) JNC NEXT. jump to NEXT target NoT OH St CF=1 then take 1's complement INC OH ‘and increment to get 2's complement NEXT. MOV DATA3,DH ‘save DH in DATA3 Solution: Foliowing the three steps for "SUB DH,DAT? 4c "0100 1100 0100 1400 =6E 01101110 2's comp +1001 0010 -22 OM104 1110 Ct (step 3) the result is negative SECTION 3.1: UNSIGNED ADDITION AND SUBTRACTION 87 SBB (subtract with borrow) This instruction is used for multibyte (multiword) numbers and will take care of the borrow of the lower operand. Ifthe carry flag is 0, SBB works like SUB, Ifthe carry flag is 1, SBB subtracts 1 from the result. Notice the "PTR" operand in Example 3-4. The PTR (pointer) data specifier directive is widely used to specify the size of the operand when it differs from the defined size. In Example 3-4, "WORD PTR" tells the assembler to use a word operand, even though the data is defined as a doubleword. DATA.A DATAB RESULT Solution: Example 3-4 ‘Analyze the following program: DD 62562FAH DD 4120638H 0D? MOV" AX.WORD PTR DATA A TAX=62FA SUB AXWORD PTR DATS )SUB 9638 from AX MOV WORD PTR RESULTAX ‘save the result MOV AX WORD PTR DATA A +2 {AX=0625 SBB AX WORD PTR DATALB +2 SUB 0412 with borrow MOV WORD PTR RESULT#2,AX ‘save the result After the SUB, AX = 62FA - 963B = CCBF and the carry flag is set. Since CF = 1, when SBB is executed, AX = 625~412—1=212. Therefore, the value stored in RESULT is 0212CCBF. Review Questions 1. The ADD instruction that has the syntax "ADD destination, source" replaces the ‘operand with the sum of the two operands. 2. Why is the following ADD instruction illegal? ADD DATA_1,DATA_2 Rewrite the instruction above in a correct form. The ADC instruction that has the syntax "ADC destination, source" replaces the operand with the sum of 5. The execution of part (a) below results in ZF = 1, whereas the execution of part (b) results in ZF = 0. Explain why. (a) MOV BL,04FH (b) MOV BX,04FH ADD BL,OBIH ADD BX.0B1H 6. The instruction "LOOP ADD_LOOP" is equivalent to what two instructions? 7. Show how the CPU would subtract 05H from 43H. 8. IfCF=1, AL=95, and BL =4F prior to the execution of “SBB AL,BL", what will be the contents of AL after the subtraction? ae SECTION 3.2: UNSIGNED MULTIPLICATION AND DIVISION 88 One of the major changes from the 8080/85 microprocessor to the 8086 was inclusion of instructions for multiplication and division. In this section we cover each one with examples, This is multiplication and division of unsigned numbers. Signed numbers are treated in Chapter 6. In multiplying or dividing two numbers in the 80x86 microprocessor, the use of registers AX, AL, AH, and DX is necessary since these functions assume the use of those registers. Multiplication of unsigned numbers In discussing multiplication, the following cases will be examined: (1) byte times byte, (2) word times word, and (3) byte times word. CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS bytex byte: In byte by byte multiplication, one of the operands must be in the AL register and the second operand can be either in a register or in memory as addressed by one of the addressing modes discussed in Chapter |. After the multiplication, the result is in AX. See the following example: RESULT DW ? :result is defined in the data segment MOV AL25H ra byte is moved to AL MOV BL65H ‘immediate data must be in a register MUL BL AL=25 65H MOV RESULTAX ithe resultis saved In the program above, 25H is multiplied by 6SH and the result is saved in word-sized memory named RESULT. In that example, the register addressing moe was used. The next three examples show the register, direct, and register indirect addressing modes. ‘from the data segment DATA! DB 25H DATA2 DB 65H RESULT DW ? ‘from the code segment: MOV AL,DATAt MOV BL.DATAZ MUL BL ‘register addressing mode MOV RESULTAX or MOV AL,DATAI MUL DATA2 idirect addressing mode MOV RESULTAX or MOV AL,DATAI MOV SI,OFFSET DATA2 MUL BYTE PTRISI] stegister indirect addressing mode MOV RESULTAX In the register addressing mode example, any 8-bit register could have been used in place of BL. Similarly, in the register indirect example, BX or DI could have been used as pointers. If the register indirect addressing mode is used, the ‘operand size must be specified with the help of the PTR pseudo-instruction, In the absence of the "BYTE PTR" directive in the example above, the assembler could. not figure out ifit should use a byte or word operand pointed atby SI. This confusion would cause an error. ‘word x word: In word by word multiplication, one operand must be in AX and the second operand can be in a register or memory. After the multiplication, registers AX and DX will contain the result. Since word x word multiplication can produce a 32-bit result, AX will hold the lower word and DX the higher word. Example: DATA3 DW 2378H DATAS DW 2F7SH RESULT1 DW 2DUP(?) MOV AX,DATAS ‘Joad first operand into AX MUL DATA :muttiply it by the second operand MOV RESULT1,AX ‘store the lower word result MOV RESULT1+2,Dx :store the higher word result SECTION 3. INSIGNED MULTIPLICATION AND DIVISION word x byte: This is similar to word by word multiplication except that AL contains the byte operand and AH must be set to zero. Example: sfrom the data segment DATAS DB 6BH DATAG DW 1203H RESULT3 DW 2 DUP(?) sfrom the code segment: MOV AL,DATAS: ‘AL holds byte operand SUB AHAH [AH must be cleared MUL — DATAG ‘oyte in AL multiplied by word operand MOV BX,OFFSET RESULTS BX points to storage for product MOV [BX]AX JAX holds lower word MOV [BX]*2,Dx :DX holds higher word Table 3-1 gives a summary of multiplication of unsigned numbers. Using the 80x86 microprocessor to perform multiplication of operands larger than 16-bit size takes some manipulation, although in such cases the 8087 coprocessor is normally used. Multiplication _|Operand 1 Operand 2 [byte x byte |AL register or memory [word x word _|AX y | word byte AL=byte, AH=0 __ register or memory Division of unsigned numbers In the division of unsigned numbers, the following cases are discussed: 1, Byte over byte 2. Word over word 3. Word over byte 4. Doubleword over word In divide, there could be cases where the CPU cannot perform the division, In these cases an interrupt is activated. In recent years this is referred to as an exception. In what situation can the microprocessor not handle the division and must call an interrupt? They are if the denominator is zero (dividing any number by 00), and 2. if the quotient is too large for the assigned register. In the IBM PC and compatibles, if cither of these cases happens, the PC will display the “divide error” message. byte/byte: In dividing a byte by a byte, the numerator must be in the AL register and AH must be set to zero, The denominator cannot be immediate but can be in a register or memory as supported by the addressing modes. After the DIV instruction is performed, the quotient is in AL and the remainder is in AH. The following shows the various addressing modes that the denominator can take. 90 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS DATA? OB 95 DATA8 DB. 10 QouTi DB. ? REMAIN1 DB ? using immediate addressing mode will give an error MOV = AL,DATA7 —— jmove data into AL ‘SUB AH.AH rolear AH piv 10 immed. mode not allowed! zallowable modes include: jusing direct mode MOV AL,DATA7—_ALholds numerator SUB AH,AH JAH must be cleared bv DATAB jdivide AX by DATA MOV QOUTI,AL_—_ quotient = AL = 09 MOV REMAIN1,AH_ temainder = AH = 05 jusing register addressing mode MOV AL,DATA7 AL holds numerator SUB AH,AH JAH must be cleared MOV —BH.DATA8-—_:move denom. to register piv 8H E Mov QOUTIAL MOV REMAINY,AH 3 05 :using register indirect addressing mode MOV ALDATA7 ‘AL holds numerator SUB AHAH {AH must be cleared MOV BXOFFSET DATA —_:BX holds offset of DATAS, piv BYTE PTR [BX] ‘divide AX by DATAB Mov QOUTZ,Ax Mov REMAIND2,DX word/word: In this case the numerator is in AX and DX must be cleared. ‘The denominator can be in a register or memory. After the DIV, AX will have the quotient and the remainder will be in DX. Mov AX,10050—_;AX holds numerator SUB Dx,0x ;DX must be cleared Mov BX,100 ;BX used for denominator piv BX Mov QOUT2,AX __ quotient = AX = 64H = 100 Mov REMAIND2,0X ;remainder = DX = 32H = 50 word/byte: Again, the numerator is in AX and the denominator can be in aregister or memory. After the DIV instruction, AL will contain the quotient, and AH will contain the remainder. The maximum quotient is FFH. The following program divides AX = 2055 by CL=100, Then AL= 14H (20 decimal) is the quotient and AH = 37H (55 decimal) is the remainder. Mov AX,2055, JAX holds numerator Mov 1,100 :CL used for denominator ov cL Mov QUOAL JAL holds quotient Mov REMI,AH :AH holds remainder SECTION 3.2: UNSIGNED MULTIPLICATION AND DIVISION 1 doubleword/word: The numerator is in AX and DX, with the most significant word in DX and the least significant word in AX. ‘The denominator can be ina register or in memory. After the DIV instruction, the quotient will be in AX, the remainder in DX. The maximum quotient is FFFFH. sfrom the data segment: DATA1 DD 105432 DATA2 OW 10000 QuoT OW 2 REMAIN OW 2 sfrom the code segment: MOV AX,WORD PTR DATA1 JAX holds lower word MOV DX,WORDPTRDATA1#2 —_;DX higher word of numerator ov DATA2 Mov = QUOTAX JAX holds quotient MOV REMAIN,OX :DX holds remainder In the program above, the contents of DX:AX are divided by a word-sized data value, 10000, Now one might ask: How does the CPU know that it must use the doubleword in DX:AX for the numerator? The 8086/88 automatically uses DX:AX as the numerator anytime the denominator is a word in size, as was seen earlier in the case of a word divided by a word. This explains why DX had to be cleared in that case. Notice in the example above that DATA is defined as DD but fetched into a word-size register with the help of WORD PTR. In the absence of WORD PTR, the assembler will generate an error. A summary of the results of division of unsigned numbers is given in Table 3-2. Table 3-2: Unsigned Division Summary ce Division Numerator __ Denominator [Quotient [Rem _| |[byte/byte |AL=byte, AH=0 register ormemory __|AL! |AH word/word = |AX= word, DX=0 __register or memory jax? DX} | word/yte LAX = word register or memory. ___AL! AH |doubleword/word _DXAX =doubleword __ register or memory Ax? DX Notes 1. Divide error interapt if AL > FFH. 2, Divide ero interrpe f AX. FFB. Review Questions 1. In unsigned multiplication of a byte in DATAL with a byte in AL, the product will be placed in register(s) 2. Inunsigned multiplication of AX with BX, the product is placed in register(s) 3. Inunsigned multiplication of CX with a byte in AL, the product is placed in regis- ter(s) 4, In-unsigned division ofa byte in AL by a byte in DH, the quotient will be placed in and the remainder in 5. In unsigned division of a word in AX by a word in DATAL, the quotient will be placed in and the remainder in 6. Inunsignéd division of a word in AX by a byte in DATA2, the quotient will be placed in and the remainder in 7. In-unsignéd division of a doubleword in DXAX by a word in CX, the quotient will be placed in and the remainder in 92 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS SECTION 3.3: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS In this section we discuss the logic instructions AND, OR, XOR, SHIFT, and COMPARE. Instructions are given in the context of examples. AND AND destination, source This instruction will perform a logical AND on the operands and place the result in the destination, The destination operand can be a reg- ister or in memory. The source operand can be a register, in memory, or immediate. Example 3-5 Show the results of the following: Mov BL.3sH AND BLOFH "AND BL with OFH. Ptace the result in BL. Solution: 38H 00110101 OFH 00001114 05H = OOOOOTOT Flag sattings will be: St 0, PF = 4, CF = OF AND will automatically change the CF and OF to zero and PF, ZF and SF are set according to the result. The rest of the flags are either undecided or unaffected. As seen in Example 3-5, AND can be used to mask certain bits of the operand, It can also be used to test for a zero operand: AND DH,DH UZ XXXX XXX ‘The above will AND DH with itself and set ZF = | if the result is zero, making the CPU fetch from the target address XXXX. Otherwise, the instruction below JZ is executed. AND can thus be used to test if a register contains zero. oR OR destination,source The destination and source operands are ORed and the result is placed in the destination. OR can be used to set certain bits of an operand to 1. The destination operand can be a register or in memory. The source operand can be a register, in ‘memory, or immediate. The flags will be set the same as for the AND instruction. CF and OF will be reset to zero and SF, ZF, and PF will be set according to the result. All other flags are not affected. See Example 3-6. ‘The OR instruction can also be used to test for a zero operand, For example, “OR BL,O" will OR the register BL with 0 and make ZF = 1 if BL is zero, "OR BL,BL" will achieve the same result. SECTION 3.3: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS 7 93 Example 3-6 Show the results of the following: MOV AX,0504 OR AX,ODA6SH Solution: 0504+ 0000 0101 0000 0100, DABSH — 1101 1010.0110 1000 Flags willbe: SF =1, ZF = 0, PF = 1, CF = OF =0. THOT 1411 0710 1100 Notice that parity is checked for the lower 8 bits only. XOR XOR dest,src LX ‘The XOR instruction will eXclusive-OR f I | f| > Pd io S a the operands and place the result in the destination. XOR sets the result bits to 1 if they are not equal; otherwise, they are reset to 0. The flags are set the same as for the AND instruction. CF = 0 and OF = 0 are set internally and the rest are changed according to the result of the operation. The rules for the operands are the same as in the AND and OR instructions. See Examples 3-7 and 3-8 more Example 3-7 ‘Show the results of the following: MOV DH.SaH XOR —DH.78H Solution: 4H 01010100 73H o1111000 2 ooror100 Fag setings willbe: SF = 0, 2F = 0, PF =0, CF = OF =0 Example 3-8 The XOR instruction can be used to clear the contents of a register by XORing it with itself. Show how "XOR AH,AH" clears AH, assuming that AH = 45H. Solution: 45H 1000101, 43H 01000101 00 90000000 Flag settings will be: SF = 0, ZF = 1, PF =1 , CF = OF =0. XOR can also be used to see ifftwo registers have the same value, "XOR BX,CX" will make ZF ~ | if both registers have the same value, and if they do, the result (0000) is saved in BX, the destination, Another widely used application of XOR is to toggle bits of an operand. For example, to toggle bit 2 of register AL: XOR AL,O4H :XOR AL with 0000 0100 This would cause bit 2 of AL to change to the opposite value; all other bits would remain unchanged. 94 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS. SHIFT ‘There are two kinds of shift: logical and arithmetic. ‘The logical shiftis for unsigned operands, and the arithmetic shift is for signed operands. Logical shift will be discussed in this section and the discussion of arithmetic shift is postponed to Chapter 6. Using shift instructions shifts the contents of a register or memory location right or left. The number of times (or bits) that the operand is shifted can be specified directly if itis once only, or through the CL register if it is more than once. SHR: This is the logical shift right. 0 The operand is shifted right bit by bit, and for every shift the LSB (east significant bit) will go to the carry flag (CF) and the MSB. (most significant bit) is filled with 0, Examples 3-9 and 3-10 should help to clarify LsB|—— cr MsB SHR. Example 3-9 ‘Show the result of SHR in the following: MOV AL.OAH MOV CL.3-_:set number of times to shit SHR ALCL Solution: 9AH = 10011010 1001101 CF=0 (shifted once) 0100110, CF=1 (shifted twice) 00010011 CF=0 (shifted three times) Alter three times of shifting right, AL = 13H and CF = 0. If the operand is to be shifted once only, this is specified in the SHR instruction itself rather than placing 1 in the CL. This saves coding of one instruction: MOV BX,OFFFFH :BX=FFFFH SHR 8X,1 :shift right BX once only ‘After the shift above, BX ~ 7FFFH and CF = 1. Although SHR does affect the OF, SF, PF, and ZF flags, they are not important in this case. The operand to be shifted can be in a register or in memory, but immediate addressing mode is not allowed for shift instructions, For example, "SHR 25,CL" will cause the assembler to give an error. Example 3-10 Show the results of SHR in the following: from the data segment: DATAL DW 7777H from the code segment: TIMES EQU 4 MOV” CL,TIMES iCL=04 SHR DATAT,CL ‘shift DATA1 CL. times Solution: Afier the four shifts, the word at memory location DATA! will contain 0777. The four LSBs are lost through the carry, one by one, and 0s fill the four MSBs. SECTION 3.3: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS 95 SHL: Shift left is also a logical shift. It is the reverse of SHR. Afterevery shift, the LSB is filled with CF ie bead : 0 and the MSB goes to CF Al the rules are the same as SHR Example 3-11 ‘Show the effeets of SHL in the following Mov ons Mov Cua Sal Bae Solution: 0000110 oo0ott00 {shifted tet once) 001000 0110000 ceo 1400000, (shited four times} Ate the four shifts left, the DH register nas 80H and CF = 6, Example 3-11 could have been coded as MOV DHS SHL DH,t SHL DH! SHL DH SHU DH.1 COMPARE of unsigned numbers CMP — destination,source :compare dest and sre ‘The CMP instruction compares two operands and changes the flags accord- ing to the result of the comparison. The operands themselves remain unchanged ‘The destination operand can be in a register or in memory and the source operand can be in a register, in memory, or immediate. Although all the CF, AF, SF, PF, ZF, and OF flags reflect the result of the comparison, only the CF and ZF are used, as outlined in Table 3-3 Table 3-3: Flag Settings for Compare Instruction [Compare operands. __jcr___jze | ‘destination > source _ destination = source ___ 0 1 | |destination < source ‘The following demonstrates how the CMP instruction is used: DATA1 = DW 235FH Mov AX,OCCCCH MP AX,DATA1 —_jcompare CCCC with 235F JNC OVER jjump if CF=0 SUB AXAX OVER: INC DATAt 96 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS In the program above, AX is greater than the contents of memory location SCCH >235FH); therefore, CF ~ 0 and JNC (jump no carry) will 20 to In contrast, look at the following: Mov BX,7888H Mov CX,9FFFH MP BX,CX scompare 7888 with 9FFF NC NEXT ADD BX,4000H NEXT. ADD CX,250H In the above, BX is smaller than CX (7888H < 9FFFH), which sets CF = 1, making "INC NEXT" fall through so that "ADD BX,4000H" is executed. In the example above, CX and BX still have their original values (CX = 9FFFH and BX. = 7888H) after the execution of "CMP BX,CX". Notice that CF is always checked for cases of greater or smaller than, but for equal, ZF must be used. ‘The next program sample has a variable named TEMP, which is being checked to see if it has reached 99: TEMP = DB? Mov ALTEMP move the TEMP variable into AL cMP AL 99 scompare AL with 99 JZ HOT_HOT if ZF=1 (TEMP INC Bx otherwise (ZI HOT_HOT: HLT ‘halt the system ‘The compare instruction is really a SUBtraction except that the values of the operands do not change. The flags are changed according to the execution of ‘SUB. Although alll the flags are affected, the only ones of interest are ZF and CF. It must be emphasized that in CMP instructions, the operands are unaffected regardless of the result of the comparison. Only the flags are affected. This is despite the fact that CMP uses the SUB operation to set or reset the flags. Program 3-3 uses the CMP instruction to search for the highest byte in a series of 5 bytes defined in the data segment. The instruction "CMP AL,[BX]" works as follows, where [BX] is the contents of the memory location pointed at by register BX. If AL < [BX\, then CF = | and [BX] becomes the basis of the new comparison. if AL> [BX], then CF = 0 and AL is the larger of the two values and remains the basis of comparison. ‘Although JC Gump carry) and JNC (jump no carry) cheek the carry flag and can be used after a compare instruction, it is recommended that JA (jump above) and JB (jump below) be used for two reasons. One reason is that DEBUG will unassemble JC as JB, and JNC as JA, which may be confusing to beginning programmers. Another reason is that "jump above" and "jump below" are easier to understand than "jump carry" and "jump no carry," since it is more immediately apparent that one number is larger than another, than whether a carry would be generated if the two numbers were subtracted. Program 3-3 uses the CMP instruction to search through 5 bytes of data to find the highest grade. The program uses register Alto hold the highest grade found so far. AL is given the initial value of 0. A loop is used to compare each of the 5 bytes with the value in AL. If AL contains a higher value, the loop continues to check the next byte. If AL is smaller than the byte being checked, the contents of AL are replaced by that byte and the loop continues. SECTION 3.3: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS ‘Assume that there is a class of five people with the following grades: 69, 87, 96, 45, and 75. Find the highest grade. TITLE PROGS-3 (EXE) CMP EXAMPLE 60,132 PAGE , MODEL SMALL ‘STACK 64 DATA GRADES DB” 60,87,96,46,75 ORG 0008 HIGHEST DB”? CODE MAIN PROC FAR MOV” AX.@DATA DS\AX Mov DS, MOV CXS ‘Set up loop counter MOV BXOFFSET GRADES “BX points to GRADE data SUB ALAL {AL holds highest grade found so far AGAIN; CMP ALIBX) ‘compare next grade to highest JA NEXT jump f AL sti highest Mov ALIBX) ‘else AL holds new highest NEXT. INC. BX ipoint to next grade LOOP AGAIN ‘Continue searen MOV HIGHEST,AL {Store highest grade MOV AHACH Int 21H 290 back to DOS MAIN ENDP. END MAIN Program 3-3 Program 3-4 uses the CMP instruction to determine if an ASCII character is uppercase or lowercase. Note that small and capital letters in ASCII have the following values: Letter _| Hex _| Binary | Letter | Hex | Binary A 41_| 01000001 a__| 61 | o1oo001 B 42} 01000010 b__| 62 | o1oooi0 c 43_| 01000011 c_{ 63 | onoo011 y. 59_| 01011001 y | 79 | o1noo1 Zz SA_| 1011010 z_| 7A | _o1i010 Asan be seen, there is a relationship between the pattem of lowercase and uppercase letters, as shown below for A and a: A 01000001 41H a 01100001 61H Tho only bit that changes is d5. To change from lowercase to uppercase, 45 must be masked. Program 3-4 first detects if the leter is in lowercase, and if it is, it is ANDed with 1101 1111B = DFH. Otherwise, itis simply left alone. To determine if itis a lowercase letter, itis compared with 61H and 7AH to see if it is in the range ato z. Anything above or below this range should be left alone. (CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS TITLE — PROG3-4 (EXE) LOWERCASE TO UPPERCASE CONVERSION PAGE 60,132 MODEL SMALL STACK 64 . DATA DATA1 DB mY NAME is jOe" ‘ORG —0020H DATA2 0B 140UP(2) ‘CODE MAIN PROC FAR MOV’ AX,@DATA Mov MOV SLOFFSETDATAI —_—_;SI points to orginal data MOV BXOFFSETOATA2 BK points to uppercase data Mov Cx14 {CX e loop counter pack: MOV ALIS ‘get next character CMP ACGIH ltless than ‘a we (OVER :then no need fo convert ‘CMP AL.7AH «if. greater than ‘2’ vA. OVER ‘then no need to convert AND AL 110117118 ‘mask 5 fo conver to uppercase over: MOV [BXIAL ‘stove uppercase character INC SI ‘increment pointer to orginal INC BK ‘increment pointer to uppercase data Loop BACK Jeontinue looping H'CX™> 0 MOV AH.ACH INT 21H :go back to DOS MAIN ENDP END MAIN Progeam 3-4 In Program 3-4, 20H could have been subtracted from the lowercase letters instead of ANDing with 1101 1111B. That is what IBM does, as shown next, IBM BIOS method of converting from lowercase to uppercase 2357 i CONVERT ANY LOWERCASE TO UPPERCASE 2358 EBre 2359 60: {LOWER TO UPPER EBFB3C81 2360 MP AL! |FIND OUT IF ALPHABETIC. EBFD 7206 © 2361 JB 61 !NOT_CAPS_STATE EBFF3C7A 2362 CMP AL,2 ECo17702 © 2363 Ja ket INOT_CAPS_ STATE, C03 2020 2364 suB AL/aA’ SCONVERT TO UPPERCASE COS 2365 kot EC0S 8B1E1C00 2366 MOV BX.BUFFER_TAIL ;GET THE END POINTER STO THE BUFFER (Reprinted by permission trom “18M Technical Reference” e. 1984 by International Business Machines Corporation) Review Questions ‘Use operands 4FCAH and C237H to perform: (a) AND) OR ()XOR 2. ANDing a word operand with FFFFH will result in what value for the word oper= ‘and? To set all bits of an operand to 0, it should be ANDed with 3, To set all bits of an operand to 1, it could be ORed with 4, XORing an operand with itself results in what value for the operand? 5. Show the steps if value AOF2H were shifted left three times. Then show the steps if AOF2H were shifted right three times. 6. The CMP instructions works by performing a(n) operation on the oper ands and setting the flags accordingly. 7, True or false. The CMP instruction alters the contents of its operands. SECTION 3.3: LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS 99 058 E058 E058 E058 FA ESC B4D5 EO5E 9& E0SF 734C E061 754A £083 7848 £065 7946 E067 9F £068 B105 E06A D2EC, E06C 733F EO8E BOSO £070 DOEO £072 7139 £074 9264 E076 9E E077 7634 E079 7832 E078 7A30 E070 9F EO7E B105 080 D2EC £082 7229 £084 DOES E086 7025 088 BAFFFF E0BB FO E08C. E08C 8ED8, E08E 8CDB E090 8EC3 E092 8CG1 094 8ED1 E096 8¢D2 098 8BE2 OSA 8BEC E0SC 8BF5 E09E SFE EOAO 7307 E0A2 33C7 EOA4 7507 EOAG F8 E0A7 EBES E0Ag E0A9 OBC7 EOAB 7401 EOAD F4 BIOS examples of logic instructions In this section we examine some real-life examples from IBM PC BIOS, programs. The purpose is to see the instructions discussed so far in the context of real-life applications When the computer is tumed on, the CPU starts to execute the programs stored in BIOS in order to set the computer up for DOS. If anything has happened to the BIOS programs, the computer can do nothing. The first subroutine of BIOS is to test the CPU. This involves checking the flag register bit by bit as well as checking all other registers. The BIOS program for testing the flags and registers is given followed by their explanation: 308, ASSUME CS:CODE,DS:NOTHING ES:NOTHING,SS:NOTHING 307 ORG 0E058H 309 START: 310 cu : DISABLE INTERRUPTS 311 MOV AH.ODSH ‘SET SF, CF ZF, AND AF FLAGS ON 312 SAHF 313 INC ERROT :GO TO ERR ROUTINE IF CF NOT SET 314 YNZ ERROt :GO TO ERR ROUTINE IF ZF NOT SET 315 NP ERROt }GO TO ERR ROUTINE IF PF NOT SET 316 JNS_ ERROt }GO TO ERR ROUTINE IF SF NOT SET 317 LAH ; LOAD FLAG IMAGE TO AH 318 MOV CLS LOAD CNT REG WITH SHIFT CNT 319 SHR AH,CL | SHIFT AF INTO CARRY BIT POS 320 INC ERROt GO TO ERR ROUTINE IF AF NOT SET 321 MOV AL40H ‘SET THE OF FLAG ON 322 SHL ALY ‘SETUP FOR TESTING 323 JNO ERROt :GO TO ERR ROUTINE IF OF NOT SET 324 XOR AH AH SET AH 325 ‘SAHF ECLEAR SF. CF, ZF, AND PF 326 JBE ERROt :GO TO ERR ROUTINE IF CF ON 327 FOR GO TO ERR ROUTINE IF ZF ON 328 JS ERRO? GO TO ERR ROUTINE IF SF ON 329 JP ERROt }GO TO ERR ROUTINE IF PF ON LaHF [LOAD FLAG IMAGE TO AH MOV CLS : LOAD CNT REG WITH SHIFT CNT SHR AH.CL SHIFT AF INTO CARRY BIT POS, JC ERRO1 }GOTO ERR ROUTINE IF ON SHL AH ; CHECK THAT OF IS CLEAR JO" ERROT }GO TO ERR ROUTINE IF ON ~ READ WRITE THE 888 GENERAL AND SEGMENTATION REGISTERS WITH ALL ONES AND ZEROES. MOV AXOFFFFH — : SET UP ONE'S PATTERN IN AX 343 MOV 0S,Ax : WRITE PATTERN TO ALL REGS 344 MOV 8XxDS 345 Mov EsBx 346 MOV CXS 347 Mov $8\cx 348 Mov DBXSs 349 MOV PDX 350 MOV BPSP 351 MOV SI.BP 352 Mov DIS! 353 JNC CS) TSTIA 354 XOR AX PATTERN MAKE IT THRU ALL REGS. 355, NZ ERROt NO - GO TO ERR ROUTINE 356 cu 357 IMP cB 358 C9: 359 OR KOI ; ZERO PATTERN MAKE IT THRU? 360 iz cto :YES-GOTONEXT TEST 361 ERROM: HLT HALT SYSTEM (Reprinted by permission from “IBM Technical Reference” c. 1984 by International Business Machines Corporation) 100 (CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS Line-by-line explanation: Line Explanation 310 CLI ensures that no interrupt will occur while the test is being conducted. 31 MOV AH,ODSH: flag SZ-AC-P-C DH 1101 0101 312 ‘SAHF (store AH into lower byte of the flag register) is one way to move data to flags. Another is to use the stack MOV: ‘AX,QODSH PUSH AX POPF However, there is no RAM available yet to use for the stack because the CPU is tested before memory is tested. 313-316 Will make the CPU jump to HLT if any flag does not work. 317 LAHF (load AH with the lower byte of flag register) is the opposite of SAHF. 318 Loads CL for five shifts 319 "SHR AHLCL". By shifting AH five times, AF (auxiliary carry) will be in the CF position. 320 Ifno AF, there is an error, Lines 317 to 320 are needed because there is no jump condition instruction for AF. 321-323 Checks the OF flag. This is discussed in Chapter 6 when signed numbers are discussed 324-335 Checks the same flags for zero. Remember that JNZ is the same as JBE. 340 Loads AX with FFFFH 3a STC (set the carry) makes CF = 1. 343 - 352 Moves the AX value (FFFFH) into every register and ends up with DI = FFFFH if the registers are good. 353 Since CF=I (remember STC) it falls through 354 Exclusive-ORing AX and DI with both having the same FFFFH value makes AX ~ 0000 and ZF = 1 if the registers are good (see lines 343 - 352). If ZF = 0, one of the registers must have commupted the data FFFF, therefore the CPU is bad. 355 If ZF = 0, there is an error. 356 CLC clears the carry flag. This is the opposite of STC. 357 Jumps to C8 and repeats the same process, this time with value 0000. The contents of AX are moved around to every register until DJ = 0000, and at 353 the NC C9 will jump since CF =O by the CLC instruction before it went to the loop. 359 AtC9, AX and DI are ORed. 1f 0000, the contents of AX are copied successfully to all registers, DI will be 0000; therefore, ORing will raise the ZF, making ZF = 1. 360 If ZF = 1, the CPU is good and the system can perform the next test. Otherwise, ZF = meaning that the CPU is bad and the system should be halted, SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS Digit BCD In 80x86 microprocessors, there are many in- Bee eee structions that handle ASC! and BCD numbers. This i. a section covers these instructions with examples. 7 a 3 ora BCD number system 7 a BCD stands for binary coded decimal. BCD is : era needed because human beings use the digits 0 to 9 for 7 ous numbers. Binary representation of 0109 is called BCD (see 8 1000 Figure 3-1). In computer literature one encounters two 8 i001 terms for BCD numbers: (1) unpacked BCD (2) packed BCD igus $1 BED eove| BCD AND ASCII OPERANDS AND INSTRUCTIONS 101 Unpacked BCD Tn unpacked BCD, the lower 4 bits of the number represent the BCD number and the rest of the bits are 0. Example: "0000 1001” and "0000 0101" are unpacked BCD for 9 and 5, respectively. In the case of unpacked BCD it takes | byte of ‘memory location or a register of 8 bits to contain it. Packed BCD In the case of packed BCD, a single byte has two BCD numbers in it, one. in the lower 4 bits and one in the upper 4 bits. For example, "0101 1001" is packed BCD for 59. Ittakes only 1 byte of memory to store the packed BCD operands, This is one reason to use packed BCD since itis twice as efficient in storing data, ASCII numbers Tn ASCII keyboards, when key "0" is activated, for example, "011 0000" (30H) is provided to the computer. In the same way, 31H (011 0001) is provided for key "I", and so on, as shown in the following list key ASCIhex) — Bina BCD (unpack O50 511 0600 a0 B000 1 31 011 0001 1900 0001 2 & 611 ota 000 bot 3 8 044 0011 0000 0011 os 044 0100 6300 0100 5 35 O14 o101 0000 0101 8 88 or on0 0000 0410 7 ¥ or om 000 0111 8% ott 4000 000 +000 9 3 ot 1004 ‘000 1001 ‘It must be noted that although ASCII is standard in the United States (and many other countries), BCD numbers have universal application, Now since the keyboard and printers and monitors are all in ASCII, how does data get converted from ASCII to BCD, and vice versa? These are the subjects covered next. ASCII to BCD conversion ‘To process data in BCD, first the ASCII data provided by the keyboard must be converted to BCD. Whether it should be converted to packed or unpacked BCD depends on the instructions to be used. There are instructions that require that data be in unpacked BCD and there are others that must have packed BCD data to work properly. Each is covered separately. ASCII to unpacked BCD conversion To convert ASCII data to BCD, the programmer must get rid of the tagged "011" in the higher 4 bits of the ASCII. To do that, each ASCII number is ANDed. with "0000 1111" (OFH), as shown in the next example. This example is written in three different ways using different addressing modes. The following three pro- grams show three different methods for converting the 10 ASCII digits to unpacked BCD. All use the same data segment: ASC OB '9562481273' ORG 010K, UNPACK 08 10 DUP(?) In Program 3-Sa, notice that although the data was defined as DB, a byte definition directive, it was accessed in word-sized chunks, This is a workable approach; however, using the PTR directive as shown in Program 3-Sb makes the code more readable for programmers. 102 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS MOV CXS MoV BXOFFSET ASC ‘8X pons to ASCII data MOV DLOFFSETUNPACK Di points to unpacked BCD date AGAIN: MOV AXIBX! ‘move next 2 ASCII numbers to AX AND AX(OFOFH ‘remove ASCII3s Mov [DIAX ‘store unpacked BCD abo Did ‘Polnt o ext unpacked BCD data ADD BX,.2 point to next ASCII data {oop AGAIN Program 3-5a Mov oxs {GXis loop counter Mov BX OFFSETASC BX ponte to ASCII data MOV DLOFFSETUNPACK ‘DI points to unpacked BCD data AGAIN; MOV AX,WORDPTR[BX] move next 2 ASCII numbers to AX AND AX.OFOFH smove ASCII 35, MOV WORDPTRIDILAX _store unpacked BCD ADD DI2 Ipoint to next unpacked BCD data ADD BX2 ‘point to next ASCII data LOOP AGAIN Program 3-Sb In both of the solutions so far, registers BX and DI were used as pointers. for an array of data, An array is simply a set of data located in consecutive memory locations. Now one might ask: What happens if there are four, five, or six arrays? How can they all be accessed with only three registers as pointers: BX, DI, and SI? Program 3-Sc shows how this can be done with a single register used as a pointer to access two arrays. However, to do that, the arrays must be of the same size and defined similarly. Mov ¢x,t0 oad the counter SUB BX'BX ‘clear BX AGAIN: MOV ALASCIBX] ‘move to AL content of mom [BX*ASC] AND ALIOFH ‘mask the upper nibble tov UNPACKEXAL ‘move to mem [BX+UNPACK] the AL INC BX make the pointer to point at next byia LOOP AGAIN Toop unl is finshed Program 3-5e Program 3-Sc uses the based addressing mode since BX+ASC is used as a pointer, ASC is the displacement added to BX. Either DI or $I could have been used. for this purpose. For word-sized operands, "WORD PTR" would be used since the data is defined as DB. This is shown below. Mov AX.WORD PTR ASCIBX] ‘AND AXOFOFH MoV WORD PTR UNPACKED[BX],AX ASCIito packed BCD conversion To convert ASCII to packed BCD, itis first converted to unpacked BCD (to get rid of the 3) and then combined to make packed BCD. For example, for 9 and 5 the keyboard gives 39 and 35, respectively. The goal is to produce 95H or "1001 0101", which is called packed BCD, as discussed earlier. This process is illustrated in detail below. Key ASCIL Unpacked BCD Packed BCD. 4 34 00000100 7 a7 0000111 01000111 or 47H 103 SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS ORG 0010H VAL_ASC DB “ar VAL_BCD DB 2 sreminder: the OB will put 34 in 0010H location and 37 in 0011H, Mov AX.WORD PTR VAL_ASC —_(AH=37,AL=34 AND. AXOFOFH umask 3 to get unpacked BCD XCHG AH AL jswap AH and AL. Mov cL4 :CL=04 to shift 4 times SHL AH.CL ishift left AH to get AH=40H OR ALAH OR them to get packed BCD Mov VAL_BCD,AL isave the result After this conversion, the packed BCD numbers are processed and the result will be in packed BCD format. As will be seen later in this section, there are special instructions, such as DAA and DAS, which require that the data be in packed BCD formand give the result in packed BCD. For the result to be displayed on the monitor or be printed by the printer, it must be in ASCII format. Conversion from packed BCD to ASCII is discussed next. Packed BCD to ASCII conversion To convert packed BCD to ASCII, it must first be converted to unpacked and then the unpacked BCD is tagged with 011 0000 (30H). The following shows the process of converting from packed BCD to ASCII. Packed BCD, Unpacked BCD ASCII SEES 29H 02H 8 09H 32H 8 39H 0010 1001 0000 0010 & 0000 1001 011 0010 & 011 1001 VAL1_BCD DB 29H VALS-ASC DW? MOV ALVAL1_BCD MOV AHAL scopy AL to AH. now AH=29,AL=29H AND AX,OFOOFH mask 9 from AH and 2 from AL. Mov CL :CL=04 for shift SHR AH,CL ‘shift right AH to get unpacked BCD OR AX,3030H ‘combine with 30 to get ASCII XCHG AH AL iswap for ASCII storage convention MOV ——-VAL3_ASC,Ax ‘store the ASCII BCD addition and subtraction After leaming how to convert ASCII to BCD, the application of BCD numbers is the next step, There are two instructions that deal specifically with BCD numbers: DAA and DAS. Each is discussed separately. BCD addition and correction There is a problem with adding BCD numbers, which must be corrected. ‘The problem is that after adding packed BCD numbers, the result is no longer BCD. Look at this example: MOV AL,17H_ ADD AL,28H Adding them gives 0011 1111B (3FH), which is not BCD! A BCD number can only have digits from 0000 to 1001 (or 0 to 9). In other words, adding two BCD numbers must give a BCD result. The result above should have been 17 + 28 = 45 (0100 0101). To correct this problem, the programmer must add 6 (0110) to the low digit: 104 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS 3F + 06 = 45H. The same problem could have happened in the upper digit (or example, in 52H + 87H = D9H). Again to solve this problem, 6 must be added to the upper digit (D9H +60H =139H), to ensure that the result is BCD (52 + 87 = 139). This problem is so pervasive that all single-chip CISC microprocessors such as the Intel 80x86 and the Motorola 680x0 have an instruction to deal with it. The RISC processors have eliminated this instruction DAA The DAA (decimal adjust for addition) instruction in 80x86 microproces- sors is provided exactly for the purpose of correcting the problem associated with BCD addition. DAA will add 6 to the lower nibble or higher nibble if needed; otherwise, it will leave the result alone. The following example will clarify these points: DATAt DB 47H DATA2 DB 25H DATA3 DB? MOV AL.DATA1 —_;ALhholds first BCD operand MOV BL.DATA2 _;BLholds second BCD operand ADD ALBL :BCD addition DAA ‘adjust for BCD adition MOV DATA3,AL _:store result in correct BCD form ‘After the program is executed, the DATA3 field will contain 72H (47 +25 = 72). Note that DAA works only on AL. In other words, while the source can be an operand of any addressing mode, the destination must be AL in order for DAA to work. It needs to be emphasized that DAA must be used after the addition of BCD operands and that BCD operands can never have any digit greater than 9. In other words, no A - F digit is allowed. It is also important to note that DAA works only after an ADD instruction; it will not work after the INC instruction. ‘Summary of DAA action Ifafter an ADD or ADC instruction the lower nibble (4 bits) is greater than 9, or if AF = 1, add 0110 to the lower 4 bits, If the upper nibble is greater than 9, or if CF = 1, add 0110 to the upper nibble. In reality there is no other use for the AF (auxiliary flag) except for BCD addition and correction, For example, adding 29H and 18H will resultin 41H, which is incorrect as far as BCD is concerned. Hex BcD 29 0010 1001 +18 +0001 1000 4a 01000001 AF=1 +6 +0110 because AF = 1 DAA will add 6 to the lower nibble ar 0100 0111" The final resutt is BCD. Program 3-6 demonstrates the use of DAA after addition of multibyte packed BCD numbers. BCD subtraction and correction ‘The problem associated with the addition of packed BCD numbers also shows up in subtraction. Again, there is an instruction (DAS) specifically designed to solve the problem. Therefore, when subtracting packed BCD (single-byte or ‘muitibyte) operands, the DAS instruction is put after the SUB or SBB instruction, AL must be used as the destination register to make DAS work. SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS 105 ‘Two sets of ASCII data have come in from the keyboard. Write and run a program to 1. Convert from ASCII to packed BCD. 2. Add the multibyte packed BCD and save it. 3. Convert the packed BCD result into ASCII TITLE PROG3-6 (EXE) ASCII TO BCD CONVERSION AND ADDITION PAGE 60,132 MODE SMALL STACK 64 DATA DATAL_ASC DB “g54g147616 ORG = odTOH DATA2_ASC OB “0072587188 ORG 0020H DATA3_BCD OB SOUP (7) ORG 0028H pata¢_eco DB. 5 DUP (2) ORG 00304 DATAS_ADD OB 5 DUP (2) ORG 0040H DATAS_ASC DB 10 DUP (?) CODE MAIN PROC FAR MOV aegpara MOV DS, MOV BX OFFSET DATAT_AsC {BX points to first ASCII data MOV OLOFFSET DATAS BCD {DI points to fst BCD data Mov OX10 CX holds number bytes t9 convert CALL CONV _BCD sconvert ASCil to BCD MOV BXOFFSET DATA2 ASC {BX pons to second ASCII data MOV DLOFFSET DATAA BCD {D1 points to second BCD data MOV OXt0 “0X holds number bytes to convert CALL CONV_8cD convert ASCII to BCD CALL BCD ADD sad the BCD operands MOV SOFFSET DATAS_ADD ‘Sipomts to BCD resuk MOV OLOFFSET DATASLASC ‘DI points fo ASCII resut MOV OX0s ‘eX molds court for convert CALL =CONV_ASC convert result to ASCII MOV AH.ACH INT 2H “go back to DOS MAIN NDP STHIS SUBROUTINE CONVERTS ASCII TO PACKED BCD CONV_BCD PROC’ AGAIN: MOV.” AXBX] X=pointer for ASCII data AL, XCHG AH, AND AXOFOFH © mask ASCII3s PUSH OX ‘save the counter MOV. CLa4 ‘shift AH left 4 bits SHE ARCL to get ready for packing OR ALAH jeombine to make packed BCD Mov [DIAL !Dispointer for BCD data ADD BX2 ipoint to next 2 ASCII bytes INC DI {point to next BCD data’ POP cK iTestore loop counter LOOP AGAIN CONV_BCD ENDP Program 3-6 (continued on the following page) 106 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS “THIS SUBROUTINE ADDS TWO MULTIBYTE PACKED BCD OPERANDS: MOV BX,OFFSET DATA3_BCD ,8X=pointer for operand 1 MOV DI,OFFSET DATA4 BCO Ol MOV SILOFFSET DATAS_ADD SI MOV CK05 cic BACK = MOV AL{BX}+4 ‘get next byte of operand 1 ADC AL[OI}+4 ‘add next byte of operand 2 DAA Jeorrect for BCD addition MOV {SI]+4.AL ‘save sum DEC 8x ‘point to next byte of operand 4 DEC Ol ‘point to next byte of operand 2 DEC SI “point to next byte of sum Loop Back Ret BCD_ADD ENDP "THIS SUBROUTINE CONVERTS FROM PACKED BCD TO ASCII CONV_ASC PROC AGAIN2” MOV AL{SI] ‘Si=pointer for BCD data MOV AHAL plicate to unpack AND AXOFOOFH “unpack PUSH Cx ‘save counter Mov Clos | shift right 4 bts to unpack SHR AH.CL tthe upper nibble OR. AX.3030H make it ASCII XCHG AH AL ‘swap for ASCII storage convention Mov [DIA ‘store ASCII data INC SI ‘point to next BCD data ADD D2 {point to next ASCII data POP cx estore loop counter LOOP AGAIN2 RET ‘CONV_ASC_ENOP END MAIN Program 3-6 (continued rom preceding page) ‘Summary of DAS action 1. Ifafter a SUB or SBB instruction the lower nibble is greater than 9, or if AF = 1, 2 subtract 0110 from the lower 4 bits. Ifthe upper nibble is greater than 9, or CF = 1, subtract 0110 from the upper nibble, Due to the widespread use of BCD numbers, a specific data directive, DT, has been created. DT can be used to represent BCD numbers from 0 to 10°91 (that is, twenty 9s), Assume that the following operands represent the budget, the expenses, and the balance, which is the budget minus the expenses. BUDGET DT _—87965141012 EXPENSES DT 31610540392 BALANCE DT? MOV CX,10 MOV BX.00 cLe BACK. MOV AL,BYTE PTR BUDGETIBX] SBB AL.BYTE PTR EXPENSES[BX] DAS. MOV BYTE PTR BALANCE[BX],AL INC BX LOOP BACK ‘balance = budget - expenses soounter=10 ‘pointer=0 :clear carry for the 1st iteration ‘get a byte of the BUDGET subtract a byte from it correct the resutt for BCD save it in BALANCE increment for the next byte ‘continue until CX=* SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS. 107 Notice in the code section above that (1) no H (hex) indicator is needed for BCD numbers when using the DT directive, and (2) the use of the based relative addressing mode (BX + displacement) allows access toll three arrays with a single register BX. In Program 3-7 the DB directive is used to define ASCII values. This makes the LSD (least significant digit) be located at the highest memory location of the array. In VALUE], 37, the ASCII for 7 is in memory location 0009; therefore, BX ust be pointed to that and then decremented. Program 3-7 is repeated, rewritten with the full segment definition. TITLE = PROGS-7 (EXE) ADDING ASCII NUMBERS PAGE 60,132 MODEL SMALL STACK 64 “DATA VALUE1 DB ‘06s0478127" ORG —0010H VALUE2 0B ‘0779563678 ORG 00204 RESULT! DB 10 DUP (2) ORG — 0030H, RESULT2 DB 10 DUP (2) MAIN, = MOV” AX,@DATA DSAK CALL ASC ADD call ASCII addition subroutine CALL CONVERT ‘call convert to ASCII subroutine MOV AHACH INT 2H {90 back to 00S THIS SUBROUTINE ADDS THE ASCII NUMBERS AND MAKES THE RESULT UNPACKED. ‘ASC_ADD PROC. cue clear the carry MOV Cx,10 ‘up loop counter MOV BXS int to LSD BACK = MOV AL'VALUE[8X] move next byte of operand 4 ADC AL\VALUEZIBX) add next byte of operand 2 AAR ‘adjust to make it ASCII MOV — RESULT1{BX|AL'store ASCII sum DEC Bx ‘point to next byte LOOP BACK RET ‘ASC_ADD ENDP STHIS SUBROUTINE CONVERTS UNPACKED BCD TO ASCII ‘CONVERT PROC. MOV" BXOFFSETRESULT1 {BX points to BCD data MOV SLOFFSET RESULT2 MOV CX.05 x BACK2; MOV AX WORD PTR [BX] et next 2 ASCII bytes OR AX.3030H Lingert ASCII 3s MOV WORDPTRISILAX ‘store ASCII ADD BX2 {increment BCD pointer ADD S12 ‘increment ASCII pointer LOOP BACK? RET CONVERT ENOP END MAIN Program 3-7 108 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS THLE PROGS-7 (EXE) REWRITTEN WITH FULL SEGMENT DEFINITION PAGE 60,132, STSEG SEGMENT DB 64.DUP(?) STSEG ENDS DISES SEGMENT DB VALUEt "0859478127" ORG 0010H VALUE2 0B 0779563676" ORG — 0020H RESULT! DB 10 DUP(?) ORG —0030H RESULT2 DB 10 DUP(?) DTSEG © ENDS COseG SEGMENT MAIN” PROC FAR ASSUME CS:CDSEG,DS:DTSEG,SSSTSEG Mov AX.DTSEG. Mov DSAX CALL ASC_ADD. ;call ASCII addition subroutine CALL CONVERT {call convert to ASCII subroutine MOV AH.ACH INT 21H igo back to DOS MAIN ENOP. STHIS SUBROUTINE ADDS THE ASCII NUMBERS AND MAKES THE RESULT UNPACKED. ASC_ADD PROC cLe sclear the canry MOV CX.10 {set up loop counter, Mov BX9 point to LSD BACK: MOV —AL,VALUE1[BX) ‘move next byte of operand 1 ADC AL\VALUEZ|BX} ‘add next byte of operand 2 AAA adjust to make it unpacked BCD MOV — RESULTI[BX]AL ‘store BCD sum DEC Bx) ‘point to next byte LOOP BACK RET ‘ASC_ADD ENDP ;THIS SUBROUTINE CONVERTS UNPACKED BCD TO ASCII CONVERT PROC MOV BX.OFFSETRESULT1 — 8X points to unpacked BCD data MOV SLOFFSETRESULT2 ‘SI points to ASCII data, MOV CX,05. CX is loop counter BACK2: MOV — AX.\WORD PTR [BX] iget next 2 ASCIi bytes. OR AK.3030H insert ASCII 3S, MOV WORD PTR[SI]AX istore ASCII ADD BX.2 ‘increment BCD poir ADD, Siz increment ASCII pointer {Oop BACK? RET ‘CONVERT ENDP. CDSEG ENDS END MAIN Program 3-7, rewritten with full segment defini ASCII addition and subtraction ASCII numbers can be used as operands in add and subtract instructions the way they are, without masking the tagged 011, using instructions AAA and AAS. MOV AL,'S* (AL=35 0011 0101 ADD AL,2" add to AL 32 the ASCII for 2 9014 0010 AAA ichanges 67H to 07H 110 0111 OR AL,30 :OR AL with 30H to get ASCII SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS 109 If the addition results in a value of more than 9, AAA will correct it and pass the extra bit to carry and add | to AH. SUB AHAH sAH=00 Mov (AL=37H_ Mov ;BL=35H_ ADD :37H+35H=6CH therefore AL=6C. AAA johanges 6CH to 02 in AL and AH=CF=1 OR ‘AX,3030H —_jAX=3132 which is the ASCII for 12H. ‘Two facts must be noted, First, AAA and AAS work only on the AL register, and second, the data added can be unpacked BCD rather than ASCII, and AAA and AAS will work fine. The following shows how AAS works on subtraction of unpacked BCD to correct the result into unpacked BCD: mov AX,105H JAX=0105H unpacked BCD for 16 Mov cL.06 sCL=06H SUB AL.CL. 15-6 =-1 (FFH) AAS :FFH in AL is adjusted to 09, and JAH is decremented, leaving AX = 0009 Unpacked BCD multiplication and division There are two instructions designed specifically for multiplication and division of unpacked BCD operands. They convert the result of the multiplication and division to unpacked BCD. AAM The Intel manual says that this mnemonic stands for "ASCII adjust multi- plication," but it really is unpacked multiplication correction. Iftwo unpacked BCD ‘numbers arc multiplied, the result can be converted back to BCD by AAM. Mov AL,7 sAL=37H AND ‘AL,OFH 3AL=07 unpacked BCD Mov DL 6. ;DL=36H AND DLOFH ;DL=06 unpacked BCD MUL. OL !AX=ALXDL. =07x06=002AH=42 AM :AX=0402 (7x6=42 unpacked BCD) oR AX,3030H ——_;AX=3432 resultin ASCII ‘The multiplication above is byte by byte and the result is HEX. Using AAM converts it to unpacked BCD to prepare it for tagging with 30H to make it ASCII, AAD Again, the Intel manual says that AAD represents "ASCII adjust for division," but that can be misleading since the data must be unpacked BCD for this instruction to work. Before dividing the unpacked BCD by another unpacked BCD, AAD is used to convert it to HEX. By doing that the quotient and remainder are both in unpacked BCD, Mov AX,3839H —_;AX=3539. ASCII for 59 AND AX,OFOFH — ;AH=05,AL=09 unpacked BCD data AAD :AX=003BH hex equivalent of 59 Mov BH,08H divide by 08 DV BH ‘3B | 08 gives AL=07 AH=03 OR AX,3030H —_AL=37H (quotient) AH=33H (rem) 110 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS, SECTION 3.5: ‘As can be seen in the example above, dividing 59 by 8 gives a quotient of 7 and a remainder of 3. With the help of AD, the result is converted to unpacked BCD, so it can be tagged with 30H to get the ASCII result. It must be noted that both AAM and AAD work only on AX. Review Questions For the following decimal numbers, give the packed BCD and unpacked BCD rep- resentations. (a) 15 (99 Match the following instruction mnemonic with its function. DAA (a) ASCII addition DAS (b) ASCII subtraction AAS {c) BCD subtraction AAA (@) BCD addition ROTATE INSTRUCTIONS In many applications there is a need to perform a bitwise rotation of an ‘operand. The rotation instructions ROR, ROL and RCR, RCL are designed specifi- cally for that purpose. They allow a program to rotate an operand right or left. In this section we explore the rotate instructions, which frequently have highly spe- cialized applications. In rotate instructions, the operand can be in a register or memory. If the number of times an operand is to be rotated is more than I, this is, indicated by CL. This is similar to the shift instructions. There arc two type of rotations. One is a simple rotation of the bits of the operand, and the other is a rotation through the carry. Each is explained below. Rotating the bits of an operand right and left ROR rotate right In rotate right, MSB LsB CF as bits are shifted from left to right they exit from the right end (LSB) and enter the left end (MSB). In addition, as each bit exits the LSB, a copy of itis given to the carry flag, In other words, in ROR the LSB is moved to the MSB and is also copied to CF, as shown in the diagram. If the operand isto be rotated once, the 1 is coded, but if itis to be rotated more than once, register CLis used to hold the number of times it is to be rotated. MOV AL36H ;AL=0011 0110 ROR ALA ‘AL=0001 1011 CF=0 ROR ALA ‘AL=1000 1101 CF=1 ROR Ait JAL=1100 0110 CF=1 or MOV AL,36H ;AL=0011 0110 MOV CL3 :CL=3 number of times to rotate ROR AL.CL JAL=1100 0110 CF=1 sthe operand can be a word: MOV S8X,0C7ESH — :BX=11000111 1110 0101 MOV CLE :CL=6 number of times to rotate ROR BX,CL 1BX=1001 0111 0001 1111 CF=1 SECTION 3.5: ROTATE INSTRUCTIONS ut ROL rotate left In rotate left, as, cr — MSB bits are shifted from right to left they exit the left end (MSB) and enter the right end (LSB). In addition, every bit that leaves the MSB is copied to the carry flag. In other words, in ROL the MSB is moved to the LSB and is also copied to CF, as shown in the diagram. If the operand is to be rotated once, the I is coded. Otherwise, the number of times itis to be rotated is in CL. LSB MOV BH,72H ;BH=0111 0010 ROL BH,1 ROL BH1 :BH=1100 1001 CF: ROL BHA ;BH=1001 0011 CF: ROL BH ;BH=0010 0111 Ci or: MOV BH,72H sBH=0111 0010 MOV CL4 ;CL=4 number of times to rotate ROL BH,CL {BH=0010 0111 CF=1 ‘The operand can be a word: MOV DX,672AH —;DX=0110 0111 0010 1010 MOV CL3 ;CL=3 number of times to rotate ROL _DX,CL :DX=0011 1001 0101 0011 CF=1 Write a program that finds the number of Is ina byte. From the data segment DATAL ~DBO7H 2 COUNT 08 From the code segment ‘SUB BLBL clear BL to keep the number of 15 Mov DL8 {olate total of 8 times MOV AL'DATAI AGAIN; ROL AL {otate tonce UNC NEXT ‘chook fort INC BL 'f CF=1 then add one to count NEXT DEC DL igo through this 8 times INZ AGAIN ‘not fished go back Mov _COUNTEL __‘save the number of 15, Program 3-8 Program 3-8 shows an application of the rotation instruction. The maxi- mum count in Program 3-8 will be & since the program is counting the number of 1s ina byte of data, Ifthe operand isa 16-bit word, the number of 1s can go as high as 16. Program 3-9 is Program 3-8, rewritten for a word-sized operand. It also provides the count in BCD format instead of hex. Reminder: AL. is used to make a BCD counter because the DAA instruction works only on AL, Wile 2 program fo count the number of 1s ina word. Provide the count in BCD. DaTAW! DW S7F4H COUNT2 DB? SUB ALAL clear AL to Keep the number of 1s in BCD mov DLI6 ‘rotate total of 16 times MOV BXDATAW! move the operand lo BX AGAIN; ROL BX. {folate it once SNC NEXT ‘check for 1. If CF=0 then jump ADD ALA ICE=1 then add one to count Daa ‘adjust tha count for BCD NEXT. DEC DL {through this 16 times JNZ AGAIN Mot finished go back MOV _COUNTZ,AL _|save the number of ts in COUNT2 Program 3-9 12 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS RCR rotate right through carry In RCR, as bits are shifted from left to right, they exit the right end (LSB) to the carry msB LsB cr flag, and the carry flag enters the left end (MSB). In other words, in RCR the LSB is moved to CF and CF is moved to the MSB. In reality, CF acts as fits part of the operand. This is shown in the diagram. Ifthe operand is to be rotated once, the | is coded, but if itis to be rotated more than once, the register CL holds the number of times. cic Mov RCR RCR RCR AL,26H Aut ALA ALA or: cic MOV Mov RCR AL,26H cL3 AL.CL she operand can be a word sTC MOV 8X.37F1H MOV CLS ROR BX,CL smake CF=0 JAL=0010 0110 :AL=0001 0011 CF=0 ;AL=0000 1001 CF JAL=1000 0100 CF=1 smake CF=0 :AL=0010 0110 :CL=3 number of times to rotate 'AL=1000 0100 CF=1 smake CF=1 :BX=0011 0111 1111 0001 ;CL=5 number of times to rotate :BX=0001 1001 1011 1111 CF=0 RCL rotate left through carry In RCL, as bits are shifted from right to left they exit the left end (MSB) and enter the carry flag, and the carry flag enters the right end F Msi LSB (LSB). In other words, in RCL the MSB is moved to CF and CF is moved to the LSB. In reality, CF acts as ifit is part of the operand. This is shown in the diagram. Ifthe operand is to be rotated once, the 1 is coded, but if tis to be rotated more than ‘once, register CL holds the number of times. sTc MOV BL,15H RCL BLA RCL BLA or: ste MOV BL,1SH Mov CL.2 RCL BL,CL ithe operand can be a word: cic MOV AX,191CH Mov CLS RCL AX,CL smake CF=1 ;BL=0001 0101 30010 1011 CF=0 10101 0110 CF=0 imake CF=1 ;BL=0001 0101 ;CL=2 number of times for rotation sBL=0101 0110 CF= imake CF=0 :AX=0001 1001 0001 1100 iCL=5 number of times to rotate ;AX=0010 0011 1000 0001 CF=1 SECTION 3.5: ROTATE INSTRUCTIONS 113 SECTION 3.6: Review Questions What is the value of BL after the following? MOV BL,25H MOV CL4 ROR BL,CL ‘What are the values of DX and CF after the following? MOV DX,3FA2H MOV CL7 ROL DX,CL ‘What is the value of BH after the following? SUB BHBH STC RCR BHI sTC RCR_ BH, ‘What is the value of BX after the following? MOV BX,FFFFH MOV CLS cic RCL BX,CL. Why does "ROR BX,4" give an error in the 8086? How would you change the ‘code to make it work? BITWISE OPERATION IN THE C LANGUAGE One of the mostimportantand pow- ‘Table 3-4: Bitwise AND, OR, erful features of the C language is its ability and EX-OR in to perform bit manipulation. Due tothe fact that many books on C do not cover this : important topie, tis appropriate to discuss it > SE in this section. This section describes the action of operators and provides examples. Bitwise operators in C While every C programmer is famil- iar with the logical operators AND (&&), OR (|)), and NOT (1), many C programmers are less familiar with the bitwise operators AND (&), OR (|), EX-OR (*), inverter (~), Shift Right (>>), and Shift Left (<<). These bitwise operators are widely used in soft- ware engineering and control; consequently, their un- derstai and mastery are critical in system design and interfacing. See Tables 3-4 and 3-5. The following code shows Examples 3-5 through 3-7 using the C 1 0 logical operators, Recall that "Ox" in the C language indicates that the data is in hex format 0x35 & OxOF = 0x05 /PANDing: see Example 3-5 “/ 0x0504 | OXDAGS = OxDFEC —_/* ORiing: see Example 3-6 *) (0x54 * 0x78 = Ox2C I" XORing: see Example 3-7 */ 0x37 = OxCB inverting 37H */ ‘The last one is like the NOT instruction in x86 microprocessors: MOV AL,37H_jAL=37H NOTAL (AFTER INVERTING 37, AL=C8H 114 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS Bitwise shift operators in © ‘There are two bitwise shift operators in C: Shift Right (> >) and Shift Left (<<). They perform exactly the same operation as SHR and SHL in Assembly language, as discussed in Section 3.3. Their format in C is as follows: data >> number of bits tobe shifted /* shifting right */ data << number of bits tobe shifted —_/* shifting left */ The following shows Examples 3-9 through 3-11 using shift operators in C. Program 3-10 shows all of these examples with C syntax. Ox9A >> 3. = 0x13 /* shifting right 3 times: see Example 3.9 */ 0x7777 >> 4 = 0x0777_—_/* shifting right 4 times: see Example 3-10 */ Ox6 << 4 = 0x60 /* shifting left 4 times: see Example 3-11 */ 7 Program 3-10 Repeats Examples 5-5 through S-11 in © *7 include main) { I Notice the way data is defined in C for Hex format using Ox unsigned char data_1 = 0x35; 0x504; unsigned int data 2: unsigned int data: unsigned char data unsigned char data_5=0x78; unsigned char data_6=0x37; unsigned char data”7=0x09A; unsigned char tery unsigned int temp_£ temp=data_1&0x0F; JIANDing printf(\nMasking the upper four bits of 96x (hex) we get %X (hex)\n",data_1,temp); temp 2=data_2|data 3; //ORing printf("The result of 96X'hex OReed with %X hex is %X hex\n" data_2,data_3.temp_2); temp=data_4*data 6; //EX-ORing printf("The results of %X hex EX-ORed with %X hex is %X hexin',data_4,data_5,temp); temp=-data_6} JINVERTING printi("The result of %X hex inverted is %X hex\n',data_6,temp); temp=data_7>>3; JISHIFTING Right print("When 96X hex is shifted right three times we get 96X hex\n' data_7.temp); print{("When 9X hex is shifted right four times we get 9X hex\n’,0x7777,0x7777>>4); temp=(0x6<<4); [SHIFTING Lett printi('When 96X hex is shited left %d times we get 9X hexin’,0x6,4,termp); b Program 3-10 Program 3-10 demonstrates the syntax of bitwise operators in C. Next we show some real-world examples of their usage. SECTION 3.6: BITWISE OPERATION IN THE C LANGUAGE, us Packed BCD to ASCII conversion in C Section 3.4 showed one way to convert a BCD number to ASCII. This conversion is widely used when dealing with a real-time clock chip. Many of the real-time clock chips provide very accurate time and date for up to ten years without the need for external power. There is a real-time clock in every x86 IBM PC or compatible computer. However, these chips provide the time and date in packed BCD. In order to display the data, it needs to be converted to ASCII. Program 3-11 isa Cversion of the packed BCD-to-ASCII conversionexample discussed in Section 3.4. Program 3-11 converts a byte of packed BCD data into two ASCII characters and displays them using the C bitwise operators. ‘ASGII conversion using logical bitwise operators in © 7 Program 3-11 shows packed BCD-+t include main { unsigned char mybed=0x29; * declare a BCD number in hex */ unsigned char asci_1; unsigned char asci_2; asci_1=mybod&0xOf, /* mask the upper four bits */ asci_t=asci_1]0x30; /* make it an ASCII character */ asci_2=mybsd&0x10; P* mask the lower four bits */ asci_ 2o>d: shift it right 4 times */ asci_2=asci_2|0x30; [* make it an ASCII character */ prinf("BCD data %X is %c , %c in ASCII\n",mybed,asci_1,asci_2); ptintf("My BCD data is %c if not converted to ASCIi\n' mmiybed) } Program 3-11 Notice in Program 3-11 that if the packed BCD data is displayed without conversion to ASCII, we get the parenthesis ")". See Appendix F. Testing bits in C In many cases of system programming and hardware interfacing, it is necessary 10 test a given bit to see if it is high. For example, many devices send a high signal to state that they are ready for an action of to indicate that they have data. How can the bit (or bits) be tested? In such cases, the unused bits are masked and then the remaining data is tested. Program 3-12 asks the uscr for a byte and tests to see whether or not DO of that byte is high. 7 Program 3-12 shows how to test Hinclude maing DOto see ifitis high *7 { Unsigned char status; insigned char temp; iC\nType in a Hex valueln"); scanf("%X",&status); iget the data temp=status&0x01; imask all bits except DO 4 temp, His thigh? printi(D0 i Tit yes, Say so be printt'DO Ie oa); Tiftne, say no } Program 3-12 116 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS. eI AMaoNe SUMMARY PROBLEMS L ‘The assembly language version of Program 3-12 is as follows: sassume AL=value (in hex) AND ALOT :MASK ALL BITS EXCEPT DO CMP AL.Ot 11S DO HIGH JNE BELOW :MAKE A DECISION . :YES DOIS HIGH BELOW: ;DOIS LOW Review Questions What is the result of Ox2F&0x27 ? ‘What is the result of Ox2F{0x27? ‘What is the result of 0x2F°0x27 ? ‘What is the result of ~0x2F ? What is the result of Ox2F >> 3? ‘What is the result of 0x27 << 4? In Program 3-11 if mybcd=0x32, what is displayed if itis not converted to BCD? Modily Program 3-12 to test D3. ‘The 8- or 16-bit data items in 80x86 computers can be treated as either signed or unsigned data. Unsigned data uses the entire 8 or 16 bits for data represcniation. Signed data uses the MSB as a sign bit and the remaining bits for data representation. This chapter covered the arithmetic and logic instructions that are used for unsigned data. The instructions ADD and SUB perform addition and subtraction on unsigned data. Instructions ADC and SBB do the same, but also take the carry flagiinto consideration. Instructions MUL and DIV perform multiplication and division on unsigned data. Logic instructions AND, OR, XOR, and CMP perform logic operations on all the bits of their operands and were therefore included in this chapter. Shift and rotate instructions for unsigned data include SHR, SHL, ROR, ROL, RCL, and RCR. ASCII and BCD data operations for addition and subtraction were also covered. Finally, bitwise logic instructions were demonstrated using the C language Find CF, ZF, and AF for each of the following. Also indicate the result of the addi- tion and where the result is saved (@) MOV BH,3FH (0) MOV DX,4599H (©) MOV AX,255 ADD BH4SH MOV CX,3458H STC ADD CX,DX ADC AX,00 (@) MOV BX,OFFOLH (@) MOV CX,0FFFFH (Q) MOV AHOFEH ADD BL,BH Fixe) sTc ADC CX,00 ADC AH,00 ‘Write, run, and analyze a program that calculates the total sum paid to a salesper- son for cight months. The following arc the monthly paychecks for thase months: $2300, $4300, $1200, $3700, $1298, $4323, $5673, $986. Rewrite Program 3-2 (in Section 3.1) using byte addition. ‘Write a program that subtracts two multibytes and saves the result. Subtraction should be done a byte ata time. Use the data in Program 3-2. State the three steps involved in a SUB and show the steps for the following data. (a) 234-124 (b) 43H-S1H (©) 99-99 ‘Write, run, and analyze the result of a program that performs the following: (1)(a) bytel x byte2 (b) bytel x word1 (©) word] x word2 (2) @) bytet / byte2 (b) wordt / word2 (c) doubleword/bytet Assume byte1=230, byte2=100, word1=9998, word2=300 and doubleword =100000. SUMMARY 117 7. Assume that the following registers contain these HEX contents: AX = F000, BX = 3456, and DX = £390. Perform the following operations. Indicate the result and the register where itis stored. Give also ZF and CF in each case. ‘Note: the operations are independent of each other. (@) AND DX,AX (b) OR DH,BL_ () XOR AL, 76H (@) AND DX,.DX (@) XOR AX,AX (0), OR BX,DX (g) AND AH,OFF (h) OR AX,9999H (i) XOR DX,OEEEEH (j) XOR BX,BX {k) MOV CL,04 () SHR DX. SHL AL,CL (m) MOV CL,3 (n) MOV CLS SHR DL,CL. SHL BX,CL (0) MOV CL.6 SHL DX,CL 8. Indicate the status of ZF and CF after CMP is executed in each of the following, cases. (a) MOV BX,2500 (b) MOV AL,OFFH (©) MOV DL,34 CMP BX, 1400 CMP AL,6FH CMP DL.88 (d) SUB AX,AX (@) XOR DX,DX () SUB CX,CX CMP AX,0000 CMP DX,OFFFFH DEC CX CMP CX,0FFFFA (g) MOV BX,2373H. (h) MOV AL,OAAH MOV DX.4000H AND AL,SSH CMP DX.BX CMP AL,00 9. Indicate whether or not the jump happens in each case. (a) MOV CLS (b) MOV BH,6SH (©)MOV AH,SSH. SUB AL,AL MOV AL.48H SUB DL,DL SHL ALCL OR AL,BH OR DL,AH INC TARGET SHLAL,1 MOV CL,AH JC TARGET AND CL,OFH SHR DL.CL INC TARGET 10. Rewrite Program 3-3 to find the lowest grade in that class. 11. Rewrite Program 3-4 to convert all uppercase letters to lowercase. 12. Inthe IBM BIOS program for testing flags and registers, verify every jump (con- ditional and unconditional) address calculation. Reminder: As mentioned in Chapter 2, in forward jumps the target address is calculated by adding the dis- placement value to IP of the instruction after the jump and by subtracting in back- ‘ward jumps, 13. In Program 3-6 rewrite BCD_ADD to do subtraction of the multibyte BCD. 14. Rewrite Program 3-7 to subtract DATA2 from DATAL. Use the following data, DATAL DB *0999999999° DATA2 DB °0077777775" 15. Using the DT directive, write a program to add two 10-byte BCD numbers 16, We would like to make a counter that counts up from 0 to 99 in BCD. What in- struction would you place in the dotted area? SUB AL,AL ADD AL. 17. Write Problem 16 to count down (from 99 to 0). 18. An instructor named Mr. Mo Allem has the following grading policy: *Curving of grades is achieved by adding to every grade the difference between 99 and the highest grade in the class.” Ifthe foilowing are the grades of the class, write & program to calculate the grades after they have been curved: 81, 65, 77, 82, 73, 55, 88, 78, 51, 91, 86, 76. Your program should work for any set of grades. 19, If we try to divide 1,000,000 by 2. (a) What kind of problem is associated with this operation in 8086/286 CPUs? (b) How does the CPU let us know that there is a problem? 118 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS 20. Which of the following groups of code perform the same operation as LOOP. XXX? (a)DECCL (b)DECCH (c)DECBX (d) DEC CX JNZ XXX INZXXX NZ. JNZ XXX 21. Write a program that finds the number of zeros in a 16-bit word. 22. In Program 3-2, which demonstrated multiword addition, pointers were updated by two INC instructions instead of "ADD SI,2". Why? 23. Write aC program with the following components: {a) have two hex values: datal=5SH and data2=AAH; both defined as unsigned char, (b) mask the upper 4 bits of datal and display it in hex, (©) perform AND, OR, and EX-OR operations between the two data items and then display each result, (6) invert one and display it, (©) shift left datal four times and shift right data2 two times, then display each result. 24, Repeat the above problem with two values input from the user. Use the scanf("%X") function to get the hex data. 25. In the same way that the real-time clock chip provides data in BCD, it also ex- pects data in BCD when it is being initialized. However, data coming from the keyboard is in ASCII. Write aC program to convert two ASCII bytes of data to packed BCD. 26. Write aC program in which the user is prompted for a hex value, Then the data is tested to see if the two least significant bits are high. If'so, a message states "DO and D1 are both high”, otherwise, it states which bit is not high. 27. Repeat the above problem for bits DO and D7. ANSWERS TO REVIEW QUESTIONS ora a UNSIGNED ADDITION AND SUBTRACTION ination in 80x86 Assembly language, there are no memory to memory operations MOV AXDATA 2 ADD DATA_1AX destination, Source + destination + CF In) he bead resis in cary t CF, n(). the word aon results in 2cary tothe hgh bye BH JNZ_ ADD_LooP 43H 07000011 01000011 =95H 00000101 2 complement=+1111 1011 oor ) therefore, the result is positive 8 AL=05-4F-12 45, SECTION 3.2; UNSIGNED MULTIPLICATION AND DIVISION 2. DX and AX 4. ALLAH & Box 6. ALAH + Box SECTION 3 LOGIC INSTRUCTIONS AND SAMPLE PROGRAMS (2) 4202 (b) CFF (c) BDFD 2. { coorand ull remain unchanged: zeros, al ones 4. allzer0s. 5. AOF2=— 1010 0000 1111 0010 shiftleft: 0100 0001 1110 0100 cr shift again: 1000 0011 1100 1000 CF =0 shift again: 0000 0111 1001 0000 Ci AOE? shifted left three times = 0790. ADF2= 1010 0000 1111 0010, shiftight: 0101 0000 0111 1001 CF shift again: 0010 1000 0011 1100 CF shift again: 0001 0100 0001 1110 CF AOF2 shifted right three times = 141E 6 SUB 7.false ANSWERS TO REVIEW QUESTIONS 19 SECTION 3.4: BCD AND ASCII OPERANDS AND INSTRUCTIONS 4. (a) 15 = 0001 0101 packed BCD = 0000 0001 6000 0104 unpacked BCD {b) 98 = 1001 1001 packed BCD = 0000 1001 0000 1001 unpacked BCD. 2. DAA~-BCD addition: DAS -- 8CD subiraction; AAS ~ ASCII subtraction: AAA ~ ASCII addition SECTION 3.5: ROTATE INSTRUCTIONS 1. BL=52H, CF =0 3. BH=COH 5. the source operand cannot be immediate; to fi it MOV CL.4| ROR BX.CL SECTION 3.6: BITWISE OPERATION IN THE C LANGUAGE 1. 0x7 2. Ox2E 3. 0x08 4 gxbo 5 0x05 8 0x70 8 T° This program shows how to test Bit D3 to see fits high */ include maing { Lnsigned char status: Unsigned char temp, print{("\nType in a Hex valueln’): scant("%X" astatus); temp=status&0x04; if (temp==0x04) printi(*D3 is high Peep nee 120 CHAPTER 3: ARITHMETIC AND LOGIC INSTRUCTIONS CHAPTER 4 BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C OBJECTIVES ‘Upon completion of this chapter, you: e ‘be able to: Use INT 10H function calls to: ‘Clear the screen Set the cursor position Write characters to the screen in text mode Draw lines on the screen in graphics mode ‘Change the video mode Use INT 21H function calls to: ‘Input characters from the keyboard ‘Output charactersto the screen Input or output strings Use the LABEL directive to set up structured data items Use INT 16H far keyboard access ‘Use C function alts inth6 and iatdos to perform BIOS and DOS reupts Tee ee es ¥eyyvyy¥y¥ YY 121 SECTION 4.1: ‘There are some extremely useful subroutines within BIOS and DOS that are available to the user through the INT (interrupt) instruction. In this chapter, some of them are studied to see how they are used in the context of applications. First, a few words about the interrupt itself, The INT instruction is somewhat like a FAR call, When itis invoked, it saves CS:IP and the flags on the stack and goes to the subroutine associated with that interrupt. The INT instruction has the following format: INT xx :the interrupt number xx can be 00 - FFH Since interrupts are numbered 00 to FF, this gives a total of 256 interrupts in 80x86 microprocessors. Of these 256 interrupts, two of them ate the most widely used: INT 10H and INT 21H. Each one can perform many functions. A list of these functions is provided in Appendices D and E. Before the service of INT 10H or INT 21H is requested, certain registers must have specific values in them, depending on the function being requested. Various functions of INT 21H and INT 10H are selected by the value put in the AH register, as shown in Appendices D and E. Interrupt instructions are discussed in detail in Appendix B, BIOS INT 10H PROGRAMMING INT 10H subroutines are burned into the ROM BIOS of the 80x86-based IBM PC and compatibles and are used to communicate with the computer's screen video. Much of the manipulation of screen text or graphics is done through INT 10H. There are many functions associated with INT 10H. Among them are changing the color of characters or the background color, clearing the screen, and changing the location of the cursor. These options are chosen by putting a specific value in register AH. In this section we show how to use INT 10H to clear the screen, change the cursor position, change the screen color, and draw lines on the sereen. screen center decimal hex screen center 12,39 0C,27 Figure 4-1. Cursor Locations (row,column) Monitor screen in text mode The monitor screen in the IBM PC is divided into 80 columns and 25 rows in normal text mode (see Figure 4-1). This is the case for all monitors regardless of ‘whether they are color or monochrome, as long as they are in text mode. When the computer is turned on, the monitor is set to the default text mode. The mode can be changed, depending on the type of the monitor. Monitor types include MDA, CGA, EGA, and VGA. The text screen is 80 characters wide by 25 characters long. Since 122 (CHAPTER 4: BIOS AND DOS PROGRAMMING IN AS EMBLY AND C both a row and a column number are associated with each location on the screen, ‘one can move the cursor to any location on the screen simply by changing the row and column values. The 80 columns are numbered from 0 to 79 and the 25 rows are numbered 0t0 24. The top left corner has been assigned 00,00 (row ~00, column = 00). Therefore, the top right comer will be 00,79 (row = 00, column ~ 79) Similarly, the bottom left comer is 24,00 (row = 24, column = 00) and the bottom right comer of the monitor is 24,79 (row = 24, column =79). Figure 4-1 shows each location of the sereen in both decimal and hex. Clearing the screen using INT 10H function 06H Itis often desirable to clear the screen before displaying data. To use INT 10H to clear the screen, the following registers must contain certain values before INT 10H is called: AH = 06, AL = 00, BH = 07, CX = 0000, DH = 24, and DL = 79, The code will look like this MOV AH,06 {AH=06 to select scroi! function MOV AL,00 {AL=00 the entire page MOV BH.O7 7 for normal attribute MOV CH.00 0 row value of start point MOV CL,00 3CL=00 column value of start point MOV DH,24 {DH=24 row value of ending point MOV DL,79 :DL=79 column value of ending point INT 10H tinvoke the interrupt Remember that DEBUG assumes immediate operands to be in hex; there- fore, DX would be entered as 184F, However, MASM assumes immediate operands to be in decimal. In that case DH = 24 and DL = 79, In the program above, one of many options of INT 10H was chosen by putting 06 into AH. Option AH =06, called the scroll function, will cause the screen to scroll upward. ‘The CH and CL registers hold the starting row and column, respectively, and DH and DL hold the ending row and column. To clear the entire Screen, one must use the top left cursor position of 00,00 for the start point and bottom right position of 24,79 for the end point. Option AH = 06 of INT 10H is in reality the "scroll window up" function; therefore, one could use that to make a window of any size by choosing appropriate values for the start and end rows and columns. However, to clear the sereen, the top left and bottom right values are used for start and stop points in order to scroll up the entire screen. It is much more efficient coding to clear the screen by combining some of the lines above as follows: Mov AX,0600H —_;scroll entire screen Mov BH,O7 normal attribute Mov x,0000 jstart at 00,00 MOV DX\184FH ——_:end at 24,79 (hex = 18,4F) INT 10H sinvoke the interrupt INT 10H function 02: setting the cursor to a specific location INT 10H function AH = 02 will change the position of the cursor to any location. The desired position of the cursor is identified by the row and column values in DX, where DH = row and DL = column. Video RAM can have more than cone page of text, but only one of them can be viewed at a time. When AH = 02, to set the cursor position, page zero is chosen by making BH = 00. It must be pointed out that after INT 10H (or INT 21H) has executed, the registers that have not been used by the interrupt remain unchanged. In other words, these registers have the same values after execution of the interrupt as before the interrupt was invoked. Examples 4-1 and 4-2 demonstrate setting the cursor to a specific location. SECTION 4.1: BIOS INT 10H PROGRAMMING 123 Example 41 Write the code to set the cursor position to row = 15 = OFH and column = 25 = 19H. Solution: Mov ‘set cursor option Mov ‘page 0 Mov teolumn position MOV DHS jfow position INT 104 “invoke interrupt 10H Example 4-2 Write a program that (1) clears the screen and (2) sets the cursor at the center of the screen Solution: The center of the screen is the point at which the middle row and middle column meet. Row 12 is at the middle of rows 0 to 24 and column 39 (or 40) is at the middle of columns 0 to 79. Therefore, by setting row = DH = 12 and column = DL = 39, the cursor is set to the screen center. iclearing the screen MOV AX,0600H :Scrol the entire page Mov BHLO7 ‘normal attribute MOV CX'0000 row and column of top left MOV OX,184FH ‘row and column of bottom right INT 10H sinvoke the video BIOS service :setting the cursor to the center of screen MOV” AH,O2 :Set cursor option MOV BH.00 page 0 Mov DL'39 ‘center column position MOV DH,12 enter row position INT 10H Jinwoke interrupt 10H. INT 10H function 03: get current cursor position In text mode, one is able to determine where the cursor is located at any time by executing the following: MOV AH.O3 option 03 of BIOS INT 10H MOV BHO page 00 INT 10H jinterrupt 10H routine After execution of the program above, registers DH and DL will have the ‘current row and column positions, and CX provides information about the shape of the cursor. The reason that page 00 was chosen is that the video memory could contain more than one page of data, depending on the video board installed on the PC. In text mode, page 00 is chosen for the currently viewed page. ‘Changing the video mode First it must be noted that regardless of what type of adapter is used (MDA, CGA, BGA, MCGA, or VGA), all are upwardly compatible. For example, the VGA ‘emulates all the functions of MCGA, EGA, CGA, and MDA. Similarly, the EGA ‘emulates the functions of CGA and MDA, and so on. Therefore, there must be a way to change the video mode to a desired mode. ‘To do that, one can use INT 10H with AH = 00 and AL = video mode. A list of video modes is given in Appendix E, Table E- 124 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C D7 06 [D5 | 04 | D3 | D2 | D1 | Do foreground intensity 0 = normal intensity 1 = highlighted intensity background intensity i 0 = nonbiinking 1= blinking Figure 4-2. Attribute Byte for Monochrome Monitors Attribute byte in monochrome monitors There is an aitribute associated with each character on the screen. The attribute provides information to the video cireuitry, such as color and intensity of the character (foreground) and the background, The attribute byte for each character on the monochrome monitor is limited. Figure 4-2 shows bit definitions of the ‘monochrome byte attribute. Foreground refers to the actual character displayed. Normal, highlighted intensity and blinking are for the foreground only. The following are some possible variations of the attributes above. Binary Hex — Result 0000 0000 00 white on white {no display) 00000111 07 ~—_ white on black normat 0000 1111 OF ~— white on black highlight 10000111 87 white on black blinking 01110111 = 77—_black on black (no display) 0117 0000 = 70 -—_black on white 11110000 =—-FO—_black on white blinking For example, "00000111" would give the normal sereen mode where the background is black and the foreground is normal intensity, nonblinking, "OOD0I111" would give the same mode with the foreground highlighted "01110000" would give a reverse video screen mode with the foreground black and the background normal intensity. See Example 4-3. Attribute byte in CGA text mode Since all color monitors and their video cir- D7 | be | D5] 04 | D3 | 02] 01] D0 cuitry are upwardly compat- ible, in examples concerning ; color, inthis chapter we use | BOR OG Bit | a Gc B CGA mode, the common de- background, foreground nominator for all color moni- tors, The bit definition of the B = blinking attribute byte in CGA text 1= intensity mode is as shown in Figure Both blinking and intensity are applied to fore- 4-3. From the bit definition it ground only. can be seen that the back- ‘ground can take eight differ- ent colors by combining the prime colors red, blue, and green. ‘The foreground can be any of 16 different colors by combining red, blue, green, and intensity. Example 4-4 shows the use of the Figure 4-3. CGA Attibute Byte SECTION 4.1: BIOS INT 10H PROGRAMMING 125 attribute byte in CGA mode, Table 4-1 lists the possible colors. As examples of some possible variations look at the following cases: Binary Hex Color effect. 00000000 00 ~—_ Black on black. 00000001 «01_—Blue on black 00010010 = 12-~— Green on blue 00010100 14_—-Red on blue 0001 1111 1F_—_Hiigh-intensity white on blue Example 4-3, Write a program using INT 10H to: (a) Change the video mode. (b) Display the letter "D" in 200H locations with attributes black on white blinking (blinking letters “D" are black and the screen background is white). (c) Then use DEBUG to run and verify the program. Solution: (a) INT 10H function AH = 00 is used with AL = video mode to change the video mode. Use AL 07 for monochrome (MDA), EGA, or VGA; otherwise, use any of the 80x25 text modes, or use AL. 03 for CGA, which all color monitors emulate. Mov AH.00 ;SET MODE OPTION Mov ALOT 17 FOR MONOCHROME OR 03 FOR CGA TEXT INT 40H |MODE OF 80X25 FOR ANY COLOR MONITOR (b) With INT 10H function AH=09, one can display a character a certain number of times with spe- cific attributes. MOV AH,9 ‘DISPLAY OPTION MOV BH.00 (PAGE MOV AL44H STHE ASCII FOR LETTER "D" MOV CX.200H JREPEAT IT 200H TIMES MOV BLOFOH BLACK ON WHITE BLINKING INT 10H (c) Reminder: DEBUG assumes that all the numbers are in hex. Spdebug 7131:0100 MOV AH,00 4131:0102 MOV AL07 {USE 03 IF MONITOR IS COLOR 1131:0104 INT 10 4131:0106 MOV AHO 1131:0108 MOV 84.00, 1131.010AMOV AL44 1131:010¢ MOV CX,200 1131 010F MOV BL.FO 4131-0111 INT 10 131.0113 INT 3 1131.0114 Now see the result by typing in the command -G. Make sure that IP = 100 before running it Asan exercise, change the BL register to other attribute values given earlier. For example, BL = 07 white on black, or BL = 87H white on black blinking, 126 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C Example 4-4 ‘Write a program that puts 20H (ASCII space) on the entire screen. Use high-intensity white on a blue background attribute for any characters to be displayed. Solution: Mov Mov INT Mov MOV MOV MOV MOV INT. AH.00 ‘SET MODE OPTION AL03 :CGA COLOR TEXT MODE OF 80X25 H AHO9 DISPLAY OPTION BH/00 {PAGE 0 AL.20H ‘ASCII FOR SPACE (CX,800H JREPEAT IT 800H TIMES BLIFH HIGH INTENSITY WHITE ON BLUE 10H Graphics: pixel resolution and color Inthe text mode, the screen viewed as a matrix of rows and col- umns of characters. In graphics mode, the screen is viewed as a matrix of horizontal and vertical pixels. The number of pixels varies among moni- tors and depends on monitor resolu- tion and the video board. In this section we show how to access and program pixels on the sereen. Before embarking on pixel programming, the relationship between pixel resolution, the number of colors available, and the amount of video memory in a given video board must be clarified. ‘There are two facts associated with 1.0/0 every pixel on the screen, (1) the lo- cation of the pixel, and (2) its attrib- utes: color and intensity, These two ray facis must be stored in the video |! 0 ,1_| 0 light green ____ RAM. Therefore, the higher the | 1 | 0 | 1 1 iighteyan e 5 = PF] =e PP ele Hb cee ep + 110 1 number of pixels and colors, the | 7 larger the amount of memory that is I 1) 1/0, 0 jlightred __ needed to store them. In other words, ! ° 0. magenta the memory requirement goes up as -—P--0-+_1_ light magenta + the resolution and the number ofcol- _1__ 1 , 1 | 0 yellow ‘ors on the monitor go up. The CGA. Board can have a maximum of 6k LLL11 1 _1 high intensity white | bytes of video memory due to its in- herent design structure. The 16K. bytes of memory can be used in three different ways. Text mode of 80 x 25 characters; This takes a total of 2K bytes (80 x 25 = 2000) for the characters plus 2K bytes of memory for their attributes, since each character ‘has one attribute byte. That means that each screen (frame) takes 4K bytes, and that results in CGA supporting a total of four pages of data, where each page represents ‘one full screen, In this mode, 16 colors are supported. To select this mode, use AL = 03 for mode selection in INT 10H option AH = 00. Graphics mode of 320 x 200 (medium resolution); In this mode there are a total of 64,000 pixels (320 columns x 200 rows = 64,000). Dividing the total video RAM memory of 128K bits (16K x. 8 bits = 128K bits) by the 64,000 pixels gives 2 bits SECTION 4.1: BIOS INT 10H PROGRAMMING 127 for the color of each pixel, These 2 bits give four possibilities. Therefore, the 320 * 200 resolution CGA can support no more than 4 colors. To select this mode, use AL = 04. Graphics resolution of 640 x 200 (high resolution); In this mode there are a total of 128,000 pixels (200 x 640 = 128,000). Dividing the 16K bytes of memory by this gives | bit (128,000/128,000 = 1) for color, The bit can be on (white) or off (black) Therefore, the 640 x 200 high-resolution CGA can support only black and white To sclect this mode, use AL — 06. The 160 100 low-resolution mode used with color TV sets was bypassed n this discussion since no computer uses that anymore. From the discussion above ‘one can conclude that witha fixed amount of video RAM, as the resolution increases the number of supported colors decreases. That is the reason that to create more colors in VGA boards, one must increase the memory on the video board since there ‘must be a storage place to store the extra colors. Many VGA boards do provide the capacity to expand the video RAM up to | megabyte or more. INT 40H and pixel programming ‘To address a single pixel on the screen, use INT 10H with AH = 0CH. The X and ¥ coordinates of the pixel must be known, The values for X (column) and Y (cow) vary, depending on the resolution of the monitor. ‘The registers holding these values are CX = the column point (the X coordinate) and DX = the row point (Y coordinate). Ifthe display mode supports more than one page, BH = page number; otherwise, it is ignored. To tum the pixel on o off, AL= I or AL=0 for black and white, The value of AL can be modified for various colors, Drawing horizontal or vertical lines in graphics mode To draw a horizontal line, choose values for the row and column to point to the beginning of the line and then continue to increment the column until it reaches the end of the fine, as shown in Example 4-5. Example 4-5 Write a program to: (a) Clear the screen. (b) Set the mode to CGA of 640 x 200 resolution, (c) Draw a horizontal line starting at column = 100, row 50, and ending at column 200, row 50, Solution MOV AX,0600H :SCROLL THE SCREEN MOV BH.O7 !NORMAL ATTRIBUTE MOV — CX'9000 ;FROM ROW=00,COLUMN=00 MOV DX{14FH —:TO ROW=18H.COLUMN=4FH INT 104 INVOKE INTERRUPT TO CLEAR SCREEN Mov AH.00 |SETMODE MOV AL,06 MODE = 06 (CGA HIGH RESOLUTION) Wr 10H ANVOKE INTERRUPT TO CHANGE MODE Mov CX.100, START LINE AT COLUMN =100 AND, Mov Xx'50 JROW = 50. BACK: = MOV AHIOCH !AH=OCH TO DRAW ALINE MOV ALO {PIXELS = WHITE INT 40H INVOKE INTERRUPT TO DRAW LINE INC cx INCREMENT HORIZONTAL POSITION CMP Cx200 ;DRAW LINE UNTIL COLUMN = 200 JNZ BACK As an exercise, put INT 3 at the end of the program above and run it in DEBUG to get a feeling of the concept. To draw a vertical line, simply increment the vertical value held by the DX register and keep CX constant. The linear equation y = mx + b can be used to draw any line. 128 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C 1g the background color CGA graphics medium resolution provides 16 colors, Option AH = OBH can be used to change the background coloras shown in Example 4-6. Running that program in DEBUG causes the entire screen to change to blue, and when INT 3 is invoked, all the registers are displayed in very large letters. The cursor gets lost as well, since the screen is in graphics mode. To get out of that mode, simply type Q, followed by the return key to get out of DEBUG and back to DOS. Then use the DOS command "MODE CO80" (the letters CO followed by the number 80) to get back to the original sereen mode. Example 4-6 Write and run a program in DEBUG to: (a) Change the video mode to 320 x 200 graphics with four colors (mode AL = 4) (b) Make the entire screen blue Solution: a Mov AH.00 :SET MODE Mov AL ‘GGA STANDARD 300X 200 WITH 4 COLOR INT 10H MOV AHOB {SET COLOR MOV BHO ‘SET BACKGROUND COLOR MOV BL ‘BLUE (SEE TABLE 4-2) INT 10H INT 3) :STOP Review Questions Interrupt 10H function calls perform what services? The monitor in text mode has columns and rows. The top left position is (_,_) and the bottom right position is (_,_). 3. Fillin the blanks in the following program, which clears the sereen. Write com- iments on each line stating the purpose of each line of code. Nov Mov. MOV Mov Moy Mov Mov Int 4, INT 10 function AH = 03 was used. Afterward, DH = 05 and DL = 34. What does this indicate? 5. What is the purpose of the attribute byte for monochrome monitors? 6. In text mode, there is one attribute byte associated with each __on the screen. 7. Write the attribute byte to display background green, foreground white blinking. 8. State the purpose of the following program, which is for a monochrome monitor. Mov AHo2 MoV BH,00 MOV DX,0000 INT “0H Mov AHO9. Mov BH.00 MOV AL2AH Mov G80 Mov BLOFOH INT “ol SECTION 4.1: BIOS INT 10H PROGRAMMING 129 SECTION 4.2: DOS INTERRUPT 21H INT 21H is provided by DOS in contrast to INT 10H, which is BIOS-ROM based. When MS-DOS (or its IBM version PC-DOS) is loaded into the computer, INT 21H can be invoked to perform some extremely useful functions. These funetions are commonly referred to as DOS INT 21H function calls. A partial list of these options is provided in Appendix D. In this section we use only the options dealing with inputting information from the keyboard and displaying it on the seteen. In previous chapters, a fixed set of data was defined in the data segment and the results were viewed in a memory dump. Starting with this chapter, data will come from the keyboard and after it is processed, the results will be displayed on the screen. This is a much more dynamic way of processing information and is the ‘main reason for placing this chapter at this point of the book. Although data is input and output through the keyboard and monitor, there is still a need to dump memory to verify the data when troubleshooting programs, INT 21H option 09: outputting a string of data to the monitor INT 21H can be used to send a set of ASCI data to the monitor. To do that, the following registers must be set: AH = 09 and DX = the offset address of the ASCII data to be displayed. Then INT 21H is invoked. The address in the DX register is an offset address and DS is assumed to be the data segment, INT 21H option 09 will display the ASCII data string pointed at by DX until it encounters the dollar sign "S", In the absence of encountering a dollar sign, DOS function call 09 will continue to display any garbage that itcan find in subsequent memory locations nti it finds "S". For example, to display the message "The earth is but one country", the following is from the data segment and code segment. DATA_ASC DB "The earth is but one country’s’ Mov AHO ;Option 09 to display string of data Mov DX,OFFSET DATA_ASC ;DX= offset address of data INT 21H sinvoke the interrupt INT 21H option 02: outputting a single character to the monitor ‘There are occasions when it is necessary to output to the monitor only a single character. To do that, 02 is put in AH, DL is loaded with the character to be displayed, and then INT 21H is invoked. The following displays letter "I". MOV AH,02 option 02 displays one character Mov DLJ’ !DL holds the character to be displayed INT 21H jinvoke the interrupt This option can also be used to display ’S’ on the monitor since the string display option (option 09) will not display ’S°.. INT 21H option 01: inputting a single character, with echo This functions waits until a character is input from the keyboard, then echoes it to the monitor. After the interrupt, the input character will be in AL. MOV AHO1 option 01 inputs one character INT | 21H rafter the interrupt, AL = input character (ASCII) Program 4-1 combines INT 10H and INT 21H. The program does the following: (1) clears the screen, (2) sets the cursor to the center of the screen, and (3) starting at that point of the screen, displays the message "This is a test of the display routine". 130 CHAPTER 4: BIOS AND DOS PROGRAMMING \ ASSEMBLY AND C TITLE © PROG4-1 SIMPLE DISPLAY PROGRAM. PAGE 60,132 MODEL SMALL STACK 64 DATA MESSAGE DB ‘Thisis atest of the display routine"’'S CODE MAIN PROC FAR MOV AX,@DATA MOV DS,AX CALL CLEAR {CLEAR THE SCREEN CALL CURSOR |SET CURSOR POSITION CALL DISPLAY DISPLAY MESSAGE MOV AH.ACH INT 21H ;GOBACK TO DOS MAIN ENDP. {THIS SUBROUTINE CLEARS THE SCREEN CLEAR PROC MOV AX,0600H :SCROLL SCREEN FUNCTION MOV BHO7 INORMAL ATTRIBUTE MOV CX,0000 ESCROLL FROM ROW=00,COL=00 MOV OX,184FH STO ROW=18H,COL=4FH INT 10H INVOKE INTERRUPT TO CLEAR SCREEN ET CLEAR —ENOP. STHIS SUBROUTINE SETS THE CURSOR AT THE CENTER OF THE SCREEN cURSOR PROC MOV AH.02 :SET CURSOR FUNCTION MOV BH.00 PAGE 00 MOV DK,12 CENTER ROW Mov DL39 ‘CENTER COLUMN INT 10H "INVOKE INTERRUPT TO SET CURSOR POSITION ET ‘CURSOR ENOP “THIS SUBROUTINE DISPLAYS A STRING ON THE SCREEN DISPLAY. PROC. MOV’ At,09 “DISPLAY FUNCTION MOV DXOFFSETMESSAGE [DXPOINTS TO OUTPUT BUFFER INT 21H INVOKE INTERRUPT TO DISPLAY STRING RET oispLay ENOP, END MAIN Program 4-1 INT 21H option OAH: inputting a string of data from the keyboard Option OAH of INT 21H provides a means by which one can get data from the keyboard and store it in a predefined area of memory in the data segment. To do that, registers are set as follows: AH = 0AH and DX = offset address at which the string of data is stored. This is commonly referred to as a buffer arca. DOS requires that a buffer area be defined in the data segment and the first byte specifies the size of the buffer. DOS will put the number of characters that came in through the keyboard in the second byte and the keyed-in data is placed in the buffer starting atthe third byte, For example, the following program will acceptup to six characters from the keyboard, including the return (carriage return) key. Six locations were reserved for the buffer and filled with FFH. The following shows portions of the data segment and code segment. SECTION 4.2: DOS INTERRUPT 21H 131 ORG. O0t0H DATAT DB. 6,2,6 DUP (FF) {0010H=06, 0012H to 0017H = FF Mov AH.OAH ystring input option of INT 21H Mov DX,OFFSET DATA1 _ load the offset address of buffer INT 21H invoke interrupt 21H ‘The following shows the memory contents of offset 0010H: 0010 0011 0012 0013 0014 0015 0016 0017 06 000 FFO FE FRR OFF OFF When this program is executed, the computer waits for the information to come in from the keyboard. When the data comes in, the IBM PC will not exit the INT 21H routine until it encounters the retum key. “Assuming the data that was centered through the keyboard was "USA" will cause the computer to sound the speaker and the ‘contents of the buffer will look like this: 132 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C 0010 0011 0012 0013 0014 0015 0016 0017 6 05 «55 «683 (410 2061S OD uo Ss A SP a cR Location 0015 has ASCII 20H for space and 0016 has ASCII 61H for "a" and finally, the OD for RETURN key at 0017. ‘The actual length is 05 at memory offset 001TH. Another question is: What happens if only the CR key is activated and no other character is entered? For example, in the following, ORG 20H DATA4 = OB 10,2,10 DUP (FF) which puts 0AH in memory 0020H, the 0021H is for the count and the 0022H is the first location which will have the data that was entered. So if only the retum key is activated, 0022H has ODH, the hex code for CR. 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 002A 0028 002c OA 00 «OD FF OFFOOFF)OFE) OFF) OFF) OFF) OFF OFF OFF “The actual number of characters entered is 0 at location 0021. Remember that CR is not included in the count, It must be noted that as data is entered itis displayed on the screen, This is called an echo. So the OAH option of INT 21H accepts the string of data from the keyboard and echoes (displays) it on the screen ast is keyed in Program 42 performs the following, (1) dears We screen, (2) sels the cursor at he beginning of the tird Ine ‘from the top of te sereen, (8) accepts the message "IBM perSonal COmputer" trom he keyboard, (4) converts lowercase leiers ofthe message to uppercase, (5) displays the converted results on the next line. TITLE == PROG42 PAGE 60,132 “MODEL SMALL STACK 64 : DATA BUFFER DB” 22722DUP() {BUFFER FOR KEYED.IN DATA 1 DATAREA DB CRILF.22 DUP(?)'S)__ ;AREA TO PLACE DATA AFTER CONVERSION DTSEG ENDS cR EQU0bH LF QU OAH oI CODE MAIN PROC FAR, MOV” AX.@DATA DS\AX Mov DS, CALL CLEAR (CLEAR THE SCREEN CALL CURSOR [SET CURSOR POSITION CALL GETDATA INPUT A STRING INTO BUFFER CALL CONVERT ICONVERT STRING TO UPPERCASE CALL DISPLAY [DISPLAY STRING DATAREA MOV AH.ACH INT 21H ;GOBACK TO DOS MAIN ENDP "THIS SUBROUTINE CLEARS THE SCREEN CLEAR PROC MOV AX,0600H ;SCROLL SCREEN FUNCTION MOV BH.O7 INORMAL ATTRIBUTE MOV — CX.0000 ESCROLL FROM ROW=00,COL=00 MOV DX,184FH ITO ROW=16H,4FH INT 1041 INVOKE INTERRUPT TO CLEAR SCREEN RET CLEAR ENP. Program 4-2 (continved on next page) SECTION 4.2: DOS INTERRUPT 21H 133 sTHIS SUBROUTINE SETS THE CURSOR TO THE BEGINNING OF THE 3RD LINE CURSOR PROC MOV” AH,O2 :SET CURSOR FUNCTION MOV BH‘OO SPAGE 0 MOV DLO ICOLUMN 1 MOV DH,03 ROWS Int ‘0H INVOKE INTERRUPT TO SET CURSOR CURSOR ENDP "THIS SUBROUTINE DISPLAYS A STRING ON THE SCREEN DISPLAY PROC MOV AHS DISPLAY STRING FUNCTION MOV DX.OFFSETDATAREA [DX POINTS TO BUFFER INT 21H INVOKE INTERRUPT TO DISPLAY STRING RET DISPLAY ENDP {THIS SUBROUTINE PUTS DATA FROM THE KEYBOARD INTO A BUFFER GETDATA PROC MOV AH,OAH INPUT STRING FUNCTION MOV DXOFFSETBUFFER [DX POINTS TOBUFFER Wr 21H INVOKE INTERRUPT TO INPUT STRING. GETDATAENDP STHIS SUBROUTINE CONVERTS ANY SMALL LETTER TO ITS CAPITAL, CONVERT PROC MOV” BXOFFSET BUFFER MOV CLIBXH1 :GET THE CHAR COUNT SUB CH,CH (CX = TOTAL CHARACTER COUNT MOV DICK INDEXING INTO BUFFER MOV — BYTE PTRIBX+DI}+2,20H IREPLACE CR WITH SPACE MOV — SIOFFSET DATAREA‘2 ISTRING ADDRESS AGAIN; MOV ALIBX}+2 IGET THE KEYED-IN DATA CMP AL61H ICHECK FOR 'a’ JB NEXT LIF BELOW, GO TO NEXT CMP AL7AH ICHECK FOR '2" Ja_ NEXT {IF ABOVE GO TO NExT AND AL 110111118 IGONVERT TO CAPITAL NEXT. = MOV [SILAL IPLACE IN DATA AREA, INC SI INCREMENT POINTERS INC BX LOOP AGAIN sLOOP IF COUNTER NOT ZERO ‘CONVERT ENDP END MAIN Program 4-2 (continued from preceding page) Use of carriage retum and line feed In Program 4-2, the EQU statement was used to equate CR (carriage retum) with its ASCII value of ODH, and LF (line feed) with its ASCII value of OAH. This makes the program much more readable. Since the result of the conversion was 10 be displayed in the next line, the string was preceded by CR and LF. In the absence of CR the string would be displayed wherever the cursor happened to be. Inthe case of CR and no LF, the string would be displayed on the same line after it had been retumed to the beginning of the line by the CR and consequently, would write over some of the characters on that line. Program 4-3 prompts the user to type in a name. The name can have a maximum of eight leters. After the name is typed in, the program gets the length of the name and prints it tothe screen Program 4-4 demonstrates many of the functions described in this chapter. 134 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C TITLE PROGA.3 READS IN LAST NAME AND DISPLAYS LENGTH PAGE — 60,132 MODEL SMALL STACK 64 (7) DATA MESSAGE1 DB ‘What is your last name”, ORG 20H BUFFER1 OB 9,7.9DUP (0) ‘ORG 30H MESSAGE2 DB CR.LF.'The number of letters in your name is: ROW EQu “Ga COLUMN ~EQU 05 oR EQU ODH {EQUATE CR WITH ASCII CODE FOR CARRIAGE RETURN F EQU CAH ‘EQUATE LF WITH ASCII CODE FOR LINE FEED CODE ‘AH,08 DISPLAY THE PROMPT MOV DX,OFFSET MESSAGE1 INT 214 ‘AH.OAH MOV DX.OFFSET BUFFERT INT (20H MOV BX.OFFSET BUFFER1 FIND OUT NUMBER OF LETTERS IN NAME :GET LAST NAME FROM KEYBOARD MOV CLIBXe1] ET NUMBER OF LETTERS oR CL30H MAKE IT ASCII Moy MESSAGE2+49,cL LACE AT END OF STRING MOV = AH,09 iDISPLAY SECOND MESSAGE MOV DX.OFFSET MESSAGE2 INT 24H Mov AH.acH Int 21 :0 BACK TO DOS MAIN ENOP CLEAR PROC CLEAR THE SCREEN MOV AX.0600H Mov BHO? Mov ¢x.0000 MOV DX,184FH INT 40H RET CLEAR ENOP CURSOR PROC ;SET CURSOR POSITION: MOV = AH,02 MOV = BH,00 MOV DLCOLUMN MOV DHROW INT. 10H RET ‘CURSOR ENDP END MAIN Program 4-3 INT 21H option 07: keyboard input without echo Option 07 of INT 21H requires the user to enter a single character but that character is not displayed (or echoed) on the screen. After execution of the interrupt, the PC waits until a single character is entered and provides the character in AL. MOV AH,07 :keyboard input without echo INT 21H SECTION 4.2: DOS INTERRUPT 21H 135 Using the LABEL directive to define a string buffer Amore systematic way of defining the buffer area for the string input is 10 use the LABEL directive. The LABEL directive can be used in the data segment to.assign multiple names to data. When used in data segment it looks like this: name LABEL attribute ‘The attribute can be either BYTE, WORD, DWORD, FWORD, QWORD, or TBYTE. Simply put, the LABEL directive is used to assign the same offset address to two names. For example, in the following, JOE LABEL BYTE TOM DB — 20 DUP(O) the offset address assigned to JOE is the same offset address for TOM since the LABEL directive does not occupy any memory space (see Appendix C for many examples of the use of the LABEL directive). Next we show how to use this directive to define a buffer area for the string keyboard input: DATA BUF LABEL BYTE MAX SIZE DB 10 BUF_COUNT DB? BUFLAREA DB 10 DUP(20H) Now in the code segment the data can be accessed by name as follows: MOV AHOAH :load string into buffer MOV DX,OFFSET DATA_BUF INT 21H Mov CL,BUF_COUNT iload the actual length of string Mov SLOFFSET BUF_AREA Si=address of frst byte of string This is much more structured and easier to follow. By using this method, itis easy to refer to any parameter by its name. For example, using the LABEL directive, ‘one can rewrite the CONVERT subroutine in Program 4-2 as follows: {n that data segment the BUFFER is redefined as BUFFER LABEL BYTE BUFSIZE DB 22 BUFCOUNT 0B 7 REALDATA, DB 22DUP) ‘and in the code segment, in place of the CONVERT procedure: CONVERT PROC MOV” CL.BUFCOUNT sdoad the counter SUB CH.CH sX=counter Mov DICK Jindex into data field MOV BX,OFFSETREALDATA actual data address in butter MOV BYTE PTRIBX+DI]20H replace the OR with space MOV SLOFFSETDATAREA —_'Sitaddress of converted data AGAIN: = MOV ALJBX] move the char into AL CMP ALGIH ‘check ifs below ‘a JB NEXT Lit'yes then go to next CMP AL7AH check for above 'z" JA NEXT Lifyes then go to next AND AL tON116 sifot then mask it to capital NEXT: Mov (Si,AL move the character INC SI ‘increment the pointer INC BX ‘increment the pointer LOOP AGAIN ‘repeat if CX not zero yet RET itetur to main procedure CONVERT ENDP. 136 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C White @ program to perform the following: (1) clear the screen {2) setthe cursor at row 5 and column 1 ofthe screen, {3} prompt "There is a message for you from Mr. Jones. To read it enter Y "Ifthe user enters Y' or'y then the Iressage "Hi! must leave town tomorrow, therefore | wil not be able to see you" will appear onthe screen, If the user enters any other Key, then the prompt "No more messages for you" should appear on the next line TITLE PROGRAM 4-4 PAGE 60,132 MODEL SMALL STACK 64 DATA PROMPT! DB” "Theres a message fr you tom Mr Jones. MESSAGE DB fave town tomorrow, * DB | will not be able to see you’ PROMPT2 DB 'No more messages for you’,'S" DISEG © ENDS cr Qu iF EQu ‘CODE MAIN PROC MOV AX@DATA MoV CALL :CLEAR THE SCREEN CALL CURSOR ISET CURSOR POSITION MOV AH.Co {DISPLAY THE PROMPT MOV DX\OFFSET PROMPTS INT 21H Mov AHO7 :GET ONE CHAR, NO ECHO INT 21H MP ALY: sIF'Y', CONTINUE, JZ OVER cMe ALY, JZ OVER Mov AH,09 ;DISPLAY SECOND PROMPT IF NOT Y MOV DX,OFFSET PROMPT2 INT 21H wMP EXIT Over: MOV AH.O9 DISPLAY THE MESSAGE, MOV — DX.OFFSET MESSAGE Wr 21H ex = MOV AH.ACH INT iH :GO BACK TO DOS, MAIN ENDP. CLEAR PROC ;CLEARS THE SCREEN MOV AX,0600H MOV BHOT MOV CX'0000. MOV DX,184FH INT 10H RET CLEAR ENDP CURSOR PROC ;SET CURSOR POSITION MOV AH,02 MOV BH.00 MOV DLS ;COLUMN 6 MOV — DH,08 iROWS INT 10H RET CURSOR ENOP END MAIN Program 4-4 SECTION 4.2: DOS INTERRUPT 21H 137 This is the same as Program 4-4, rewritten with full segment definition TITLE PROGRAM 4-4 REWRITTEN WITH FULL SEGMENT DEFINITION PAGE 60,132 STSEG | SEGMENT DB 64 DUP (2) STSEG ENDS OTSEG SEGMENT PROMPT! DB "There is a message for you from Mr. Jones. * DB "Toread tenter Y, MESSAGE DB CR,LF’Hi! Imust leave town tomorrow, * DB ‘therfore I will not be able to see you’ PROMPT2 DB__ GR,LF.'No mare messages for you,’s DTSEG © ENDS cR EQU) ODH uF EQU OAK SDSEG SEGMENT MAIN PROC” FAR ASSUME CS:CDSEG DS'DTSEG,SS:STSEG MOV AXOTSEG. MOV DS,AX CALL CLEAR CLEAR THE SCREEN CALL CURSOR ‘SET CURSOR POSITION MOV AH.09 DISPLAY THE PROMPT MOV OXOFFSET PROMPT! INT 21H Mov AH.O7 GET ONE CHAR, NO ECHO INT 21H MP ALY" FY", CONTINUE JZ OVER cMP AL’! JZ OVER Mov AH.09 ‘DISPLAY SECOND PROMPT IF NOT Y MOV DX OFFSET PROMPT2 INT 21H MP EXIT OVER: MOV AH,O9 ;DISPLAY THE MESSAGE, MOV DX OFFSET MESSAGE INT (21H exit = MOV AH.ACH INT 21H {GO BACK TO DOS MAIN ENOP CLEAR PROC. ;CLEARS THE SCREEN MOV AX.0600H MOV BH.O7 MoV Cx.0000 MOV DX,1e4FH INT 10H RET CLEAR ENOP CURSOR PROC ‘SET CURSOR POSITION Mov Mov Mov ;COLUMN 5 MoV ROWS INT. RET CURSOR ENOP CDSEG ENDS END MAIN Program 4-4, rewritten with full segment definition 138 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C SECTION 4.3: Review Questions INT ___ function calls reside in ROM BIOS, whereas INT __ function calls are provided by DOS. ‘What is the difference between the following two programs? MOV AH,09 MOV AH,OAH MOV DX,OFFSET BUFFER MOV DX,OFFSET BUFFER INT 21H INT 21H INT 21H function 09 will display a string of data beginning at the location specified in register DX. How does the system know where the end of the string is? Fill in the blanks to display the following string using INT 21H. MESSAGE! DB "What is your last name?S" MOV AH, MOV DX, INT 21H The following prompt needs to be displayed. What will happen if this string is out- put using INT 21H function 09? PROMPT] DB __ “Enter (round to nearest $) Use the FQU directive to equate the name "BEL ing the bell. Write a program to sound the bell. Code the data definition directives for a buffer area where INT 21H Option 0AH will input a social security number. INT 16H KEYBOARD PROGRAMMING The last section demonstrated the use of INT 21H function AH-07, which waits for the user to input a character. What if a program must run a certain task continuously while checking for a keypress? Such cases require the use of INT 16H, BIOS interrupt used exclusively for the keyboard. Checking a key press To check a key press we use INT 16H function AH=01 MOV AH.01 soheck for key press. INT 16H zusing INT 16H, Upon return, ZF=0 if there is @ key press; ZF=1 if there is no key press. Notice that this function does not wait for the user to press a key. It simply checks to.see if there is a key press. The use of this function is best understood in the context of examples. Program 4-5 sends the ASCII bell character, 07 hex (see Appendix F), to the screen continuously. To stop the bell sound, the user must press any key. Which key is pressed? ‘There are times when the program needs to know not only ifa key has been pressed but also which key was pressed. To do that, [NT 16H function AH=0 can bbe used immediately after the call to INT 16H function AH=01. MOV AHO get key pressed INT 16H cusing INT 16H Upon return, AL contains the ASCII character of the pressed key; its scan key is in AH. Notice that this function must be used immediately after calling INT 16H function AH=01. Program 4-6 demonstrates how it works. Keyboard scanning is discussed further in Chapter 18. SECTION 4.3: INT 16H KEYBOARD PROGRAMMING 139 TITLE PROGRAM 4-5: KEYBOARD HIT USING INT 16H ‘THIS PROGRAM SOUNDS THE BELL CONTINUOUSLY UNLESS ANY KEY IS PRESSED. MODEL SMALL STACK DATA MESSAGE D3 ‘TO STOP THE BELL SOUND PRESS ANY KEYS Cot MAIN PROC MOV AX.@DATA Mov DSAK Mov AH'0a MOV DXOFFSET MESSAGE DISPLAY THE MESSAGE INT Ott AGAIN: MOV AH02 {SENDING TO MONITOR ASINGLE CHAR Mov DLO? {SEND OUT THE BELL CHAR INT 2tH Mov Alot {CHECK THE KEY PRESS INT 46H {USING INT 18H Zz AGAIN |IF NO KEY PRESS STAY IN THE LOOP Mov AHACH LIF ANY KEY PRESSED GO BACK TO DOS INT 21H MAIN _ENDP END Program 4-5 TITLE PROGRAM 4-6: MODIFIED VERSION OF PROGRAM 4.5 sTHIS PROGRAM SOUNDS THE BELL CONTINUOUSLY UNTIL’ OR 'q' IS PRESSED. MODEL SMALL STACK DATA MESSAGE DB "TO STOP THE BELL SOUND PRESS @ (or q) KEYS’ cone MAIN PROC MOV AX@DATA Mov DSAK Mov AHlO9 MOV DXOFFSETMESSAGE (DISPLAY THE MESSAGE INT 24H AGAIN, MOV Mov ‘SOUND THE BELL BY SENDING OUT BELL CHAR Mov {CHECK FOR KEY PRESS INT USING INT 16H az ENO KEY PRESS KEEP SOUNDING THE BELL Mov 570. GET THE CHARACTER INT AWE MUST USE INT 16H ONE MORE TIME cmp is ima? JE JE YES EXIT cme is itq E AP YES EXIT ome NO. KEEP SOUNDING THE BELL ext Mov 5G BACK TO DOS MAIN ENDP ENO Program 4-6 Review Questions 1. Which function of INT 16H is used for key press detection? 2. Inthe above question, how do you know if'a key is pressed? 3. Inthe above question, how can the ASCH value for the pressed key be obtained? 4. Indicate the main difference between INT 21H function AH-07, and INT 16H func- tion AH=O1, 5. Write a simple program to sound the bell unless letier "X’ is pressed. If*X pressed, the program should exit. 140 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C SECTION 4.4: INTERRUPT PROGRAMMING WITH C Although C is a high-level language, it has strong bit manipulation capa- bility, For this reason, some programmers refer to C as a "high-level assembly” language. For C/C++ programmers who do not have detailed knowledge of 80x86 Assembly language programming but want to write programs using DOS function calls [NT 21H and BIOS interrupis, there is help from compilers in the form of intS6 and intdos functions. The int86 function is used for calling any of the PC’s interrupts, while the intdos function is used only for the INT 21H DOS function calls, Programming BIOS interrupts with C/C++ To use the int86 function, registers are first set to desired values, then int86 is called. Upon return from int86, the 80x86 registers can be accessed. In this regard, {nt86 is just like the "INT #" instruction in 80x86 Assembly language. To access the 80x86 registers, use the union of the REGS structure already defined by the C compiler. Ithas the following format, where regin and regout are variable names: union REGS regin,regout; The union of the REGS structure allows x86 registers to be accessed in cither their 16-or 8-bit format. The 16-bit registers are referred to asx; 8-bitregisters, are referred to as h (for "halfword"). This is shown in Table 4-2. ‘Table 4-2: REGS Union Elements and Assembly Equivalent annie = n wy rae | aa 16-bit HE Z 8-bit Clanguage ___ Assembly Language | __C Language Assembly Language | ___regin.x.ax | AK regin.h.al : AL 4 jE eee regin.hah 7 AH regin.x.bx_ BX. _regin.hbl BL Ee Z reginhbh | BH | | regin.x.ex z for | _reginuel cL | L. pao ——__|_. reginhch | CH r regin.x.dx DX | reginhdl | DL. L | regin.h.ah ___DH | __regin.x.si SI zl Eee regin.x.di OTS EEE eae {___regin.x.eflag cy l “The following code compares loading the registers and invoking the inter rupt in C and Assembly language. i Clanguage Assembly language */ union REGS regin,regout;, regin.h.ah=0x25; Pt mov ah,25h_ Al ” regin.x.dx=0x4567; 1° mov dx,4567h ;DX=4567H *! regin.x.si0x1200; P* mov si,1290h ;SI=1290H ‘7 int86 (interrupt ®in, ®out); int 4" SECTION 4.4: INTERRUPT PROGRAMMING WITH C 141 In the code above, interrupt # is a value from 00 to 255 (or 0x00 to OxFF in hex, using the C syntax for hexadecimal numbers), and ®in and &cregout are the addresses of the REGS variables. Upon returning from the int86 function, we can access the contents of registers just as in 80x86 Assembly language programs, This is shown as follows: mydata=regouth.ah; /* mov mydata,ah assign AH to mydata */ myvalu=regoutx.bx;_/* mov myvalu,bx assign BX to myvalu */ Examples 4-7 and 4-8 demonstrate how int86 is used in C programming. Example 4-9 shows how to access registers upon returning from int86, Example 4-10 uses INT 21H, AH ~ 30H to display the DOS version Solution: Example 4-7 Use the int86 function to clear the sereen. Show the equivalent INT 10 instruction. We can mix 8- and 16-bit registers as shown ne: ft example 4-7A using 16-bit registers */ #include P* int86 is part of this library */ main() { union REGS regin,regout; regin.x.ax=0x0600; I MOV AX,0600H_—*/ regin.h.bh=0x07; 7 MOV BH,O7H " regin.x.cx=0; I MOV CX,0 1 rogin.x.dx=0x184F; MOV DX\184FH "7 int86(0x10, 8regin, ®out); PINT 10H 4 * example 4-7B using B-bit registers */ include intB6 is part of this library */ main) { union REGS regin,regout; I MOV AHS 7 MOV ALO 7 MOV BH,7 7 I MOV CX,0 7 ” regin.h.dh=0x18; int86(0x10.®in.®out); PINT 10H 7 } Solution: Example 4-8 Use function int86 with INT 12H to find the size of conventional memory installed on a given PC. INT 12H provides the size of conventional memory in register AX. #include #include main() { unsigned int convmem; union REGS regin,regout; int86(0x12,®in, ®out); ‘convmem=regout.x.ax; printf("This PC has %dKB of Conventional memory\n" ,convmem); } 142 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C Use the int86 function to perform the following functions (a) save the current cursor position, (b) set the cursor to row 12, column 8, and (©) display the message “Hello” using the printf function. Solution: include include main() { Unsigned char odrow; unsigned char oldcol; union REGS regin.regout regin.hal [MOV AH,3 joption 3 INT 10H */ regin.h.bt I MOV BH,0 ;page 0 int86(0x10,8regin,®out),__/* INT 10H ;video INT oldrow=regout.h.dh; f* MOV oldrow,DH ;save row ‘oldcol=regout.n.cl;, f* MOV oldcol,DL ;save cot printf("Cursor was at row=%d,column=%d \n",oldrow,oldcol); 1 MOV AH.2 option 2 of int 10H M MOV BH.O ;Page zero 7 MOV DL.B co! location 5 1" MOV DH, 12 ;row location int86(0x10,8regin,regout); _/* INT 10H printf(*Helio\n"}; } Example v 7 ” 1 7 7 ” " ‘Using function int86 with INT 21H option 30H, write a program to display the DOS version. Solution: #include ‘#include ‘#include main() union REGS regin,regout, unsigned char minor, major; clrser(); regin.h.eh=0x30; __H/got DOS version using AH=30H of INT 21H int86(0x21 Sregin,®out); minor=regout.h.ah; major=regouth.al; printf("The DOS version on this PC is %d.%d\n" major,minor); } Example 4-10 Programming INT 21H DOS function calls with C/C++ Although we can use the int86 function for INT 21H DOS function calls, there is a specially designated function, intdos, that can be used for DOS function calls. The format of intdos is given below. Example 4-11 shows how to use intdos, intdos(®in.8regout); /* to be used for INT 21H only *! SECTION 4.4; INTERRUPT PROGRAMMING WITH C 143 Example Use INT 21H option 2AH to display the date in the form dd-mm-yy on the screen (a) Use intdos functions, (b) Use the int86 function. Solution: Upon returning from the INT 21H function 2AH, DL contains the day, DH the month, CX the year. (a) This program uses intdos. ‘include #include main() { unsigned int year; unsigned char month; unsigned char day; union REGS regin,regout; regin.h.ah=0x2A; intdos(®in, ®out}; day=regouth.di; ‘month=regout.h. dh; year=regout x.cx, printf("Today’s date is %d-%d-Yecin’, month,day.year); } (b) In this program we can replace the intdos statement with int86(0x21, ®in, ®out) Accessing segment registers Both int86 and intdos allow access to registers AX, BX, CX, DX, SI, and DI, but not segment registers CS, DS, SS, and ES. In some of the interrupt services, we need access to the segment registers as well. In such cases we must use int86x instead of int86, and intdosx instead of intdos. In using int86x and intdosx, we must also pass the argument SREGS, Functions int86x and intdosx have the following formats, int86x(interrupt # ®in, ®out ®seg) intdosx(®in, regout, ®seg): struct SREGS regseg: struct SREGS given below Functions int86x and intdosx provide access only to registers ES and DS. and not the segment registers CS and SS. The contents of SS and CS cannot be altered since their alteration will cause the program to crash. Fortunately, BIOS and DOS function calls that use segment registers do not request the alteration of CS and SS. Example 4-12 shows how to get the values of interrupt vector tables. Accessing the carry flag in int86 and intdos functions Upon return from many of the interrupt functions, we need to examine the carry flag, Functions int86, intdos, int86x and intdosx allow us to examine the carry flag bit only, and no other flag bits are available through these functions, To access the carry flag bit we write: iffregout x.cflag) 144 ‘CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C The structures of word, byte, and segment registers are shown below. union REGS { struct WORDREGS { ‘unsigned int ax: unsigned unsigned unsigned unsigned unsigned unsigned }x struct BYTEREGS { Unsigned char al.ah; Unsigned char bi.bh unsigned char cl,ch: ‘unsigned char dd Eh. }inregs; union REGS “outregs, struct SREGS { unsigned int es: unsigned int cs; unsigned int ss; unsigned int ds; Y'seregs; Example 4-12 Using INT 21 option 35H, get the CS:IP in the interrupt vector table for INT 10H. Solution: From Appendix D, we have INT 21H, AH=35, and AL=interrupt number. Upon return, ES contains the code segment (CS) value and BX has the instruction pointer (IP) value from the vector table. #include include main() { unsigned int ipvalu; unsigned int csvalu; union REGS regin,regout; struct SREGS regseg; regin.h.ah=0x35; 1 MOV AH,35H */ regin.h.al0x10; it MOV AL,10H */ int86x(0x21,®in,®out ®seg); /* or we can use intdosx(®in,®out,®seg) */ ipvalu=regout x.bx; ‘1 MOV ipvalu,BX */ regseg.es; I" MOV csvalu,ES */ printi"The CS:IP of INT 10H is %X:%X \n* esvalu,ipvalu; } Mixing C with Assembly and checking ZF The vast majority of interrupts in the PC use the carry flag to indicate special conditions such as errors. For example, functions AH~3CH and AH=3DH for INT 21H (see Appendix D) both deal with files and need the carry flag to indicate certain conditions. As discussed earlier, the REGS union provides only the CF (carty flag). If the zero flag needs to be checked to see if certain conditions are met, the only alternative is to mix C with assembly. This is called in-line Assembly, and is shown, in Program 4-7, Any x86 valid instruction can be used ina C program ifitis prefixed with "asm", In Program 4-7 a statement is displayed several times. Instead of using. aC "for" loop, register CX is used as a counter while "JNZ" checks the zero flag, SECTION 4.4: INTERRUPT PROGRAMMING WITH C 145 TTusing in-line assembly to check the zero flag, this program displays a statement 11a certain number of fimes, using the cx register for a counter i} hanks to Mark Lessley for his input on this example Hindlude #include main Byte size data Unsigned int counter=5; clrser(); ‘asm MOV CX.counter —_f/ex=counter AGAIN: asm MOV AH.2 1 AH=02 of INT 10H to set cursor asm MOV BHO lipage 0 ‘asm MOV DH,row toad the row value ‘asm MOV DL,col load the coluran value asm INT 10H. call INT 10H to set cursor ‘asm PUSH CX 11 save the counter print{"This is a test) Fowet; colt: asm POP CX restore the counter ‘asm DEC CX; 1] decrement the counter ‘asm JNZ AGAIN 11.go back if ZF not high getch(); Note: While C statements must end with a semicolon; tis optional for statements with the prefix asm. Program 4-7 C function kbhit vs INT 16H keyboard input The kbhit function is the C equivalent of INT 16H in Assembly language. Program 4-8 is a C version of Program 4-5. keep sounding the bell unless any Key Ws pressed #include #include main() airscr(); printf(‘To Stop the Bell Sound Press Any Key\n") While(tkbnit)) /continue as long as there is no keyboard hit, pane"0x07), send the Bell ASCII character to monitor Program 4-8 Program 4-9 checks fora specific key while checking the key press by using, the getch() function in C. TKeap sounding the ball unless Gor is pressed] #include #include main) Unsigned char data; cirscr(): print{("To Stop the Bell Sound Press Q or q Keyin*); do. { while(Ikbhit()) //keep sounding Bell unless a key is pressed printf"%o", 0x07); data=getch(); iiget the key press }; continue as long as itis not Q or q Program 4-9 146 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C Review Questions 1, True or false. Function int86 can be used for any interrupt number. 2. True or false. Function intdos can be used for any interrupt number. 3. The int86 has ‘arguments, whereas intdos has . 4. True or false. Operand regin.h.al accesses the 16-bit register. 5. Is the following code correct? union REGS rin,rout; rin.x.ax=0x1250; 6. To access segment registers we use (int86x, int86, intdos). 7. The int86x function has __arguments and they are 8. True or false. In the int86x and intdosx functions, only the ES and DS registers are accessible. SUMMARY INT 10H funetion calls provide the capability to manipulate text and graphics on the screen. These interrupts reside in ROM BIOS because speed is an important factor in these often-used routines. The function calls described in this chapter include calls to clear or scroll the screen, change the video mode, and write text or graphics to the screen. In text mode, the programmer works with a matrix of 80x 25 characters. In pixel mode, the programmer works with a matrix of pixels, the number varying with the video mode used. INT 21H function calls are provided by DOS to perform many useful functions. The function calls described in this chapter include calls to inputor output characters or strings to the monitor. INT 16H function calls provide access to the keyboard through the BIOS programs. Interrupt programming can also be performed in the C programming language through the use of structure REGS and C functions int86 and intdos. In addition, Assembly language instructions can be coded into C programs when the "asm" prefix is used. This is called in-line assembly. PROBLEMS, 1. Write a program that: (2) Clears the screen, and (b) sets the cursor position at row = 5 and column = 12, 2. What is the fimction of the following program? MOV AH,02 MOV BH.00 MOV DL,20 MOV DH,10 INT 10H 3. The following program is meant to set the cursor at position row = 14 and col = 20. Fix the error and run the program to verify your solution MOV AH,02 MOV BH,00 MOV DH,14H MOV DL,20H INT 10H 4, Write a program that sets the cursor at row = 12, col = 15, then use the code below to get the current cursor position in register DX with DH = row and DL = col. Is, the cursor position in DH and DL in decimal or hex? Verify your answer. MOV AH,03 MOV BH,00 INT 10H 5. In clearing the sereen, does the sequence of code prior to INT 10H matter? In set- ting a cursor position? Verify by rearranging and executing the instructions, SUMMARY 147 6. You want to clear the sercen using the following program, but there are some errors. Fix the errors and run the program to verify MOV AX,0600H MOV BIT.07 MOV CX,0000 MOV DX,184F INT 10H Write a program that: (a) Clears the screen (b) Sets the cursor at row = 8 and column = 14 (c) Displays the string "IBM Personal Computer” 8. Run the following program and dump the memory to verify the contents of memory locations 0220H to 022FH if "IBM PC with 8088 CPU" is keyed in. ORG 220H BUFFER DB _15, 16 DUP (OFFH) and for the code : MOV AHOAH MOV DX,OFFSET BUFFER INT 21H 9, Write a program that: (a) Clears the screen. (b) Puts the cursor on position row = 15 and column = 20. (c) Displays the prompt "What is your name?” (qd) Gets a response from the keyboard and displays it at row = 17 and column = 20. 10. Write 2 program that sets the mode to medium resolution, draws a vertical line in the middle of the screen, then draws a horizontal line across the middle of the screen. LL. Write @ program to input a social security number in the form 123-45-6789 and transfer it to another area with the hyphens removed, as in 123456789. Use the following data definition SS_AREA LABEL BYTE SSSIZE DB I2 SS_ACTUAL DB? SS_DASHED DB 12DUP(?) SSNUM DB 9 DUP(?) 12. Write a program (use the simplified segment definition) to input two seven-digit numbers in response to the prompts "Enter the first number" and "Enter the sec- ‘ond number". Add them together (using AAA, covered in Chapter 3) and display the sum with the message "The total sum is: 13. Show how to use the union REGS to set AX =9878H, BH=90H, and CL=F4H. 14, Write a C function to set the cursor using int86. Then use it to set the cursor to row=10, col=20 and display the message "HELLO" 15. Write a C program with the following objectives. (a) Clear the screen. Use int86. (b) Set the cursor to somewhere around the middle of the screen. Use int86. (©) Display the date and time continuously in the following format. Use intdos. Time: ‘hr-min:sec Date: monday/yt (G) A prompt should ask for "Q" to quit. Use C functions. (e) When the user types in Q, it should quit displaying time and date and go back to DOS. Use C functions. 16. A programmer has declared the REGS union as follows. Would this work? union REGS inregs,outregs; 17, Write two versions of a C program that counts upward by increments of 1 and dis- plays the count with a I-second delay in between counting. When any key is pressed, it stops counting and goes back to DOS. The count should start from 0. (a) use the kbhit function (b) use the INT 16 function 01 18. Repeat the (b) version of the above program, with the user entering the initial count instead of starting from zero. 148 CHAPTER 4: BIOS AND DOS PROGRAMMING IN ASSEMBLY AND C ANSWERS TO REVIEW QUESTIONS SECTION 4.1; BIOS INT 10H PROGRAMMING 1. perform screen ilo 2, 80, 25; 00,00 and 24,79 3. MOVAH.08 ‘SELECT CLEAR SCREEN FUNCTION MOVAL,00 AH=0 TO SCROLL ENTIRE PAGE MOV BH.07 'BH=07 FOR NORMAL ATTRIBUTE MOV CH00 ‘START AT ROW cO MOV CL.00 (START AT COLUMN 00 MOV DH.24 END AT ROW 24 MOV DL,79. SEND ATROW 79 INT 10H ‘INVOKE THE INTERRUPT indicates that the cursor is at row 5, column 34 ‘provides information about the foreground and background intensity, whether the foreground is blinking andlor highlighted charactor 10700111 the fist time INT 10H is invoked, it sets the cursor to position 00,00; the second time itis invoked, it displays the character "' 80 times with attributes of white on black, blinking, SECTION 4.2: DOS INTERRUPT 21H 1, 10H, 214 2. the leftmost code inputs a string from the keyboard into a buffer; the code on the right outputs a string from a buffer to the monitor 3. the end of the string is the dollar sign '$° 4 OAH, OFFSET MESSAGE1 5 6 7 When the '$' within the string is encountered, the computer wil stop displaying the string BELL QU OH Using the EQU in Answer 6, the code segment would include the following: MoV’ ‘AHO Mov DLBELL INT 21H 8 SS.AREA LABEL BYTE SSSZE 0B 12 SS_ACTUALDB ? SSINUM DB 12DUP(?) SECTION 43; INT 16H KEYBOARD PROGRAMMING INT 16H function AH=01 2. Mor return rom INT 16H function AH01, 121 there i no key press; ZF=0 thon a key has been prossed 3. IFZF=O, then we use INT 16H function AH=0 to get the ASCII character for the pressed key. 4. INT 21H waits for the user to press the key; INT 16H scans the keyboard, allowing the program to continue executing other tasks while scanning for the key press. 5. AGAIN: MOV AH.02 ;USE FUNCTION AH=02 OF INT 21H MOV DLO7 ‘SOUND THE BELL BY SENDING OUT BELL CHAR 1 MOV AHO1 CHECK FOR KEY PRESS INT 16H LOSING INT 18H. JZ AGAIN IF NO KEY PRESS KEEP SOUNDING THE BELL MOV AHO {TO GET THE CHARACTER INT 16H AWE MUST USE INT 16H ONE MORE TIME CMPALX = ISTT'X?. JE EXIT IF YES EXIT JMP AGAIN, NO. KEEP SOUNDING THE BELL EXIT MOVAHCH {GO BACK TO DOS, INT 21H SECTION 4.4: INTERRUPT PROGRAMMING WITH G 1. tue 2. false; only for the INT 21H 3 3,2 4. false 5. Yes: we can use any name, Otver commonly used names are rege outegs, and 1.2 7. four. INT#, Bregin, Bregout, ®seg 8 tue ANSWERS TO REVIEW QUESTIONS, 149 CHAPTER 5 MACROS AND THE MOUSE OBJECTIVES Upon completion ofthis chapter, you wil be able to: ee of sing macros Code Assembly tariguage | imstructions to initialize the mouse __ Code Assembly langitage instructions fo set or get the mouse cursor. » Use INT 33H functions to retrieve mouse button press and release information » Limit mouse cursor positions by setting boundaries or defining exclusion areas: 150 CHAPTER 5: MACROS AND THE MOUSE. In this chapter we explore the concept of the macro and its use in Assembly language programming. The format and usage of macros are defined and many examples of their application are explored. In addition, this chapter demonstrates the use of INT 33H to control mouse functions in Assembly language programs. SECTION 5.1: WHAT IS A MACRO AND HOW IS IT USED? There are applications in Assembly language programming where a group of instructions performs a task that is used repeatedly. For example, INT 21H function 09H for displaying a string of data and function 0AH for keying in data are used repeatedly in the same program, as was seen in Chapter 4. So it does not make sense to rewrite them every time they are needed. Therefore, to reduce the time that it takes to write these codes and reduce the possibility of errors, the concept of macros was born. Macros allow the programmer to write the task (set of codes to perform a specific job) once only and to invoke it whenever it is needed wherever it is needed, MACRO definition Every macro definition must have three parts, as follows: name MACRO — dummy1,dummy2,...,dummyN ENDM ‘The MACRO directive indicates the beginning of the macro definition and the ENDM directive signals the end, What goes in between the MACRO and ENDM directives is called the body of the macro, The name must be unique and must follow Assembly language naming conventions. The dummies are names, or parameters, or even registers that are mentioned in the body of the macro. After the macro has been written, it can be invoked (or called) by its name, and appropriate values are substituted for dummy parameters. Displaying a string of data using function 09 of INT 21H is a widely used service. The following is a macro for that service: STRING MACRO DATA1 Mov AH.09 Mov DX,OFFSET DATA1 INT 21H ENDM The above is the macro definition. Note that dummy argument DATA| is mentioned in the body of macro. In the following example, assume that a prompt has already been defined in the data segment as shown below. In the code segment, the macro can be invoked by its name with the user’s actual data: MESSAGE1 DB ‘Whats your name?,’’ TRING MESSAGE1 The instruction "STRING MESSAGE" invokes the macro. The assembler expands the macro by providing the following code in the LST file: 1 MOV AH09 1 MOV DX.OFFSET MESSAGE1 1 INT 21H The (1) indicates that the code is from the macro. In earlier versions of MASM, a plus sign (+) indicated lines from macros. SECTION 5.1: WHAT IS AMACRO AND HOW IS IT USED? 151 Comments in a macro Now the question is: Can macros contain comments? The answer is yes, butthere isa way to suppress comments and make the assembler show only the lines that generate opcodes. There are basically two types of comments in the macro: listable and nonlistable. If comments are preceded by a single semicolon (;) as is done in Assembly language programming, they will show up in the "Ist" file, but if ‘comments are preceded by a double semicolon (;;) they will not appear in the "Ist" file when the program is assembled. There are also three directives designed to make programs that use macros more readable, meaning that they only affect the ".[st” file and have no effect on the "obj" or ".exe” files. They are as follows: -LALL (List ALL) will list all the instructions and comments that are preceded by a single semicolon in the”. Ist" file. The comments preceded by a double semicolon cannot be listed in the " 1st” fie in any way. -SALL (Suppress ALL) is used to make the list file shorter and easier to read. It suppresses the listing of the macro body and the comments. This is especially useful if the macro is invoked many times within the same program and there is no need to see it listed every time. Tt must be emphasized that the use of SALL will noteliminate any opcode from the object file. It only affects the listing in the "Ast" file -XALL (cXecutable ALL), which is the default listing directive, is used to list only the part of the macro that generates opcodes. Example 5-1 demonstrates the macro definition Example ‘Write macro definitions for setting the cursor position, displaying a string, and clearing the screen. Solution: CURSOR MACRO ROW,COLUMN STHIS MACRO SETS THE CURSOR LOCATION TO ROW,COLUMN, {USING BIOS INT 10H FUNCTION 02 MOV" AHO2 ‘SET CURSOR FUNCTION MOV BH.O0 {PAGE 00 MOV DH.ROW :ROW POSITION MOV DL,COLUMN {COLUMN POSITION INT, 10H ANVOKE THE INTERRUPT DISPLAY MACRO STRING ‘THIS MACRO DISPLAYS A STRING OF DATA ;OX = ADDRESS OF STRING. USES FUNCTION 09 INT 21H. MOV AK.09 DISPLAY STRING FUNCTION MOV DX,OFFSETSTRING (DX = OFFSET ADDRESS OF DATA INT 21H INVOKE THE INTERRUPT ENOM CLEARSCR MACRO THIS MACRO CLEARS THE SCREEN ;USING OPTION 08 OF INT 10H MOV AX,0600H —;SCROLL SCREEN FUNCTION MOV BH.O7 [NORMAL ATTRIBUTE MOV Cx0 JFROM ROW=00,COLUMN=00 MOV DX.184FH TO ROW=18H,COLUMN=4FH INT 10H INVOKE THE INTERRUPT ENOM Remember that the comments marked with for Program 5-1 will not be listed in the list file as seen in the list file 152 CHAPTER 5: MACROS AND THE MOUSE Using the macro definition in Example 5-1, write a program that clears the screen and then at each of the following screen locations displays the indicated message: at row 2 and column 4 “My name” at row 12 and column 44 “what is” at row 7 and column 24 “is Joe” at row 19 and column 64 “your name?”” TITLE —-PROGS-1 PAGE — 60,132 CLEARSCR MACRO. STHIS MACRO CLEARS THE SCREEN USING OPTION 06 OF INT 10H MOV AX,0600H ;SOROLL SCREEN FUNCTION Mov BHO7 INORMAL ATTRIBUTE MOV Xo {FROM ROW=00, COLUMN=00 MOV OX,184FH ‘TO ROW=18H,COLUMN=4FH INT, 10H INVOKE THE INTERRUPT DISPLAY MACRO_ STRING THIS MACRO DISPLAYS A STRING OF DATA ;DX = ADDRESS OF STRING. USES FUNCTION 09 INT 21H. MOV” AH.09 DISPLAY STRING FUNCTION. MOV DX.OFFSETSTRING DX = OFFSET ADDRESS OF DATA INT 21H INVOKE THE INTERRUPT ENDM CURSOR MACRO_ROW.COLUMN ‘THIS MACRO SETS THE CURSOR LOCATION TO ROW,COLUMN {USING BIOS INT 10H FUNCTION 02 ;SET. CURSOR FUNCTION ;PAGE 00. }ROW POSITION ICOLUMN POSITION. INVOKE THE INTERRUPT MODEL SMALL STACK 64 DATA UIST ALL, "INVOKE CLEAR SCREEN MACRO CURSOR 24 {SET CURSOR TO ROW2.COL 2 DISPLAY MESSAGE1 INVOKE DISPLAY MACRO JXALL sLIST ALL EXECUTABLE CURSOR 7,24 [SET CURSOR TO ROW 7,COL 24 DISPLAY MESSAGE2 INVOKE DISPLAY MACRO SALL SUPPRESS ALL CURSOR 12,44 {SET CURSOR TO ROW 12,COL. 44 DISPLAY MESSAGES. CURSOR 19,64 DISPLAY MESSAGE4 INVOKE DISPLAY MACRO MOV AH.ACH INT 2H :GO BACK TO DOS: MAIN ENDP END MAIN Program 5-1 SECTION 5.1: WHAT IS A MACRO AND HOW IS IT USED? 153 Analysis of Program 5-1 ‘Compare the ".asm" and " Ist” sto see the use of LALL, XALL, and SALL. The LALL directive was used for each macro and then .XALL was used for two of them, From then on, all were suppressed. Microsoft (R) Macro Assembler Version 5.10 4119/92 00:17:15 PROGS-1 Page 1-4 TITLE PROGS- PAGE 60,132 CLEARSCR MACRO THIS MACRO CLEARS THE SCREEN USING OPTION 06 OF INT 10H MOV” AX,0600H ‘SCROLL SCREEN FUNCTION MoV BH.O7 INORMAL ATTRIBUTE Mov CxO [FROM ROW=00, COLUMN=00 MOV OX1@4FH TO ROW=18H.COLUMN=4FH iNT 10H {INVOKE THE INTERRUPT ENOM Display MACRO STRING ‘THIS MACRO DISPLAYS A STRING OF DATA "DX = ADDRESS OF STRING. USES FUNCTION 09 INT 21H, Mov "AH.09 ;DISPLAY STRING FUNCTION MOV DX.OFFSET STRING [OX = OFFSET ADDRESS OF DATA INT 21H INVOKE THE INTERRUPT ENDM ‘cuRsoR: MACRO ROW,COLUMN: STHIS MACRO SETS THE CURSOR LOCATION TO ROW,COLUMN USING BIOS INT 10H FUNCTION 02 MOV AH.02 :SET CURSOR FUNCTION MOV BH.00 PAGE 00 MOV OHROW ROW POSITION MOV OL.COLUMN [COLUMN POSITION INT 10H) “INVOKE THE INTERRUPT ENOM ‘0000 MODEL SMALL, (0000 ‘STACK 64 0000 DATA, 0000 4079206E616D MESSAGE1 DB ‘Myname’, 65 20.24 0009 6973204A6F65 MESSAGE2 DB ‘is Joe's’ o010 $768.61 742069 MESSAGES DB What 6S 73 0019 796F 7572206E © MESSAGE4 D8 —_‘yourname?, 61 6D 65 3F 24 0024 CODE, ‘0000 MAIN PROC FAR ‘0000 88 0000s, MOV" AX.QDATA (0003 8E D8 Mov DS, LALL (UST ALL CLEARSCR INVOKE CLEAR SCREEN MACRO List File for Program 5-1 (continued on next page) 154 CHAPTER 5: MACROS AND THE MOUSE Microsoft (R) Macro Assembler Version 5.10 WISE OO:ATAS PROGS-1 Page 1-2 4 :THIS MACRO CLEARS THE SCREEN 1 005 Be0600 1 MOV AX.0600H SCROLL SCREEN FUNCTION 0008 87 07, 1 MOV BH.O7 NORMAL ATTRIBUTE 00A B90000 1 MOV 0X0 |FROM ROW=00,COLUMN=00 0000 BA184F 1 MOV DX184FH TO ROW=18H,COLUMN=4FH 0010 CD 10 1 INT 10Fi INVOKE THE INTERRUPT CURSOR 2,4:CURSOR MACRO WILL SET CURSOR TO 2, 2 4;THIS MACRO SETS THE CURSOR LOCATION 4 0012 B4 02 1 Mov AH.02 SET CURSOR FUNCTION 0014 B7 00 1 MOv BH.00 PAGE 00 0016 88 02 1 MOV DH.2 ROW POSITION 0018 8204 1 Mov OL,4 COLUMN POSITION 0014 CD10 1 INT 10H INVOKE THE INTERRUPT DISPLAY MESSAGE1 ‘INVOKE DISPLAY MACRO 4 :THIS MACRO DISPLAYS A STRING OF DATA u otc 8409 1 MOV AH,09 DISPLAY STRING FUNCTION OO1E BAOODOR 1 MOV OXOFFSET MESSAGE1 {DX = OFFSET ADDRESS OF DATA 0021 CD21 1 INT 21H INVOKE THE INTERRUPT ALL JUST ALL EXECUTABLE ‘CURSOR 7.24 'SET CURSOR TO ROW=7,COL= 24 0023 B4 02 1 MOV AH.02 ISET CURSOR FUNCTION 0025 87 00 1 MOV BH,00 IPAGE 00 0027 B6 07, 1 Mov 0H,7 [ROW POSITION 0029 8218 1 MOV 01,24 SCOLUMN POSITION. 0028 CD 10 1 INT 10H INVOKE THE INTERRUPT DISPLAY MESSAGE? ‘INVOKE DISPLAY MACRO 0020 84.09 1 MOV AH.09 DISPLAY STRING FUNCTION 002F BAODD9R 1 MOV DX.OFFSET MESSAGE2 _;DX = OFFSET ADDRESS OF DATA 0032 C021 1 INT 21H INVOKE THE INTERRUPT SALL [SUPPRESS ALL CURSOR 12,44 {SET CURSOR TO ROW=12,COL=44 DISPLAY MESSAGES ‘INVOKE DISPLAY MACRO CURSOR 19,64 ‘SET CURSOR TO ROW=19,COL=64 DISPLAY MESSAGES ‘INVOKE DISPLAY MACRO 0056 B4 4c MOV AH.ACH (0058 CD24 INT 21H :GOBACK TO DOS 005A, MAIN ENP. END) MAIN List File for Program 5-1 (continued from preceding page) LOCAL directive and its use in macros In the discussion of macros so far, examples have been chosen that do not have alabel orname in the body of the macro. This is because if'a macro isexpanded more than once in a program and there is a label in the label field of the body of the macro, these labels must be declared as LOCAL. Otherwise, an assembler error would be generated when the same label was encountered in two or more places. ‘The following rules must be observed in the body of the macro: 1. All labels in the label ficld must be declared LOCAL. 2. The LOCAL directive must be right after the MACRO directive. In other words, it must be placed even before comments and the body of the macro; otherwise, the assembler gives an error. 3, The LOCAL directive can be used to declare all names and labelsat once as follows: LOCAL —namett,name2,name3 or one ata time as: LOCAL — namet LOCAL — name2 LOCAL —name3 To clarify these points, look at Example 5-2. SECTION 5.1: WHAT IS A MACRO AND HOW IS IT USED? 155 Example 5-2 Write a macro that multiplies two words by repeated addition, then saves the result, Solu ‘The following macro can be expanded as often as desired in the same program since the label "back" has been declared as LOCAL. MULTIPLY MACRO VALUE1, VALUE2, RESULT LOCAL BACK :. THIS MACRO COMPUTES RESULT = VALUE? X VALUE2 BY REPEATED ADDITION /ALUE1 AND VALUE2 ARE WORD OPERANDS: RESULT IS A DOUBLEWORD MOV BX,VALUE1 — (BX=MULTIPLIER MOV CX\VALUE2 —_:CX=MULTIPLICAND SUB AXAK (CLEAR AX Mov DXAX CLEAR DX BACK ADD AX'8x JAD BX TO AX ADC DX,00 [ADD CARRIES IF THERE IS ONE LOOP BACK ICONTINUE UNTIL CX=0. MOV RESULTAX SAVE THE LOW WORD Mov | RESULT+2,0x [SAVE THE HIGH WORD EN Use the macro definition in Example 5-2 to write a program that multiplies the following: (1) 2000 x 500 (2) 2500 x 500 (3) 300 x 400 TILE _ PROGS2 PAGE 60,132 MULTIPLY MACRO VALUE1, VALUE2, RESULT LOCAL BACK sTHIS MACRO COMPUTES RESULT = VALUE1 X VALUE2 "BY REPEATED ADDITION |SVALUE1 ANO VALUE2 ARE WORD OPERANDS; RESULT IS A DOUBLEWORD MOV) BX.VALUE1— (BX=MULTIPLIER MOV CX\VALUE2 — [CX=MULTIPLICAND. SUB AXAK ICLEAR AX Mov DKAK [CLEAR DX, BACK ADD. AXBX JADD BX TO AX AOC DX.00 JADD CARRIES IF THERE IS ONE LOOP BACK ICONTINUE UNTIL CX=0 MOV RESULTAX ISAVE THE LOW WORD MOV RESULT#2,0X /SAVE THE HIGH WORD “MODEL SMALL ‘STACK 64 (DATA RESULT! DW 2DUP (0) RESULT2 OW — 2 DUP (0) RESULT3 DW — 2DUP (0) MAIN PROC FAR MOV" AX,@DATA MOV DSAK MULTIPLY 2000500, RESULT! INT 21H {GO BACK TO DOs MAIN NOP END MAIN Program 5-2 156 CHAPTER 5: MACROS AND THE MOUSE Notice in Example 5-2 that the "BACK" label is defined as LOCAL right afier the MACRO directive. Defining this anywhere clse causes an error. The use ‘ofa LOCAL directive allows the assembler to define the labels separately cach time it encounters them. The list file below shows that when the macro is expanded for the first time, the list file has "??0000", For the second time it is"??0001", and for the third time it is "??0002" in place of the "BACK" label, indicating that the label "BACK" is local. To clarify this concept, try Example 5-2 without the LOCAL directive to see how the assembler will give an error. Microsoft(R) Macro Assembler Version 5.10, 419/92 00:33:14 PROGS.2 Page 1-1 TITLE PROGS-2 PAGE 60,132 MULTIPLY MACRO VALUE1, VALUE2, RESULT LOCAL BACK "THIS MACRO COMPUTES RESULT = VALUE X VALUE2 BY REPEATED ADDITION VALUE AND VALUE? ARE WORD OPERANDS; RESULT IS A DOUBLE MOV BX,VALUEt "BXSMULTIPLIER MOV CX.VALUE2 'CX=MULTIPLICAND. SUB AK AX SCLEAR AK MOV DXAX :OLEAR DX BACK ADD AX'BX ‘ADD BX TO AK ‘ADC_DX00 JADD CARRIES IF THERE IS ONE LOGr BACK ‘CONTINUE UNTIL CX=0 MOV RESULTAX, ISAVE THE LOW WORD MOV RESULT#2,0X SAVE THE HIGH WORD ENOM ‘0000 MODEL SMALL ‘0000 STACK 64 0040 STSEG ENDS ‘9000 DATA 8000 0002, RESULT! DW — 2DUP (0) 00 J 0004 0002 RESULT2 DW 2 DUP (0) J 0008 0902 RESULTS OW — 2DUP (0) ‘o00c ‘CODE, ‘0000 MAIN PROC FAR ASSUME CS.CDSEGDS:DTSEG,SS:STSEG 9000 B8 00008 MOV AX.@DATA (0003 8E D8 MOV DSAX MULTIPLY 2000,500,RESULT1 9005 BBO7D0 1 MOV -BX.2000 {BX=MULTIPLIER 0008 B90IF4 = 1 Mov CX.500 JCX=MULTIPLICAND 0008 28 CO 1 SUB AXAX ‘CLEAR AX ‘0000 88 DO 4 Mov DXAX ‘CLEAR OX ‘000F 03.3 1 720000: ADD ‘ADD BX TO AX 0011 830200 1 ‘ADC. ‘ADD CARRIES IF THERE IS ONE 0014 E2F9 1 LOOP ‘CONTINUE UNTIL CX=0 0016 A3O000R 1 MOV RESULTI,AX SAVE THE LOWWORD 0019 89160002 1 MOV —RESULT1#2,DX SAVE THE HIGH WORD List File for Program 5-2 (continued on next page) SECTION 5.1: WHAT IS A MACRO AND HOW IS IT USED? 187 Microsot (R) Macro Assembler Version 5.10 0010 BBosca = t 0020 BOOIFS 1 0023 28¢co 1 0027 033 4970001: 0028 8302001 002¢ E2F9 1 002E AS0O4R 1 0031 89 16 0006 R 1 0035 BBOIZe 0038 890190 0038 28 CO 1 0030 88.00 1 003F 03 C3 3770002: 0041 8302001 0044 E2 FO. 1 0046 A30008R 1 0049 89 16000AR 1 0040 B4 4c 004F CD24 0051 MAIN 4113992 00:33:14 Page 12 MULTIPLY 2500,500, RESULT2 MOV BX,2500 :SX=MULTIPUER Mov CX,500 ICXSMULTIPLICAND SUB AKAX ICLEAR AX. ADD AXBX JAD BX TO AX ‘ADC DX.00 [ADD CARRIES IF THERE IS ONE Loop 776001 SCONTINUE UNTIL CX=0 MOV RESULT2AX SAVE THE LOW WORD. MOV. RESULT2¥2.0X ‘SAVE THE HIGH WORD MULTIPLY 300,400,RESULTS MOV BX,200 YBX=MULTIPLIER Mov ¢x,400 ‘CS=MULTIPLICAND SUB AX/AX (CLEAR AX MOV DXAX ‘CLEAR DX ADD AX'BK ‘ADD BXTO AX ADC DX.00 JADD CARRIES IF THERE IS ONE Loop 776002 ICONTINUE UNTIL CX=0) MOV RESULT3,AX SAVE THE LOW WORD MOV — RESULT3#2,.DX ‘SAVE THE HIGH WORD, MOV AH.ACH INT 2tH {GO BACK TO DOS ENDP END MAIN List File for Program 5-2 (continued from preceding pege) INCLUDE directive Assume that there are several macros that are used in every program. Must they be rewritten every time? The answer is no if the concept of the INCLUDE directive is known. The INCLUDE directive allows a programmer to write macros and save them ina file, and later bring them into any file. For example, assume that the following widely used macros were written and then saved under the filename "MYMACROI.MAC". CLEARSCR DISPLAY REGSAVE, MACRO. ithe clear screen macro Mov Mov Mov MoV INT ENOM ‘AX,0600H BH,07 ex,0000 DX\184FH. 10H MACRO STRING —_;the string display macro Mov Mov INT ENDM ‘AH,O9 DX,OFFSET STRING 24H MACRO, ithis macro saves all the registers PUSH AX PUSH 8X PUSH CX PUSH DX PUSH DI PUSH SI PUSH 8P PUSHF ENDM 158 CHAPTER 5: MACROS AND THE MOUSE REGRESTO MACRO :this macro restores all the registers POPF POP BP POP SI POP DI POP DX POP cx POP BX POP AX ENDM ‘Assuming that these macros are saved on a disk under the filename "MY- MACROLMAC", the INCLUDE directive can be used to bring this file into any ™ asm’ file and then the program can call upon any of the macros as many times as needed. When a file includes all macros, the macros are listed at the beginning of the "Ist" file and as they are expanded, they will be part of the program. To understand this, see the following program Program 5-3 includes macros to clear the screen, set the cursor, and display strings, These macros are all saved under the "MYMACRO2 MAC" filename. The "asm" and "Ist" versions of the program that use the clear screen and display string ‘macros only to display "This isa test of macro concepts" are shown on the following pages. Notice that in the list file of Program 5-3, the letter "C" in front of the lines indicates that they are copied from another file and included in the present file. TLE —_ PROGS-3 PAGE 60,192 INCLUDE MYMACRO2 MAC MODEL SMALL. STACK 64 DATA MESSAGE1_DB_ This is atest of macro concepts, CODE MAIN PROC FAR MOV” AX@DATA Moy Os CLEARSCE INVOKE CLEAR SCREEN MACRO DISPLAY MESSAGE1 INVOKE DISPLAY MACRO MoV AHACH INT 21H {60 BACK TO DOS MAIN ENDP END MAIN Program 5-3 Review Questions 1 Discuss the benefits of macro programming, 2. List the three parts of a macro, 3, Explain and contrast the macro definition, invoking the macro, and expanding the macro. 4, True or false. A label defined within a macro is automatically understood by the as- sembler to be local ‘True or false. In the list file for Program 5-3, the "C" at the beginning of a line indi- cates that it is a comment. SECTION 5.1: WHAT IS A MACRO‘AND HOW IS IT USED? 159 Microsoft (R) Macro Assembler Version 5.10 PROGS.3 413/92 00.41:48 Page 1-4 TITLE _ PROGS.3 PAGE 60,132 INCLUDE _MYMACRO2.MAC MYMACRO2 (MAC) FOR PROGRAMS.3 CURSOR MACRO ROW.COLUMN ,THIS MACRO SETS THE CURSOR LOCATION AT ROW,COLUMN, JSING BIOS INT 10H FUNCTION 02 MOV AH.02 MOV BH.00 MOV DH, MOV DL,COLUMN, )SET CURSOR FUNCTION PAGE 00) ROW POSITION COLUMN POSITION INT. 10H ENDM INVOKE THE INTERRUPT DISPLAY MACRO STRING ‘THIS MACRO DISPLAYS A STRING OF DATA X= ADDRESS OF STRING. USES FUNCTION 09 INT 21H, DISPLAY STRING FUNCTION DX = OFFSET ADDRESS OF DATA INVOKE THE INTERRUPT MOV DX.OFFSET STRING INT. 21 ENOM CLEARSCR MACRO. sTHIS MACRO CLEARS THE SCREEN EUSING OPTION 06 OF INT 10H MOV AX.GOOH {SCROLL SCREEN FUNCTION. MOV BH,O7 MOV Cx.0 MOV OX:184FH INT OH ENOM ¢ é c ¢ ¢ ¢ c c c ¢ c c ¢ ¢ "MOV AH.Og ¢ ¢ c ¢ c ¢ c ¢ é ¢ ¢ c ¢ ¢é ;NORMAL ATTRIBUTE. {FROM ROW-00,COLUMN=00 (TO ROW=18H.COLUMN=4FH {INVOKE THE INTERRUPT MODEL SMALL. ‘STACK 64 DATA MESSAGE1 0B "This isa test of macro concepts ‘0000 ‘0000 (0000 88 0000s 0003 8E D8 0005 88 0600 CODE, MAIN” PROC FAR MOV AX.@DATA MOV __DS,AK CLEARSCR” INVOKE CLEAR SCREEN MACRO MOX”AX,0600H List File for Program 5-3 (continued on next page) 160 CHAPTER 5: MACROS AND THE MOUSE Microsoft (R) Macro Assembler Version 5.10 TASRZ 0044S PROGS-3 Page 12 0008 87 07 1 Mov BH.07 NORMAL ATTRIBUTE 000A 8900001 MOV Cx.0 [FROM ROW=00, COLUMN=00 0000 BA184F 1 MOV DX,184FH 'TO ROW=18H,COLUMN=4FH 0010 CD10 1 INT 10H INVOKE THE INTERRUPT DISPLAY MESSAGE1 INVOKE DISPLAY MACRO, 0012 8409 1 MOV AK.09 DISPLAY STRING FUNCTION 0014 BADOOOR «1 MOV DX.OFFSET MESSAGE1 ,DX =OFFSET ADDRESS OF DATA 0017 CD24 1 INT. 21H INVOKE THE INTERRUPT 0019 84 4c MOV AH.ACH 0018 CD24 INT. 21H :GO BACK TO DOS 0010 MAIN. ENDP END MAIN List File for Program 5-3 (continued from preceding page) SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H Next to the keyboard, the mouse is one of the most widely used input devices. This section describes how to use INT 33H to add mouse capabilities to programs. INT 33H ‘The original IBM PC and DOS did not provide support for the mouse. For this reason, mouse interrupt INT 33H is not part of BIOS or DOS. Thisis in contrast to INT 21H and INT 10H, which are the DOS and BIOS interrupts, respectively, INT 33His part of the mousc driver software that is installed when the PC is booted. Detecting the presence of a mouse ‘While new PCs come with a mouse and driver already installed by the PC manufacturer, many older-generation PCs in use do not have a mouse. Therefore, the first task of any INT 33H program should be to verify the presence of a mouse and the number of buttons it supports. This is the purpose of INT 33H function ‘AX=0. Upon retam from INT 33H, if AX=0 then no mouse is supported. If AX-FFFFH, the mouse is supported and the number of mouse buttons will be ‘contained in register BX. Although most mice have two buttons, right and left, there are some with middle buttons as well. See the following code. MOV AX0 mouse initialization option INT 33H CMP AX.0 check AX contents upon return from INT 33H JE EXIT ‘exit if AX=0 since no mouse available MOV M_BUTTON,BX ;mouse is there, save the number of buttons, exit: Notice the following points about the way INT 33H is called. 1.In INT 21H and INT 10H, the AH register is used to select the functions. This is not the case in INT 33H, In INT 33H the register AL is used to select various functions and AHis set to 0. That is the reason behind the instruction "MOV AX.0". 2. Do not forget the "H" indicating hex in coding INT 33H. In the absence of the " the compiler assumes it is decimal and will execute DOS INT 21H since 33 decimal is equal to 21H. SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H 161 ‘Some mouse terminology Before further discussion of INT 33H, some terminology conceming the mouse needs to be clarified. The mouse pointer (ot cursor) is the pointer on the screen indicating where the mouse is pointing at a given time. In graphics mode, the mouse pointer (cursor) isan arrow; in text mode, the mouse pointer is a flashing block. Ineither mode, as the mouse is moved, the mouse cursor is also moved. While the movement of the mouse is measured in inches (or centimeters), the movement of the mouse cursor (arrowhead) on the screen is measured in units called mickeys. Mickey units indicate mouse sensitivity. For example, a mouse that can move the Cursor 200 units for every inch of mouse movement has a sensitivity of 200 mickeys. In this case, one mickey represents 1/200 of an inch on the screen. Some mice have a sensitivity of 400 mickeys in contrast to the commonly used 200 mickeys. In that case, for every inch of mouse movement, the mouse cursor moves 400 mickeys. Displaying and hiding the mouse cursor ‘The AX=01 function of INT 33H is used to display the mouse cursor. Mov AXO1 iNT 33H Afier executing the above code, the mouse pointer is displayed. If the video mode is graphics, thc mouse arrow becomes visible. If the video mode is text, a rectangular block representing the mouse cursor becomes visible, In text mode, the color of the mouse cursor block is the opposite of the background color in order to be visible. It is best to hide the mouse cursor after making it visible by executing option AX=02 of INT 33H. This is shown in Example 5-3, Try Example 5-3 in DEBUG (remember to omit the "H" and place INT 3 as the last instruction when in DEBUG). Then try it with mode AH=03 for INT 10H to see the mouse cursor in text mode, Example 5-3, (a) Use INT 10H option OF to get the current video mode and save it in BL; (b) set the video mode to VGA graphics using option AH=10H of INT 10H; (c) initialize the mouse with AX=0, INT 33H; (d) make the mouse visible; (e) use INT 21H option AH=01 to wait for key press; (f) if any key is pressed restore the original video mode. Solution: MOV AHOFH :get the current video mode INT 10H MOV BL.AL sand save it MOV AHO sset the video mode MOV AL,10H 0 VGA graphics INT 10H MOV AX0 sinitialize the mouse INT 33H MOV AXQ1 smake the mouse cursor visible INT 33H MOV AHI wait for key press INT 21H MOV AX2 when any key is pressed INT 33H smake mouse invisible MOV AHO MOV ALBL sand restore original video mode INT 10H 162 CHAPTER 5: MACROS AND THE MOUSE Video resolution vs. mouse resolution in text mode As discussed in Chapter 4, the video screen is divided into 640x200 pixels in text mode. This means that in text mode of 80x25 characters, each character will use 8x8 pixels (80 x 8 = 640 and 25 x 8 = 200), When the video mode is set to text mode (AH=03 of INT 10H), the mouse will automatically adopt the same resolution ‘0640x200 for its horizontal and vertical coordinates, Therefore, in text mode when 1 program gets the mouse cursor position, the values are provided in pixels and must be divided by 8 to get the mouse cursor position in terms of character locations 0 to 79 (horizontal) and 0 to 24 (vertical) on the screen. Video resolution vs. mouse resolution in graphics mode In graphics, resolution is not only 640x200 but also 640x350 and 640x480. ‘When the video resolution is changed to these video modes, the mouse also adopts the graphics resolutions. See Table 5-1 ‘Table 5-1: Video and Mouse Resolution for Some Video Modes Video Mode |Video Resolution, ‘Type __ Mouse Resolution | Characters per Screen _| : | ——;. -640x200___ Text__ 640x200 i 80x25 _ =0FH | 640x200 _|_ Graphics 640x200, 80x25 640x350 Graphics_| __640x350_ 80x44 640x350 | Graphics | ___640x350__| 80x44 640x480 __|_ Graphics 640x480, 80x60 Hu 640x480 _|_Graphics 640x480 | 80x60_ i Getting the current mouse cursor position (AX=03) Option AX=03 of INT 33H gets the current position of the mouse cursor. Upon return, the X and Y coordinates are in registers CX (horizontal) and DX (vertical), BX contains the button status as follows: D0 left button status, D1 right button status, D2= center button status. The status is | if down, 0 if up. Notice that the cursor position provided by this function is given in pixels. For example, the position retumed will be in the range of 0 - 639 (horizontal) and 0 - 199 (vertical) for a 640x200 screen in most text and graphics video modes. However, the mouse cursor position is often needed in terms of character positions such 2s 80x25 and not in terms of pixels. To get the mouse cursor character position, divide both the horizontal and vertical values of CX and DX by 8. See Programs 5-4 and 5-5. TITLE PROGRAM 5.4: DISPLAYING MOUSE POSITION :Performs the following tasks: (a) gets the current video mode and saves it, (b) sets the mode to a new video mode, (e) gets the mouse pointer position, converts itto character position and displays it continuously unless a “key is pressed, (d) upon pressing any key, it restores the original video mode and exits to DOS. PAGE "60,132 CURSOR “MACRO ROW,COLUMN ‘MOV AH.O2H Mov BH.00 Mov DHROW Mov DL,COLUMN INT 10H ENDM DISPLAY MACRO STRING MOV AH.O9H MOV DXOFFSETSTRING _ cad string address INT 21H ENOM Program 5-4 continued on the next page) SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H. 163 MODEL SMALL ‘STACK DATA’ MESSAGE_1 DB ‘PRESS ANY KEY TO GET OUT’’$ MESSAGE_2 DB THE MOUSE CURSOR IS LOCATED AT POS_HO. 0B 7,2,"AND$° POS_VE DB 27's" OLDVIDEO = OB. seurrent video mode NEWVIDEO DB OEH new video mode CODE, MAIN PROC MOV” AX,@DATA MOV DS/AX MOV AHOFH ‘get current video mode INT 104, MOV OLDVIDEO,AL ‘save it MOV AX,0600H jelear screen MOV BH.O7 Mov oxo MOV DX.184FH INT 10H MOV AH.OOH ‘set new video mode MOV AL.NEW VIDEO INT 40H MOV AX. + Initialize mouse iNT 33H Mov AX01 :show mouse cursor INT 33H. CURSOR 20,20 DISPLAY MESSAGE_1 AGAIN: MOV AX,O3H get mouse location INT 33H. MOV AX.cx ‘get the hor. pixel position CALL CONVERT {onvert to displayable data MOV POS _HO,AL ‘save the LSD MOV POS_HO*1,AH save the MSD MOV AX,DK sget the vert. pixel position CALL CONVERT Jeonvert MOV POS_VE,AL ‘save MOV POS_VE+1,AH CURSOR 5,20 fl DISPLAY MESSAGE 2; DISPLAY POS_HO DISPLAY POS-VE MOV AHOT check for key press INT 16H JZ AGAIN iifno key press, keep monitoring mouse position Mov AH,02 ‘hide mouse INT 33H Mov AHO store original video mode MOV ALOLDVIDEO 5Noad original video mode INT 104 MOV AH.ACH 90 back to DOS INT 2H MAIN ENDP. idivide pixels position by 8 and convert to ASCII to make it displayable ‘ax=pixels position (itis in hex) son return ax= two ASCII digts CONVERT PROC SHR AX, ‘divide SHR AX,1 ‘by 8 SHR AX 510 get screen position by character Mov BL,10 SUB AHAH DV BL :divde by ten to conver from hex. to decimal OR AX,3030H make it ASCII RET Jreturn with AX=two ASCII digits CONVERT ENOP END MAIN Program 5-4 (continued from preceding page) 164 CHAPTER 5: MACROS AND THE MOUSE TITLE PROGRAM 5-5: MOUSE BOX PROGRAM ‘on whether the mouse is clicked inside or outside the box Pressing any key will return to DOS. “Thanks to Travis Erck and Gary Hudson for their input on this program CURSOR MACRO ROW.COLUMN MOV” AH.O2H Mov BH400 Mov DHROW MOV DL.COLUMN INT 0H ENDM DISPLAY MACRO STRING MOV AHO3H MOV DKOFFSETSTRING _ jJoad string address INT 21H ENDM FILL MACRO ROW. START,COL_STARTROW_END,COL_END,COLOR LOCAL STARTAGAIN MOV) DX ROW START START. MOV CXCOL START AGAIN; MOV AH.OCH MOV AL,COLOR INT 10H INC Cx’ CMP CX.cOL_END JNE AGAIN INC Ox CMP DXROW_END INE START ENOM MODEL SMALL ‘STACK DATA MESSAGE_1 — DB'AN EXAMPLE OF HOW TO USE INTERRUPT 33H FOR MOUSE. MESSAGE-2 DB "IT WORKSI''S’ MESSAGE-3 DB CLICK IN THE BOX TO SEE WHAT HAPPENS!'’$° MESSAGE_4 DB 'No, NO, NOI SAID IN THE BOX!" $" MESSAGES DB NOW PRESS ANY KEY TO GET BACK TO DOS. $' OLDVIDES” = (0B? NEWVIDEO DB 12H CODE MAIN PRoc MOV AX,@DATA Mov DSAX MOV AHOFH get the current video mode INT 10H MOV OLDVIDEOAL —jsaveit MOV AX,0600H ‘elear screen Mov BH.O7 Mov CX0 Mov DX,164FH INT 10H Mov AH.00H sset new video mode MOV ALNEWIDEO INT 104 CURSOR O.0 FILL 10,280,250,350,4 draw rad box ‘CURSOR 1,1 DISPLAY MESSAGE_1; CURSOR 5,22 DISPLAY MESSAGE. 3; MOV AX,0000H Jnttialize mouse INT 33H Mov AX0tH INT3 3H! :show mouse cursor ;Performs the following: (a) gets the current video mode and saves it, (b) sets the video mode to a new one and ‘clears screen, (c) draws a colored box and gets the mouse position, (6) displays different messages depending Program 5-5 (continued on next page) SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H, 165 BACK NOT_INSIDE: EXIT: MAIN MOV AX 03 ‘check for mouse bution press INT 33H snow CX =COL and DX=ROW location CMP BX,0001H ‘check to s9e if left button is pressed JNE, BACK 3fnot keep checking CMP Cx250 "See if on right side of box. JB NOT_INSIDE _‘ifless it must be outside box CMP CX.350 ‘See if on left side of box JA NOT_INSIDE _“ifnot then itis outsido the box CMP Ox.150 ‘check for the top of the box JB NOT_INSIDE —_“ifnot then outside the box CMP DX.250 :See if bottom of the box JA” ___NOT_INSIDE CURSOR 18,78 ‘then it must be inside box DISPLAY MESSAGE_2 _ indicate mouse is inside the box JMP EXIT 'g0 prepare to exit to DOS, ‘cuRsoR 20,18 {indicate mouse is not inside box DISPLAY MESSAGE_4 MOV" AH.O2H ‘hide mouse before exiting to DOS INT 33H CURSOR 22,18 DISPLAY MESSAGE_5 Mov” AHO7 ‘wail for a key press INT 21H Mov AHO restore original video mode MOV AL,OLDVIDEO INT 10H MOV AH,4CH ‘exit INT 21H to DOS, ENDP END Program 5-5 (continued from preceding page) Setting the mouse pointer position (AX=04) This function allows a program to set the mouse pointer to a new location. anywhere on the screen. Before calling this function, the coordinates for the new location must be placed in registers CX for the horizontal (x coordinate) and DX for the vertical (y coordinate). These values must be in pixels in the range of 0 - 639 and 0 - 199 for 640x200 resolution. Coordinate (0,0) is the upper left comer of the sereen, For example, to set the mouse cursor at location 9x5 (on a 80x25 screen), simply multiply both by 8 to get the pixel location. Therefore, a character coordinate of 9x5 becomes 72440 in pixel coordinates. Getting mouse button press information (AX=05) This function is used to get information about specific button presses since the last call to this function, It is set up as follows. AX=05 BX=0 for left button; 1 for right button; 2 for center bution Upon return: AX=button status where DO= Lett button, if 1 it is down and if 0 itis up D1 Right button, if 1 it is down and if 0 itis up D2= Center button, if 1 it is down and if 0 it is up BX=button press count coordinate at the last button press in pixels (horizontal) /-coordinate at the last button press in pixels (vertical) Notice in function AX=05 that upon returning from INT 33H, register AX thas the button status (up or down), while register BX has the number of times the specific button is pressed since the last call to this function. Program 5-6 shows one way to use this function. 166 CHAPTER 5: MACROS AND THE MOUSE Monitoring and displaying the button press count program Program 5-6 uses the AX=05 function to monitor the number of times the left button is pressed and then displays the count. It prompts the user to press the left button a number of times. When the user is ready to see how many times the button was pressed, any key can be pressed. TITLE PROGRAM 5-6: DISPLAY MOUSE PRESS COUNT THIS PROGRAM WAITS FOR THE MOUSE PRESS COUNT AND DISPLAYS IT WHEN ANY KEY |S [PRESS ANY PRESSED. KEY TO GO BACK TO DOS PAGE 60,132, CURSOR DISPLAY MESSAGE_1 MACRO ROW.COLUMN Mov AH.O2H Mov BH00 MOV DH.ROW Mov DL,COLUMN INT 10H ENDM MACRO STRING MOV) AH.O9H MOV —DX.OFFSET STRING ;LOAD STRING ADDRESS INT 21H ENOM MODEL SMALL, ‘STACK DATA, DB ‘PRESS LEFT BUTTON ANUMBER OF TIMES.LESS THAN 99." MESSAGE_2 DB TO FIND OUT HOW MANY TIMES, PRESS ANY KEY’ S' MESSAGE-3 DB "YOU PRESSED IT",S° P COUNT 08 ?,?," TIMES S" MESSAGE_4 DB ‘NOW PRESS ANY KEY TO GO BACK TO 00S’ OLDVIDEG =—B?, urrent video mode NEWVIDEO DB 12H —_inew video mode CODE MAIN PROC MOV’ AX.@DATA MOV DSAK MOV AH.OFH :get current video mode INT 10H MOV OLDVIDEO AL ssaveit MOV AX.0600H ‘clear screen Mov BH.07 MOV CxO MOV DX,184FH INT 104 MOV AH.OOH :set new video mode MOV ALNEVWIDEO INT 10H MOV AXO jinitiaize mouse INT 334 MOV AX01 show mouse cursor INT 33H CURSOR 2,1 DISPLAY MESSAGE_1 CURSOR 4,1 DISPLAY MESSAGE _2 MOV AH.O7 wait for key press INT 21H MOV AX.05H get mouse press count Mov 8X0 ‘Check press count for left bution WNT 33H Program 5-6 (continued on next page) SECTION 5.2: : MOUSE PROGRAMMING WITH INTERRUPT 33H 167 MAIN Mov AXBx 1BX=buton press count Mov BL'10 Dv BL OR AX,3030H soonvert ito ASCII MOV P_COUNTAL ‘save the number MOV P-COUNT#.AH ‘CURSOR 10,2 DISPLAY MESSAGE_3 DISPLAY P_COUNT CURSOR 20,2 DISPLAY MESSAGE_4 MOV” AHO? ‘wait for a key press to get out WT 21H Mov AH.02 thide mouse INT 33H Mov AHO xrestore original video mode MOV ALOLDVIDEO ‘load original vide mode INT 40H MOV AH.4CH igo back to DOS. INT 21H ENDP END MAIN Program 5-6 (continued from preceding page) Getting mouse button release information (AX=06) This funetion is the same as AX= about the button release. 5 except that it provides information Call with: AX BX Upon return: ‘AX = button status where DO = Left button, if 1 itis down and if 0 itis up D1 = Right button, if 1 itis down and if@ itis up D2 = Center button, if1 itis down and if 0 itis up BX = button release count ‘CX = x coordinate at the last button release in pixels (horizontal) DX = y coordinate at the last button release in pixels (vertical) Setting horizontal boundary for mouse pointer (AX=07) When the mouse is initialized using the AX=0 function, the mouse cursor can move anywhere on the screen. This function and the following one set the x and Y coordinate boundaries in which mouse movement is confined. Call as follows, =08 0 for left button; 1 for right button; 2 for center button AX = 07 CX = minimum x coordinate in pixels (0 - 639) DX = maximum x coordinate in pixels (0 - 639) ‘Notice that CX must be less than DX; otherwise, they are swapped, Setting vertical boundary for mouse pointer (AX=08) This and the previous function allow a program to set the x and y coordinates in which the mouse movement is confined AX = 08 CX = minimum y coordinate in pixels (0 - 199) DX = maximum y coordinate in pixels (0 - 199) Notice that CX must be less than DX; otherwise, they are swapped. 168 CHAPTER 5: MACROS AND THE MOUSE Setting an exclusion (off-limits) area for the mouse pointer (AX=10H) This function is used to set an area that is off-limits for the mouse cursor. Ifthe mouse cursor moves fo the exclusion area, it disappears. Call with: AX = 10H. CX = upper x coordinate, SI = lower x coordinate OX = upper y coordinate, DI = lower y coordinate Getting mouse driver information (version) (AX=24H) This function allows a program to determine which version of the mouse driver is installed. It also indicates the type of mouse, such as an internal bus mouse ‘or an external mouse via a serial port type. Call with: AX = 24H Upon return we have: BH = major version BL = minor version (e.g., "7.20" 7 is major, 20 is minor) CH = mouse type where 1 = bus mouse, 2 = serial mouse, 3 = InPort mouse, 4= PS/2 mouse, 5 = HP mouse CL=0 if mouse is PS/2 type. If tis a serial mouse, the number is between 2 and 7, indicating the IRQ number being used (such as IRQ2, IRQS, etc). Program 5-7 gives an example of a C program using the mouse. Interrupt 33H is called using the int86 C function to see if there is a mouse present. If there is a mouse present, the program will print the number of buttons on the mouse. T/This program uses INT 33H to see if mouse driver is installed and if so it displays {ithe number of buttons on the mouse finclude #include #include main() {ion REGS Heo: elrser(); fix.ax=Ox0; Iicheck to see if there is mouse int86(0x33,8ri,8r0); iffrox.acd) printf"\ninYou have %d button(s) on the mouse on this PC",ro.x.bx); else print{("This PC has no mousein"); Program 5-7 Review Questions 1, Which function of INT 33H is used to detect the presence of a mouse in a PC? In which register do we expect to get that information? In which register do we find the number of buttons in the mouse? 2. The following code is an attempt to call INT 33H function 2. Is it correct? If not, correct it MOV AH,02 INT 33H, 3. Why do we need to save the original video mode before changing it for the mouse? 4. In INT 33H function AX=03, how can a left button press be detected? 5. True or false. The mouse coordinate is the same as video mode, SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H 169 SUMMARY Macros are used by programmers to save time in coding and debugging. Whenever a set of instructions must be performed repeatedly, these become ideal candidates for a macro, Values can be passed to macros to be used by instructions within the macro. Programmers can place several ofien-used macros within a file that can be brought into one or more programs, In recent years, the mouse has become a standard input device on the IBM PC. The original IBM PC BIOS and DOS did not provide for the mouse. INT 33H is part of the mouse driver software that is installed when the PC is booted. Function calls to INT 33H are used for mouse input and cursor control PROBLEMS 1. Every macro must start with directive and end with directive 2. Ideniify the name, body, and dummy argument in the following macro: WORK_HOUR MACRO OVRTME_HR MOV AL,40 ;WEEKLY HRS, ADD ALOVRTME_HR —STOTALHRS WORKED ENDM 3. Explain the difference between the SALL, LALL, and XALL directives. 4, Whatiis the total value in registers DX and AX after invoking the following macro? WAGES MACRO SALARY,OVERTIME,BONUSES sTOTAL WAGES=SALARY + OVERTIME + BONUSES SUB AX,AX :CLEAR MOV DX,AX sAX AND DX ADD AX,SALARY ADD AX,OVERTIME ADC DX,0 STAKE CARE OF CARRY ADD AX,BONUSES ADC DX,0 ENDM The macro is invoked as WAGES. 60000,25000,3000 5. In Problem 4, in the body of the macro, dummies were used as they are listed from left to right. “Can they be used in any order? Rewrite the body (leave the dum- mies alone) by adding OVERTIME first 6. In Problem 4, state the comments that are listed if the macro is expanded as: LALL WAGES XYZ 7. Macros can use registers as dummies. Show the "st" file and explain what the macro in Problem 4 does if it is invoked as follows: WAGES BX,CX,SI 8. Fill in the blanks for the following macro to add an array of bytes. Some blanks might not need to be filled, SUMMING MACRO COUNT, VALUES. LOCAL ssthis macro adds an array of byte size elements. ax will hold the total sum MOV CX.... ssize of array MOV SLOFFSET..... jload offset address of array SUB AX,AX selear ax AGAIN: ADD ALST] ADC AHO sadd bytes and takes care of carries INC SI spoint to next byte LOOP AGAIN continue until finished ENDM .. 170 CHAPTER 5: MACROS AND THE MOUSE 10, ul 13. 14, 16. 17. 19. a 22, 23, 24, Invoke and run the macro above for the following data, In the data segment: DATAL DB_ 89,75,98,91,65 SUML DW? DATA2 DB _ 86,69,99,14,93,99,54,39 SUM2 Dw? DATA3 DB_ 10 DUP (99) SUM3 Dw? (Hint: For the format, see Problem 10.) Insert the listing directives in Problem 9 as follows and analyze the "Ist" file. From the code segment: LALL SUMMING 5,DATAI vadding and saving data XALL SUMMING. sadding and saving data2 SALL sadding and saving data3 Rewrite Problem 8 to have a third dummy argument for SUM. Then rework Prob- Jem 9. Rewrite Program 5-2 using the DD directive for RESULT1, RESULT2, and RE- SULTS. Using INT 33H, write and test an Assembly language program to check the pres- ence of a mouse in a PC. Ifa mouse driver is installed, it should state the number of buttons it supports. If no mouse driver is installed, it should state this. Change the video mode of Program 5-4 to the following and verify the data given in Table 5-1 (a) 1OH (b) 12H Change the size and color of the box to the coordinates and color of your choice in Program 5-5, then run and test it. Make the box large enough to display your first name followed by your last name inside it Modify Program 5-6 to indicate the number of times the right button is pressed Repeat the above problem to indicate the number of times the right button is re- leased. Write an Assembly language program using INT 33H to display the mouse driver version and number of buttons supported by the mouse in a given PC. Note: The following are additional problem assignments for the mouse in C. Write a C language version of Program 5-4. Your program performs the following. (a) clears the screen {b) saves the video mode and changes it to mode AL=OBH (640x200) (0) gets the mouse pointer position continuously and displays it in the 80x25 character screen coordinate (@ upon a pressing any key, it restores the original video mode and exits to DOS Repeat the above problem. This time display the mouse pointer position in pixels. Test it for video modes AL=10H and AL=12H, Write a C language program to state (display) which mouse button is being pressed at any given time. Upon pressing any key, the program goes back to DOS, Write a C program version of Program 5-6 that performs the following: (a) save the video mode and change it to one of the graphics modes (b) ask the user to press the left button a number of times (©) display the number when a key is pressed (@) upon pressing any Key, restore the original video mode and go back to DOS Repeat the above problem for the right button. Using INT 33H functions AX-0 and AX~24H, write a C program to display the following: (a) the number of mouse buttons (b) the mouse type (bus mouse, serial mouse, PS/2 mouse, ete.) (c) mouse driver version in a form such as "7.5" (@) the IRQ number, if used by the mouse PROBLEMS 171 ANSWERS TO REVIEW QUESTIONS SECTION 5.1: WHAT IS AMACRO AND HOW IS IT USED? 4. Macro programming can save the programmer time by allowing a set of frequently repeated instructions to be invoked within the program by a single line, This can also make the code easier to read. 2. The three paris of @ macro are the MACRO directive, the body, and the ENDM directive 3. The macre defniton is thelist of siatoments the macro wil perform. It begins with the MACRO directive and ‘ends with the ENDM directive Invoking the macro is when the macro is called from within an assembly language rogram. Expanding the macro is when the assembly replaces the line invoking the macro withthe assembly Fanguage code in the body of the macro, 4, False. Alabel thats to be local to a macro must be declared local with the LOCAL directive. 5. False, The °C al the begining of ain indicates thet sine of code was brought in rom anther fe byte INCLUDE directive ‘SECTION 5.2: MOUSE PROGRAMMING WITH INTERRUPT 33H (=0; reg AX; number of buttons in reg. BX 2. tis wrong. Register AL=02 and AH=0. MOV ax.02 INT 33H 3. In the absence of doing that, when we go back to DOS we lose our cursor if the mouse program has changed the video to graphics. 4: We check the conti of agiter BX for value OY ue 172 CHAPTER 5: MACROS AND THE MOUSE CHAPTER 6 SIGNED NUMBERS, STRINGS, AND TABLES 173 Inthe first section ofthis chapter we focus on the concept of signed numbers in software engineering, Signed number operations are explained along with ‘examples. In the second section we discuss string operations and table processing. SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS All data items used so far have been unsigned numbers, meaning that the entire 8-bit or 16-bit operand was used for the magnitude, Many applications require signed data. In this section the concept of signed numbers is discussed along with related instructions. Concept of signed numbers in computers In everyday life, numbers are used that could be positive or negative. For example, a temperature of 5 degrees below zero can be represented as —5, and 20 degrees above zeroas +20, Computers must be able to accommodate such numbers, To do that, computer scientists have devised the following arrangement for the representation of signed positive and negative numbers: The most significant bit (MSB) is set aside for the sign (+ or -) and the rest of the bits are used for the magnitude. The sign is represented by 0 for positive (+) numbers and I for negative (C) numbers. Signed byte and word representations are discussed below. Signed byte operands 07 | 06 | Ds | ba | bs | 2 [bt | bo sign magnitude, In signed byte operands, D7 (MSB) is the sign and DO to Dé are set aside for the magnitude of the number. If D7 = 0, the operand is positive, and if D7 = 1, itis negative. Positive numbers ‘The range of positive numbers that can be represented by the format above is 0 to +127. © — 0000 oo00 +1 0000 0001 +5 0000 102 4127 Ifa positive number is larger than +127, a word-sized operand must be used. ‘Word operands are discussed later. Nogative numbers For negative numbers D7 is 1, but the magnitude is represented in 2's complement, Although the assembler does the conversion, itis still important to understand how the conversion works. To convert to negative number repre- sentation (2s complement), follow these steps: 1, Write the magnitude of the number in 8-bit binary (no sign). 2. Invert each bit. 3. Add I tot. Examples 6-1, 6-2, and 6-3 demonstrate these three steps, 174 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES Example 6-1 ‘Show how the computer would represent 5. Solution: 1, 0000 010i 5 in 8-bit binary 2, 1211 1010 _ invert each bit 3.1111 1011 add1 (hex = FBH) This is the signed number representation in 2’s complement for -5. Example 6-2 ‘Show —34H as it is represented internally. Solution: 1. o011 0100 2, 1100 1011 3.1100 1100 (which is CCH) Example 6-3 ‘Show the representation for ~128;9 Solution: 1. 1000 0000 2.0111 1121 3. 1000 0000 Notice that this is not negative zero (-0). From the examples above it is clear that the range of byte-sized negative numbers is—1 to-128. The following lists byte-sized signed numbers ranges: Binary Hex 1000 0000 80 1000 0001 a1 1000 0010 82 2 aiia 1110 FE a qian 1a1 FF oO 0000 0000 00 4 0000 0001 o1 +2 0000 0010 02 +i27 iii iii ie Word-sized signed numbers Gs GG Gla aa GG sign Pe | In 80x86 computers a word is 16 bits in length. Setting aside the MSB (D15) for the sign leaves a total of 15 bits (D14 - D0) for the magnitude, This gives a range of -32768 to +32767. If a number is larger than this, it must be treated as, a multiword operand and be processed chunk by chunk the same way as unsigned numbers (as discussed in Chapter 3). The following shows the range of signed word operands, To convert a negative to its word operand representation, the three steps discussed in negative byte operands are used. SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 175 Decimal Binary zt Hex ~32 768 1000 0000 0000 0000 8000 -32 767 1000 0000 0000 0002 g002 32 766 1000 0000 0000 0010 g002 2 3111 1111 1211 1110 FFE a 3222 1112 1211 1111 FREE ° 9000 0000 0000 0000 0000 se 001, 0001 42 0010 432 766 dito 432 767 2111 ‘EFF ‘Overflow problem in signed number operations ‘When using signed numbers, a serious problem arises that must be dealt with. This is the overflow problem. The CPU indicates the existence of the problem by raising the OF (overflow) flag, but itis up to the programmer to take care of it ‘The CPU understands only 0s and 1s and ignores the human convention of positive and negative numbers, Now what is an overflow? If the result of an operation on signed numbers is too large for the register, an overflow has occurred and the programmer must be notified. Look at Example 6-4 Example 6-4 Look at the following code and data segments: DATAL DATA2 + 96 +10 +166, DB +96 DB +70, ov” ALDATAL ;AL=0110 0000 (AL=60H) MOV BL;DATAZ 'BL=0100 0110 (BL=46H) ADD ALBL ‘AL=1010 0110 (AL=AGH= -90 invalid!) 0110 0000 01000110 1010 0110 According to the CPU, this is -90, which is wrong. (OF = 1, SF = 1, Cl 9) In the example above, +96 is added to+70 and the result according to the CPU was —90. Why? The reason is that the result was more than what AL could handle, Like all other 8-bit registers, AL could only contain up to +127. The designers of the CPU created the overflow flag specifically for the purpose of informing the programmer that the result of the signed number operation is errone- ous. When the overflow flag is set in 8-bit operations In 8-bit signed number operations, OF is set to | if either of the following two conditions occurs: 1. There is a carry from D6 to D7 but no carry out of D7 (CF = 0). 2. There is a carry from D7 out (CF = 1) but no carry from D6 to D7, In other words, the overflow flag is set to 1 if there is a carry ftom D6 to D7 or from 17 out, but not both. This means that if there is a carry both from D6 to D7 and from D7 out, OF = 0. In Example 6-4, since there is only a carry from D6 to D7 and no carry from D7 out, OF = 1. Examples 6-5, 6-6, and 6-7 give further illustrations of the overflow flag in signed arithmetic. 176 CHAPTER 6; SIGNED NUMBERS, STRINGS, AND TABLES Example 6-5 Observe the results of the following: Mov Mov ADD +126 invalid!) =128 10000000 #2 U0 -130 01111110 OF=1 , SF-0 (positive), CP=1 According to the CPU, the result is +126, which is wrong. The error is indicted by the fact that OF=1 Example 6-6 ‘Observe the results of the following: mov Mov ADD 2 111 1110 +75 1111 1011 =. 11111001 OF = 0, CF = 0 SF = 1 (negative); the result is correct since OF = 0. Example 6-7 ‘Observe the results of the following: Mov DHs7 Mov BH +8 ADD BHDH 47 0000 0111 +418 0001 0010 +25 0001 1001 OF = 0, CF = 0 and SF = 0 (positive). Overfiow flag in 16-bit operations In 2 16-bit operation, OF is set to 1 in either of two cases: I. There is a carry from D14 to DIS but no carry out of DIS (CF = 0). 2. There is a carry from D155 out (CF = 1) but no carry from D14 to D15. Again the overflow flag is low (not set) if there is a carry from both D14 to D15 and from D15 out, The OF is set to 1 only when there is a carry from D14 to D15 or from D15 out but not from both. See Examples 6-8 and 6-9. Example 6-8 Observe the results in the following: MOV AK6EZFH 23207 MOV GX.{304H + 8078 ADD AIX = 35,283 is the expected answer 6E2F 0110 1110 0010 1111 +13D4 0001 0011 1101 0100 8203 1000 0010 0000 0011 = -32,253 incomect! _OF = 1, CI SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 177 Example 6-9 Observe the results in the following: MOV DX.542FH 21.551 MOV BX.12E0H +4832 ADD DXBX 126,383 543F 0101 0100 0010 1111 + 12E0 0001 0010 1110 0000 670F. 0110 0111 0000 1111 = 26,383 (correct answer) _OF = 0, CF = 0, SF=0 Avoiding erroneous results in signed number operations To avoid the problems associated with signed number operations, one can ssign-extend the operand. Sign extension copies the sign bit (D7) of the lower byte ofa register into the upper bits ofthe register, ot copies the sign bit of 16-bit register into another register, CBW (convert signed byte to signed word) and CWD (convert signed word to signed double word) are used to perform sign extension, They work as follows: ° © CBW will copy D7 (the sign flag) to all bits of AH. This is demonstrated below. Notice that the operand is assumed to be AL and the aH A previous contents of AH are destroyed. Mov AL+96 ;AL=0110 0000 cew snow AH=0000 0000 and AL=0110 0000 or: Mov AL-2 TAL=1111 1110 caw SAH=1111. 1111 and AL=1111 1110 CWD sign extends AX. It copies D15 of AX to all bits of the DX register, This is used for signed word operands, This is illustrated below. 15 0 45 o ox me Look at the following example MOV AX,+260 ;AX=0000 0001 0000 0100 or AX=0104H cWD ;DX=0000H and AX=0104H_ Another example: MOV AX,-32766AK= cwo 2) 1000 0000 0000 0010B or AX=8002H ‘FFF and AX=8002 ‘As can be seen in the examples above, CWD does not alter AX. The sign of AX is copied to the DX register. How can these instructions help correct the overflow error? To answer that question, Example 6-10 shows Example 6-4 rewritten to correct the overflow problem. In Example 6-10, if the overflow flag is not raised (OF = 0), the result of the signed number is correct and JNO (jump if no overflow) will jump to OVER However, if OF = 1, which means that the result is erroneous, each operand must be sign extended and then added. That is the function of the code below the JNO instruction. The program in Example 6-10 works for addition of any two signed bytes. 178 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES Example 6-10 Rewrite Example 6-4 to provide for handling the overflow problem. Solution: DATAI DB +98 DATA2_ DB +70 RESULT DW? SUB AHAH vAH=0. MOV ALDATAL IGET OPERAND 1 MOV BLDATAZ IGET OPERAND 2 ADD ALBL ‘ADD THEM JNO OVER IF OF=0 THEN GO TOOVER Mov ALDATA2 JOTHERWISE GET OPERAND 2 TO cow SIGN EXTEND IT MOV BXAX SAVE ITINBX Mov AL'DATAt IGET BACK OPERAND 1 TO caw JSIGN EXTEND IT ADD AX.BX [ADD THEM AND OVER: MOV RESULTAX —'SAVEIT The following is an analysis of the values in Example 6-10. Each is sign extended and then added as follows: SAW AL © — 0000000 © 01100000 +96 after sign extension G 00000000 ©» 01000110 #70 after sign extension 0 000000» t0100110 +166 Asarule, ifthe possibility of overflow exists, all byte-sized signed numbers should be sign extended into a word, and similarly, all word:-sized signed operands should be sign extended before they are processed. This will be shown shortly in Program 6-1. Before discussing that, itis important to understand the division and multiplication of signed operands. IDIV (Signed number division) ‘The Intel manual says that IDIV means “integer division’; it is used for signed number division. In actuality, all arithmetic instructions of the 8088/86 are for integer numbers regardless of whether the operands are signed or unsigned. To perform operations on real numbers, the 8087 coprocessor is used. Remember that real numbers are the ones with decimal points such as "3.56". Division of signed umbers is very similar to division of unsigned numbers discussed in Chapter 3. Table 6-1 summarizes signed number division. Itis very similar to Table 3-2, which summarized unsigned number division. Table 6-1: Signed Division Summary t wordbyte == |AX=word ———_tegisterormemory |AL! AH DXAX = doubleword register or memory AX? 1. Divide enor interrupt if -127 > AL > +127, 2, Divide ervor interrupt if-32767 > AL > +32767 ‘Division Numerator _ [Denominator |Quotient_|Remainder | byte/byte 'AL=byte CBW __lregister or memory |AL AH ‘word/word AX=word WD register ormemory |AX __|DX. | SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 179 ‘Table 6-2: Signed Multiplication Summary |Multiptication Operand 1 Operand 2 Result z byte x byte AL register or memory _|AX! word x word AX register or memory |DXAX? Jword x byte 'AL=byte CBW register or memory _ |DXAX? Notes: 1. CF = 1 and OF = 1 if AH fas part of the reaul, but ithe result i not large enough to need the AH, the sgn bits copied to the unused bts andthe CPU makes CF = 0 and OF ~ 0 indicate tat 2. CF= | and OF « | sf DX has pat ofthe resul, but ite result is ot large enough to need the DX, the sign bite copied tothe unused bts andthe CPU makes CF = O and OF = Ot indicate tat. ‘One can se the condition io find out which o the condition above has occured, The ret ofthe fags ae undefined. IMUL (Signed number multiplication) Signed number multiplication is similar in its operation to the unsigned multiplication described in Chapter 3. The only difference between them is that the operands in signed number operations can be positive or negative; therefore, the result must indicate the sign. Table 6-2 summarizes signed number multiplication; it is similar to Table 3-1 TILE — PROG6t FIND THE AVERAGE TEMPERATURE PAGE 60,192 MODEL STMALL STACK 64 DATA, SIGNDAT DB +13,-10,+19,+14,-18,-9,+12,-19,16 ORG _0010H AVERAGE DW 7 REMAINDER DW 7 CODE MAIN PROC FAR MOV" AXQDATA Ds/ Mov Mov cx {LOAD COUNTER SUB BX,BX iCLEAR BX, USED AS ACCUMULATOR MOV SIOFFSETSIGN_DAT SETUP POINTER BACK: MOV ALIS!) (MOVE BYTE INTO AL cBW iSIGN EXTEND INTO AX ADD BXAK {ADD TOBX INC SI "INCREMENT POINTER Loop Back ‘LOOP IF NOT FINISHED MOV ALS !MOVE COUNT TO AL ‘cBW {SIGN EXTEND INTO AX MOV cxAX SAVE DENOMINATOR IN CX Mov AXX |MOVE SUM TO AX cw IGN EXTEND THE SUM IDV ox IND THE AVERAGE MOV AVERAGE,AX STORE THE AVERAGE (QUOTIENT) MOV — REMAINDER,OX (STORE THE REMAINDER MOV AHACH INT 2th {60 BACK TO DOS MAIN ENDP END MAIN Program 6-1 180 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES An application of signed number arithmetic is given in Program 6-1, It ‘computes the average of the following Celsius temperatures: +13, 10, +19, +14, AB, 9, +12, 49, and +16. ‘The program is written in such a way as to handle any overflow that may ‘occur. In Program 6-1, each byte of data was sign extended and added to BX, ‘computing the total sum, which isa signed word. Then the sum and the count were sign extended, and by dividing the total sum by the count (number of bytes, which in this case is 9), the average was calculated The following is the " Ist" file of the program above. Notice the signed number format provided by the assembler. PROGE-1 Microsoft (R) Macro Assembler Version 5.10 12/90/91 06:14:2 Page 1-4 TITLE _PROGS-1 FIND THE AVERAGE TEMPERATURE PAGE 60.132 ‘000 MODEL SMALL ‘9000 STACK 64 ‘9000 DATA’ 0000 ODFE130EEEF7 —SIGNLOAT 0B +13,-10,+19,414,-18,-9,+12,-19,+16 OC F7 10 0010 ORG —_0010H 010 0900 AVERAGE OW 7? 0012 0000 REMAINDER OW ? 0014 ‘CODE 0000 MAIN PROC FAR (0000 &8 0000s MOV" AX.@DATA (0003 8E 08 MOV DS\AK (0005 89 6009 Mov cx :LOAD COUNTER 0008 28 0B SUB BX.BX CLEAR BX, USED AS ACCUMULATOR ‘000A BE 0000 R MOV — S|,OFFSET SIGN/DAT ‘SET UP PONTER 0000 8A 04 BACK: MOV ALJSI] TMOVE BYTE INTO AL 00F 98 caw JSIGN EXTEND INTO AX 9010 03 D8, ADD BXAK ‘ADD TO BX 0012 48 INC Si INCREMENT POINTER 0013 E2 FB Loop Back “LOOP IF NOT FINISHED 0015 80.09 Mov ALS ‘MOVE COUNT TO AL, 0017 98 caw ISIGN EXTEND INTO AX 0018 88.c8 Mov CXAX |SAVE DENOMINATOR IN CX 001A 8B C3 Mov AXBX [MOVE SUM TO AX o01c 99 owD ISIGN EXTEND THE SUM O01D F7 FO ‘Ow cx [FIND THE AVERAGE. ODIF A30010R MOV AVERAGEAX [STORE THE AVERAGE (QUOTIENT) 0022 89 19 0012 MOV — REMAINDER,DX ‘STORE THE REMAINDER 0026 B4 4c MOV AH.ACH 0028 CD21 INT 21H :GO BACK TO DOS 0024 MAIN ENDP END MAIN List File for P1 rogram 6-1 SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 181 Arithmetic shift As discussed in Chapter 3, there are two types of shifts: logical and arithmetic. Logical shift, which is used for unsigned numbers, was discussed previously. The arithmetic shift is used for signed numbers. Its basically the same as the logical shift, except that the sign bit is copied to the shifted bits. SAR (shift arithmetic right) and SAL (shift arithmetic left) are two instructions for the arith- metic shift. SAR (shift arithmetic right) -ps—} SAR destination,count As the bits of the destination are shifted to the right into CF, the empty bits are filled with the sign bit. One can use the SAR instruction to divide a signed ‘number by 2, as shown next: MOV AL,-10 JAL=-10=F6H=1111 0110 SAR ALA Ais shifted right arithmetic once :AL=1111 1011=FDH=- Example 6-11 demonstrates the use of the SAR instruction. SAL (shift arithmetic left) and SHL (shift left) ‘These two instructions do exactly the same thing. It is basically the same instruction with two mnemonics, As far as signed numbers are concerned, there is no need for SAL. For a discussion of SHL (SAL), see Chapter 3. Example 6-11 Using DEBUG, evaluate the results of the following: Mov Mov ov. Mov SAR Solution: ‘The DEBUG trace demonstrates that an IDIV of -9 by 2 gives FCH (~4), whereas SAR -9 gives FBH (-5). This is because SAR rounds negative numbers down but IDIV rounds up. Axo BL’ divide -9 by 2 results in FCH ‘AX. divide -9 by 2 with arithmetic shit results in FBH Signed number comparison CMP dest,source Although the CMP (compare) instruction is the seme for both signed and unsigned numbers, the J condition instruction used to make a decision for the signed numbers is different from the unsigned numbers. While in unsigned number comparisons CF and ZF are checked for conditions of larger, equal, and smaller (see Chapter 3), in signed number comparison, OF, ZF, and SF are checked: destination > source destination = source destination < source F or ZF=0 jegation of SF 182 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES The mnemonics used to detect the conditions above are as follows: JG Jump Greater jump if OF=SF or ZF=0 JGE Jump Greater or Equal F JL Jump Less JLE Jump Less or Equal JE Jump if Equal Example 6-12 should help clarify how the condition flags are affected by the compare instruction. Program 6-2 is an example of the application of the signed number comparison. It uses the data in Program 6-1 and finds the lowest tempera~ ture ‘The concept of signed number arithmetic is so important and widely used that even the RISC processors in their attempt to streamline the instruction set could not eliminate these instructions. Example 6-12 Show the DEBUG trace of the following, instructions comparing several signed numbers MOV ALS CMP AL-9 CMP AL.2 CMP ALS eMP ALS? Solution: (C>debug -2 100 403D:0100 mov al,-5 103D:0102 emp al--9 1030:0104 cmp af, 103D:0106 emp al-5 4103D:0108 omp al,7 4103D:010A int 3 1030:0108, $1005, ‘AX=00FB 8X=0000 CX=0000 DX=0000 SP=CFDE BP=0000 Si=0000 01=0000 D$=103D ES=103D $$=103D CS=103D IP=0102 NV UP DI PLNZNAPO NC. 1030:01023CF7, CMP ALF7 ‘AX=00FB BX=0000 CX-0000 DX=0000 SP=CFDE BP=0000 SI=0000 01-0000 D$=103D ES=1030 $S=103D CS=1030 IP=0104 NV UP DIPLNZNAPO NC. 1030:0104 3CFE CMP ALFE AX=00F8 8X=0000 CX=0000 D) DS=103D ES=103D $S=103D CS= 409D:0106. 3CFB_ = CMP ALFB. 1000 SP=CFOE BP=0000 SI=0000 DI=0000 103 IP=0106 NV UP DING NZ AC POCY FB BX=0000 CX=0000 OX=0000 SP=CFDE BP=0000 SI=0000 Di=0000 DS=1030 ES=1030 $S=103D CS=1030 1P=0108 NV UP DI PL ZR NAPE NC 4030:0108 3007s CMP_—AL.OT 10 SP=CFDE BP=0000 $1000 DI=0000 103D IP=010A NV UP DING NZ NAPO NC os 403D:010ACC INT 3 4 SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS 183 TITLE — PROGE.2 SFIND THE LOWEST TEMPERATURE PAGE 60,132 7 MODEL SMALL ‘STACK 64 7 DATA, SIGN_DAT "0B #13,-10,419,414,-18,-0,412,-19,418 OR Lowest DB? SO010H CODE MAIN PROC FAR MOV" AX.QDATA Mov MOV Cx; ;LOAD COUNTER (NUMBER ITEMS - 1) MOV SI,OFFSETSIGN_DAT “SET UP POINTER Mov ALIS ‘AL HOLDS LOWEST VALUE FOUND SO FAR BACK INC SI "INCREMENT POINTER CMP ALIS {COMPARE NEXT BYTE TO LOWEST Sle SEARCH LIF AL IS LOWEST, CONTINUE SEARCH Mov AL{Si) {OTHERWISE SAVE NEW LOWEST SEARCH: LOOP BACK LOOP If NOT FINISHED MOV LOWESTAL SAVE LOWEST TEMPERATURE MOV ARCH nr 21H GO BACK TODOS MAIN NOP END MAIN Program 6-2 Review Questions 1. Inan &bit operand, bit ___is used for the sign bit, whereas in a 16-bit operand, bit ___ is used for the sigan bit. 2. Covert 16H to its 2's complement representation 3. The range of byte-sized signed operands is—____to-+ The range of word- sized signed operands is — to 4. Explain the difference between a carry and an overflow. 5. Explain the purpose of the CBW and CWD instructions. Demonstrate the effect of ‘CBW on AL = F6H. Demonstrate the effect of CWD on AX = 124CH. 6. The instruction for signed multiplication is, “The instruction for signed di- vision is 7. Explain the difference between the SHR (discussed in Chapter 3) and SAR instruc tions 8. For each of the following instructions, indicate the flag condition necessary for each jump to occur. (a) ILE (b) IG. SECTION 6.2: STRING AND TABLE OPERATIONS ‘There are a group of instructions referred to as string instructions in the 80x86 family of microprocessors. They are capable of performing operations on a series of operands located in consecutive memory locations. For example, while the CMP instruction can compare only 2 bytes (or words) of data, the CMPS (compare string) instruction is capable of comparing two arrays of data located in memory locations pointed at by the SI and DI registers. These instructions are very powerful and can be used in many applications, as will be shown shortly. 184 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES Use of SI and DI, DS and ES in string instructions For string operations to work, designers of the CPU must set aside certain registers for specific functions. These registers must permanently provide the source and destination operands. This is exactly what the 80x86 has done. In 8088/86 microprocessors, the SI and DI registers always point to the source and destination operands, respectively. Now the question is: Which segments are they combined with to generate the 20-bit physical address? To generate the physical address, the 8088/86 always uses SI as the offset of the DS (data segment) register and Dias the offset of ES (extra segment). This is the default mode. It must be noted that the ES register must be initialized for the string operation to work. Byte and word operands in string instructions. Ineach of the string instructions, the operand can be a byte or a word. They are distinguished by the letters B (byte) and W (word) in the instruction mnemonic. Table 6-3 provides a summary of all the string instructions. Each one will be discussed separately in the context of examples. Table 6-3: String Operation Summary ‘Instruction Mnemonic Destination _|Source Prefix move string byte MOVSB___ES:DI DS:SI REP move string word MOVSW __ES:DI ‘DS:sI REP |store string byte STOSB ESDI |AL REP |store string word __STOSW AX REP load string byte .LODSB DS:SI none {load string word Lopsw DS:SI none ‘compare string byte _CMPSB DS:SI REPE/REPNE compare string word _|CMPSW___ ___|Ds:st ___|REPE/REPNE ‘scan string byte 'SCASB \ES:DI |AL REPE/REPNE {scan string word ISCASW ESDI [AX REPE/REPNE DF, the direction flag To process operands located in consecutive memory locations requires that the pointer be incremented or decremented. In string operations this is achieved by the direction flag. Of the 16 bits of the flag register (D0 - D15), bit 11 (D10) is set aside for the direction flag (DF). It is the job of the string instruction to increment or decrement the $1 and DI pointers, but itis the job of the programmer to specify the choice of increment or decrement by setting the direction flag to high or low. The instructions CLD (clear direction flag) and STD (set direction flag) are specifically designed for that. ‘CLD (clear direction flag) will reset (put to zero) DF, indicating that the string instruction should increment the pointers automatically. This automatic incrementation sometimes is referred to as autoincrement. STD (set the direction flag) performs the opposite function of the CLD instruction. It sets DF to 1, indicating to the string instruction that the pointers SI and DI should be decremented automatically. SECTION 6.2: STRING AND TABLE OPERATIONS, 185 REP prefix ‘The REP (repeat) prefix allows a string instruction to perform the operation repeatedly. Now the question is: How many times is it repeated? REP assumes that CX holds the number of times that the instruction should be repeated. In other words, the REP prefix tells the CPU to perform the string operation and then decrements the CX register automatically. This process is repeated until CX becomes zero. To understand some of the concepts discussed so far, look at Example 6-13. Example 6-13 Using string instructions, write a program that transfers a block of 20 bytes of data, Solutior inthe data segmont DATA1 DB 'ABCDEFGHIJKLMNOPQRST ‘ORG 30H DATA2DB 20 DUP (?) Inthe code segment: MOV AX@DATA MOV DSAK INITIALIZE THE DATA SEGMENT MOV ES|AX LINITIALIZE THE EXTRA SEGMENT. CLD 'CLEAR DIRECTION FLAG FOR AUTOINCREMENT MOV SIOFFSET DATAI [LOAD THE SOURCE POINTER MOV — DILOFFSET DATA2 [LOAD THE DESTINATION POINTER MOV CX20 ‘LOAD THE COUNTER REP = MOVSB |REPEAT UNTIL CX BECOMES ZERO In Example 6-13, after the transfer of every byte by the MOVSB instruction, both the SI and DI registers are incremented automatically once only (notice CLD). ‘The REP prefix causes the CX counter to be decremented and MOVSB is repeated until CX becomes zero, Notice in Example 6-13 that both DS and ES are sct to the same value. ‘An alternative solution for Example 6-13 would change only two lines of code: MoV cx,10 REP Movsw In this case the MOVSW will transfer a word (2 bytes) at a time and increment the SIand DI registers each twice. REP will repeat that process until CX ‘becomes zero. Notice the CX has the value of 10 in it since 10 words is equal to 20 bytes. STOS and LODS instructi The STOSB instruction stores the byte in the AL register into memory locations pointed at by ES:DI and increments (if DF = 0) DI once. If DF = 1, then Dlis decremented. The STOSW instruction stores the contents of AX in memory locations ES:DI and ES:DI+l (AL into ES:DI and AH into ES:DI+1), then inere- ments DI twice (if DF = 0), If DF = 1, DI is decremented twice. ‘The LODSB instruction loads the contents of memory locations pointed at by DS‘SI into AL and increments (or decrements) SI once if DF = 0 (or DF = 1). LODSW loads the contents of memory locations pointed at by DS:ST into AL and DS:SI+1 into AH, The SI is incremented twice if DF = 0. Otherwise, it is decremented twice. LODS is never used with a REP prefix. 186 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES Testing memory using STOSB and LODSB Example 6-14 uses string instructions STOSB and LODSB to test an area of RAM memory. AGAIN: oveR: EXIT: Solution: Assuming that ES and DS have been assigned in the ASSUME directive, the following is from the code segment: cample 6-14 Write a program that: (1) Uses STOSB to store byte AAH into 100 memory locations. (2) Uses LODS to test the contents of each location to see if AAH was there. If the test fails, the system should display the message "bad memory". PUT PATTERN AAAAH IN TO 50 WORD LOCATIONS: MOV AXDTSEG “INITIALIZE MOV DS\AX {DS REG MOV ESIAX {AND ES REG. cD ICLEAR OF FOR INCREMENT MOV CX,50 SCOAD THE COUNTER (50 WORDS) MOV DLOFFSETMEM_AREA LOAD THE POINTER FOR DESTINATION MOV AXOAAAAH ;LOAD THE PATTERN, REP STOSW IREPEAT UNTIL CX=0 BRING IN THE PATTERN AND TEST IT ONE BY ONE MOV SLOFFSETMEM_AREA ;LOAD THE POINTER FOR SOURCE. Mov CX:100 {LOAD THE COUNT (COUNT 100 BYTES) Loose :LOAD INTO AL FROM DS'SI XOR LAH IS PATTERN THE SAME? NZ OVER IF NOT THE SAME THEN EXIT Loop AGAIN ‘CONTINUE UNTIL CX=0 JMP EXIT EXIT PROGRAM Mov AH.09 {DISPLAY MOV DX, OFFSETMESSAGE { THE MESSAGE WNT. 2 {{ ROUTINE In the program in Example 6-14, first AAH is written into 100 locations by using word-sized operand AAAAH and acount of 50. In the test part, LODS brings in the contents of memory locations into AL one by one, and each time it is eXclusive-ORed with AAH (the AH register has the hex value of AA). If they are the same, ZF = | and the process is continued. Otherwise, the pattern written there by the previous routine is not there and the program will exit. This, in concept, is somewhat similar to the routine used in the IBM PC’s BIOS except that the BIOS routine is much more involved and uses several different patterns of data forthe test and it can be used to test any part of RAM, either the main RAM or the video RAM. ‘The REPZ and REPNZ prefixes ‘These prefixes can be used with the CMPS and SCAS instructions for testing purposes. They are explained below. REPZ (repeat zero), which is the same as REPE (repeat equal), will repeat the string operation as long as the source and destination operands are equal (ZF 1) or until CX becomes zero. REPNZ (repeat not zero), which is the same as REPNE (repeat not equal), will repeat the string operation as long as the source and destination operands are not equal (ZF = 0) or until CX become zero. These two prefixes will be used in the context of applications after the explanation of the CMPS and SCANS instructions. SECTION 6.2: STRING AND TABLE OPERATIONS 187 CMPS (compare string) allows the comparison of two arrays of data pointed at by the SI and DI registers. One can test for the equality or inequality of data by use of the REPE or REPNE prefixes, respectively. The comparison can be performed a byte at a time or a word at time by using CMPSB or CMPSW. For example, if comparing "Euorop" and "Europe" for equality, the com- parison will continue using the REPE CMPS as long as the two arrays are the same. from the data segment: DATA DB ‘Europe’ DATA2 DB Euorop’ ‘from the code segment: CLD {DF=0 for increment Mov SLOFFSET DATA! {SI MoV DIOFFSETDATA2 —_;DI=DATA2 offset MOV Cx,06 :load the counter REPE — CMPSB repeat until not equal or CX=0 In the case above, the two arrays are to be compared letter by letter. The first characters pointed at by SI and DI are compared. In this case they are the same ("E’), so the zero flag is set to 1 and both SI and DI are incremented. Since ZF = 1, the REPE prefix repeats the comparison. This process is repeated until the third letter is reached. The third letters "o" and "s" are not the same; therefore, ZF is reset to zero and the comparison will stop. ZF can be used to make the decision as shown in Example 6-15. xample 6-15 ‘Assuming that there is a spelling of "Europe" in an electronic dictionary and a user types in "Euorope", write a program that compares these two and displays the following message, depending on the result: 1. If they are equal, display "The spelling is correct". 2. If they are not equal, display "Wrong spelling”. Solution: DAT DICT DAT-TYPED MESSAGE1 MESSAGE from the code segment: ‘LD MOV — SLOFFSET DAT_OICT MOV DLOFFSETDATLTYPED FOR INCREMENT -DATA1 OFFSET [DI-DATA2 OFFSET MOV CX.06 SLOAD THE COUNTER REPE CMPSB REPEAT AS LONG AS EQUAL OR UNTIL OX=0 JE OVER THEN DISPLAY MESSAGE MOV DX,OFFSET MESSAGE2 JMP DISPLAY OVER: MOV DX.OFFSET MESSAGE1 DISPLAY: MOV AH.09 INT 2TH THEN DISPLAY MESSAGE One could juggle the code in Example 6-15 to make it more efficient and use fewer jumps, bit for the sake of clarity itis presented in this manner. ‘CMPS canbe used to test inequality of wo arrays using "EPNE CMPSB". For example, when comparing the following social security numbers, the compari- son will continue to the last digit since no two digits in the same position are the same, 231-24-7659 564-77-1338 188 CHAPTER }IGNED NUMBERS, STRINGS, AND TABLES SCAS (scan string) The SCASB string instruction compares cach byte of the array pointed at by ES:DI with the contents of the AL register, and depending on which prefix of REPEor REPNE isused, a decision is made for equality or inequality. For example, in the array "Mr. Gones”, one can scan for the letter "G" by loading the AL register with the character "G” and then using the "REPNE SCASB" operation to look for that letter. in the data segment: DATA1 DB ‘Mr. Gones’ and in the code segment CLD ;DF=0 FOR INCREMENT MOV DILOFFSETDATAt —_:DI=ARRAY OFFSET Mov Cx.09 ;LENGTH OF ARRAY MOV ALG {SCANNING FOR THE LETTER 'G’ REPNE SCASB. [REPEAT THE SCANNING IF NOT EQUAL OR JUNTIL THE CX IS ZERO. In the example above, the letter "G" is compared with "M". Since they are not equal, DI is incremented and CX is decremented, and the scanning is repeated until the letter "G" is found or the CX register is zero. In this example, since "G" is found, ZF is set to 1 (ZF = 1), indicating that there is a letter "G" in the array. Replacing the scanned character SCASB can be used to search for a character in an array, and if it is found, it will be replaced with the desired character. See Example 6-16. Example 6-16 Write a program that scans the name "Mr. Gones" and replaces the "G" with the letter "J", then displays the corrected name. Solution: inthe data segment DATA1 DB 'Mr Gones:,S" and in the code segment, MOV AX@DATA Mov DSAK MOV ESAX CLD {DF=0 FOR INCREMENT MOV — DLOFFSET DATA1 [ES:DISARRAY OFFSET MOV CX,09 ILENGTH OF ARRAY MOV _ AL'S: ISCANNING FOR THE LETTER‘ REPNE SCASB [REPEAT THE SCANNING IF NOT EQUAL OR NE OVER {UNTIL CXIS ZERO. JUMP IF 2=0 DEC DI 1 TO POINT AT'G MOV BYTEPTRIDIL JREPLACE 'G' WITH 'J OVER: MOV AH.09 [DISPLAY MOV DX.OFFSETDATA1 = (THE INT 21H (CORRECTED NAME, In string operations, after cach execution, the pointer is incremented (that is, if DF = 0), Therefore, in the example above, DI must be decremented, causing the pointer to point to the scanned character and then replace it. SECTION 6.2: STRING AND TABLE OPERATIONS. 189 XLAT instruction and look-up tables There is often a need in computer applications for a table that holds some ‘important information. To access the elements of the table, 8088/86 microproces- sors provide the XLAT (translate) instruction. To understand the XLAT instruction, ‘one must first understand tables, The table is commonly referred to as a /ook-up table. Assume that one needs a table for the values of x, where x is between 0 and 9. First the table is generated and stored in memory: SQUR_TABLE DB 0,1,4,9,16,25,36,49,64,81 Now one ean access the square of any number from 0 to 9 by the use of XLAT. To do that, the register BX must have the offset address ofthe look-up table, and the number whose square is sought must be in the AL register. Then after the execution of XLAT, the AL register will have the square of the number. ‘The following shows how to get the square of 5 from the table: MOV BX,OFFSETSQUR_TABLE __;load the offset address of table MOV AL,05 /AL=05 will retrieve 6th element XLAT ‘pull out of table the element jand put in AL After execution of this program, the AL register will have 25 (19H), the square of 5. It must be noted that for XLAT to work, the entries of the look-up table ‘must be in sequential order and must have a one-to-one relation with the element itself, This is because of the way XLAT works. In actuality, XLAT is one instruction, which is equivalent to the following code: SUB AHAH vAH=0 MOV SIAX iS1=000X MOV ALJBX+SI] ;GET THE Sith ENTRY FROM BEGINNING :OF THE TABLE POINTED AT BY BX In other words, if there was no XLAT instruction, the code above would do the same thing, and this is the way many RISC processors perform this operation ‘Now why would one want to use XLAT to get the square of a number from a look-up table when there is the MUL instruction? The answer is that MUL takes longer. Code conversion using XLAT In many microprocessot-based systems, the keyboard is not an ASCII type of keyboard. One can use XLAT to translate the hex keys of such keyboards to ASCII. Assuming that the keys are 0 - F, the following is the program to convert the hex digits of 0 - F to their ASCII equivalents. data segment: ASC_TABL DB '0','1'/2',3'/4",' DB '9''A,'B''C' HEX_VALU DB ? ‘ASC_VALU DB ? code segment: MOV 'BX,OFFSETASC_TABL ;BX= TABLE OFFSET MOV ALHEX_VALU ‘AL=THE HEX DATA XLAT :GET THE ASCII EQUIVALENT MOV ASC_VALU,AL {MOVE IT TO MEMORY 190 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES SUMMARY PROBLEMS 1 Review Questions Instring operations, register ____ is used to point to the source operand and regis- ter___ is used to point to the destination operand. Sl is used as an offset into the segment, and DI is used as an offset into the segment. The flag, bit___ of the flag register, is used to tell the CPU whether to in- cerement oF decrement pointers in repeated string operations. State the purpose of instructions CLD and STD. Ifa string instruction is repeatedly executed because of a REP prefix, how docs the CPU know when to stop repeating it? In the following program segment, what condition will cause the REPNZ to fail? MOV ‘SI, OFFSET DATAL MOV _ DI, OFFSET DATA2 MOV CX,LENGTH REPNZ CMPSB ‘Signed number representation in the 8086/88 is achieved by using the MSB (most significant bit) as a sign bit. In a byte operand, the sign bit is D7, and ina word operand, the sign bit is DIS. A sign bit of zero indicates a positive number, and a sign bit of I indicates a negative number. Negative numbers are represented in 2's complement. Signed addition and subtraction instructions use the same instructions as unsigned addition and subtraction: ADD and SUB. However, signed multiplication and division use the instructions IMUL and IDIV instead of MUL and DIV. In signed number arithmetic, the programmer must check for the overflow problem. An overflow occurs when either there is a carry into the MSB, or there is a carry out and no carry into the MSB. The overflow problem can be avoided by use of the sign extension instructions CBW and CWD. ‘Arithmetic shift instructions work similarly to the logic shift instructions except that the arithmetic shift instructions must take the sign bit into account, Therefore, they copy the sign bit into the shifted bits. The compare (CMP) instruction works the same for signed numbers as it does for unsigned numbers, but different conditional jump instructions are used after the CMP in programs. The 80x86 has many instructions that operate on strings of data, These instructions include STOS and LODS instructions to store and load data and the SCAS scanning instruction. String operations use registers DI and SI as pointers to data in the extra and data segments. These instructions can be repeated by using any of the various forms of the REP prefix. Whether the pointers DI and SI will be incremented or decremented with each repetition depends on the setting of the direction flag. There is also an instruction for table processing, the XLAT instruc- tion ‘Show how the 80x86 computer would represent the following numbers and verify each with DEBUG. (@)-23 (b) +12 (©) -28H (@) +6FH (e) -128, (+127 (g) +365 (h) 32.767 Find the overflow flag for each case and verify the result using DEBUG. (a) (F15)*12) (b) (-123)+(-127) (©) (425h)+ (+34) (d) 127) + (+127) (e) (+1000) + (-1000) Sign-extend the following and write simple programs in DEBUG to verify them. (a) -122 (b)-999h — (@) 17h (d) +127 (@)-129 SUMMARY 191 4, Modify Program 6-2 to find the highest temperature, Verify your program. 5. Which instructions are used to set and reset the direction flag? State the purpose of the direction flag, 6, The REP instruction can be used with which of the following instructions? (@)MOVSB — (b) MOVSW (c) CMPSB ()LODSB_ @)STOSW (f) SCASW In Problem 6, state the source and destination operand for each instruction, Write and verify a program that transfers a block of 200 words of data. Use instructions LODSx and STOSx to mask the 3 from a set of 50 ASCII digits and transfer the result to a different memory location. This involves converting from ASCTI to unpacked BCD, then storing it at a different location, for example source destination ASCII for ’5' 00110101 0000 0101 10. Which prefix is used for the inequality case for CMPS and SCAS instructions? 11. Write a program that scans the initials "IbM" and replaces the lowercase "b" with uppercase " 12. Using the timing chart in Appendix B.2, compare the clock count of the instruction XLAT and its equivalent to see which is more efficient. 13, Write a program using a look-up table and XLAT to retrieve the y value in the equation y = x? + 2x +5 for x values of 0 to 9. ANSWERS TO REVIEW QUESTIONS SECTION 6.1: SIGNED NUMBER ARITHMETIC OPERATIONS, 4. 7.418 2. 16H4= 0001 01102 in 2's complement: 1110 10102 3. 428 to +127; -32,768 to +32,767 (decimal) 4. an overfiaw is a carry into the sign bit, a carry is a carry out of the register 5. the CBW instruction sign extends the sign bil of a byte into a word: the CWD instruction sign extends the sign bit of a word into a doubleword. FoH sign extended into AX = FFF6H 424C sign extended into DX AX would be OX = 0000 and AX = 124CH, 6 IMUL, IDV 7. SHR ‘shifts each bit right one position and fils the MSB with zero. SAR shifts each bit right one position and fis the MSB with the sign bit in each; the LSB is shifted into the carry flag 8. (a) JLE’ wil jump if OF is tho invorse of SF, or if 2 (6) JG wil jump it OF equals SF, or if ZF = 0 SECTION 62: STRING AND TABLE OPERATIONS 1. Si, 2) data, extra 3. direction flag, bit 11 oc D10 4. CLD clears DF to 0; STD sets DF to 1 5. when CX=0 6. ifCX=00or the point at which DATA and DATAZ are not equal 192 CHAPTER 6: SIGNED NUMBERS, STRINGS, AND TABLES CHAPTER 7 MODULES; MODULAR AND C PROGRAMMING 193 In this chapter the concept of modules is presented along with rules for writing modules and linking them together, Some very useful modules will be given, along with the methods of passing parameters among various modules. In the final section we show how to combine Assembly language programs with C programs SECTION 7.1: WRITING AND LINKING MODULES Bene Why modules? It is common practice in writing software packages to break down the project into small modules and distribute the task of writing those modules among several programmers. This not only makes the project more manageable but also has other advantages, such as Each module can be written, debugged, and tested individually. The failure of one module does not stop the entire project. ‘The task of locating and isolating any problem is easier and less time consuming. One can use the modules to link with high-level languages such as C, Pascal, or BASIC. . Parallel development shortens considerably the time required to complete a project. In this section we explain how to write and link modules to ereate a single executable program, Writing modules In previous chapters, a main procedure was written that called many other subroutines. In those examples, if one subroutine did not work properly, the entire program would have to be rewritten and reassembled. A more efficient way to develop software isto treat each subroutine as a scparate program (or module) with a separate filename. Then each one ean be assembled and tested, After testing each program and making sure that each works, they can all be brought together (linked) to make a single program. To enable these modules to be linked together, certain Assembly language directives must be used. Among these directives, the two most widely used are EXTRN (extemal) and PUBLIC, Each is discussed below. EXTRN directive ‘The EXTRN directive is used to notify the assembler and linker that certain names and variables which are not defined in the present module are defined extemally somewhere else. In the absence of the EXTRN directive, the assembler would show an error since it cannot find where the names are defined. The EXTRN directive has the following format: EXTRN namet type jeach name can be in a separate EXTRN EXTRN name2:type EXTRN namettype,name2type or many can be listed in the same EXTRN External procedure names can be NEAR, FAR, or PROC (which will be NEAR for smail models or FAR for larger models). The following are the types for data names, with the number of bytes indicated in parentheses: BYTE (1), WORD (2), DWORD (4), FWORD (6), QWORD (8), or TBYTE (10). PUBLIC directive ‘Those names or parameters defined as EXTRN (indicating that they are defined outside the present module) must be defined as PUBLIC in the module where they are defined. Defining a name as PUBLIC allows the assembler and linker to match it with its EXTRN counterpart(s). The following is the format for the PUBLIC directive: 194 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING PUBLIC namet ‘each name can be in a separate directive PUBLIC name2 PUBLIC name1,name2 jor many can be listed in the same PUBLIC Example 7-1 should help to clarify these concepts. It demonstrates that for every EXTRN definition there is a PUBLIC directive defined in another module. In Example 7-1 the EXTRN and PUBLIC directives were related to the name of a FAR procedure. END directive in modules In Example 7-1, notice the entry and exit points of the program. The entry point is MAIN and the exit point is "END MAIN". Modules that are called by the main module have the END directive with no label or name after it ‘Notice that SUBPROG! and SUBPROG? cach have the END directive with no labels after them. shows the use of the EXTRN and PUBLIC directives: Solution: ‘one file will contain the main module: EXTRN) SUBPROGI-FAR EXTRN SUBPROG2FAR MODEL SMALL ‘CODE MAIN PROC FAR CALL SUBPROGI CALL SUBPROG2 Nov AHACH INT 21H MAIN ENDP END MAIN j——-—— and in a separate fie: PUBLIC SUBPROG1 “MODEL SMALL ‘CODE SUBPROG1 PROC FAR RET SUBPROG1 ENOP END in another fi PUBLIC SUBPROG2 MODEL SMALL. CODE SUBPROG2 PROC FAR Ret SUBPROG2 ENDP END Examp! ‘Assume there is a program that constitutes the main routine, and two smaller subroutines named SUBPROGI and SUBPROG2. The subprograms are called from the main routine. The following SECTION 7.1: WRITING AND LINKING MODULES 195 Linking modules together into one executable unit ‘Assuming that each program module in Example 7-1 is assembled sepa- rately and saved under the filenames EXAMPLEI.OBJ, PROCI.OBJ, and PROC2.0BJ, the following shows how to link them together in MASM in order to generate a single executable file: (C> LINK EXAMPLE1.OBJ + PROC1.OBJ + PROG2.0BJ Program 7-1 shows how the EXTRN and PUBLIC directives can also be applied to data variables. In Program 7-1, the main module contains a data segment and a stack segment, but the subroutine modules do not, Each module can have its ‘own data and stack segment. While it is entirely permissible and possible that the ‘modules have their own data segments if they need them, generally there is only one stack that is defined in the main program and it must be defined so that itis combined with the system stack. Later in this chapter we show how to combine many segments of different modules to generate one uniform segment for each segment of code, data, and stack, Use the program shells in Example 7-1 to: 1. Add two words. 2. Multiply two words. Each one should be performed by a separate module. The data is defined in the main module, and the add and multiply modules have no data segment of their own. TITLE PROG7-1MM DEMONSTRATES MODULAR PROGRAMMING PAGE 60,132, EXTRN SUBPROGI-FAR EXTRN SUBPROG2FAR PUBLIC VALUE, VALUE2, SUM, PRODUCT MODEL SMALL STACK 64 DATA VALUE! DW 2050 VALUE2 OW 500 SUM DW = 2DUP(?) PRODUCT DW 2DUP(?) ‘CODE, MAIN PROC FAR MOV’ AX.@DATA MOV DS, AX, CALL SUBPROG1 {CALL SUBPROG TO ADD VALUE! + VALUE2 CALL SUBPROG2 —_[CALL SUBPROG TO MUL VALUE1 * VALUE2 MOV AH.ACH INT 21H {G0 BACK TO DOs, MAIN ENOP END MAIN Program 7-1: Main Module 196 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING TTHIS PROGRAM FINDS THE SUM OF TWO EXTERNALLY DEFINED WORDS JAND STORES THE SUM IN A LOCATION DEFINED BY THE CALLING MODULE TITLE PAGE ‘SUBPROGI ‘SUBPROG1 BROG?-IMa_ PROGRAM TO ADD TWO WORDS 4 EXTRN VALUE1:WORD EXTRN VALUE2WORD EXTRN. SUMWORD PUBLIC SUBPROGI MODEL SMALL CODE PROC FAR SUB BX.BX {INITIALIZE CARRY COUNT MOV AX,VALUE1 MOV DX.VALUE2 ADD AXDX {ADD VALUE1 + VALUE ‘ADC BX.00 [ACCUMULATE CARRY MOV SUMAX ISTORE SUM MOV SUM#2,—BX STORE CARRY RET NDP. END Program 7-1: Module 2 THIS PROGRAM FINDS THE PRODUCT OF TWO EXTERNALLY DEFINED WORDS JAND STORES THE PRODUCT IN A LOCATION DEFINED BY THE CALLING MODULE TTL PAGE ‘SUBPROG2 ‘SUBPROG2 BROGT.1M3, PROGRAM TO MULTIPLY TWO WORDS 132 EXTRN VALUE1:WORD EXTRN VALUE2WORD EXTRN PRODUCT.WORD PUBLIC SUBPROG2 MODEL SMALL ‘CODE PROC FAR MOV" AXVALUE? MOV CX.VALUE2 MUL ox :MULTIPLY VALUE4 * VALUE2 MOV PRODUCTAX ‘STORE PRODUCT MOV PRODUGT#2,0x;STORE PRODUCT HIGH WORD REI ENP END Program 7-1: Module 3 Analysis of Program 7-1 Notice in the main module that each of the two subroutines was declared with the EXTRN directive, indicating that these procedures would be defined in another file. The extemal subroutines were defined as FAR in this case. In the files where each subroutine is defined, itis declared as PUBLIC, so that other programs can call it. In the main module, the names VALUE1, VALUE2, SUM, and PROD- UCT were defined as PUBLIC, so that other programs could access these data items, In the subprograms, these data items were declared as EXTRN. These three programs would be linked together as follows: C> LINK PROG7-1MM.OBJ + PROGT-1M2 + PROG7-1M3 The linker program resolves extemal references by matching PUBLIC and EXTRN names. The linker program will search through the files specified in the LINK command forthe external subroutines. Notice that the filenames are unrelated to the procedure names. "MAIN" is contained in file "PROG7-1MM OB)!" |: WRITING AND LINKING MODULES 197 Example 7-2 shows the shell of modular programs using the simplified segment definition. Modular programming with full segment definition is defined later in this section Example 7- Create a shell for modular programming using the simplified segment definition. Solution: Modular program shells for the simplified segment directives are as follows. ‘The main file will contain: MODEL SMALL ‘STACK 64 DATA ‘ope EXTRN SUBPROGI-NEAR EXTRN SUBPROG2NEAR MAIN: MOV AX@DATA "this isthe program entry point CALL SUBPROGI CALL SUBPROG2 MOV AH.ACH INT 21H END MAIN _thisis the program exit point : ‘and in a separate fe: -MODEL SMALL ICODE PUBLIC sUBPROGI SUBPROGI PROC RET ‘SUBPROG! ENDP END = and in another fie: MODEL SMALL CODE, PUBLIC SUBPROG2 ‘SUBPROG2 PROC Ret ‘SUBPROG2 ENDP END Notice that in the main module of Example 7-2, the name MAIN has acolon after it and is used for the first executable instruction. ‘This is the entry point of the program. The exit point of the program is indicated by the same label, which must be named in the END directive. No program can have more than one entry and one exit point, The label MAIN was chosen in this instance, but of course any name could have been chosen. Remember that the END directives in other modules do not have a label after the word "END". Program 7-2 is the same as Program 7-1, rewritten for the full segment definition. Compare the two programs to see the ease ofthe simplified segment definition, When using the simplified scgment definition shown in Example 7-2, procedures will default to NEAR for small or compact models and to FAR for medium, large, or huge models. 198 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING Tme STSEG STSEG DTsES VALUE1 VALUE SUM, PRODUCT DTSEG Modular programming and full segment definition Program 7-2 uses full segment definition to redefine all the segments of Program 7-1. An analysis of how the segments are combined as shown in the link map follows the program. The code segments were not made PUBLIC in this ‘example. Notice that in order to combine various segments from different modules into one segment, the segment names must be the same, PROG7-2MM — PROG7-1 REWRITTEN WITH FULL SEGMENT DEFINITION 60,132 EXTRN SUBPROGI:FAR EXTRN SUBPROG2:FAR PUBLIC VALUE1, VALUE2, SUM, PRODUCT ‘SEGMENT. PARA STACK STACK’ DB 100 DUP(?) ENDS: ‘SEGMENT PARA ‘DATA’ DW 2050 Dw 500 DW = 2DUP(7) DW 20UP(?) ENDS CODSG_A SEGMENT PARA ‘CODE’ MAIN MAIN ‘CODSG_A PROC FAR ASSUME CS:CODSG_ADS:DTSEG,SS'STSEG MOV AXOTSEG Mov DS,AX CALL SUBPROGI —_ CALL SUBPROG TO ADD VALUE! + VALUE2 GALL SUBPROG2 CALL SUBPROG TO MUL VALUE1 * VALUE2 MOV AH.ACH INT 21H ;GO BACK TO DOS. END MAIN — : Main Module THIS PROGRAM FINDS THE SUM OF TWO EXTERNALLY DEFINED WORDS [AND STORES THE SUM IN A LOCATION DEFINED BY THE CALLING MODULE TMe PAGE copsc_B SUBPROG SUBPROG copsG_B BROGT-2N2 PROGRAW TO ADD TWO WORDS EXTRN VALUEL:WORD EXTRN VALUE2\WORD EXTRN SUMMWORD PUBLIC SUBPROG1 SEGMENT PARA‘CODE" PROG | FAR ASSUME CS:CODSG_8 SUB BXBX INITIALIZE CARRY COUNT MOV AX\VALUEt MOV DX,VALUEZ ADD AXDX "ADD VALUES + VALUE2 ADC BX.00 [ACCUMULATE CARRY Mov SUMAX. ISTORE SUM MOV SUM#2.BX [STORE CARRY RET ENDP ENDS. END Program 7-2: Module 2 SECTION 7.1: WRITING AND LINKING MODULES 199 TTHIS PROGRAM FINDS THE PRODUCT OF TWO EXTERNALLY DEFINED WORDS JAND STORES THE PRODUCT IN A LOCATION DEFINED BY THE CALLING MODULE TITLE PROG7-2M3- PROGRAM TO MULTIPLY TWO WORDS PAGE 60,132 EXTRN VALUE1.WORD EXTRN VALUE? WORD EXTRN PRODUCT WORD PUBLIC SUBPROG2 CODSG_C SEGMENT PARA'CODE’ ‘SUBPROG2 PROC FAR ‘ASSUME CS:CODSG_C MOV "AX.VALUE1 MOV CX'VALUEZ MUL Cx {MUL VALUE? * VALUE2 MOV PRODUCT,AX_ STORE PRODUCT MOV PRODUCTS2,Dx STORE PRODUCT HIGH WORD rE SUBPROG2 ENDP CODSG_C ENDS END Stan Stop Length Name Class 00000H 00063H 00064H STSEG STACK 00070H 0007BH 0000CH DTSEG DATA ‘00080H 00092H 00013H CODSG_A CODE ‘O00AOH 000BSH 00016H CODSG_B CODE O00CCH 000D0H 00011H CODSG_C CODE Program 7-2: Module 3 and the Link Map Analysis of Program 7-2 link map ‘The link map shows the start and end of cach segment. Notice that each ‘segment starts at a 16-byte boundary: 00070H, 00080H, etc. The code segment for the main module has the name "CODSG_A", starts at 00080H, and ends at 00092H, taking a total of 0001 3H bytcs. It was classified as CODE”. The next code segment isdefined under the name "CODSG_B". Notice thatit starts atthe 16-byte boundary 000A0H since it was defined as PARA. This means that from 00093H to 0009FH is unused, Similarly, the third module starts at 000COH. Notice that each code segment is separate. They can all be merged together into one segment by using the PUBLIC option. “This is shown in Example 7-3. To merge the code scgments together, each code segment must have the samo name and be declared PUBLIC. Example 7-3 ‘Show the link map for Program 7-2 rewritten to combine code segments (use PARA boundaries) using direc- tive. COSEG SEGMENT PARA PUBLIC ‘CODE’ Solution: Start ‘Stop Length Name Class (00000H 00063H 00064H STSEG ‘STACK 00070H 0007BH 0000CH DTSEG DATA 00080H O00DOH 00051H CDSEG CODE The foliowing are the SEGMENT directives using word boundaries STSEG SEGMENT WORD STACK 'STACK” DISEG SEGMENT WORD ‘DATA CDSEG SEGMENT WORD PUBLIC ‘CODE’ ‘The folowing is the link map when the program used WORD boundaries: Stat Stop Length Name. Class. (00000H 00063H 00064H STSEG ‘STACK (00064H O006FH 0000CH DTSEG DATA IH CDSEG CODE 200 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING SEGMENT directive Tn previous chapters, when a segment was defined using full segment definition, no other attributes were mentioned after it. It was simply written name SEGMENT ‘This kind of definition of segments was acceptable since there was only one of each segment of code, data, and stack. However, when there are many modules tobe linked together, the segment definition must be adjusted, The complete segment definition used widely in modular programming is as follows name SEGMENT alignment combine type class name Appendix C (see SEGMENT) gives a complete description of the fields of the SEGMENT directive. A brief explanation of each field is given below. ‘The alignment ficld indicates whether a segment should start on a byte, ‘word, paragraph, or page boundary. For example, if WORD isgiven inthe alignment field, the seement will start atthe next available word. When the WORD boundary is used, ifa previous segment ended at offset 0048H, the next segment will start at OO4AH. The default alignment is PARA, meaning that each segment will start on a paragraph boundary. A paragraph in DOS is defined as 16 bytes; therefore, each segment will start on a 16-byte boundary. When PARA is used, if the previous segment ended at 0048H, the next segment would begin at the next paragraph boundary, which is 00SOH. Paragraph boundaries end in 0; they are evenly divisible by 16 (10H). ‘The combine type field indicates to the linker whether segments of the same type should be linked together. Typical options for combine type are STACK or PUBLIC. An example below shows how to use this field in the stack segment definition to combine the stack segment of a program with the system stack to faring: no stack segment" message generated by the linker. If the combine type is PUBLIC, the linker will combine that segment with other segments of the same type in other modules. This can be used to combine code segments with various names under a single name. The class name field has four options: CODE’, STACK’, ‘DATA’, and “EXTRA, It must be enclosed in single quotes. It is used in combining segments of the same type from various modules. ‘Complete stack segment definition ‘The following stack segment definition in the main module will eliminate the "Warming: no stack segment” message generated by the linker: mame SEGMENT PARA STACK 'STACK’ ‘Complete data and code segment definitions ‘The following is a data segment definition that can be used if no other module has defined any data segment: name SEGMENT PARA ‘DATA’ If any other module has defined a data segment then PUBLIC should be placed between PARA and ‘DATA’. The following are the code and data segment definitions to combine segments from different modules: name SEGMENT PARA PUBLIC ‘CODE’ name SEGMENT PARA PUBLIC ‘DATA’ SECTION 7.1: WRITING AND LINKING MODULES 201 Example 7-4 rewrites Example 7-2 to define segments using the complete segment definition, Example Create a shell for modular programming using the complete segment definition Solution: ‘The main file will contain: TILE = PROG PROGRAM SHELL WITH COMPLETE SEGMENT DEFINITION PAGE 60,132 EXTRN SUBPROG1-FAR EXTRN SUBPROG2FAR PUBLIC declare data here to be shared STSEG SEGMENT PARA STACK’STACK’ DB 100 DUP(?) STSEG ENDS DTSEG SEGMENT PARA ‘DATA’ define data here OTSEG ENDS CODSG_A SEGMENT PARA'CODE’ MAIN, PROC FAR ASSUME CS:CODSG_ADS:DTSEG,SS:STSEG MOV AXDTSEG MOV DS.AX CALL SUBPROG1 —_;CALL SUBPROG CALL SUBPROG2 CALL SUBPROG Mov AHACH INT 21H iGO BACK TO DOS, MAIN, ENDP CODSG_A ENDS END MAIN and in another fle: TITLE SUBPROG! PROGRAM PAGE 60,132 EXTRN ‘declare data that is defined externally PUBLIC SUBPROGt declare procedures that are called externally CODSG_B SEGMENT PARA'CODE’ ‘SUBPROG! PROC | FAR ‘ASSUME CS:CODSG_B je instuctions that perform the work ofthe subroutine go here SUBPROG1 ENDP CODSG_8 ENDS END =————and in another fie: TITLE ~~ SUBPROG2. PROGRAM TO PAGE 60,132 EXTRN ‘declare data that is defined extemally PUBLIC “SuBROG2 declare procedures that are called externally CODSG_C SEGMENT PARA ‘CODE’ ‘SUBPROG2 PROC FAR ‘ASSUME CS:CODSG_C j.the instructions that perform the work of the subroutine go here RET ‘SUBPROG2 ENDP CODSG_C’ ENDS END 202 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING ae Ne ee sw ay SECTION 7.2: Review Questions List three advantages of modular programming, The directive is used within a module to indicate that the named variable can be used by another module. The directive is used within a module to indicate that the named variable was defiried in another module How does the system determine the entry and exit points of a program consisting of ‘more than one module? ‘What is a paragraph? Write the directive used in complete segment definition that will define the stack segment so that it will be combined with the system stack. If'a word-sized data item named TOTAL was defined in module1, code the directive to define TOTAL in module2. TEPARA were used for the alignment type of a code segment that ended at S6H, where would the next code segment begin? Write the code segment directives for a calling program and a module so that they will be combined into one code segment. SOME VERY USEFUL MODULES This section shows the development of two very useful programs that convert from hex to decimal, and vice versa. Then they are rewritten as modules that can be called from any program. Finally, the calling program is written Binary (hex)-to-ASCII (decimal) conversion ‘The result of arithmetic operations is, of course, in binary. To display the result in decimal, the number is first converted to decimal, and then cach digit is tagged with 30H to put it in ASCII form so that it can be displayed or printed. The first step is to convert the binary number to decimal. Look at the following example, which converts 34DH to decimal. 34DH = (3 x 167) + (4 x 16") + (De13 x 160) = (3 x 256) + (4x16) + (13x 2) 768 + 64 +13 845 Another method to convert a hex number to decimal is to divide it repeat- edly by 10 (OAH), storing each remainder, until the quotient is less than 10. The following steps would be performed: 34DH/A = 84 remainder § 84H JA=8 remainder 4 8 ( 0 JA BACK Mov AH,4CH INT 21H (GO BACK To DOS Program 7-3 ASCII (decimal)-to-binary (hex) conversion ‘When a user keys in digits 0 to 9, the keyboard provides the ASCII version of the digits to the computer. For example, when the key marked 9 is pressed, in reality the keyboard provides its ASCII version 00111001 (39H) to the system. In Chapter 3 we showed how in some cases, such as addition, the numbers can be processed in ASCII and there is no need to convert them to hex (binary). However, in the majority of cases the number needs to be converted to hex in order to be processed by the CPU. Look at the example of converting decimal 482 to hex. The following shows the steps to convert this number to hex 482 / 167 = 482/256 = 1 482- (1 256) = 226 226 /16' = 226/16 = 14=E (226 - (14 x 16) =2 482 decimal = 1E2 hexadecimal However, a computer would use a different method since it works in binary arithmetic, not decimal. First the 30H would be masked off each ASCII digit, Then cach digit is multiplicd by a weight (a power of 10) such as 1, 10, 100, or 1000 and they are then added together to getthe final hex (binary) result, Converting decimal 482 to hex involves the following steps. First a user types in “482” through the PC ASCII keyboard, yielding 343832, the ASCII version of 482. Then the following steps are performed: 2x = = 2 8x10 = 80= 50H 4100 = 400= 190H 1E2 hexadecimal 204 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING Program 7-4 converts an ASCII number to binary, It assumes the maxi- mum size of the decimal number to be 65535. Therefore, the maximum hex result is FFFFH, a 16-bit word. It begins with the least significant digit, masks off the 3, and multiplies it by its weight factor. Register CX holds the weight, which is 1 for the least significant digit. For the next digit CX becomes 10 (OAH), for the next it becomes 100 (64H), and so on. The program assumes that the least significant ‘ASCII digit is in the highest memory location of the data. This is consistent with the conventionsof storing ASCII numbers with the most significant digitin the lower memory address and the least significant digit in the highest memory address. For ‘example, placing 749” at memory offset 200 gives offset 200 = (37), 201 = (34), and 202 = (39), DOS 21H function call 0A also places ASCII numbers this way. TITLE PAGE TEN ASCNUM STRLEN BINNUM AGAIN: ASC2B_CON PROC FAR MoV INT. ‘ASC2B_CON ENDP PROGT7-4 CONVERT ASCII TO BINARY 60,132 MODEL SMALL ‘STACK 64 DATA DW 10 DB 09325 be 5 ORG 10H ow oO ‘CODE ‘AX@DATA MOV SAX SUB D101 :CLEAR DI FOR THE BINARY(HEX) RESULT MOV SIOFFSET ASCNUM JEGINNING OF ASCII STRING. MOV BLSTRLEN (BL = LENGTH OF ASCII STRING SUB BHBH 'BH=0 USE BX IN BASED INDEX MODE DEC 8X “BX IS OFFSET TO LAST DIGIT MOV C4 [CX = WEIGHT FACTOR MOV AL[si*Bx! |GET THE ASCIIDIGIT AND ALOFH STRIP OFF 3° SUB AHAH ‘CLEAR AH FOR WORD MULTIPLICATION MUL CX MULTIPLY BY THE WEIGHT. ADD DIAX [ADD {T TO BINARY (HEX RESULT MOV AXCX ‘MULTIPLY THE WEIGHT FACTOR MUL TEN PBY TEN MOV CXAX FOR NEXT ITERATION DEC 8X [DECREMENT DIGIT POINTER NS. AGAIN NJUMP IF COUNTER >= 0 Mov BINNUMDI ISAVE THE BINARY(HEX)RESULT MOV AHACH 2H GO BACK TO DOS END ASC2B_CON Program 7-4 Programs 7-3 and 7-4 have been written and tested with sample data, and now can be changed from programs into modules that can be called by any program Binary-to-ASCIl module Program 7-5 is the modularized Program 7-3. The procedure is declared as public, sit ean be called by another program. All values used are declared external since the data will be provided by the calling program. Therefore, this module does not need its own data segment. Notice the following points about the module: 1. Since this module will be called by another module, no entry point and exit point were given, Therefore, the END directive does not have the label BZASC_CON, 2. The module must return to the caller and not DOS as was the case in Program 7-4. 3. This module does not need its own data or stack segments. SECTION 7.2: SOME VERY USEFUL MODULES 205 PAGE 60,132 THLE PROGT- his module converts a inary hex) number upto FFFFH to decrnal | then makes it displayable ( CALLING PROGRAM SETS [AX = BINARY VALUE TO BE CONVERTED TO ASCII i FFSET ADDRESS WHERE ASCII VALUE IS TO BE STORED MODEL SMALL PUBLIC B2ASC_CON 5 BINARY TO DEGIMAL CONVERSION MODULE Cll) PAGE 60,192 EXTRN ‘CODE B2ASC_CONPROC_ FAR PUSHF ;STORE REGS CHANGED BY THIS MODULE PUSH. BX PUSH DX MOV BX,10 ;BX=10 THE DIVISOR ADD Si4 {SI POINTS TO LAST ASCII DIGIT B2ALOOP: SUB DX.OX ‘OX MUST BE 0 INWORD DIVISION DV Bx {DIVIDE HEX NUMBER BY 10 (BX=10) OR DL.30H STAG '3' TO REMAINDER TO MAKE IT ASCII Mov [sijoL IMOVE THE ASCII DIGIT DEC SI IOECREMENT POINTER CMP AKO (CONTINUE LOOPING WHILE AX > 0 JA B2A_LOOP POP Dx RESTORE REGISTERS POP_ BX POPF RET B2ASC_CON ENDP END Program 7-5 TITLE PROGT-S ASCII TO BINARY CONVERSION MODULE sthis module converts any ASCII number between 0 to 65535 to binary ICALLING PROGRAM SETS SI= OFFSET OF ASCII STRING. [BX= STRING LENGTH - 1 (USED AS INDEX INTO ASCII NUMBER) {THIS MODULE SETS } AX = BINARY NUMBER MODEL SMALL PUBLIC ASC2B_CON TEN:.WORD CODE ‘ASC2B_CON PROC FAR PUSHE ;STORE REGS CHANGED IN THIS MODULE PUSH 01 PUSH CX, SUB IDI {:CLEAR DI FOR THE BINARY (HEX) RESULT MOV CX. jOX= WEIGHT FACTOR A2B LOOP: MOV AL|SHBX] {GET THE ASCII DIGIT AND AL‘OFH [STRIP OFFS" SUB AHAH ICLEAR AH FOR WORD MULTIPLICATION MUL Cx’ IMULTIPLY BY THE WEIGHT. ADD [ADO IT TO BINARY (HEX) RESULT Mov IMULTIPLY THE WEIGHT FACTOR MOL BY TEN Mov FOR NEXT ITERATION DEC Bx’ DECREMENT DIGIT POINTER UNS A2B_LOOP JUMP IF OFFSET >= 0 MOV AXOI ‘STORE BINARY NUMBER IN AX POP Cx IRESTORE FLAGS POP bi POP RET ‘ASC2B_CON ENOP END Program 7-6 206 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING ASCIl-to-binary module Program 7-6 is the modularized version of Program 7-4. Notice the follow- ing points about the module: 1. TEN is defined in the calling program. 2. This module must retum to the caller and not DOS. Calling module Program 7-7 shows the calling program for the module that converts ASCII to binary. This program sets up the data segment, inputs the ASCII data from the keyboard, places itin memory, then calls the routine to convert the number to binary. Finally, the hex result is stored in memory. TITLE —- PROGT-7_ CALLING PROGRAM TO CONVERT ASCII TO BINARY PAGE 60,132, 7 PUBLIC TEN MODEL SMALL Sink 6a asc AREA DABEL vr E MAXLEN DB 6 ACCtEN D3 ASEHUM BE Bourn Okc fe BINNUM = DW 0 Prowir Be ‘PuesscenteR AS DGTNUMBER’'s TEN ow 10 Soo EXTRN ASC2B_CON‘FAR wan Brot ear ,DISPLAY THE PROMPT MOV AH,09 MOV DX,OFFSET PROMPT1 Nan INPUT SRNG MOV AH.OAH. MOV DX‘OFFSET ASC_AREA ae Moy $1 Serserasc_num Mov Sho MOV BLACT_LEN bee Bx CALL ASC2B_CON MOV BINNUMAX” SAVE THE BINARY (HEX) RESULT MOV AHACH INT 24H {60 BACK TO DOS MAIN ENDP END MAIN Program 7-7 Review Questions 1. Show a step-by-step analysis of Program 7-3 with data F624H. Show the sequence of instructions and the data values. 2. Show a step-by-step analysis of Program 7-4 with data ‘1456". Show the sequence of instructions and the data values. SOME VERY USEFUL MODULES 207 SECTION 7.3: PASSING PARAMETERS AMONG MODULES Occasionally, there is a need to pass parameters among different Assembly language modules or between Assembly language and BASIC, Pascal, or C lan- guage programs, The parameter could be fixed values, variables, arrays of data, or even pointers to memory. Parameters can be passed from one module to another through registers, memory, or the stack. In this section we explore passing parame~ ters between Assembly language modules. Passing parameters via registers ‘When there is a need to pass parameters among various modules, one could use the CPU’s registers, For example, if a main routine is calling a subroutine, the values are placed in the registers in the main routine and then the subroutine is called upon to process the data. In such cases the programmer must clearly document the registers used for the incoming data and the registers that are expected to have the result after the execution of the subroutine. In Chapter 4 this concept was demon- strated with INT 21H and INT 10H. Program 7-7 demonstrated this method. In that program, registers BX and SI were set to point to certain data items before the module was called, and the called module placed its result in register AX prior to returning to the calling routine Passing parameters via memory Although parameter passing via registers is widely used in many of the DOS and BIOS interrupt function calls, the limited number of registers inside the CPU is, a major limitation associated with this method of parameter passing. This makes register management a cumbersome task. One altemative is to pass parameters via memory by defining an area of RAM and passing parameters to these RAM locations. DOS and IBM BIOS use this method frequently. The problem with passing parameters to a fixed area of memory is that there must be a universal agreement to the address of the memory area in order to make sure that modules can be run on the hardware and sofiware of various companies. This kind of stand~ ardization is hard to come by. The only reason that BIOS and DOS use an area of, memory for passing parameters is because IBM and Microsoft worked closely together to decide on the memory addresses. Another option, and indeed the most widely used method of passing’ parameters, is via the stack, as discussed nest. Passing parameters via the stack makes the parameters both register and memory independent. Passing parameters via the stack “The stack is a very critical part of every program and playing with it can be risky. When a module is called, itis the stack that holds the address where the program must return after execution. Therefore, if the contents of the stack are altered, the program can crash. This is the reason that working with the stack and passing parameters through it must be understood very thoroughly before one embarks on it Program 7-8, on the following page, demonstrates this method of parameter passing and is written with the following requirements. The main module gets three ‘word-sized operands from the data segment, stores them on the stack, and then calls the subroutine. The subroutine gets the operands from the stack, adds them together, holds the result in a register, and then returns control to the main module. The main module stores the result of the addition. Following the program is a detailed stack contents analysis that will show how the parameters are stored on the stack by the main routine and retrieved from the stack by the called routine, 208 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING TITLE PROGTS PASSING PARAMETERS VIA THE STACK PAGE —— 60,132 MODEL SMALL EXTRN. SUBPROGEFAR STACK 64 DATA VALUE1 DW 3F62H VALUEZ DW 1979 VALUES DW -25FIH RESULT DW 2DUP(?) MAIN PROC FAR, AX@DATA )SAVE VALUES ON STACK PUSH VALUEZ SAVE VALUE? ON STACK PUSH VALUEt }SAVE VALUE1 ON STACK CALL SUBPROGS ‘CALL THE ADD ROUTINE, MOV RESULTAX STORE MOV RESULT#2,8x | THE RESULT MOV AH.ACH INT 21H MAIN ENDP END MAIN Program 7-8: Main Module et THLE SUBPROGS MODULE TO ADD THREE WORDS BROUGHT IN FROM THE STACK PAGE 60,132 MODEL SMALL PUBLIC =SUBPROGS ‘CODE ‘SUBPROGS PROC FAR SUB. BXeX {CLEAR BX FOR CARRIES PUSH BP’ ‘SAVE BP MOV BP,SP- SET BP FOR INDEXING MOV AXIEP}#G [MOV VALUE? TOAX MOV CX{BP}+8 IMOV VALUE2 TO CX MOV OxIBphio (MoV VALUES TOox ADD AX; ADD VALUE2 TO VALUE1 ADC BX,00 KEEP THE CARRY IN BX ADD AXOX {ADD VALUES. ADC = BX,00 (KEEP THE CARRY IN BX Pop BP RESTORE BP BEFORE RETURNING RET 6 [RETURN AND ADD 6 TO SP TO BYPASS DATA SUBPROGS ENDP ‘Stack contents analysis for Program 7-8 To clarify the concept of parameter passing through the stack, the following is a step-by-step analysis of the stack pointer and stack contents. Assume that the stack pointer has the value SP = 17FEH before the "PUSH VALUES" instruction in the main module is executed, 1. VALUE3 = 25FIH is pushed and SP = 17FC (remember little endian: low byte to ow address and high byte to high address) 2. VALUE? = 1979H is pushed and then SP = 17FA. 3. VALUE] = 3F62H is pushed and then SP = 17F8 PASSING PARAMETERS AMONG MODULES 209 4. CALL SUBPROG6 is a FAR call; therefore, both CS and IP are pushed onto the stack, making SP = 17F4. Ifithad been anear call only iP wouldhave been saved. 5. Inthe subprogram module, register BP is saved by PUSHing BP onto the stack, which makes SP = 17F2, In the sabprogram, BP is used to access values inthe stack. First SP is copied to BP since only BP can be used in indexing mode with the stack segment (SS) register. In other words, "MOV AX,[SP+4]" will cause an error. "MOV AX,[BP}#6" loads VALUEI into AX, [BP]+6 = 17F2+6 = 17F8, which is exactly where VALUE is located. Similarly, BP+8 = 17F2+8 = 17FA is the place where VALUE? is located, and BP+10= 17F2H+10 = 17FCH is the location of VALUE3. 6. Afterall the parameters are brought into the CPU by the present module and are processed (in this case added), the module restores the original BP contents by POPping BP from stack. Then SP= Program 7-8: Stack 17F4 : 7. RET 6: This is a new instruction. The RETums Contents Dieta shown previously did not have numbers right after them. The "RET a’ instruction means first to POP CS.IP (IP only ifthe CALL was NEAR) off the top of the stack and then add n to the SP. As can be seca from the Program 7-8 diagram, after popping CS and IP off the stack, the stack pointer is incremented four times, making SP = 17F8. Then adding 6 to it to bypass the six locations of the stack where the parameters are stored makes the SP = 17FEH, its onginal value. Now what would happen if the program had a RET instruction instead of the "RET 6"? The problem is that every time this subprogram is executed it will cause the stack to lose six locations. If that had been done in the example above, when the same routine is called again the stack stars at 17F8 instead of 17FE. Ifthis practice of losing some area of the stack continues, eventually the stack could be reduced to a point where the program would run out of stack and crash Review Questions 1. List one advantage and one disadvantage of each method of parameter passing (a) via register (b) via stack (c) via memory 2, Assume that we would like to access some parameters from the stack. Which of the following are correct ways of accessing the stack? (a) MOV AX,[BP]+20 (b) MOV AX,[SP]+20 (©) MOV AX,[BP+DI] (@ MOV AX|SP+SI] SECTION 7.4: COMBINING ASSEMBLY LANGUAGE AND C PROGRAMS Although Assembly language is the fastest language available for a given CPU, it cannot be run on different CPUs. For example, Intel’s 80x86 Assembly programs cannot be run on Motorola’s 68000 series computers since the opcode, mnemonics, register names, and size are totally different. Therefore, a portable language is needed Why c? Although the dream of a universal language among the peoples of the world is still unrealized, C language is becoming the universal language among all the various CPUs. Today, a large portion of programs written for computers, from PCs to supercomputers such as CRAY, are in the C language. C is such a universal programming language that it can be run on any CPU architecture with little or no modification. Itis simply recompiled for that CPU. The fact that C is such a portable 210 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING language is making it the dominant language of programmers. However, C is not as fast as Assembly language. Combining C and Asscmbly language takes advan- tage of C’s portability and Asscmbly’s speed. Today itis very common to see a software project written using, 70 to 80% C and the rest Assembly language. There are two ways to mix C and Assembly. One is simply to insert the Assembly code in C programs, which is commonly referred to as in-line assembly. ‘The second method is to make the C language call an extemal Assembly language procedure. In this section we first discuss how to do in-line assembly coding and then show a C language program calling an Assembly procedure. Readers without a C programming background can bypass this section without loss of continuity. ‘This section covers Borland’s Turbo C++. Inserting 80x86 assembly code into C programs. In this section we discuss in-lining with Borland’s Turbo C++. For other C compilers, consult their C manual. The following code demonstrates how to change the cursor position to row = 10 and column = 20 in a C program, Assembly instructions are prefaced with "asm", which isa reserved word. Microsoft uses the keyword "_asm”. Note that in Microsoft, not all interrupts may be supported in the latest versions of Visual C++. The following shows two variations of Borland’s format for in-line assembly. version 1: using keyword asm before each line of in-line code */ /* Microsoft uses keyword "_asm* */ main { asm movah,2; —_/* each line shoud end with semicolon or */ asm — mov bh,0; asm movdl,20; —_/* comments must be C style, not ":" assembly style */ asm — mov dh,10; asm int 10h; , 1 version 2: using the keyword asm before a block of in-line code */ I" Microsoft uses keyword "_asm* */ main 0 { asm { mov ah2 ‘mov bh,O mov dl,20 mov dh,10 int toh } } As shown above, each line of in-line code is prefaced by the keyword ‘asm’, or a block of in-line code is prefaced by "asm", Each line must end in a semicolon or newline, and any comments must be in the correct form for C. Example 7-5, on the following page, shows two programs that display a string of data. Solution A uses C language exclusively, Solution B uses Borland’s ‘Turbo C with in-line Assembly code, Notice that in mixing C with Assembly code, Assembly directives such as OFFSET in "MOV DX.OFFSET MESSAGE" are not recognizable by C. Example 7-6 also shows in-line assembly. The in-line code sets the cursor at row = 10 and column = 20 and then displays a string of data using a combination of Borland Cand Assembly. SECTION 7.4: COMBINING ASSEMBLY LANGUAGE AND C PROGRAMS 211 Example 7-5 Solution A: A C language program include main() { : print{"The planet Earth. \n"); Solution B: Turbo C with in-line Assembly ‘char const ‘MESSAGE = "The planet Earth \ns" rman) asm mov ah9 asm mov dx, MESSAGE asm int 2th Example 7-6 Borland C and in-line Assembly code main () int const row int const column Shar Const MESSAGE = "The planet Earth. ns asm { mov ah,2 ‘mov bhvO ‘mov dl.column mov dh,row int 10h PP set cursor position */ mov ah,09 oy dx MESSAGE int 2th PP display message */ y C programs that call Assembly procedures Although in-line assembly is fast and easy, in real-life applications it is common to write Assembly language subroutines and then make them available for C to call as if calling a C function, What is referred to in C language terminology as a function is called a procedure (subroutine) in Assembly language. Before embarking on writing Assembly routines to be used with C, one must firstunderstand how parameters are passed from Cto Assembly language. All high-level languages, such as C, BASIC, FORTRAN, and Pascal, pass parameters to subroutines (Func tions) that they are calling via the stack. Some of them pass the value itself (C, Pascal), while some others pass the address of the value (BASIC, FORTRAN). In BASIC, only the offset address is passed, while in FORTRAN both the segment and offset addresses are passed. Even the order in which they pass parameters differs among high-level languages. The terminology calling convention refers tothe way that a given language passes parameters to the subroutines it calls. The following describes the C calling convention. 212 (CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING 4 ‘cursor (18,12), PreiTh program sets the cursor) {in cursor.asm: ————————~- MODEL SMALL ‘CODE PUBLIC _CURSOR ‘this procedure i wntten to be called by a C program "CURSOR PROC PUSH BP :save the BP (itis being altered) MOV BP.SP suse BP as indexing into stack MOV DH[BP+4) get the x (row) value from stack MOV DOL [BP+6) get the y (column) value from stack Mov AHO2 ‘Set registers fr INT cal Mov BHO INT 40H POP EP “restore BP RET _CURSOR ENDP 7 1982 C calling convention ees How does C pass pa- 1984 a rameters to functions? It is 1985 Bree ee extremely important to un- 1986 derstand this since failure to 1987 'P Bee hekae oar aes do so can cause getting the oo OF wrong data from the stack a 00 _—_|8Pesnotix vale 15=FH when trying to access it through the Assembly sub- 198A oc +6 holds y value 12=CH routine, The following de- igsp_| og | scribes the C calling 198C convention formixing C with Rectan Example 7-7 Stack Contents Diagram ‘cedure tobe called by C must follow these rules: 1. The parameters are passed by value to the stack in reverse order of encountering them. For example, in the function prog (x2), first z is passed, then y, and so on. 2. After parameters are passed in reverse order, Calso saves the address (CS,IP). If is compiled in the SMALL or COMPACT memory model (or if the procedure is NEAR) only the IP is saved. IFC is compiled for MEDIUM, LARGE, or HUGE (or if the procedure is FAR), both CS and IP are passed to the stack (CS is passed first then IP) 3. BP must be saved on the stack and then the parameters must be accessed by the BP register and displacement, since BP is the offset ofthe stack segment (SS) register. 4, The last instruction should be RET with no number after it, since it is the job of C to restore the stack to its original place when it takes back control . Any name shared publicly with C must be prefaced with an underscore, and only the frst eight characters of the name are recognized by C. . C passes the parameters by value except for arrays, which are passed by reference. IEC is compiled in the MEDIUM, HUGE, or LARGE model, use the FAR option for the Assembly language procedure. If C is compiled with the SMALL model, use the NEAR option for the Assembly language procedure. To understand the concepis above, assume that there isa C function named cursor (x.y), whore x and y are the column and row values, respectively. Example 7-1 shows how x and y arc passed tothe stack and then accessed by Assembly code. ‘The step-by-step sequence of the stack contents is shown also. SECTION 7.4: COMBINING ASSEMBLY LANGUAGE AND C PROGRAMS 213 maw When C calls the cursor function, it saves y first, then x, and then the retum address IP, and finally, gives control to the Assembly code. The first instruction of the assembly procedure must be saving the BP register, "PUSH BP". The last instruction must be the RET instruction. ‘Notice that the first two instructions of the procedure must always be saving BP and moving SP to BP. Similarly, the last two instructions must be popping BP and RET. The body of Assembly code goes in between them. This way of accessing arguments in the stack is standard, and saving any other registers will have no effect on displacement calculation as long as the number of PUSH and POP instructions are equal. Example 7-8 shows the cursor routine rewritten to save all registers altered by the routine, ‘The stack contents analysis is shown in the diagram, MODEL SMALL ‘CODE PUBLIC CURSOR :this proc&dure is wrtten to be called by C language __CURSOR PROC PUSH BP :save the BP since contents are atered Mov PSP PUSH AX spush rags altered by this module PUSH DX PUSH BX MOV) DHJeP+4} get x the row valve fom stack MOV DL{BP+6] get the column value from stack Mov AHO2 {Set Up for INT call Mov BH|00 INT 108 POP BX srestore registers POP DX POP AX Pop BP RET CURSOR ENOP END 197E BL How parameters are WE. BH__|BX retumed to C 1980 DL 1981 Dx Inthe preceding sec- tion we described how argu- 1982 AL Ax ments are passed from C to the stack and from there toan | 1984 ep Assembly procedure, What | 1985, happens if a C function ex- i986 pesstor receive an argument? 1987 IP Jap +2 poids retum address en C expects an argument from an Assembly proce- fae OF area aks x vate 15H dure, it expects to find the retumed parameter in certain 198A [OC epee hokey vale 12°CH register(s), depending on the 198B 00 Te ee Ts &S “Example 7-8 Stack Contents Diagram Table 7-1: Returned Values from Assembly Procedures 214 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING In Table 7-1 the register indicates the register used for the return value. If the value returned is a pointer (address), AX will hold [P ifit is NEAR and DX.AX will hold CS:IP ifitis FAR. Thisis illustrated in Example 7-9, where the sum of x, y, and zis retumed to C through DX and AX as expected by C. DX has the higher Word and AX the lower word. In the stack frame illustration, first notice that since the procedure is FAR, both CS and IP are saved on the stack. Therefore, to access the C arguments, itis necessary to use BP+6, BP-+8, and BP+10 displacements ‘As aule, if the Assembly procedure is NEAR, the last argument passed by. Cis accessed by the displacement of BP+4, and if itis FAR, itis accessed by BP+6 displacement. In order not to be bothered by these rules, new assemblers have become more user friendly, as shown in the next topic. ‘Three values of int size are passed by a C function to an Assembly procedure. The assembly code adds them together and retums the total sum back to C, which displays the result. fate net ong st Train() Print("The sum is equal to %u", sum (500,6500,200)), ‘he oewig sum asm MODEL MEDIUM OSE PUBLIC _SUM is far procedure gets three words from the stack and adds ithem together. Atthe end DX:AX has the total sum Proc PUSH save BP Mov BP; use it as SP SUB AX AX ‘lear AX Mov ‘and OX ADD add the first ADC Ant ADC ADD ‘ADC PoP {restore BP RET Igo back to ENDP END New assemblers and linking [— 1750 with C EI In recent years some 17F2 BP ‘SP=EP Assemblers have made link- 17E3 ing with C much easier. Us- 17F4 iP ing MASM 5.1, or TASM 1.0 ‘LIES and higher, ends the need to iF6 | og worry about the displace- 17E7 ment or about beginning the TIFe names common to Cand As- 17E9 hc value pointed at by BP +6 sembly with an underscore or To about saving BP. The pro- V y value pointed at by BP+8 gram in Example 7-9 is re- He written on the follo : page. Notice letierCin direc- 17ED Fee rere tive "MODEL SMALL, C". ‘This automatically makes the Example 7-9 Stack Contents Diagram COMBINING ASSEMBLY LANGUAGE AND C PROGRAMS 215 assembler calculate [BP+n] for all the parameters. Compare thesc two programs to ssce the convenience of the new assemblers. Example 7-9 in the new format follows. MODEL MEDIUM, C CODE PUBLIC SUM sthis FAR procedure gets three words from the stack and adds sthem together. At the end DX:AX has the total sum ‘SUM PROC FAR DATA1:WORD, DATA2:WORD, DATA3:WORD ‘SUB AXAX CLEAR AX MOV OX,AX {CLEAR DX ADD AX,DATAI ADC Xo ADD AX,DATA2 ADC. oxo ADD AX.DATA3 ADC Dx.0 RET SUM ENOP END Passing array addresses from C to the stack ‘The C language passes variables to the stack by value and arrays by a pointer. In other words, the offset address of the array is pushed onto the stack ifthe memory model is SMALL or MEDIUM; otherwise, both the segment and offset address of the array are pushed. Example 7-10 illustrates this point, It uses a C language array to define daily wages for a five-day week, using an unsigned int (0 10 255 range values) data definition. It then uses Assembly code to add them and return the total sum back to C to be displayed. Example 7-10 int wages [5] = (154, 160, G6, 120, 245), exter unsigned short weekpay(nt wages main { ‘cued Pay = %u", weekpay(wages)); ‘weekpay.asm is as follows: -MODEL MEDIUM CODE, PUBLIC WEEKPAY procedure adds five bytes together. ‘Atthe end AX has the total sum -WEEKPAY PROC FAR Moy, BRSP SUB AXAK cxS MOV Si{BP+6] AGAIN: = ADD AL{SI] sad a day's wages ‘ADC AHO INC St ‘increment pointer to next wage INC SI LOOP AGAIN POP SI POP BP RET _WEEKPAY ENDP END ‘save BP isave St 216 CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING Linking Assembly language routines with C The following steps describe how to link Borland C++ with MASM Assembly language routines. 1 Make sure that the Assembly language procedure declares the procedure as PUBLIC. The procedure name should begin with an underscore, For example, ifthe procedure is called "sum" in the C program, it should be "_sum" in the Assembly language routine. Make the Assembly language procedure NEAR for the small model and FAR for the medium model. 2. In the C program, declare the procedure as extemal. 3. Assemble the Assembly language program with MASM to produce the object file: for example, module! obj. 4. Compile the C program to produce the object file: for example, prog] obj. 5. Link them together to produce the executable file. (C> link progl.obj + module L.obj In Borland C++, they can be linked together as follows: C> bee progl.c module].asm ‘The "bec" command will compile the C program. Use the TASM assembler toassemble the Assembly language program, and then link them together. Note that Borland C is case sensitive. If your procedure is called "_SUM" in the Assembly language program and "sum" in the C program, the linker will not be able to link them together. Make the procedure name lowercase in the Assembly language program. If you are using Borland C++, it is recommended not to use the "cpp" filename extension since this will cause the function name to be mangled, and therefore the linker will not be able to find the function. If you must use the “ep” extension, you must compile the C program with the /S option to obtain an Assembly language listing, then see how the function name was mangled and use that name in your Assembly language program. For example, suppose that the function name "sum" was listed as "@sumqSiii". In that case, all references to "sum" in the Assembly language program will have to be changed to "@sumqSiii" in order to allow the program to be linked with the C++ program. Review Questions 1. A Cprogram can either call an Assembly language program or use coding that inserts the Assembly language code into the C program 2. Describe the C convention for passing parameters to functions. SUMMARY Modular programming involves breaking down a project into independent subprograms. Each subprogram accomplishes a specific set of tasks. Good pro- gramming practices dictate that the input and output variables to each subprogram be clearly documented. Variables within subprograms are defined by the EXTRN and PUBLIC directives. These provide the means by which the computer can locate variables, Various methods for passing parameters are used, including passing by register, by memory, or by the stack. ‘The C programming language has gained widespread popularity because of the ease with which code can be ported from one machine to another, However, it is often desirable to include Assembly language programs because of the increased speed that can be gained. Assembly language routines can be called by C programs, or the Assembly language code can be coded directly into the C program by a technique called in-line coding, SUMMARY 217 PROBLEMS L we emeee 10. Fill in the blanks in the following program. The main program defines the data and calls another module to add 5 bytes of data, then saves the result, Note: Some blanks may not need anything. MODEL SMALL STACK 100H_ DATA PUBLIC DATAL DB 35,12,345698 RESULT = DW ? CODE EXTRN FAR HERE: MOV AX,@DATA MOV DS,AX CALL SUM MOV AHSCH INT 21H END In another file there is the module for summing 5 bytes of data: MODEL SMALL DATALBYTE SULT. WORD COUNT EQU_ 5 CODE SUM SUM PROC MOV BX,OFFSET DATAI SUB AX,AX MOV CX,COUNT ADD AL, BYTE PTR [BX] ADC AHO INC BX LOOP AGAIN MOV RESULT,AX RET ENDP END Ifa label or parameter is not defined in a module, it must be declared as Ifa label or parameter is used by other modules, it must be declared as in the present module. List the options for the EXTRN directive when itis referring to a procedure. List the options for the EXTRN directive when itis referring to a data item. List the options for the PUBLIC directive when itis referring to a procedure. List the options for the PUBLIC directive when itis referring to a data item. Convert Program 4-I to the modular format, making each of the INT subroutines a separate module. Each module should be NEAR. Assemble and test the program. Write a program that accepts two unsigned numbers (each less than 999) from the keyboard, converts them to hex, takes the average, and displays the result on the monitor. Use the hex-to-decimal and ASCII-to-hex conversion modules in the text. Write a program (similar to Program 7-1) with the following components. {a) In the main program, two values are defined: 1228 and 52400. (b) The main program calls two separate modules, passing the valucs by stack (0) Inthe first module, the two numbers are multiplied and the result is passed back to the main module. 218 (CHAPTER 7: MODULES; MODULAR AND C PROGRAMMING (d) The second module performs division of the two numbers (52400 /122) and passes both the quotient and remainder back to the main program to be stored. (c) Analyze the stack and its contents for each module if SP = FFF8H immediately before the first CALL instruction in the main module. 11. Write an in-line assembly program to set the cursor to row 14, column 27, and then display the message "This is a test" 12. Modify Example 7-8 to add twelve monthly salaries. The total yearly salary can- not be higher than $65,535, ANSWERS TO REVIEW QUESTIONS SECTION 7.1: WRITING AND LINKING MODULES 1. (1)each module can be developed individually, aliowing parallel development of modules, which shortens. development time, (2) easier to locate source of bugs, (3) these modules can be linked with high-level languages such as C 2. PUBLIC 3, EXTRN 4 the module that is the enty and ext point will havea abe afer the END statement 5, a paragraph consists of 16 bytes and begins on an address ending in OH. 6 name SEGMENT PARA STACK ‘STACK’ 7. EXTRN-_TOTALWORD 8 GOH @ name SEGMENT PARA PUBLIC ‘CODE’ SECTION 7.2: SOME VERY USEFUL MODULES 1 F624/A = 189D remainder DI 189D/A=0276 remainder DI O276/A=003F O03F/A=6 remainder OL=3 ‘Sth iteration:AX=0006 __OO06/A=0 remainder OL=8 ‘AX is now zero, so the conversion is complete: F624H=6301219 2. Astiteration 6x16 Di8 2nd eration: O5xA=32 Di=6+32=38 SECTION 7.3: PASSING PARAMETERS AMONG MODULES (a) by register, one advantage is the execution speed of registers; one disadvantage is that there is a limited ‘numberof registers available so that not many values can be passed (b) by stack, one advantage is it does not use up available registers, one disadvantage is that errors in processing the stack can cause the system to crash. {c) by memory, one advantage is a large area available to store data; one disadvantage is that the program ‘would not be portable to other computers 2. {apond ) ae correct (band (@) are nol correct because SP cannot be used in indexing made with SS SECTION 74; COMBINING ASSEMBLY LANGUAGE AND C PROGRAMS insine 2. parameters are passed by value except for arrays, which are passed by reference; parameters are passed in reverse order of argument ist, after arguments are pushed onto the stack, C pushed CS:1P for FAR procedures ‘or IP for NEAR procedures ANSWERS TO REVIEW QUESTIONS 219 CHAPTER 8 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES Upon completion of this chapter, you will be able to: ‘OBJECTIVES Discuss the major differences betveen the 8086/286 and the 80386/486 List the registers of the 80386/486 machines Diagram the register sizes available in the 386/486 ‘Describe the difference between rea} and protected modes ‘Explain the difference in register: usage between the 8086/286 aid 386/486 Discuss how the increased register size ofthe 386/486 relates to an increased maemory.range Diagram how the "little endian" storage convention of 80x86 machines stores doubleword-sized operands: Code programs for 386/486 machines using extended registers and new direc- tives: 3 Code arithmetic statements using the extended registers of the 386/486 Describe the factors resulting in the increased performance of 386/486 over pre- vious generations of microprocessors 220 ‘CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES [All programs discussed so far were intended for 16-bit machines such as ‘8088, 8086, and 80286 IBM and compatible computers. Although those programs will run on 80386- and 80486-based machines with much improved speed, the true power of 386 and 486 microprocessors shows up when they are switched to protected mode. What is protected mode? As mentioned in Chapter 1, the 386 and 486 can operate in two modes: real mode and protected mode. In real mode they function essentially the same as 8086/286 machines with the exception that 32-bit registers are available, They still have a capacity of addressing a maximum of 1 megabyte of memory. More important, they runall MS-DOS programs without any modification, In protected mode they can access up to 4 gigabytes of memory, but they also require a very complex operating system, one of whose tasks is to assign a privilege level of 0 to 3 (0 being the highest) to cach program nun on the CPU. At this time only the Unix operating system is taking advantage of protected mode in both the 386 and 486 microprocessors. In March 1992, IBM introduced OS/2 version 2.0, designed specifically for 386 systems. This is a 32-bit version of the 08/2 operating system written to take advantage of protected mode in 386 and higher microprocessors. This is in contrast to OS/2 version 1, which was designed for 16-bit 80286 protected mode. The combination of 32-bit processing power and ‘an operating system with multitasking and multiuser capability makes the 386 and 486 computers comparable to the minicomputers of the 1970s at a fraction of the cost. The term multiuser refers to a system that can support more than one terminalikeyboard at a time, Multitasking refers to systems that can execute more than one program at a time. In this chapter we discuss the characteristics of 386 and 486 microproces- sors in real mode that affect programming. Then some program examples will be given that use the 32-bit capability of these machines. Finally, a timing comparison of several programs run across 80x86 machines wili be given in order to appreciate the speed of 386/486-based computers. Protected mode and other capabilities of these processors are discussed further in Chapter 21. SECTION 8.1: 80386/80486 MACHINES IN REAL MODE In this section we concentrate on some of the most important differences between the 8086/286 and 386/486 in real mode, One major difference is the register size. While in the 8086/286 the maximum register size is 16 bits wide, in the 386/486 the maximum size of registers has been extended to 32 bits, Allregister names have been changed toreflectthis extension. Therefore, AX has become EAX, BX is now EBX, and so on, as illustrated below and outlined in Table 8-1. For example, the 386/486 contains registers AL, AH, AX, and EAX with 8, 8, 16, and 32 bits, respectively. In the 86/286, register AX is accessible either as AL or AH or AX, while in the 386/486, register EAX can be accessed only as AL or AH or AX or EAX. In other words, the upper 16 bits of FAX are not accessible as a separate register. The same rule applies to EBX, ECX, and EDX. Registers DI, SI, BP, and SP have become EDI, FSI, EBP, and FSP, respectively. That means the 386/486 can access Di, EDI, SI, ESI, BP, EBP, SP, and ESP. All of these registers are accessible in real mode. 15 a7 6 AH AL The 16-bit register AX (pay, of the 8086/80286. i 16.15 B7 2 AH AL The 32-bit register EAX Poo ay of the 80386/486, SECTION 8.1: 80386/80486 MACHINES IN REAL MODE. 221 In addition to the CS, DS, SS, and ES segment registers, there are also two. new segment registers which are accessible in real mode: FS and GS. With the addition of these new segment registers, there are a total of six segment registers, ‘making it possible to access 384K bytes (6 x 64 384), since each segment register can access up to 64K bytes of memory. Again, all these registers are accessible in real mode. Although both the flag register and IP are extended to 32 bits, only the lower 16 bits are available in real mode. To access all 32 bits of these registers, ‘one must switch to protected mode, There are soveral control registers (CRO, CR1, CR2, and CR3) in protected mode but only bit 0 of CRO is available in real mode. Bit 0 of CRO is the protection-enable bit. When power is applied to the 386/486, it selects real mode automatically and PE (bit 0 of CRO) is low. To switch from real mode to protected mode, this bit must be set to 1. Again, only the Unix and OS/2 ‘operating systems use protected mode at this time. MS-DOS, up to version 5, is not using the protected mode of the 386/486. This might change with future versions of MS-DOS. Table 8-1: Registers of the 80386/486 by Category egisters ry Category (Category ‘Bits ___|Register Names ___ (General | 32 |BAX,EBX,ECX,EDX | 16 |AX,BX,CX,DX {Ss eee __8 _|AH,AL,BH.BL,CH.CL,DH.DL _ Pointer 32. /ESP (extended SP), EBP (extended BP) | po 16 _ |SP (stack pointer), BP (base pointer) \ Index | 32___|ESI (extended SI), EDI (extended DI) a _ |_ 16 [SI (source index), DI (destination index) ‘Segment | 16 |CS (code segment), DS (data segment), I | SS (stack segment), ES (extra segment) | IFS (extra segment), GS (extra segment) _| Instruction a 32. EIP (extended instruction poi I Flag | 32 BER (extended flag register) Control |__32___|CRO, CRI, CR2, CR3 ‘Nore: Only bit 0 of CRO is available in real mode. Ail other control registers are available n protected mode only General registers are pointers in 386/486 Another major change from 86/286 to 386/486 is the ability of general registers such as EAX, ECX, and EDX to be used as pointers. As shown in previous chapters, AX, CX, and DX could not be usedas pointers. For example, an instruction such as "MOV CL,[AX]" would have cause an error in the 86/286 CPU since only BX, SI, DI, and BP were allowed to be used as pointers to memory. This has changed starting with the 80386 microprocessor, In the 386/486 CPU, the following instructions are perfectly legal: Mov AX|ECX] ADD SIIEDX] OR EBX [EAX}+20 It must be noted that when FAX, ECX, or EDX are used as offset addresses, DSis the default segment register. Thatmeans that SS isthe default segmentregister 222 CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHT for ESP and EBP, CS for EIP, and DS for all other registers. The segment override symbol (;) can be used to change the default segment register as shown next as discussed in Chapter 1 MOV AX,FS:[ECX] _:move contents of FS:ECX to AX Calculation of physical addresses in real mode is the same as for the 86/286 In the example above, the physical addresses can be calculated by shifting left the segment register FS one hex digit and adding it to offset ECX. For example, if FS = 12E0 and ECX = 00000120, the physical address specified by FS:ECX would be 14000H (12E00 + 0120 = 14000H). Table 8-2 summarizes addressing modes for the 386/486. ‘There are addi- tional addressing modes available for 386 and higher CPUs which will be covered in future volumes. ‘Table 8-2: Addressing Modes for the 80386/486 Addressing Mode Operand \Default Segment {Register register none immediat data, none Direct {OFFSET} Ds _ pa Register indirect [BX] Ds (stl Ds (D0 [Ds (EAX] Ds [EBX] Ds [BCX] Ds [EDX] DS (Est) Ds HH S [EDI] Ds Based relative [BX}+disp Ds [BP}+disp ss [EAX}+disp Ds [EBX}+disp Ds (ECX}+disp DS [EDX}+disp Ds - [EBP] disp ss Indexed relative [DI}+4isp DS [Si}+disp Ds. [EDI}+disp Ds. SI}+dis Ds Based indexed relative [R1J[R2}+disp If BP is used, segment ‘where RI and R2 are is SS; otherwise, DS is the lany of the above segment ‘Note: In based indexed relative addressing, disp is optional. SECTION 8.1: 80386/80486 MACHINES IN REAL MODE 223 386/486 maximum memory range in real mode: 1M ‘There is a dilemma in the 386/486 working under DOS in real mode. If'in real mode the maximum range of memory is 1M (00000 to FFFFFH), what happens if'a 32-bit register is used as an offset into the segment? The range of the 32-bit offset is 00000000H to FFFFFFFFH; therefore, using a 32-bit register as an offct will place the address range beyond IM. This is a situation that the programmer must avoid in 386/486 computers working with DOS version $ and lower. For example, to execute the instruction "MOV AX,[ESI}", the programmer must make the upper 16 bits of ESI all zeros. This means that the segment offset range in real mode 386/486 under DOS is 0000 to FFFFH. The following are some cases of legal and illegal codings for real mode of the 386/486 under DOS. ADD EAX,(BX) LEGAL ADD ECX,[DX] ILLEGAL! EDX CAN BE USED {AS POINTER BUT NOT DX MOV AX,\WORD PTR [ECX] ;LEGAL ADD EBX [EDX] DLEGAL Accessing 32-bit registers with commonly used assemblers In Assembly language the directive ".386" is used to access the 32-bit registers of 386/486 computers in real mode under DOS and to employ the new instructions of the 386 microprocessor. Every new generation of 80x86 has some new instructions that do not execute on lower processors, meaning that they are upwardly compatible. In other words, using the 386" directive in a program means that the program must be run only on 386 and higher (486 and 586) computers and cannot be run on 8088/86- and 286-based computers. In contrast, all programs in previous chapters were written to be run on any 80x86 computer. The following are additional assembler directives, which indicate the type of microprocessor sup- ported by Microsoft's assembler (MASM) and Borland’s Turbo assembler (TASM), MASM TASM Meaning = 86 eos will run on any 80x86 computer (default) 286 286 will run on any 286 and higher computer, also allows use of new 286 instructions 386 386 will run on any 386 and higher computer; also allows use of new 386 instructions 486 486 will run on any 486 and higher computer; also allows use of new 486 instructions Program 8-1 demonstrates the use of the ".386" directive and the 80386 32-bit instructions. The simplified segment definition was used. The program used the 32-bit register EAX to add and subtract values of various size to demonstrate 32-bit programming of the 386/486 under DOS. Now the question is how to run this program and see the register contents in 386/486 machines. Unfortunately, the DEBUG utility used in earlier chapters cannot be used since it shows only the 16-bit registers. In many assemblers, including MASM and TASM, there are advanced debugging tools that one can use to see the execution of the 386/486 programs. In the case of MASM, the CodeView utility, and for TASM, the Turbo Debugger, are tools that allow one to monitor the execution of the 386/486 in addition to 8086/88 and 286 programs. Below is shown a trace of Program 8-1, using Microsoft's CodeView program. To examine the 32-bit registers in CodeView, press F2 to display registers; then select the Options menu from the top of the screen, and a drop-down menu appears. Select "386" from the drop-down menu to display the registers in 32-bit format 224 CHAPTER 8; 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES PAGE — 60,132 MODEL SMALL 386 ‘STACK 200H DATA ResuLT DD? ‘CODE BEGIN: MOV AX,@DATA Mov DS.AK SUB EAKEAX ADD EAX'100000 ‘ADD EAX,200000 ‘ADD EAX'40000 SUB EAX.80000 SUB EAX,35000 TITLE ADD AND SUBTRACT USING 32-BIT REGISTERS IN 386 MACHINES, Write a program using the 32-bit registers of the 386 to add the values 100000, 200000, and 40000. Then subtract from the total sum the values 80000, 35000, and 250. Place the result in memory loca- tions allocated using the DD, doubleword directive, used for 32-bit numbers. SUB EAX250 {EAX = 36338H - FAH = 36DEEH = 224750 decimal MOV RESULTEAX MOV AHACH INT 21H END BEGIN Program 8-1 File Viev Search Ria Watch Options Language Galle Help | Foetrace 75-Go 4833: 0020 6630400 4933:0030 B44c Ditord Pex [0004] ,RAX aac 4833:0000 583648 ‘MOV = AX, 4836 EAX=00036DEE 4893:0003 6ED8 wov SAX 18x=00000000 4822:0005 662Bc0 SUR BAX, RAK cx=00000000 4833:0008 6605A0960200 ADD «EA, OOOTBGAD -2px=00000000 4839;0008 669540000300 apD—=—=FAX, 00030040 sP=00000200 4833;0014 6605405¢0000 ADD ©——EA, 00009c40, z5P=00000000 4933;001A 662080380100 sus EA, 00013880, - 351=00000000 4833;0020 662088880000 SUB -—~EAX, 00008888, xD1=00000000 4833:0026 6620FA000000._svB___ EAX, 000000FA 036 4823 0000 voy Mov 4833:0032 Cozi mt a. = + +0000 4833:0034 0000 app Byte Ptr [Bx+8Z],AL 4937 4833:0036 0000 ADD Byte Ptr (BX+SI] ,AL 4833 4833:0038 0000 ADD Byte Ptr [BX+ST] ,AL rP20000002¢ 4833:003A 0000 ADD Ryte Ptr [BX+ST] AL 4833:003¢ 0000 app Byte Per [BX#SI] AL nv uP 4833:003E 0000 app Byte Per [BX#SI] AL EI NG Nz AC >t PE NC >t > Ds: 0008 > 00000000 Program 8-1: CodeView Screen of Program Execution SECTION 8.1: 80386/80486 MACHINES IN REAL MODE 225 Little endian revisited Inanalyzing how the 386/486 stores 32-bit data in memory or loads a 32-bit operand into a register, recall the little endian convention: The low byte goes to the ow address and the high byte to the high address. For example, an instruction such as "MOV RESULT,EAX" in Program 8-1 will store the data in this way: OFFSET CONTENTS RESULT d0-d7 RESULT+1 — d8-d15 RESULT#2 16-423 RESULT+3 24-431 Assuming that Example 8-1 struction "MOV [SI],EAX”. Solution: (in HEX) DS:1298 = (56) DS:1299 = (2F) DS:129A = (99) DS:129B = (41) 298 and EAX = 41992FS6H, show the contents of memory locations after the in- aay SECTION 8.2: Review Questions In the 80386/486, the bits of register EDX can be accessed either as DL, bits _to _, or DH, bits__to_,orDX, bits _to__, or EAX, bits to__. In the 386/486 segment size is" _ Bytes. ‘What is real mode? What is protected mode? True or false: The instruction "MOV DX,[AX]" is illegal in the 8086 but "MOV. DX,EAX)" is legal in the 386/486. What is the default segment register when EAX is used as a pointer? What is the purpose of the ".386" directive? If DI = 148F and EBX = 6B2415F9, show the contents of memory after the instruc~ tion "MOV [DI], EBX" is executed, SOME SIMPLE 386/486 PROGRAMS (One way to increase the processing power of the microprocessor is to widen the register size, This allows processing large numbers as 2 whole rather than breaking them into smaller chunks to fit into small registers, The 32-bit registers have become standard in all recent microprocessors. Powerful supercomputers use 64-bit registers. In this section we show revisions of some earlier programs using the 32-bit capability of the 386/486 machines to see the impact of the wider registers in programming. By comparing 32-bit versions of these programs with the 16-bit versions, onc can sce the increased efficiency of 32-bit coding. The impact on speed is discussed in the final section. Adding 16-bit words using 32-bit registers Program 3-Ib used 16-bit registers for adding several words of data. The sum was accumulated in one register and another register was used to add up the carries. This is not necessary when using 32-bit registers. First, refresh your memory by looking at Program 3-1b and then examine Program 8-2, 32-bit version of the same program, written for 386/486 CPUs. 226 CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES Te REVISION OF PROGRAM 3-18 USING 32-BIT REGISTERS PAGE © 60,132 MODEL SMALL 326 STACK 2004 DATA DATAI DD 27345,28621,29593,30105,32375 suM Bb ? COUNT EQU 5 CODE BEGIN: MOV AX.@DATA Mov DSIAX Mov GXCOUNT GX is loop counter Mov SiOFFSETOATA! —“Slis data pointer SUB EAKEAX ‘EAX wil hold sum BACK: ADD EAXBWORDPTRISI] add next word to EAX ADD Si ‘SI points to next word DEC Gx ‘decrement oop counter INZ BACK “continue adding Mov SUMEAX ‘sore sum Mov AHACH INT. 2H ENO BEGIN Program 8-2 TIME PAGE Dara DATA Daas BEGIN BACK: Rewrite Program 3-2 in Chapter 3 to add two 8-byte operands using 32-bit registers. ‘ADD TWO 6-BYTE NUMBER USING 32-BIT REGISTERS IN THE 386 60,132 MODEL SMALL 1386 ‘STACK 200H DATA Da S48FB9963CE7H ORG o010H1 DQ 3FCD4FA23BaDH ORG “00204 0a? CODE MOV” AX,@DATA Mov DS\AX cus. ‘glear carry before first addition MOV SLOFFSETDATAI {Slis pointer for operand’ MOV DLOFFSET DATAZ {Dlis pointer for operand? MOV BX,OFFSETDATA3 BX is pointer for the sum. Mov CX.02 SCX the loop counter MOV EAX,DWORD PTR{SI]__ move the operand to EAX ADC EAX|DWORD PTR [Dl] add the operand to EAX MOV QWORD PTRIEXIEAK ‘star the sum 1 INC Spoint to next dword of operand INC SI INC SI INC SI INC Di spoint to next dword of operand. INC DL INC OL INC DL INC BX INC BX spoint to next dword of sum INC BX INC BX LOOP BACK {if not finished, continue adding MOV AH.CH INT 21H ‘90 back to DOS END BEGIN Program 8-32 SECTION 8.2: SOME SIMPLE 386/486 PROGRAMS 227 Adding multiword data in 386/486 machines In Program 3-2, two multiword numbers were added using 16-bit registers Each number could be as large as 8 bytes wide. That program required a total of four iterations. Using the 32-bit registers ofthe 386/46 requires only two iterations, as shown in Program 8-3a on the previous page. This loop version of the multiword addition program is very long and inefficient. It can be made more efficient by saving the flag register that holds the carry bit of the first 32-bit addition on the stack and then adding four to each pointer instead of incrementing the pointers 4 times. The loop is shown below in Program 8-3b. his revision of Program 3-1a shows how to save the flags before updating tne pointers “BACK: MO ADC EAXDWORDPTR GH aud the operand Io EAX MOV _ DWORO PTR{BX].EAX [store the sum EAX,OWORD PTR [SI] move the operand to EAX PUSHF ‘save the flags ADD Si4 ‘point to next word of operand Add Dla Doin 9 next éword of operand ADD BX.4 {point to next dword of sum. POPF {estore the flags Loop BACK ‘fmol firished, continue adding Program 8-3 Due to the high penalty associated with branch instructions such as the LOOP and Jcondition instructions in the 386/486, it is better to use the nonloop version of this program, shown in Program 8-4. Firstnotice that the dataii stored exactly the same way asiinthe loop version of the program. Data directive DQ is used to set up storage for the 8-byte numbers. First, the lower dword (4 bytes) of DATA1 is moved into EAX, and the lower dword of DATA2 is added to EAX. Then the upper dword of DATA| is moved into EBX, and the upper dword of DATA2 is added to EBX, with any carry that may have been generated in the addition of the lower dwords. EAX now hoids the lower 4 bytes Of the result, and EBX holds the upper 4 bytes of the result. Program 8-4 is much more efficient than using the loop concept. To see why and for a discussion of the impact of branching instructions on the performance of programs in the 80386/486, see Section 8 3. TITLE ‘ADD TWO &-BYTE NUMBERS USING 32-8IT REGISTERS IN THE 386 (NO-LOOP VERSION) PAGE — 60,132 MODEL SMALL 386 STACK 2004 DATA DATA1 DQ S4eFB99630E7H ORG 00t0H DATA? DQ. 3FCDAFAZIBBDH ORG 0020H paras 00? ‘CODE BEGIN: MOV AX,@DATA MOV DS,AX MOV EAXDWORD PTR DATA _moye lower dword of DATA into EAX ADD EAX/DWORD PTR DATA? “aod lower dword of DATA2 10 EAX MOV EBX/DWORD PTR DATA1+4 ove upper cword of DATAM into EBX ADC EBXDWORDPTRDATAZ«4 acd upper dword of DATA2 fo EBX MOV DWORD PTR DATA3.EAX ‘Store lower dword of result MOV DWORDPTRDATAS+4,28X ‘Store uppor dword of result Mov AHACH iNT 24H END BEGIN Program 8-4 228 CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES ‘Multiplying a 32-bit operand by a 16-bit operand in the 386/486 As a final example of the power of 32-bit registers in 386/486 machines, in this. Multiplying a 32-bit register: section we look at the multiplication of a EAX 32-bit operand by a 16-bit operand. See Fig- ure 8-1. Comparing the 386/486 version with by a 32-bit operand: the 86/286 version of this program clearly reveals the coding efficiency of the 32-bit register of the 386/486 systems, First look at 7 the 386/486 version of the multiplication of | The products stored in: 32-bit operand by a 16-bit operand, shown EDX EAX in Program 8-5. Notice that the 16-bit oper- and is placed in a 32-bit register in order to Figure 6-1. 386/486 Mullipication perform 32-bit arithmetic. Write a program using the 386/486 to multiply a 32-bit operand by a 16-bit operand. TITLE MULTIPLICATION OF DOUBLE WORD BY WORD USING 386/486 PAGE — 60,132 ‘MODEL SMALL 386 STACK 2004 DATA DATA: DD 500000 _:MULTIPLICAND (UP To 32.8IT SIZE DATA) BATA? BD $0000 ‘MULTIPLIER (UP TO 16-BIT SIZE) RESULT DQ ? PRODUCT (UP TO 48-BIT SIZE) CODE MAIN: MOV AX.@DATA Mov Os MOV EAXDATAI ‘92.817 OPERAND MUL DWORD PTR OATAZ HIMES 16-81T OPERAND MOV BWORDPTRRESULTEAX SAVE THE RESULT MOV WORD PTR RESULT +4.E0X MOV AHACH INT 2th END MAIN Program 8-5 To appreciate the processing power of the 32-bit registers of the 386/486, the next topic shows a revision of Program 8-5, using the 16-bit registers of the 8086/286 processors. 32-bit by 16-bit multiplication using 8086/286 registers For the sake of clarity in the following discussion, word size WI and W2 will be used to represent the 32-bit multiplicand. To multiply that by the 16-bit operand W3, the following algorithm must be followed, Assume that all values are in hex, w2wi ‘muttipficand x w3 muttiplier waxwi 2 32-bit result + waxw2 a 32-bit result must be shifted left one hex position, then added X3.X2X1 a 48-bit result (X1, X2, X3 are word size) Wi DW. we DW §W2:W1 IS A32-BIT MULTIPLICAND W3 DW. AWS |S A 16-BIT MULTIPLIER SECTION 8.2: SOME SIMPLE 386/486 PROGRAMS 229 TITLE PAGE DATAI DATA RESULT MAIN: x1 DW? x2 DW? iX3X2X1 THE 48-BIT PRODUCT RESULT x3 DW? Mov AX.W1 {GET THE LOW WORD MUL. W3 MULTIPLY MOV X1,AX_ {SAVE THE LOW WORD OF THE PRODUCT MOV X2,0X__ SAVE THE HIGH WORD OF THE PRODUCT MOV AX,W2 {GET THE HIGH WORD MUL W3 :MULTIPLY ADD X2,AX ;ADD THE MIDDLE 16-BIT WORD ADC DX,0 ;PROPAGATE THE CARRY TO DX MOV X3,DX__;SAVE THE HIGH WORD RESULT Now after understanding the process above, Program 8-6 will show how it is actually coded. First, a DD directive is used to define a 32-bit data instead of using DW twice. Similarly, since there is no directive to define a 48-bit data, the DQ (define quad word) directive is used, which defines a 64-bit operand. ‘The unused bits become zeros. MULTIPLICATION OF DOUBLEWORD BY WORD USING 85/286 60,132 MODEL SMALL ‘STACK 200H DATA ‘DD s00000 :MULTIPLICAND (UP TO 32-BIT SIZE DATA) ‘Dw 50000 [MULTIPLIER (UP TO 16-BIT SIZE) Da? :PRODUCT (UP TO 48-8IT SIZE) CODE MOV AXQDATA Mov DS, MOV — AXWORD PTR DATAT ;GET LOW WORD OF MULTIPLICAND MUL — WORD PTR DATAZ !MULTIPLY THE MULTIPLIER MOV WORD PTR RESULTAX JSAVE LOW WORD OF THE PRODUCT MOV WORD PTR RESULT +2,0X SAVE MIDDLE WORD OF PRODUCT. MOV AX.WORD PTR DATAI +2 GET THE HIGH WORD OF MULTIPLICAND MUL — WORD PTR DATA2 IMULTIPLY THE MULTIPLIER. ADD WORD PTR RESULT +2,AX ADD THE MIDDLE 16-BIT WORD ADC DX0 sPROPAGATE THE CARRY TO DX MOV WORD PTR RESULT +4,.0X SAVE THE HIGH WORD RESULT MOV AH,ACH 21H MAIN, ‘Comparing these two programs, one can see why the 32-bit registers have become the standard for all new generations of microprocessors. Review Questions 1. Compare the number of iterations for adding two 8-byte numbers for the following, CPUs. (a) 8085 (a 8-bit) CPU (b) 8086/88/286 (c) 386/486 (d) Cray supercomputer (64-bit system) 2. What data directives are used to define 32-bit and 64-bit operands? 3. What directive is used in MASM to inform the assembler that the program is using 386 instructions? In TASM? 230 CHAPTER 8: 32-BIT PROGRAMMING FOR 386 AND 486 MACHINES SECTION 8.3: 80x86 PERFORMANCE COMPARISON ‘The newer generations of the 80x86 family not only have powerful main- frame features such as protection capabilities, but they also exceute the instructions ‘of previous generations much faster. In the preceding section it was seen how efficient the 32-bit coding can be. In this section we compare the performance of the 80x86 family of processors. To do that, the number of clock cycles (ticks) that each instruction of a given program takes to execute for the 8086, 286, 386, and 486 CPUs will be examined. To fully understand the remaining material in this section, it is necessary to review the introduction to Appendix B, Section B.2. The clock cycles table in Appendix B does not show the total time taken for each processor to execute. This is because such a calculation in terms of microseconds or milliseconds depends on the hardware design of the system, primarily on the factors of working frequency and memory design. Further discussion of the hardware and its impact ‘on the performance of the computer can be found in Chapter 21. Comparing the performance of the 80x86 family can take one of two approaches: 1. Taking a program written for the 8086 and calculating the number of clocks taken to execute it, unchanged, on each of Intel’s 8086, 80286, 386, and 486 microproc- essors, 2. Modifying the same program for 32-bit processing of the 386/486 and then calculating the total number of clocks to execute it for the 386 and 486 microproc- essors. Running an 8086 program across the 80x86 family Intel has employed some very advanced techniques in pipelining to enhance the processing power of 386 and 486 microprocessors, For example, many instruc~ tions that took fouror five clocks to execute on the 8086, take only one or two clocks on the 386 and 486 machines. This is shown next. Problem 3-1b showed a program that calculated the total sum of five words of data, Since the loop is the most time-consuming part of this program, below is shown a comparison of the total number of clocks taken for one iteration across all of Intel's 80x86 processors. The number of clocks for each instruction is taken from Appendix B. 8086 286 86 486 BACK: ADD AX{SI] 14 7 6 2 ADC BX0 4 3 2 1 INC SI 2 2 2 1 INC SI 2 2 2 1 DEC Cx 2 2 2 1 NZ BACK 16/4 73 73 3n total clocks per iteration 40 23 2 9 To calculate the total clocks for the five iterations, simply multiply the total number of clocks for one iteration by 5 and then adjust it for the last iteration, since the number of clocks for no jump in the last iteration is less than for jump in previous iterations. To adjust it, subtract the difference between the jump anid no jump clock. For example, the total clocks for the five iterations in the 8086 column is 40 x 5 = 200, so adjusting for the last iteration involves subtracting 12 (16 ~ 4 = 12) from 200, which results in 188 clocks. The same procedure followed for the 80286 results in total clocks of (23 x 5)-4= 111. ‘The data above shows clearly the power of the newer generation of the 80x86 family. The same program originally written for 8086 machines runs twice as fast on Intel's 386 and four times faster on the 486 microprocessor. This plus the fact that 386/486 microprocessors have 32-bit data buses transferring data in and oat of the CPU makes the case for the 386/486 even stronger. Now the question is SECTION 8.3: 80x86 PERFORMANCE COMPARISON 231 how much faster will itrun ifthe program is rewritten to utilize the 32-bit processing power of the 386/486 CPU. This is shown next. Program 8-2 showed the 32-bit version of the same program. The iteration section of the program and the number of clocks for the 386 and 486 are as follows: 386 486 BACK: ADD EAX,DWORD PTR [Si] 6 2 ADD SI4 2 4 DEC cx 2044 JNZ BACK 73 at total clocks per iteration 7 7 Multiptying for the five iterations gives (5 x 17) ~ 4 = 81 clocks for the 386 and (5 x 7) ~ 2 = 33 clocks for the 486. By comparing the results above, one might conclude that rewriting all 16-bit programs for the 32-bit registers of the 386/486 is about 25% faster. That is true for some applications but not all. For example, examine the case of adding multibyte operands shown next. Compare the performance of Program 3-2 run actoss 80x86 CPUs. The following times assumed two clock cycles for m (memory fetch), 286 386 486 BACK: MOV AXJSI] 10 5 4 1 ADC AXIDI] 14 7 6 2 MOV [BX]AX 10 3 2 1 INC SI 2 2 2 1 INC SI 2 2 2 1 INC DI 2 2 2 1 INC DI 2 2 2 1 INC BX 2 2 2 4 INC BX 2 2 2 1 LOOP BACK 17/5 (oy (ayn 716 total per iteration 53 a7 a7 7 for four iterations: 200 142 148 67 ‘Now compare this result with the 32-bit version of the same program. The modified version was discussed in the preceding section (Program 8-4) and the clock count is as follows: 386486 MOV EAX,DWORD PTR DATA1 ADD EAX,DWORD PTR DATAZ MOV EBX,OWORD PTR DATA1+4 ADC EBX.DWORD PTR DATA2+4 MOV DWORD PTR DATA3,EAX MOV DWORD PTR DATA3+4,EBX Rroron 1 2 1 2 1 : 8 total clocks for the entire operation: 24 Ii the 386, the clock count is reduced from 146 to 24, and for the 486 itis reduced from 67 to 8. Using the same hardware but changing the software to take advantage of the 32-bit capability of the 386/486 sped up the processing power 6-fold (146 divided by 24) and 8.5-fold (67 divided by 8), respectively, for the 386 and 486. The discussion above clearly indicates there is a very heavy penalty associated with branching in the advanced processors. It also shows that if one ‘wants to spend the resources and rewrite the old 16-bit programs for the new 32-bit architecture, it can be well worth the effort. 232 CHAPTER }2-BIT PROGRAMMING FOR 386 AND 486 MACHINES, SUMMARY PROBLEMS Review Questions ‘Compare the execution clocks (refer to Appendix B) for "ADD BX,AX" (ADD reg,reg) for the 8086, 286, 386, and 486 CPUs. Using the 8086 as a base, show the increase in speed as a percentage for each proc- essor in Question | For instruction JNZ, compare the branch penalty for the 8086, 286, and 386. As- sume that m=2. (a) in clocks (b) in percent (notake/take x 100) ‘The 386/486 CPUs represent a major inprovement over the 8086/286 in several areas. Not only are they much faster in terms of execution speed, they also have much better processing power because they are 32-bit machines. They are also capable of accessing an address range of 4 gigabytes of memory. The 386/486 was designed in such a way that all programs written for the 8086/286 will run on it with no modification. Other changes in the 386/486 include two additional segment registers and the ability to use general registers as pointers. In a 386/486 program, show the content of each register indicated in parentheses af- ter exccution of the instruction. (a) MOV EAX,9823F4B6H — (AL,AH,AX and EAX) (b) MOV EBX,985C2H (BL,BH,BX,EBX) {c) MOV EDX,2000000 (DL,DH,DX,EDX) (¢) MOV ES1,120000H (SLESI) Show the destination and its contents in each of the following cases. (a) MOV FAX,209FF94H ADD EAX,34FFFFH (b) MOV EBX,500 000 ADD_ EBX,700 000 (©) MOV EDX.40 000 000 SUB EDX,1 500.000 (4) MOV EAX,39393834H AND EAX,OFOFOFOFH (e) MOV EBX.9FE3SDH XOR EBX,OFOFOFOH Using the little endian convention show the contents of the destination in each case. (a) MOV [SI],EAX ;ASSUME SI=2000H AND EAX=9823F456H (b) MOV [BX],ECX, [ASSUME BX,348CH AND ECX=1F23491H (©) MOV EBX,[DI] :ASSUME DI=4044H WITH THE, [FOLLOWING DATA. ALL IN HEX. DS:4046=(A2) DS:4047=(4C) ‘Compare the clock count for the 80x86 microprocessor in each case: (a) Write a program for the 8086/286 to transfer 50 words of data, one word (16 bits) at a time. Do not use string instructions. (b) Modify the program in part (a) to transfer 2 words (32 bits) at a time and calculate the clock count for the 386 and 486. In both parts (a) and (b), the clock count should be calculated for one iteration and for all iterations. SUMMARY 233 5. Instruction DAA, described in Chapter 3, works only on the AL register, regardless of which of Intel's 80x86 microprocessors is used. Write a program that adds ‘two multibyte packed BCD numbers, each 10 bytes wide (use the DT directive) and compare the clock count for one iteration if itis run on Intel’s 8086, 286, 386, and 486 CPUs. ANSWERS TO REVIEW QUESTIONS ‘SECTION 8.1: 80386/80486 MACHINES IN REAL MODE 0107, 810 15, 010 15, 01031 4k bytes: real mode is similar to the operation of 8086 machines; protected mode assigns a priority to programs and has other advanced features thal take advantage of the 388's power true os allows use of 386 instructions DS:148F =F, DS-1490= 15, 0S1491=24, 0S:1492= 68. ECTION 8 2: SOME SIMPLE 386/486 PROGRAMS ews (C12 (1 386, P386 29 No9R eno 2 8086:3° | 286: 2-386: 2486.1 286 is a 33% improvement over 8086 386 is a 0% improvement over 286 ‘486 is a 100% improvement over 386 3. (2)8086:8 "286-6 386:6 (b) 8086: 400 286: 300 386: 300 3 ‘SECTION 8.3: 80X86 PERFORMANCE COMPARISON 1 2 234 CHAPTER 8: 32-BIT PROGRAMM. G FOR 386 AND 486 MACHINES CHAPTER 9 8088, 80286 MICROPROCESSORS AND ISA BUS OBJECTIVES: == 235 Since the original IBM PCs used 8088 and $0286 microprocessors, this chapter is a detailed hardware study of these two microprocessors, 2s well as the ‘major signals of the ISA bus. In Section 9.1, a detailed look at the 8088 CPU, icluding pin descriptions, is provided. Two IC chips that support the 8088, the 8284 clock generator and the 8288 chip, are discussed in Section 9.2. Next, the IBM PC address, data, and control buses are covered in Section 9.3. In Section 9.4, the 80286 microprocessor is discussed, including pin descriptions. Finally, in Section 9.5, the PC’s ISA buses are covered. SECTION 9.1: 8088 MICROPROCESSOR The first IBM PC used the 8088 microprocessor, and moder PCs still carry that legacy. In this section, the function of each pin of the 8088 CPU is described, as well as how the microprocessor chip is connected with some simple logic gates to create the address, data, and control signals. The 8088 is a 40-pin microprocessor chip that can work in two modes: minimum mode and maximum. mode. Maximum mode is used when we need to connect the 8088 to an 8087 math coprocessor. If we do not nced a math coprocessor, the 8088 is used in imum mode. First we look at the 8088 in minimum mode since it is much simpler and easy to understand. Maximum mode and supporting chips are discussed in Section 9.2. In 1978 Intel introduced the 16-bit microprocessor called the 8086. It was 16-bit both internally and externally. A year later Intel introduced the 8088 to allow the use of 8-bit peripheral chips and to make system boards cheaper. The 8088 is internally identical to the 8086, but has only an 8-bit external data bus. Since the original IBM PC introduced in 1981 used the 8088, we explore the 8088 instead of the 8086. Microprocessor buses Every microprocessor-based system must have three sets of separate buses: the address bus, the data bus, and the control bus. The address bus provides the path for the address to locate the targeted device, while the data bus is used 10 transfer data between the CPU and the targeted device. The control bus provides the signals to indicate the type of operation being executed, such as read or write. Next we discuss how these signals are provided by the 8088 microprocessor. Data bus in 8088 Figure 9-1 shows the 8088/86 in minimum mode. Pins 9-16 (ADO - AD7) are used for both data and addresses in the 8088. At the time of the design of this microprocessor in the late 1970s, due to IC chip packaging limitations, there was a great effort to use the minimum number of pins for external connec tions. Therefore, designers multiplexed the address and data buses, meaning that Intel used the same pins to carry two sets of information: address and data. Notice that the name of the pins reflects this dual function. in the 8088, the address/data bus pins are named ADO - AD7, “AD” for “address/data.” The ALE (address latch enable) pin signals whether the information on pins ADO - ADT is address or data. Every time the microprocessor sends out an address, it activates (sets high) the ALE to indicate that the information on pins ADO - AD7 is the address (A0 - A7). This information must be latched, then pins ADO - AD7 are used to carry data. When data is to be sent out or in, ALE is low, which indicates that ADO - AD7 will be used as data buses (D0 - D7). This process of separating address and data from pins ADO - AD7 is called demultiplexing. 236 ‘CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS. 48 INTR 49CLK 20 GND 20 GND Figure 9-1. The 8086 and 8086 in Minimum Mode ‘Reprinted by permission of Intel Corporation, Copyright Intel, 1989) Veco Ato AIS 12-bit Address bus AB x Bet Ri Address bus x a or 83 Data Bs bus Figure 9-2, Role of ALE in Address/Data Demuttiplexing ———— 237 Address bus in 8088 The 8088 has 20 address pins (AO - A19), allowing it to address a max- imum of one megabyte of memory (220 = 1M). Pins ADO - AD7 provide the AO - ‘AT addresses with the ass tance of a latch. To demulti- plex the address signals from Enable the address/data pins, a latch Output control must be used to grab the addresses, The most widely used latch is the 7418373 IC (sce Figures 9-2 and 9-3). We can also use the 74LS573 chip since it is a variation of the 74LS373 chip. ADO to ADT of the 8088 go into the 74L8373 latch. ALE pro- Figure 9-3. 741373 D Latch vides the signal for the latch- (Reprinted by permission of Texas Instruments, Copyright ing action. For the 8088, the _Te*#s Insrumenss, 1988) TALS373 aa oe ee vidoe: £0", Table 9-1: Control Signal Generation Voc GND AT, while A8 - A15 come [RD_[_WR_| 10M | Signal directly from the micro- > —}~)~ | 9 | MEMIC processor (pins 2 - 8 and pin 39). The last 4 bits of the | ojo | MEMW. address come from AI6 - f0_| 1 [1 [TOR 19, pin numbers 35 - 38. In {i _| 0 | 1 TOW. any system, all addresses imust be latched to provide a [| ° | * | Never happens stable, high-drive-capability address bus for the system (see Figure 9-5). 8088 control bus There are many control signals associated with the 8088 CPU; however, for now we discuss those that deal with read and write operations. The 8088 can access both memory and I/O devices for read and write operations. This gives us four operations for which we need four control signals: MEMR (memory read), MEMW (memory write), JOR (1/0 read), and 1OW (1/0 write). ow FSD MEMW pases za EMW 10m MEMR WENR 1oR ® pf >) Figure 9-4. Control Signal Generation 238 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS The 8088 provides three pins for these control signals: RD, WR, and IO/M. The RD and WR pins are both active low. [O/M is low for memory and high for /O devices. From these three pins, four control signals are generated: IOR, IOW, MEMR, and MEMW, as shown in Figure 9-4 and listed in Table 9-1. Notice that all of these signals must be active low since they go into the RD and WR inputs of memory and peripheral chips that are active low. Figure 9-5 shows the use of simple logic gates (inverters and ORs) to generate control signals. One can use CPLD (complex programmable logic devices) for that purpose and that is exactly what chipsets do in today's PCs. Vee: S_Twiniix co ata Atg 8 Address ALE - 07 ‘ADO 8088 Data Bus tom EWR MER a a MEMW Control ee — Signals ioR Tow Figure 9-5. Address, Data, and Control Buses in 8088-based System ra Rm | 3 4 oN IN Mo Eee CECE cee eee ata t 7 ? H id ‘AD? a float ae float AE eZ NF MEMR: SE SEEEEEE EE fo See EEE SSS See Figure 9-6, ALE Timing Bus timing of the 8088 In Figure 9-6 the timing for ALE is shown. The 8088 uses 4 clocks for memory and V/O bus activities. For example, in the read timing, ALE latches the address in the first clock cycle. In the second and third clock cycles, the read sig- nal is provided. Finally, by the end of the fourth clock cycle the data must be at SECTION 9.1; 8088 MICROPROCESSOR 239 the pins of the CPU to be fetched in. Notice that the entire read or write cycle time is only 4 clock cycles. If the task of reading or writing takes more than 4 clocks due to the slowness of memory or I/O devices, wait states (WS) can be requested from the CPU. This will be demonstrated in Chapter 10. Other 8088 pins Pins 24 - 32 of the 8088 have different functions depending on whether the 8088 is used in minimum mode or maximum mode. As stated earlier. maxi- mum mode is used only when we want to connect the 8088 to an 8087 math coprocessor. In maximum mode, the 8088 needs supporting chips to generate the control signals, as described in the next section. Table 9-2 lists the functions of pins 24 - 32 of the 8088 in minimum mode. ‘Table 9-2: Pins 24 - 32 in Minimum Mode ‘Name and Function FTA (interrupt acknowledge low output signal. Informs interrupt controller that an INTR has occurred and that the vector number is available on the lower 8 lines of the data bus. ALE (address latch enable) Active-high output signal. Indicates that a valid address is available on the extemal address bus. DEN (data enable) Active-low output signal. Enables the 74LS245. This allows isolation of the CPU from the system bus. DT/R (data transmitreceive) Active-low output signal used to control the direction of data flow through the 74LS245 transceiver. TO/M (input-output or memory) Indicates whether address bus is accessing memory or ‘an /O device. In the 8088, it is low when accessing memory and high when accessing VO. This pin is used along with RD and WR pins to generate the four control signals MEMR, MEMW, IOR, and IOW. WR (write) Active-low output signal. Indicates that the data on the data bus is being written to memory or an I/O device. Used along with signal JO/M (pin 28) to generate the MEMW and JOW control signals for write operations. HLDA (hold acknowledge) Active-high output signal. After input on HOLD, the CPU responds with HLDA to signal that the DMA controller can use the buses. “| HOLD (hold) Active-high input ‘the DMA controller that indicates that the device is requesting access to memory and 1/0 space and that the CPU should release control of the local buses. ctive-Tow output signal in) from memory of 1/O to the CPU. Used along with signal IO/M (pin 28) 10 generate MEMR and IOR control signals for read operations. Other pins of the 8088 are described below. MINIMX (minimum/maximum) Minimum mode is selected by connecting MN/MX (pin number 33) directly to +5V; maximum mode is selected by grounding this pin. NMI (nonmaskable interrupt) ‘This is an edge-triggered (going from low to high) input signal to the processor that will make the microprocessor jump to the interrupt vector table after it finishes the current instruction. This interrupt cannot be masked by sofi- ‘ware, as we will see in Chapter 14, 240 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS INTR (interrupt request) INTR is an active-high level-triggered input signal that is continuously ‘monitored by the microprocessor for an extemal interrupt. This pin and INTA are connected to the 8259 interrupt controller chip, as we will see in Chapter 14. cLock Microprocessors require a very accurate clock for synchronization of ‘events and driving the CPU. For this reason, Intel has designed the 8284 clock generator to be used with the 8088 processor. CLOCK is an input and is connect- ced to the 8284 clock generator. It acts as the heartbeat of the CPU. Any irregular- ity causes the CPU to malfunction. The 8284 chip is used whether the 8088 is connected in minimum mode or in maximum mode. The details of the 8284 chip are covered in the next section. READY READY is an input signal used to insert a wait state for slower memories and /O. It inserts wait states when it is low. The READY signal is needed to interface the CPU to low-speed memories and I/O devices. TEST In minimum mode this is not used. In maximum mode, however, this is an input from the 8087 math coprocessor to coordinate communications between the two processors. RESET ‘Table 9-3: IP and Segment Register Yo terminate the present Contents after Reset activities of the microprocessor, a high is applied to the RESE’ put ister__| Contents pin. A presence of high will force the microprocessor to stop all activity and set the major registers to the val- ues shown in Table 9-3. The data in Table 9-3 has certain implications in the allocation of memory space to RAM and ROM that we will clarify (Reprinted by permission of Intel next. Corporation, Copyright Intel Corp. 1983) At what address does the 8088 wake up? According to Table 9-3, when power is applied to the 8088, it wakes up at physical address FFFFOH, since a CS:IP address of FFFF:0000 leads to a phys- ical address of FFFFOH. Therefore, we must have a non-volatile memory such as ROM at the FFEFOH address. This is discussed further in Chapter 10. Review Questions Describe the differences between the external data bus of the 8086 and 8088. In the 8088, pins ADO - AD7 are used for both data and addresses. How does the CPU indicate whether the information on these pins is data or an address? ‘The 8088 memory or I/O read cycles take ___clock pulses to complete. If we do not need an 8087 math coprocessor, the 8088 is connected in mode. 5. Indicate whether each of the following pins are input pins, output pins, or both. (@)ADO-AD7 —(b) ALE (AB- AIS Give the status of the IO/M and RD pins when MEMR is active. Give the status of the IO/M and WR pins when MEMW is active. SECTION 9.1: 8088 MICROPROCESSOR 241 SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS ‘The original IBM PC introduced in 1981 used the 8088 in maximum mode with a socket for the 8087 math coprocessor. In maximum mode, the 8088 requires the use of the 8288 to generate some of the control signals. In this sec- tion we cover the 8088's supporting chips, the 8284 and 8288, and their use in maximum mode. Modern microprocessors such as the Pentium have all these chips incorporated into a single chip. Therefore, this section can be skipped unless {you are interested in the design of the original PC. Figure 9-7 shows the 8086/88 in maximum mode, Comparing Figure 9-7 with Figure 9-1, we see that pins 24 - 32 have different functions. To use the 8088 in maximum mode we must use the 8288 supporting chip. We describe the 8288 next and how it is used with the 8088 in maximum mode. ROIGTO 31 RQIGTT 30 TOCK 29 18 INTR 19 CLK RESET 21 20GND RESET 21 Figure 9-7. The 8086 and 8088 in Maximum Mode (Reprinted by permission of Intel Corporation, Copyright Intel, 1989) 8288 bus controller ‘As shown in Figure 9-8, the 8288 is a 20-pin chip specially designed to provide all the control signals when the 8088 is in maximum mode. The input and ‘output signals are described below. Input signals ‘50, 51, 52 (status input) Input to these pins comes from the 8088. Depending upon the input from the CPU, the 8288 will provide one of the commands or control signals shown in Table 9-4. 242 ‘CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS CLK (clock) ‘This is input from the 8284 clock generator, providing the clock pulse to fIOe. the 8288 to synchronize all command and | | See control signals with the CPU. The 8284 | G|2CLK § 50 19 chip is discussed later in this section. Eee 8 Sie = —— a O}4 DIR MCE/PDEN 17 AEN, an active-low signal, acti- vates the 8288 command output at least | 5S ALE aaa 115 ns after its activation. Inthe IBM PC | cle AEN CEN 15 iisconected tothe AEN gewatoncie: |), Gag NTH 14 CEN (command enable) as AMWC ORC 13 ‘An active-high signal is used to | ©)9 MWTC — AIOWG 12 activate/enable the command signals and ND ONG DEN. In the IBM PC itis connected to | 1" © ee the AEN generating circuitry. 10B (input/output bus mode) Figure 9-8. 8288 Bus Controller An active-high signal makes the 8288 operate in input/output bus mode rather than in system bus mode. Since the IBM PC is designed with system buses, it is connected to low. (Reprinted by permission of Intel Corporation, Copyright intel, 1983) Sz ie $285 Command 10. oO oo l0[ 0 | 1 | Read input/output port TORC lo [1 [0 | Write input/output port TOWC, ATOWC lo Hate None. 1_[-0 [0 | Code access MRDC_ T 0 T Read memory _ MRDC_ [1 [9 _|_Write memory’ MWTC, AMWC it 1 IT Passive None (Reprinted by permission of intel Corporation, Copyright Intel Comp. 1989) Output signals ‘The following are the output signals of the 8288 bus controller chip. ROC (memory read command) This is active low and provides the MEMR (memory read) control signal. It activates the selected device or memory to release its data to the data bus. HAWITC (memory write command), AMIWG (advanced memory write) These two active-low signals are used to tell memory to record the data present on the data bus. These two are the same as the MEMW (memory write) signal, the only difference being that AMWC is activated slightly earlier in order to give extra time to slow devices. SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS 243 TORE (V0 read command) JORC is an active-low signal that tells the /O device to release its data to the data bus. In the PC it is called the IOR (VO read) control signal. FOWE (VO write command), ATOWC (advanced VO write command) Both are active-low signals used to tell the /O device to pick up the data on the data bus. ATOWC is available a little bit early to give sufficient time to slow devices. It is unused in the IBM PC. In the PC, {OWC is labeled as TOW. INTA (interrupt acknowledge) ‘An active-low signal will inform the interrupting device that its interrupt has been acknowledged and will provide the vector address to the data bus. In the IBM PC this is connected to INTA of the 8259 interrupt controller chip. DT/R (data transmitireceive) DT/R is used to control the direction of data in and out of the 8088. In the IBM PC it is connected to DIR of the 74LS245. When the 8088 is writing data, this signal is high and will allow data to go from the A side (o the B side of the 74LS245, so that data is released to the system bus. Conversely, when the CPU is reading data, this signal is low, which allows data to come in from the B to the A side of the 74LS245 data transceiver chip so that it can be received by the CPU. DEN (data enable) An active-high signal will make the data bus either a local data bus or the system data bus. In the IBM PC it is used along with a signal from the 8259 inter- rupt controller to activate G of the 74LS245 transceiver. MCE/PDEN (master cascade enable/peripheral data enable) This is used along with the 8259 interrupt controller in master configura- tion, In the IBM PC the 8259 is used as a slave; therefore, this pin is ignored. ALE (address latch enable) ALE is an active-high signal used to activate address latches. The 8088 multiplexes address and data through ADO - AD7 in order to save pins. In the IBM PC, ALE is connected to the G input of the 741373, making demultiplex- ing of the addresses possible. 8284 CLOCK GENERATOR ‘The 8284 is used in both minimum and maximum modes since it pro- vides the clock and timing for the 8088-based system. Figure 9-9 shows the 8284A, an 18-pin chip especially designed for use with the 8088/86 microproces- sor. It provides not only the clock and synchronization for the microprocessor, but also the READY signal for the insertion of wait states into the CPU bus cycle. A description of each pin and how it is connected in the IBM PC follows. Input pins RES (reset in) This is an input active-low signal to generate RESET. In the IBM PC, it is connected to the power-good signal from the power supply. When the power switch in the IBM PC is turned on, assuming that the power supply is good, a low signal is provided to this pin and the 8284 in tum will activate the RESET pin, forcing the 8088 to reset; then the microprocessor takes over. This is called a cold boot. 244 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS X4 and X2 (crystal in) XI and X2 are the pins to VU which a erystal is attached. The | Gli csync 8 Vcc 18/0 crystal frequency must be 3 times 2 the desired frequency for the | 2 PCLK 8 x1 17/0 microprocessor. The maximum | o|3 AENT A x2 1610 crystal for the 8284A is 24 MHz. The IBM PC is connected to a | )4 ROY! — ASYNT 15 crystal of 14.31818 MHz. c/s READY EFI 14 FE (frequencyiclock) | }6 RDY2 FE 13 This pin provides an option for the way ‘the clock is | 47 AENZ state generated. If connected to low, | Ca} CLK RES the clock is generated by the 8284 with the help ofa ae 9 GND RESET 10 oscillator. If it is connected to high, it expects to receive clocks at the EFI pin, Since the IBM PC = uses. erysal, this pin isconnect- fgSyinat by pero el Corpraton ed to low. Copyright Intel, 1983) EFI (external frequency in) External frequency is connected to this pin if FC has been con- nected to high. In the IBM PC this is not connected since a crystal is used instead of an external frequency generator. CSYNC (clock synchronization) This active-high signal is used to allow several $284 chips to be connected together and synchronized. The IBM PC uses only one 8284; therefore, this pin is connected to low. RDY1 and AENT RDY1 is active high and AENT (address enable) is active low. They are used together to provide a ready signal to the microprocessor, which will insert a WAIT state to the CPU read/write cycle. In the PC, RDY! is connected to DMAWAIT, AENT_ is connected to RDY/WAIT. This allows a wait state to be inserted by either the CPU or DMA. RDY2 and AEN2 These function exactly like RDY | and AEN! but are designed to allow for a multiprocessing system. In the IBM PC, RDY2 is connected to low, AEN2 is connected to high, which permanently disables this fune- tion since there is only one 8088 microprocessor in the system. ASYNC This is called ready synchronization select. An active low is used for devices that are not able to adhere to the very strict RDY setup time requirement. In the IBM PC this is connected to low, making the timing, design of the system easier with slower logic gates. Output signals RESET This is an active-high signal that provides a RESET signal to the 8088. It is activated by the RES input signal discussed earlier. SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS. 245 OSC (oscitlator) ‘This provides a clock frequency equal to the crystal oscillator and is TTL compatible. Since the [BM crystal oscillator is 14.31818 MHz, OSC will provide this frequency to the expansion slot of the IBM PC. CLK (clock) This is an output clock frequency equal to one-third of the erystal oscil- tator, or EFI input frequency, with a duty cycle of 33%. This is connected to the clock input of the 8088 and all other devices that must be synchronized with the CPU. In the IBM PC it is connected to pin 19 of the 8088 microprocessor and other circuitry under the CLK88 label. This frequency, 4.772776 MHz (14.3 1818 divided by 3), is the processor frequency on which all of the timing calculations of the memory and I/O eycle are based. PCLK (peripheral clock) PCLK is one-half of CLK (or one-sixth of the crystal) with a duty cycle ‘of 50% and is TTL compatible. In the IBM PC this 2.386383 MHz is provided to the 8253 timer to be used to generate speaker tones, and for other functions. READY ‘This signal is connected to READY of the CPU. In the IBM PC itis used to signal the 8088 that the CPU needs to insert a wait state due to the slowness of the devices that the CPU is trying to contact. Review Questions 1. Pin RESET is an (input, output) for the 8284 and an (input, output) for the 8088. 2. True or false. Regardless of whether the 8088 is in minimum or maximum mode, the 8284 clock generator is needed to provide a reliable clock. 3. True ot false. The 8288 is used to provide control signals for the 8088 when itis in minimum mode. 4, The 8288 output pin ‘controls the direction data flows in and out of the CPU. SECTION 9.3: 8-BIT SECTION OF ISA BUS Previous sections have explained the 8088 CPU and supporting chips. This section will explain how they are all connected in the original IBM PC to produce the required buses to communicate with memory, input/output peripher- als, and the 8-bit section of the ISA bus. The study of the 8-bit section of ISA is the main topic of this section. A bit of bus history The original IBM PC introduced in 1981 used an 8088 microprocessor, whose 8-bit data bus gave birth to the 8-bit section of the ISA bus. In 1984 when IBM introduced the IBM PC/AT using the 80286 microprocessor, the data bus ‘was expanded to 16 bits. The 8-bit data bus can be scen as a subsection of the 16- bit ISA bus. Very often the 8-bit data bus was referred to as the IBM PC/XT (extended technology) bus in order to differentiate it from the IBM PC AT (advanced technology). Eventually the [BM PC AT bus became known as the ISA (Undustry Standard Architecture) bus since the term “PC AT” was copyrighted by IBM. Throughout this book we use the terms PC/XT and PC interchangeably to refer to the 8-bit portion of the IBM PC AT (ISA) bus. The following is the description of the three main buses of IBM PC as generated by the 8088 and sup- porting chips. 246 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS Local bus vs. system bus In the discussion of PC design we often see the terms local bus and system bus, The sys- tem bus not only provides neces- sary signals to all the chips (RAM, ROM, and_ peripheral chips) on the motherboard, but also goes to the expansion slot for any plug-in expansion card. In contrast, the local bus is connect- ed directly to the CPU. Any com- munication with the CPU must go through the local bus. There is a bridge between the local bus and the system bus to make sure they are isolated from each other. Sometimes the system bus is referred to as a global bus. We use tri-state buffers to isolate the local bus and system bus. For example, 74LS245 is a widely used chip for the data bus buffer since idi- rectional. See Figure 9-10, Figure 9-11 shows an example of local and system buses. Figure 9-11 gives an overview of the 8088 and its supporting chips as designed in the original PC. Notice the role of the 74LS245 and 74LS373 in iso- lating the local and system buses. Everything on the left of the 8288, Figure 9-10. 74LS245 Bidirectional Buffer TALSS735, and. TALSZ45.repree (ReBned hy peminion of lex seamen, Cony sent the local bus and everything "*% !nsrunenss 1985) con the right side of those chips are the system bus. The 74LS245 and 74L$373s play the role of bridge to isolate the local and system buses. Now let's look at each of the buses. Address bus ‘Three 741S373 chips in Figure 9-11 are used for two functions: 1. Tolatch the addresses from the 8088 and provide stable addresses to the entire computer. The address bus is a unidirectional bus. The 741S373 chips are activated by control signals AEN and ALE. When AEN is low, the 8088 pro- vides the address buses to the system. The 8288's ALE (connected to G) enables the 74.8373 to latch the addresses from the CPU, providing a 20-line stable address to memory, peripherals, and expansion slots. Demultiplexing addresses AO - A7 is performed by the 74LS373 connected to pins ADO - ADT of the CPU. The CPU's AB - ALS is connected to the second 74L$373, and A16 - A19 to the third one. Half of the third 74LS373 is unused. 2. To isolate the system address buses from local address buses. The system buses must be allowed to be used by the DMA or any other board through the expansion slot without disturbing the CPU. This is achieved by the 74L8373s through AEN. The AEN signal is described shortly. SECTION 9.3; 8-BIT SECTION OF ISA BUS 247 RESET 7EN (reset drive) ck 8088 so| SOINTKGENGLK] CONTROL BUS fa 8088 gi} —»——_|51 MemR ~] s2}-»———|s2_ 8288 Me ux pew oR 8284A| IReady Ov lReset ALE AEN tow g ADDRESS BUS a ‘AD0-AD7| G A0 a AB-arof one rass73 | || 2 > ate | & "AEN: foe INA 07] __pr an SPIEN}—> iS a 07 8259 spiaata >7ALS245| oy LOCAL BUS Figure 9-11, 8088 Connections and Buses in the PC/XT (Reprinted by permission from “IBM Technical Reference” c. 1984 by International Business Machines Corporation) 1 2 3 ‘14 Clk AS AIS ADO “baa > AD7 Agaress float Data’ float ALE aS S Seas eo see et BRIE ee Ce eee ome \ / DEN $$$ ae Figure 9-12. ALE, DEN, and DTR Timing for the 8088 System Data bus The bidirectional data bus goes through the 74L$245 transceiver (see Figures 9-10 and 9-11). DT/R and DEN are the two signals that activate the 74LS245, DTMR goes to DIR of the 74.8245 and makes the transceiver transmit information from the A side to the B side when D1V/R is high. Conversely, when 248 ‘CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS DT/R makes DIR low, the transceiver transfers information from the B side to the A side, thereby receiving information from the system data bus and bringing it to the microprocessor. DEN (an active low signal) enables the 74LS245. This iso- lates the data buses to make them either a local bus or a system bus. When the 74LS245 is not active, the system data bus is isolated from the local data bus. Control bus ‘The four most important control signals of the IBM PC are TOR (VO. read), TOW (1/0 write), MEMR (memory read), and MEMW (memory write). ‘They are provided by the 8288 chip as shown in Figure 9-11. The timing for the bus activity is shown in Figure 9-12. ‘One bus, two masters While the 8088, the main processor, is designed for fetching and execut- ing instructions, itis unacceptably slow for transferring large numbers of bytes of data such as in hard disk data transfers. Instead, the 8237 chip is used for data transfers of large numbers of bytes. The detailed function of this chip is explained in Chapter 15, All that is needed here is to know that the 8237's job is to transfer data and it must have access to all three buses to do that. Since no bus can serve two masters at the same time, there must be a Way to allow either the 8088 proces- sor or the 8237 DMA to gain control over the buses. This is called bus arbitration and is achieved by the AEN (address enable) generation circuitry. AEN signal generation Table 9-5: AEN Bus Arbitration When the system is turned on, the [AEN | Bus Control 8088 CPU is in control of all the buses. Buses controlled by CPU ‘The CPU maintains control as long as it is Buses controlled by DMA Tr fetching and executing instructions. As can be seen from Figure 9-13, AEN is the output signal of the D flip-flop. Since Q is either high or low, depending on the status of this signal, either the CPU or the DMA can access the buses. Table 9-5 shows the role of AEN in bus arbitra- tion. HLDA to 8237 rp | oir cir 8| tock D3 loo aot E ‘AEN £ Vee TALS175 7ALS175 clock clock _| A A Sh FRQOMA 0 8088 Buses from clk88 of 82848 1 8237 DMA Buses Figure 9-13. AEN Generation Circuitry in the PC/XT (Reprinted by permission from "IBM Technical Reference” c. 1984 by Intemational Business Machines Corporation) ECTION 9.3: 8-BIT SECTION OF ISA BUS 249 Control of the bus by DMA How does AEN become high, handing control of the system buses to DMA? The answer is that when DMA receives a request for service, it will noti- fy the CPU that it needs to use the system buses by putting a LOW on HRQDMA (his is the same as the HOLD signal in minimum mode of the 8088). This in tum will provide a high on the D3 output of the 74LS175, assuming that the current memory cycle is finished and that LOCK is not activated. In the following clock cycle, HLDA (hold acknowledge) is provided to the DMA and AEN becomes high, giving control over the buses to the DMA. Bus boosting ‘One more point that needs explaining is bus boosting of the control, data, and address buses to provide sufficiently strong signals to drive various IC chips. When a pulse leaves an IC chip it can lose some of its strength, depending on how far away the receiving IC chip is located. In addition, the more pins a signal is ‘connected to, the stronger the signal must be to drive them all. Therefore, the sig- nals must be amplified. Stated another way, every pin connected to a given signal has input capacitance, and the capacitances are in parallel; thus as far as that sig- nal is concerned they are all added together, making one big capacitor load, This requires that the signal be strong enough to drive all the inputs (see Chapter 26 for ‘more details on this topic). It is common to combine the functions of bus isolation and bus boosting into a single chip. For example, 74LS373 chips are used to boost the addresses provided by the 8088 microprocessor in addition to the bus isola- tion mentioned earlier. The signals provided by the CPU need boosting since the 8088 is a CMOS chip. CMOS has a much lower driving capability than TTL, of which 74LS373s are made. Likewise, the 74LS245 is used for both data bus booster and data bus isolation. Details of IC interfacing and how 74LS245 chips are used for signal amplification (boosting) are shown in Chapter 26. 8-bit section of the ISA bus As stated earlier, the original [BM PC had an 8-bit data bus. Later with the introduction of the 80286, the 16-bit version of the bus became available. The 80286 bus became known as the ISA bus. The 8-bit bus is a subset of the 16-bit ISA bus and used in many peripheral boards. Figure 9-14 shows the 8-bit portion of the ISA bus expansion slot. From that figure notice that addresses AO - AI9 and data signals D0 - D7 are on the A side of the expansion slot. On the A side, also notice the AEN pin, On the B side are found control signals IOR, 1OW, MEMR, and MEMW. The - sign on these and other control signals implies an active low signal. In Chapter II we use signals AO - 419, DO - D7, AEN, IOR, and OW to design an /O interfacing card. The rest of the signals in Figure 9- 14 will be covered in subsequent chapters. The signals asscociated with interrupts, (IRQs) are covered in Chapter 14; signals associated with DMA (DREQs and DACKs) are covered in Chapter 15. Review Questions The system bus can be accessed either by the CPU or by The control signal that provides bus arbitratio A True or false, After a cold boot, DMA is given control of the buses. The bidirectional data bus goes through the 74LS245 transceiver. Signal determines whether data is flowing from the A to B side or from the Bio A side. 5. Bus is required to provide strong signals to various IC chips in the IBM PC. 250 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS REAR PANEL SIGNAL NAME ‘SIGNAL NAME Gn Bi AI YOCH CK RESET DRV- ‘S07 48V ‘S06 IRC S05 “VDC sp4 Raz 803 “ZV DC- sp2 ows spt +12V DC spo GN BIO AO) “VO CH ROY -SMEM AEN -SMEMR: SAIS +101 SAI8 -1OR- SAIT -DACK: SAI6 DRG: SAIS, -DACKT: SAI4 DRQI- SAI3, -REFRESH SAI2 cL SAN IRQ?. SA10 IR Sag IRQ SAB 18 SAT IRQ’ SAG -DACK2: SAS TH SAS BALE. SA3 +5V Di SA2 Sai GND. SAO Figure 9-14. ISA (IBM PC AT) Bus Slot Signals Detail (8-bit Section) {Reprinted by permission from “IBM Technical Reference” c. 1985 by International Business Machines Corporation) SECTION 9.4: 80286 MICROPROCESSOR ‘The 80286 is a 68-pin microprocessor available in either of two packag- ing formats: LCC (leaded chip carrier) and PGA (pin grid array). This is in con- trast to the 8088, which is a 40-pin DIP (dual in-line package). To package the 68- IC in DIP packaging would have made it a long IC physically and conse- quently more fragile. Such packaging would also necessitate a longer path for some signals and as a result make it unsuitable for use in high-frequency systems. Figure 9-15 shows the 80286 in LCC packaging. ‘The 80286 can work in one of two modes: real mode or protected mode. In real mode, the maximum memory it can access is 1M, 00000H to FFFFFH. To access the entire 16M bytes of memory, 000000H to FFFFFFH, it must work in protected mode. In real mode, the 80286 is a faster version of the 8086 with a few new instructions. When power is applied to the 80286, it starts up in real mode and can be switched to protected mode at any time through a software instruction. However, to use the 286 in protected mode requires an extremely complex mem- ory management system. Since very few systems are using the 286 in protected mode, it is not discussed here (even in protected mode it is still a 16-bit comput- er, meaning that all registers are 16-bit, as opposed to 32-bit). SECTION 9.4: 80286 MICROPROCESSOR 251 Pin descriptions The following are pin descriptions of the 80286 microprocessor. Pins AO - A23 (address bus) ‘These output signals provide a 24-bit address to be used by the decoding circuitry to locate memory or /O, When providing an address for memory, all 24 pins must be used (A0 - A23); therefore, it can access a maximum of 16M bytes ‘of memory (224 = 16M). To access an I/O address, only AO - A15 are used. If the WO address is a 16-bit address, AO - A15 are used to provide the address, and pins A16 - A23 are low. If the VO address is an 8-bit address, only AO - A7 are used, and A8 - A23 are all low. Pins DO - 045 (data bus) ‘These pins provide the 16-bit path for data to be transferred in and out of the CPU. It must be noted that unlike the 8088, the data bus is not multiplexed. ‘The use of separate pins for address and data results in higher pin counts, but saves time since it eliminates the need for a demultiplexer. This 2-byte data path to the CPU allows the transfer of data on both bytes or on either byte, depending ‘on the operation. The 80286 coordinates the activity on the DO - D15 data bus with the help of AO and BHE. Pin BHE (bus high enable) ‘This is an active-low output signal used to indicate that data is being transferred on D8 - D15. Table 9-6 shows how BHE and AO are used to indicate whether the data transfer is on DO - D7, D8 - D15, or the entire bus, DO - DIS. Table 9-6: BHE, A0, and Byte Selection in the 80286 ‘AO_|_ Data Bus Status 0 “Transferring 16-bit data on DO - DIS” r “Transferring a byte on the upper half of data bus D8 - DIS ‘Transferring a byte on the lower half of data bus DO - D7 T Reserved (the data bus is idle) (Reprinted by permission of Intel Corporation, Copyright Intel Corp. 1983) Pin CLK (clock) CLK is an input providing the working frequency for the 80286. The processor always works on half of this frequency. For example, if CLK = 16 MHz, the system is an 8-MHz system. In other words, for the 80286 computer 10 be an 8-MHz system, the CLK must be 16 MHz. Pin M/IO (memory UO select) ‘MA is an output signal used by the CPU to distinguish between /O and memory access. When it is high, memory is being accessed, and when it is low, WO is being addressed. Pin COD/INTA (code/interrupt acknowledge) ‘This is an output signal used by the CPU to indicate whether it is per- forming memory read/write of data or an instruction fetch. It is also used to dis- tinguish between the action of interrupt acknowledge and /O cycle. This signal, along with the status signals and M/IO, is used to define the bus cycle. Pins 57 and S0 (status signals) These status signals for the bus cyele arc both output signals used by the CPU along with M/IO and COD/INTA to define the type of bus eycle. 252 ‘CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS Figure 9-15. 80286 Microprocessor (LCC Packaging) ‘Reprinted by permission of Imel Corporation, Copyright Invel Corp. 1983) Pins HOLD and HLDA (hold and hold acknowledge) HOLD and HLDA allow the CPU to control the buses. HOLD is an input signal to the 80286 and is active high. It is used by devices such as DMA to request permission to use the buses. In response, the CPU activates the output nal HLDA by putting a high on it to inform the requesting device that it has released the buses for the device's use. The DMA has control over the buses as long as HOLD is high, and in response the CPU keeps HLDA high. Whenever the DMA brings HOLD low, the CPU responds by making HLDA low, and regains control over the buses. RESET pin This is an input signal and is active high. When there is a low-to-high transition on RESET (and it stays high for at least 16 clocks), the 80286 initial- izes all registers to their predefined values and the output pins of the 80286 will have the status shown in Table 9-7. Of the above signals, the status of the fol- lowing pins must be noted since they are used in the memory design of the IBM PC AT computers: A20= 1, A21 = 1, A22=1, and A23 = 1. ‘As long as the RESET pin is high, no instruction or bus activity is allowed, The contents of the instruction pointer and segment registers of the 80286 after RESET are shown in Table 9-8. SECTION 9.4: 80286 MICROPROCESSOR 253 Ic must be noted that Table 9-7. Pin State (Bus Idle) During Reset Neva i Tones the 80286 Bose | Name_|_ Signal Level daring Reset to enter into real mode. In |DO-DIS_ | Hi lance other words, the CPU wakes |AQ—A23_ [High up in real mode. In real mode | W/R Low the 80286 (indeed, all the |M/O Thigh x86s from the 80286 to the Pentium4) processor can address only I megabyte since it uses only address lines AO - A19. Since RESET also Table 9-8: IP and Segment Registers causes A20 - A23 to be high, the After RESET first instruction for the 286 must be at physical address FFFFFOH. This [Register| Contents is due tothe fact that at reset, CS | Oe fag F000 and IP = FFFO, making the (Reprinted by permission of Intel Corporation, Copyright Intel Corp. 1983) FFO logical address of the first instruc- ae tion FO00:FFFO. This provides the ‘0000 physical address of FFFFOH on AQ - ‘0000 AI9, and since A20 - A23 is high at reset, the physical address ofthe first -(ggprinied by parmission of Ivel insiriction must be FFFFFOH. This eee en Corp 1983) is 16 bytes from the top of the 16M address range of the 80286. The 80286 expects to have a far jump at location FFFFFOH and when the JMP is exe- cuted, the 286 puts 0s on pins A20 - A23, making it effectively a 1M range real ‘mode system. Further implications of these facts are discussed in Chapter 10. Pin INTR (interrupt request) INTR is an input signal into the 80286 requesting suspension of the cur- rent program execution. Its used for external hardware interrupt expansion along, with the 8259 interrupt controller chip. See Chapter 14 for more information, Pin NMI (nonmaskable interrupt request) ‘ML is an active-high input signal. When this pin is activated, the 80286 will automatically perform INT 2, meaning that there is no INTA response since INT 2 is assigned to it. See Chapter 14 for more details of this pin. READY pin READY is an active-low input signal used to insert a wait state and con- sequently prolong the read and write cycle for slow memory and VO devices. TOCK, PEREQ, BUSY, ERROR, and PEACK ‘These five signals coordinate bus activities with the math coprocessor. Figure 9-16 shows the 80286 timing. Notice the 2-clock cycle time for read. This is discussed further in Chapter 10. Review Questions 1. When power is applied to the 80286, which mode does it wake up in, real mode or protected mode? 2. The 286 can access. of memory in real mode and in protected mode. 3. When RESET is set to high, what are the contents of the CS and IP registers? 254 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS Previous cycle Read cycle Next cycle — Te és | Ts << + + + tre 1 ipezagn (AREA FERN LGR READY FS Figure 9-16. ALE, DEN, and DTR Timing for the 80286 CPU SECTION 9.5: 16-BIT ISA BUS ‘The origin of technical specifications of many of today’s x86 PCs is the 80286-based IBM PC/AT. Much of the PC/AT in turn is based on the original '8088-based IBM PC introduced in 1981. A major legacy of those original PCs is the ISA (Industry Standard Architecture) bus slot. Remember that ISA is another name for the PC/AT bus since PC/AT is a trademark copyrighed by IBM Corp. In this section we examine the address, data, and control buses of the ISA expansion bus and some of the issues related to them. Whether the microprocessor used in a PC is Intel’s Pentium, 386, 486, or an equivalent AMD processor, if it has an ISA bus slot, the material in this section is relevant and needs to be understood if you ‘want to design expansion cards for ISA slots. Figure 9-17 shows the 80286 microprocessor, along with supporting chips used in the original PC/AT computers. The address, data, and control buses in this figure are used throughout the motherboard and are also provided to the ISA expansion slot. In today’s PC the 80286 is replaced with Intel’s Pentium or AMD's Athion microprocessor, and all the control signals are provided by a chipset. A chipset is an IC chip containing all the circuitry needed to support the CPU in a given motherboard. For educational purposes throughout the book, we use simple logic gates from the original PC to discuss some design concepts, even though in the real world the chipset uses CPLDs (Complex Programmable Logic Devices) for design with all the circuitry details buried inside. Next, we examine the major signals of the ISA expansion slot. Exploring ISA bus signals In Section 9.3 we discussed the 8-bit section of the ISA bus. The 8-bit section uses a 62-pin connector to provide access to the system buses. In order to maintain compatibility with the original PC, the 16-bit ISA slot used the 8-bit sec- tion as a subset. A 36-pin connector was added to incorporate the new signals as shown in Figure 9-18, In designing a plug-in peripheral card for the ISA slot we need to understand the basic features of the ISA signals, The ISA. bus has 24 address pins (AO - A23), 16 data pins (D0 - DLS), plus many control signals. ———— $ — SECTION 9.5: 16-BIT ISABUS 255 WENR bows iEMW | Control ioR | Signals TT tow mar TEE eee eee ees TS] oreany}- tt o7 ine ee . £ Bk > Set 8 oot 2 BHE /}———> BHE 8 (bus high enable) 6 (24-bit [>———_ | Address Bus A19-—————_ A20 oe A21 to A22 = A23) A20 A20 control———}_ Arbitration Circuitry Figure 9-17. 60286 Block Diagram and Supporting Chips in the PC AT Address bus Addresses AO - A19 are latched using ALE. These addresses are used throughout the motherboard and are also provided to the 62-pin part of the ISA slot as SAO - SA19 (system address). See Figure 9-18. Notice that this is already latched and cannot be latched again by a plug-in card. The A20 - A23 part of the address is provided in the 36-pin section. In the 36-pin section of the ISA slot, AI7- A23 are also provided as LA17 - LA23 (latchable address). We need to use the ALE signal to latch these addresses in the design of plug-in cards. The ALE signal is provided as BALE (buffered ALE) and can be used to latch LAI7 - LA23 Data bus The data bus is composed of pins D0 to D15. The data bus is buffered by a pair of 74ALS245 data bus transceivers that are used throughout the mother- board to access memory and ports. They are also provided at the expansion slot as SDO - SDI5 (system data). However, it must be noted that SDO - SD7 are pro- vided at the 62-pin part in order to make it compatible with the original 8088- based PC/XT, while SD8 - SDIS show up on the 36-pin part. This allows the 16- bit data bus 10 access any 16-bit peripheral. To select the upper byte or the lower byte of 16-bit data, we use BHE (bus high enable). BHE is latched and used on the system board and also provided at the expansion slot under SBHE (system bus high enable). We will see how to use this pin below. Memory and 1/0 control signals JOR and IOW are the two control signals used to access ports throughout ‘the system. They show up on the 62-pin section of the ISA expansion slot. This 256 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS REAR PANEL SIGNAL NAME GN BI At RESET DRV- +5V Di IRQ -SVDC- DRQz “av +12V DC GN B10 A10: -SMEh -SMEMI I ~10R -DACK: ‘DRA’ -DACKt ‘DRat -REFRES! -MEM cSt OCS 1 Rati IRO1t IRQI: Rat! irate -DACI DR -DACKS Dio cto. DRO! -DAGKE Rae -DACKT- DRQ?- +5V DC -MASTER- ‘GN 18 C18. ‘SIGNAL NAME WO CH CK SA10 SAI SAO COMPONENT SIDE Figure 9-18. ISA (IBM PC AT) Bus Slot Signals (Reprinted by permission from “IBM Technical Reference” c. 1985 by Intemational Business Machines Corporation) SECTION 9.5: 16-BIT ISA BUS 257 Figure 9-19. ISA and PCI Expansion Slots in x86 Motherboard makes them 8088 PC/XT compatible. We will discuss how these signals are used peripheral interfacing in Chapter 11 Signals MEMR, MEMW, SMEMR, and SMEMW are used to access memory. There is a reason for duplicate memory read and write signals, To allow access to any memory within the range of 16 megabytes, read/write control sig- nals are provided to the 36-pin section of the ISA expansion slot under the desig- nations of MEMR and MEMW, respectively. To maintain compatibility with the original 8088-based PC/XT, MEMR and MEMW are designated as SMEMR and SMEMW and are provided on the 62-pin part of ISA on the same strip as the XT bus systems. In other words, MEMR and MEMW can be used to access memory in any location, but to access memory within the } megabyte range, we must use SMEMR and SMEMW on the 62-pin part of the ISA bus. in this case they can be used only to address memory locations 0 - FFFFFH. Of course, to allow the same signals, MEMR and MEMW, from the support chip to show up in two distinctive places with two different names and functions requires some extra logic circuitry. Such details are buried inside the chipsets in today’s PC. Other contro! signals Examining the ISA bus pins in Figure 9-18, we see numerous control sig- nals that we have not seen before. The rest of the control signals in Figure 9-18 are related to the interrupt and DMA chips. IRQ and DMA signals are covered in subsequent chapters. Figure 9-19 shows the expansion slots in today’s x86 PC. In the vast majority of them we have both ISA and PCI buses. The PCI bus is a much faster and superior bus; it is discussed in Chapter 27. Some PC makers offer mother- boards with PCI bus only and no [SA expansion slots. 258 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS ODD and EVEN bytes and BHE In the 36-pin section of the ISA bus there is a pin called SBHE that we explain next, Pin C1 is the same as the BHE pin from the 80286 that we studied in the last section, The BHE pin has to do with the differences between the 8-bit and 16-bit data bus CPUs, Like all general-purpose microprocessors, the memo- ry (and 1/O) space of x86 microprocessors is byte addressable. That means that every address location can provide a maximum of one byte of data. If the CPU has an 8-bit data bus, like the 8088, then the addresses are designated as 0 to FFFFFH, as shown in Figure 9-20. Notice in Figure 9-20 that the bus width for the data bus is only 8 bits. In other words, only 8 strips of wire connect the ‘CPU’S data bus to devices such as mem- ory and /O ports. Since the vast major- ity of memory and 1/O devices also have an 8-bit data bus of DO - D7, their interfacing to CPUs with an 8-bit data bus is simple and straightforward. The CPU's DO - D7 data bus is connected directly to the DO - D7 data bus of memory and /O devices. This is a per- fect match. If the CPU has a 16-1 bus, like 8086/80286/80386SX micro- processors, then the address spaces are designated as odd and even bytes, as shown in Figure 9-21. In such cases, the D0 - D7 byte is designated as even and the D8 - DIS byte as odd. ‘To distin- guish between odd and even bytes, 8086/286/386SX CPUs provide an extra signal called BHE (bus high enable). BHE, in association with the AO pin, is used to select the odd or even byte according to Table 9-9. In Figure 9-21, notice the odd and even banks. ‘They are called odd and even banks since the memory chips have only an 8- bit data bus of DO - D7 and two IC Table 9-9: Distinguishing Between Odd and Even Bytes (BRE | A 0 Even word | DO- Di5| lo 1_|_Odd byte “| D8- DIS 1 [0 [Even byte [| DO- D7 T 1 None b7 bo 00000 00001 00002 FFFFF Figure 9-20. Memory Byte Addressing in 8088 (8-bit Data Bus) (Odd Bank (BHE = 0) DIS 8 00001 00003 ‘0005 FFFFF Even Bank (A0 = 0) D7 Do 00000 0002 00004 FFFFE SECTION 9.5: 16-BIT ISA BU: Figure 9-21. Odd and Even Banks of Memory in 16-bit CPUs (80286) 259 chips must be used, one for cach byte. Although Figure 9-21 shows only 1 megabyte of memory space, the concept of odd and even bytes applies to the entire memory and 1/0 space of the x86 CPU. This is also the case for the 386 and 486 CPUs with a 32-bit data bus. ‘A20 gate and the case of high memory area (HMA) A20 is an anomaly associated with 286 and higher microprocessors that needs to be discussed. In the 8088, when the segment register added to the offset is more than FFFFFH, it automatically wraps around and starts at 00000H. However, in 80286 and higher processors in real mode, such a wrap-around will not occur. Instead, the result will be 100000H, making A20 = 1. The problem is that A20 - A23 is supposed to be activated only when the CPU is in protected mode. To control activation of A20, IBM used a latch controlled by the keyboard in the original PC AT; however, with the introduction of PS computers, control of ‘A20 can also be handled by port 92H. One can use this A20 gate (as it is com- monly called) to create a high memory arca (HMA). This concept is important for understanding HMA memory in x86 PCs and is discussed in Chapter 25. See Examples 9-1 and 9-2 for clarification on this issue. Notice that the process of enabling and disabling the A20 gate i is handled by a piece of soft- ware called the 420 handler, which is provided with MS-DOS and Windows operating systems. Example 9-1 (a) If the A20 gate is enabled, show the highest address that 286 (and higher processors) can access while still in real mode. (b) How far high above 1M is this address? Solution: (@) —_Toaccess the highest physical location in real mode, we must have CS = FFFFH and IP = FFFFH. We shift left the segment register CS and add the offset IP = FFFF: CS shifted left one hex digit FFPFOH adding the offset IP +_FFFFH TOFFEFH ‘Therefore, the addresses FFFFOH - 10FFEFH are the range that the CPU can access while itis in real mode. This is a total of 64K bytes. (b) If the A20 gate is enabled, accessible memory locations above 1M are 100000 to JOFFEF. This is a total of 65,520 bytes, or 16 locations short of IM + 64K. ixample 9-2 ‘Assume that CS = FF25H. Find the lowest and highest physical addresses for (@) the 8088 (b) the 80286 Specify the bit on A20. Solu (2) The lowest physical address is FF250H and the highest is OF24FH (FF250H + FFFFH), since there are only lines AO - A19 in the 8088, the 1 is dropped. (b) In the 286 the lowest address is the same as in the 8088, but the highest physical address is 10F24F; therefore, A20 = | 260 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS Review Questions \. The first IBM PC AT used the microprocessor. 2. What is the advantage of using the 74LS573 chip address latch over the 7A4LS373? What is the purpose of the A20 handler circuitry? What address area is called the high memory area in the 80286? Since the control signals MEMR and MEMW are available on the 62-pin part of the expansion slot, why are they duplicated on the 36-pin part? SUMMARY This chapter began with a detailed examination of the 8088, including a description of all input and output pins and their functions. The IBM PC always uses the 8088 in maximum mode. Next, the 8284A clock generator was described, as well as its role in providing timing synchronization for the bus controller was also described, which is essential for pro for the 8088 CPU in maximum mode. Then, the system buses were examined in further detail in terms of their connections among the various IC chips and how ‘control over them is achieved. In addition, an overview of the core of the IBM PC system, the CPU, the 8284A, and the 8288, was presented. This chapter also explored the 80286 microprocessor. All the pins of the 80286 were described and an overview was given of the differences between real and protected mode. The address, data, and control buses of the 286 were described in terms of accessing the 16M memory range of the 80286. Finally, the ISA bus architecture of the PC AT and the expansion slot were examined. It was shown how these were designed so that devices could remain compatible with PC/XT expansion slots but also take advantage of unique features of the PC AT, such as extended memory. PROBLEMS SECTION 9.1: 8088 MICROPROCESSOR 1. State the main differences between the 8088 and 8086 pinouts. Are the two chips interchangeable? 2. ALEis an ‘input, output) signal for the 8088. 3. What is the maximum number of bytes of memory addressable by the 8088, and why? 4, RESET is an (input, output) signal for the 8088, 5. When the 8088 uses the pins for addresses, they are (input, ‘output, both in and out), but when they are used for data, they will be (input, output, both in and out). 6. To use a math coprocessor with the 8088, one must connect the 8088 in __ (maximum, minimum) mode. 7. True or false. An address must be latched from pins ADO - AD7 in the 8088. 8. Which of the following signals is provided by the 8088 CPU in minimum mode? (a) INTR (b) ALE (¢) WR (@) 1O™M 9. What is the advantage of demultiplexing address/data in the 8088 CPU? 10. What is the penalty (disadvantage) in terms of clocks in Problem 9? 11. ALE is activated in which T state? 12. Why are 8086-based systems more expensive compared to 8088-based sys- tems? 13. To use the 8088 with the 8087 math coprocessor, is the minimum/maximum pin connected to low or high? —————— PROBLEMS 261 262 14, When the input signal RESET in the 8088 is activated, what are the contents of the IP and CS registers? Use the following for the next 3 problems. MEMR* MEMW* (a)0 0 (by 0 1 ©! 0 @1 1 * Active low. 15, Which of the above control signals is activated during the memory read cycle? 16. Which one is activated during memory write? 17, Which of the above absolutely cannot happen at the same time? SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS 18, In maximum mode in an 8088-based system, which chip provides the ALE ignal, the 8088 of the 8288? 19, Which of the following signals are provided by the 8288 chip? (@IOR (b)RESET (c)1IOW (d)NMI_ (¢) EMR (f) MEMW Use the following for the next 3 problems. MEMR* =MEMW* (a) 0 0 (b)0 1 (1 0 @1 1 * Active low. 20. Which of the above control signals is activated during the memory read cycle? 21. Which one is activated during memory write? 22. Which of the above absolutely cannot happen at the same time? SECTION 9.3: 8-BIT SECTION OF ISA BUS 23, When the computer is RESET, which master takes over, the 8088 or DMA? 24, To latch all the address bits of the 8088, how many 74373 chips are need- ed? 25. In the IBM PC, when AEN = 0 it indicates that the (8088 CPU, DMA) is in charge of the buses. Which controls the buses when AEN = 1? 26. Which chip is used for the following? (a) bidirectional bus buffering (b) unidirectional bus buffering 27. In the IBM PC, the 74LS373 is used for which of the following? (a) address latch (b) isolating the address bus (c) address bus boosting (d) all of the above 28. Draw a block diagram for the 8088 minimum mode connection to the 741.8373 and 741.8245. (Modify Figure 9-5.) 29. To access the buses for interfacing with the CPU, AEN must be (low, high). 30. In the 741.S245, to allow the transfer of data from side A to B, DIR and G 31. Answer Problem 30 if data is transferred from side B to A. 32. In the 748245, what happens if G = | and DIR = 0? 33. The 748245 chip is used for (address, data) buses. 34, To allow the passage of data through the 74LS373, G = and OF When is the data actually latched? CHAPTER 3088, 80286 MICROPROCESSORS AND ISA BUS SECTION 9.4: 80286 MICROPROCESSOR, 35, True or false. The 80286 is available in both LCC and PGA packages. 36. The salesclerk at the local computer store says that the 80286 has 24 bits for address and 16 bits for data; therefore, it has 224 times 2 bytes = 33,554,432 bytes = 32M memory space. Is this person right? Give justification for your answer. 37. When AO = 0 and BHE = 0, which section of the data bus (the high byte or the low byte or both) is transferring information? 38. When AO = 0, it makes the address an (odd, even) address. 39. True or false. If CLK is 20 MHz, the $0286-based system is a 10-MHz sys- tem. 40. True or false, The entire 16-megabyte memory space of the 80286 is accessi- ble in real mode. 41. When power is applied to the 80286, it wakes up in (real, protected) mode. 42. Indicate the contents of CS, IP, DS, SS, and ES when power is applied to the 286. 43. In what physical address does the 286 look for the first opcode? 4, Justify your answer in Problem 43. SECTION 9.5: 16-BIT ISA BUS 45. The ISA expansion slot of the 80286 has two parts. How many pins does each part have? State also the number of pins for A, B, C. Which side is the com- ponent side? 46. In the ISA bus, which part of the expansion slot provides signals A20 - A23? 47. In the ISA bus, which part of the expansion slot provides signals D8 - D15? 48. Why is DO - D7 provided on the 62-pin part of the expansion slot? 49. The BHE signal is provided on the (62-pin, 36-pin) section of the ISA bus. Why? 50. If CS = FC48H and IP = 7652H, find the status of A20 for each member of the 80x86 famil 51. Which of the 8088 and 80286 microprocessors have the BHE pin? 52. To access memory anywhere in the 16M range, we must use _ and for the memory write and memory read control signals. Which part Of the ISA bus provides them? 53. True or false. In the 8088, there is no A20 pin. 54, True or false. The 62-pin part of the ISA bus is almost the same as the 62-pit expansion slot of the original PC/XT. ANSWERS TO REVIEW QUESTIONS SECTION 9.1: 8088 MICROPROCESSOR 1, Inthe 8086, pins ADO - ADIS are used for the data bus; the 8088 has an 8-bit exter- nal data bus, pins ADO - AD7. 2. ‘The ALE (address latch enable) pin signals whether the information is data or an address. 32 4 4. minimum 5. (a) both (b) output (c) output 6 7, IO/M = 0 and RD JOM = 0 and WR = 0 ee ANSWERS TO REVIEW QUESTIONS, 263 SECTION 9.2: 8284 AND 8288 SUPPORTING CHIPS 1. output, input 2. true 3. false 4. DIR SECTION 9.3; -BIT SECTION OF ISA BUS 1, 8237 DMA 2. AEN 3. false 4. DUR 5. baffering SECTION 9.4; 80286 MICROPROCESSOR 1. real mode 2. L megabyte, 16 megabytes 3. CS = FOOOH and IP = FFFO SECTION 9.5: 16-BIT ISA BUS 1, 80286 2. The advantage of the 573 is that all outputs are on one side and all inputs on the other, which reduces noise in high-frequency systems and makes the circuit board easier to design. 3. The A20 handler circuitry allows control of the A20 address bit by software, thereby solving the problem associated with the A20 pin in the 80286. 100000H - FFFFFFH 5. They are provided on the 36-pin part to allow access to extended memory. 264 CHAPTER 9: 8088, 80286 MICROPROCESSORS AND ISA BUS CHAPTER 10 MEMORY AND MEMORY INTERFACING 265 This chapter explores memory and memory interfacing of the x86 PC. We first study the basics of semiconductor memory chips, then in Section 10.2 we present memory address decoding using simple logic gates. The memory map and memory space allocation of the PC are discussed in Section 10.3. Section 10.4 explores the issue of data integrity in RAM and ROM. Section 10.5 discusses the CPU’s bus cycle time for memory and shows how to calculate bus bandwidth. The specifies of memory cycle time for the ISA bus are covered in Section 10.6. SECTION 10.1: SEMICONDUCTOR MEMORY FUNDAMENTALS In the design of all computers, semiconductor memories are used as mary storage for code and data. Semiconductor memories are connected directly to the CPU and they are the memory that the CPU first asks for information (code and data). For this reason, semiconductor memories are sometimes referred to as primary memory. The main requirement of primary memory is that it must be fast in responding to the CPU; only semiconductor memories can do that. Among the most widely used semiconductor memories are ROM and RAM. Before we dis- cuss different types of RAM and ROM, we discuss terminology common to all semiconductor memories, such as capacity, organization, and speed. Memory capacity The number of bits that a semiconductor memory chip can store is called its chip capacity. It can be in units of Kbits (kilobits), Mbits (megabits), and so on, This must be distinguished from the storage capacity of computers. While the memory capacity of a memory IC chip is always given in bits, the memory capac- ity of a computer is given in bytes. For example, an article in a technical journal may state that the 64M chip has become popular. In that case, although it is not mentioned that 64M means 64 megabits, it is understood since the article is refer- ring to an IC memory chip. However, if an advertisement states that a computer comes with 64M memory, since it is referring to a computer it is understood that 64M means 64 megabytes. Memory organization Memory chips are organized into a number of locations within the IC. Each location can hold 1 bit, 4 bits, 8 bits, or even 16 bits, depending on how it, is designed internally. The number of bits that each location within the memory chip can hold is always equal to the number of data pins on the chip. How many locations exist inside a memory chip depends on the number of address pins. The number of locations within a memory IC always equals 2* where x is the number of address pins. Therefore, the total number of bits that a memory chip can store is equal to the number of locations times the number of data bits per location. To summarize: 1, Each memory chip contains 2* locations, where x is the number of address pins on the chip. 2. Each location contains y bits, where y is the number of data pins on the chip. 3. The entire chip will contain 2* « y bits, where x is the number of address pins, and y is the number of data pins on the chip. 4, The 2* x y is referred to as the organization of the memory chip, where x is, the number of address pins and y is the number of data pins on the chip. For 2*, use Table 10-1 to give the number of locati in K or M units, 210 =1024= 1 K. Notice that in common speech, IK is 1000 (as in discussing salaries or distance), but in computer terminology it is 1024. 266 ‘CHAPTER 10: MEMORY AND MEMORY INTERFACI Speed 7 Table 19-1: Powers of 2 One of the most important characteristics of @ — memory chip is the speed at which data can be accessed E____|_™ | from it. To access the data, the address is presented tothe [10____|__K address pins, and after a certain amount of time has [11 2K elapsed, the data shows up at the data pins. The shorter [12 aK this elapsed time, the better, and consequently, the more [13 3K, expensive the memory chip. The speed of the memory [ia 16K chip is commonly referred to as its access time. The access time of memory chips varies from a few nanosec- onds to hundreds of nanoseconds, depending on the IC technology used in the design and fabrication. The three important memory characteristics of capacity, organization, and access time will be used exten- sively in this chapter and throughout the book. Many of these topics will be explored in more detail in the context of applications in this and future chapters. Table 10-1 serves as a reference for the calculation of memory ongan- ization. See Examples 10-1 and 10-2 for clarification. [Example 10-1 ‘A given memory chip has 12 address pins and 8 data pins. Find: (a) the organization (b) the capacity Solution: (@) This memory chip has 4096 locations (2'? = 4096), and each location can hold 8 bits of data. This gives an organization of 4096 x 8, often represented as 4Kx8. (b) The capacity is equal to 32K bits since there is a total of 4K locations and each location can hold 8 bits of data. Example 10-2 ‘A512K memory chip has 8 pins for data. Find: (a) the organization (b) the number of address pins for this memory chip Soh (a) Amemory chip with 8 data pins means that cach location within the chip can hold 8 bits of data, To find the number of locations within this memory chip, divide the capacity by the number of data pins, $12K/8 = 64K; therefore, the organization for this memory chip is 64Kx8. (b) The chip has 16 address lines since 26 = 64K. ROM (read-only memory) ROM is a type of memory that does not lose its contents when the power is turned off. For this reason, ROM is also called nonvolatile memory. There are rent types of read-only memory, such as PROM, EPROM, EEPROM, Flash ROM, and mask ROM. Each is explained next. SECTION 10.1: SEMICONDUCTOR MEMORY FUNDAMENTALS PROM (programmable ROM) or OTP ROM PROM refers to the kind of ROM that the user can burn information into. In other words, PROM is a user-programmable memory. For every bit of the PROM, there exists a fuse. PROM is programmed by biowing the fuses. If the information bumed into PROM is wrong, that PROM must be discarded since internal fuses are blown permanently. For this reason, PROM is also referred to as OTP (one-time programmable). The process of programming ROM is also called burning ROM and requires special equipment called a ROM burner or ROM programmer. EPROM (erasable programmable ROM) EPROM was invented to allow changes in the contents of PROM after it is bumed. In EPROM. one can program the memory chip and erase it thousands of times. This is especially useful during development of the prototype of a micro- processor-based project. The only problem with EPROM is that erasing its con- tents can take up to 20 minutes. All EPROM chips have a window that is used to shine ultraviolet (UV) radiation to erase its contents. For this reason, EPROM is also referred to as UV-erasable EPROM or simply UV-EPROM. Figure 10-1 shows the pins for a 64Kbit UV-EPROM chip. Notice the A0 - Al? address pins and 00 - O7 (output) for DO - D7 data pins. The OE (out enable) is for the read signal. To program a UV-EPROM chip, the following steps must be taken: 1. Its contents must be erased. ‘To erase a chip, it is removed from its socket on the system board and placed in EPROM erasure equipment to expose it to UV radiation for 15 - 20 minutes. 2. Program the chip. To program a UV-EPROM chip, place it in the ROM burner (programmer). To bum code and data into EPROM, the ROM burner uses 12.5 volts or higher, depending on the EPROM type. This voltage is referred to as VPP in the UV- EPROM data sheet 3. Place the chip back into its sock- et on the system board. ‘As can be scen from the above steps, in the same way that there is an EPROM programmer (burner), there is also separate EPROM erasure equipment. The main problem, and indeed the major disadvantage of UV-EPROM, is that it cannot be programmed while in the system board (motherboard). To find Figure 10-1. UV-EPROM Chip a solution to this problem, EEPROM (Reprinted by permission of Intel Corporation, ‘ves favenibd! Copyright Intel Corp, 1987) HBoGg0D GOO oOooGooOD 268 (CHAPTER 10: MEMORY AND MEMORY INTERFACING EEPROM (electrically erasable programmable ROM) EEPROM has several advantages over EPROM, such as the fact that its method of erasure is electrical and therefore instant, as opposed to the 20-minute erasure time required for UV-EPROM. In addition, in EEPROM, one can select which byte to be erased, in contrast to UV-EPROM, in which the entire contents of ROM are erased, However, the main advantage of EEPROM is the fact that one can program and erase its contents while itis still in the system board. It does not require physical removal of the memory chip from its socket. In other words, unlike UV-EPROM, EEPROM does not require an external erasure and pro- gramming device. To utilize EEPROM fully, the designer must incorporate into the system board the circuitry to program the EEPROM, using 12.5 V for VPP. EEPROM with VPP of 5 - 7 V is available, but it is more expensive. In general, the cost per bit for EEPROM is much higher than for UV-EPROM. ‘Table 10-2: Examples of ROM Memory Chips Part Number] Speed (ns) | Capaci ~ Organization | Pins | VPP_ |UV-EPROM | “2716 450 ese 24 [25 2716-1 350 16K 2Kx8 24 [25 37168 450 16K 2Kx8 24 [125 2732A-45 | 450 32K AKx8 24 [21 2732A-20__ | 200 32K AKx8 24 [2 27032 450. 32K aKx8 24__[ 25 2764A-25__ | 250 64K BKXS 28__[ 125 27C64-15 [150 AK BKx8. 28 | 125 27128-20 | 200 128K T6KxB. 28 [125 27C128-25 | 250 128K. T6Kx8 28 | 12.5 7256-20 | 200 256K 32Kx8 28 [125 27C256-20 | 200 256K 32Kx8 28 [125 27512-25 | 250 312K 64KXB 28 [12.5 37C512-25_| 250 512K 6AKXB 28 [125 27C010-12_| 120 1M, T28KxB 32_ | 125 27C201-12 | 120 2M 256Kx8 32_| 25 27CA01-12_ | 120 aM ‘S12Kx8 32_[ 125 EEPROM |~28C16A-25 | 250 16K 2KxB. 24 |S 2864 250 64K BKx8 28 [5 28C256-15_| 150 256K, 32Kx8 28 [5 28C256-25 | 250 236 32x8 28_ [5 \Flash ROM | 28F256-20 | 200 256K 32Kx8 32 [12 28F256-15 _ | 150 256K, 32Kx8 32 [2 28F010-20 | 200 IM T28Kx8 32_| 12 28F020-15 _ | 150 2M 256Kx8 32:12 ‘Table 10-2 shows examples of some popular ROM chips and their char- acteristics. Notice the patterns of the IC numbers. For example, 27128-20 refers to UV-EPROM that has a capacity of 128K bits and access time of 200 nanosce- onds. The capacity of the memory chip is indicated in the part number and the access time is given with a zero dropped. In part numbers, C refers to CMOS technology. While 27xx is for UV-E PROM, 28xx is for EEPROM. SEMICONDUCTOR MEMORY FUNDAMENTALS 269 [Example 10-3 For ROM chip 27128, find the number of data and address pins, using Table 10-2. Solution: ‘The 27128 has a capacity of 128K bits. Table 10-2 also shows that it has 16Kx8 organization, which indicates that there are 8 pins for data, and 14 pins for address (24 = 16K), ey] 8 8 ]2 | vw is NN IS | a2 see yee AT 12 | ar a7) a7 jaz iar} 48 as | as | as} as) AS as as | as} as| a4 at | aa | as lag] a3 as as | as tas) a2 a2] az | az iiaz| ad ari at Jat iat] ao ao | ao | ao ao} oo 0 | 00 | oo | 00 or] or | or ffor} ot 02} 02 | o2//o2| 2 ]GND|| GND | GND||GND} GNO Figure 10-2. Pin Configurations for 27xx ROM Family Flash Memory Since the early 1990s, Flash ROM has become a popular user-program- mable memory chip, and for good reasons. First, the process of erasure of the entire contents takes only a few seconds, or one might say in a flash, hence its name: Flash memory. In addition, the erasure method is electrical and for this rea- son it is sometimes referred to as Flash EEPROM. To avoid confusion, it is com- monly called Flash ROM. The major difference between EEPROM and Flash memory is the fact that when flash memory's contents are erased the entire device is erased, in contrast to EEPROM, where one can erase a desired section or byte. Although there are some flash memories recently made available in which the contents are divided into blocks and the erasure can be done block by block, unlike EEPROM, no byte erasure option is available. Because Flash ROM can be programmed while it is in its socket on the system board, it is widely used 10 upgrade the BIOS ROM of the PC or the operating system on Cisco routers. Some designers believe that flash memory will replace the hard disk as a mass storage medium. This would increase the performance of computers tremendously, since flash memory is semiconductor memory with access time in the range of 100 ns compared with disk access time in the range of tens of milliseconds. For this to happen, flash memory's program/erase cycles must become infinite, just like hard disks. Program/erase cycle refers to the number of times that a chip can be erased and programmed before it becomes unusable. At this time, the program/erase cycle is 500,000 for Flash and EEPROM, 2000 for UV-EPROM, and infinite for RAM and disks. In Table 10-2, notice that the part number for Flash ROM uses the 28Fxx designation, where F indicates the Flash type ROM. 270 CHAPTER 10: MEMORY AND MEMORY INTERFACING Mask ROM Mask ROM refers to a kind of ROM whose contents are programmed by the IC manufacturer. In other words, it is not a user-programmable ROM. The terminology mask is used in IC fabrication. Since the process is costly, mask ROM js used when the needed volume is high and it is absolutely certain that the contents will not change. It is common practice to use UV-EPROM or Flash for the development phase of a project, and only after the code/data have been final- ized is mask ROM ordered. The main advantage of mask ROM js its cost, since it is significantly cheaper than other kinds of ROM, but if an error in the data is found, the entire batch must be thrown away, RAM (random access memory) RAM memory is called volatile memory since cutting off the power to the IC will mean the loss of data. Sometimes RAM is also referred to as RAWM (read and write memory), in contrast to ROM, which cannot be written to. There are three types of RAM: static RAM (SRAM), dynamic RAM (DRAM), and NV- RAM (nonvolatile RAM). Each is explained separately. SRAM (static RAM) Storage cells in static RAM memory are made of flip-flops and therefore do not require refreshing in order to keep their data. This is in contrast to DRAM, discussed below. The problem with the use of flip-flops for storage cells is that each cell requires at least 6 transistors to build, and the cell holds only 1 bit of data, In recent years, the cells have been made of 4 transistors, which is still too many. The use of 4-transistor cells plus the use of CMOS technology has given birth to a high-capacity SRAM, but the capacity of SRAM is far below DRAM. Table 10-3 shows some exampies of SRAM. SRAMS are widely used for cache ‘memory, which is discussed in Chapter 22, Figure 10-3 shows the pin diagram for the 6116 SRAM chip. The 6116 has an organization of 2Kx8, which gives a capacity of 16 Kbits, as indicated in the part number. The following is a descrip- tion of the 6116 SRAM pins. ‘AO - All are for address inputs, where 12 address lines gives 216 = 2K. 00 - 107 are for data VO, where 8-bit data lines gives an organiza- tion of 2Kx8. WE (write enable) is for writing data into SRAM (active low). OE (output enable) is for read- ing data out of SRAM (active low) CS (chip select) is used to select the memory chip. The functional diagram for the 6116 SRAM is given in Figure 10-4. Figure 10-5 shows the following steps to write data into SRAM. 1. Provide the addresses to pins AO - ALL ‘Activate the CS pin. Make WE = 0 while RD = Provide the data to pins 1/00 - 1/07. Make WE = | and data will be writ- ten into SRAM on the positive edge of the WE signal, Figure 10-3. 6116 2Kx8 SRAM SECTION 10.1: SEMICONDUCTOR MEMORY FUNDAMENTALS 2m VO CONTROL CONTROL CIRCUIT setup = hold? Figure 10-5. Memory Write Timing for SRAM) Address <—__—naress vai > 8 EEG ENE ee ee BOE ETI CEEEES Sco Steed eee Data out : Kawase > tan —__*_ Figure 10-6. Memory Read Timing for SRAM 272 ‘CHAPTER 10: MEMORY AND MEMORY INTERFACING

You might also like