Bem-vindo(a) ao Scribd!
Escolar Documentos
Profissional Documentos
Cultura Documentos
Passatempos e Artesanato Documentos
Crescimento Pessoal Documentos
) E CS:107 Ad 04 02 90 (press ) E DS:0205 2 2A 2A (press ) The first E command stores the 3 words (6 bytes) at the start of the data area at offset 8200. You have to key in each of these words with the bytes reversed, so that 0123 is 2301 and 0025 is 2500. When a MOV instruction subsequently accesses these words and loads them into a register, it “warevesses” the bytes, so that 2301 becomes 0123 and 2500 be- comes 0025. “The second E command stores three asterisks (***), defined as 2A2A2A so that you can view them later using the D (Dispiay) command. Otherwise, these asterisks serve no pasticular purpose in the data area. Figure 3.3 shows all the steps in the program, including the E commands. Your screen should display similar results, aithough addresses in the CS and DS probably differ. To ex- amine the stored data (at DS:200H through 208) and the instructions (at CS:100H through, TOAH), key in the folowing D commands: To view the cove: D €$:100,20A DEBUG displays the address of the code segment and the offset (0100) as xxxx:0100. Type in the following instructions, each followed by : Moy cL.42 Enters Mv 0L,28 ADD CL,DL <énter> oP sinters, again to exit from the A command, ‘That's one extra , which tells DEBUG you bave nn mere symbolic instructions (0 enter. On completion, DEBUG should display the following:a Executing Computer Instructions Chap. 3. 000100 Nov cL.4z xxxs0102 mov DL,2A RXX:0104 Apo CLL 1900x0106 NOP You can see that DEBUG has determined the starting location of each instruction, But be- fore executing the program, let’s use DEBLG's U (Unasserable) command to examine the generated machine language. The U (Unassembie) Command DEBUG's U command displays the machme code for your axsembly language instructions. ‘You can use this command to tell DEBUG the locations of the first and last instructions thal ‘you wamt w see. in this case, [OOH and L6H. Key in U 200, 106 <€nter> The screen displays columns for the location, machine code, and symbolic code Tike this: 08 Buz Mov Laz 00 22k MOV DL 2A xx ooo: ADDL. wxxx:0108 90) NOP Now trace the execution of the program—the machine code is what actually executes. Be- gin by keying in R to display the registers and the first instruction, and then successive T ‘commands to trace subsequent instructions. When youget to the NOP at location 106H, the IP should contain 106H and the CL should contain 6CH. Continue with the next exercise or press Q to quit execution. ‘You have now seen how to key in a program in machine language and in assembly Janguage. However, DEBUGis really intended for what its name implies—debugging pro- grams—and most of your efforcs will Involve the use of conventional assembly sanguage. which is not associated with DEBUG. USING THE INT INSTRUCTION ‘The following four examples show how to request information about the system. To this end, you use the INT (interrupt) instruction, which exits from your program, enters a DOS ‘or BIOS routine, performs the requested function, and returns to your program. There art different types of INT operations, some of which require a function code in the AH reg- ister to request a specific action. Rather than using the T command for single-stepping. ‘we'll use the P (Proceed) command to execute through the whole interrupt routine. Be sure to reset the 1? to LOH,Using the INT Instruction a Getting the Curent Date ‘The instruction tp access the enrrent date is INT 21H function code 2H. Once again, type in the DEBUG command A 100 and then the following assembler instructions: NOY AW,2A ir nor Type in R to display the registers and T to execute the MOV. Then type in P to proceed through the interrupt routine; the operation stops at the NOP instruction, The registers con- tain this information in hex format: AL: Day of the week, where 0 = Sunday x Year (for example, 0700H = 2000 DH: Month COLH through OCH) DL: Day of the month (1H through 1FH) Press Q to quit, or continue with the next exercise (and reset the IP to 100). Determining the Size of Memory Tn an early exercise in this chapter, you checked tocations 413H and 414H for the amount of base memory that your computer contains, BIOS also provides an interrupt routine, INT 12H, that delivers the size of memory. Type in he DEBUG command A 100 ant then these instructions: It 32 nor Key in R to display the registers and the first instruction. The instruction, INT 12H, passes control (o a routine in BIOS that delivers the size of base memory to the AX. Press T and repeatedly to see each BIOS instmction execute. (Yes, we are violating a rule against tracing through an interrupt, but this operation works all right.) “The actual insteuetions in your BIOS may differ somewhat from these, depending ow the version Installed (he comments 1o the right are the author's); or sSet inverrupt PUSH. 0S Save DS address 4n stack MOV Ax,0040 Retrieve memory size at WV DS.AX 5 Segment 40(0]N plus vow ax, [001]; offset 00238 Fors Restore address in DS RE {Return From interrupt Af you survived this adventure into BIOS, the AX row contains the size of base memory. in IK bytes. The last T command exits from BIOS and returns to DEBUG. The displayed in-“4 Executing Computer Instructions Chap. 3 steuction is now the NOP that you enttered, Press Q to quit or continue with the next exer- cise (and reset the IP 0 100). Using INT to Display This exercise, which displays data on the screen, introduces a few new features. Type in the DEBUG command A 100 and chen these assembler instructions: 100 HOY AH,08 202 NOY Dx, 108 10S TAT 21 107 MOP 108 0B ‘your rane’. “$' The two MOV instructions tell INT 21H to display (AH = 09) and from what starting ad- dress (DX = 108). Note that offset 108 begins the definition of your name. The DB means “define hyie” and the characters are contained in single quotes. Following your name is a dollar sign, also in quotes, which tells the INT to end the display. Key in R to display the registers and the first instruction and key in T commands for the two MOVs. Key in Pto execute the INT and you'll see your name displayed, Press Q to quit or continue with the next exercise (and reset the IP 10 100). Using INT for Keyboard Input This exercise, which accepts characters from the keyboard, also introduces a few new foa- tures. Type in the DEBUG command A 100 and then these assembler instructions: 100 Mov AH,10 102 INT 16. 108 IMP 100 106 NOP The fist instruction, MOV, is to tell INT 1611 to acoegt data from the keyboard (AH = 10); the operation delivers the character ftom the keyboard to the AL register. The IMP instrus- tion causes the processor to replace the value in the IP with 100, so that the nest instruction © execute is the MOV back at JOOH. By this means, you can execute one or more instro+ tions repeatedly. Key in R to display the registers and the first instruction and key in aT command for the MOV. When you type in P for the INT. the system waits for you to press a key. If you press *I,’ you'll see that the operation delivers 31H (hex for ASCII *1') to the AL. Key in T to execute the IMP, and you're back at the MOV at 100. Use T to exceute the MOV. When you key in P for the INT, the system again waits for you to press a key. If you press *2," you'll see that the operation delivers 32H co the AL. You can continue like this indefinitely. Press Q to quit or continue with the next exercise (and reset the [P to 100).Using the PTR Operator 5 SAVING A PROGRAM FROM WITHIN DEBUG You may use DEBUG (0 savea program on disk under two circumstances: 1, To setricve an existing program from disk, modify it, and then save it, fellow these steps: + Read the program urder its name: DEBUG nifilename. + Use the D command te view the machine language program and Eto enter changes, + Use the W (Write) command to wrile the revised program. 2, To use DEBUG th create a very small machine Language program that you now want to save, follow these steps: + Request the DEBUG program. + Use the 4 (Assemble) and E commands to key in the source program. + Type NV filename,COM to name the program, The prdgram extension must be COM. (See Chapter 7 for details of COM files.) + Because only you know where the program really ends, insert in the BX:CX pair the size of the program in bytes. Consider this example: 00 HOV CLA? vox:0162 ON DL, 28 woox:0104 ADO CL,DL. wot: 0106 NOP Note that although you key in symbolic code, DEBUG generates machine code, and that is what you are going to save. Because the last instruction, NOP. is 1 byte, the program size is 100H through 106H inclusive, or 7. » First use R BX to display the BX (the high portion of the size), and encer 0 ta clear it. » Next use R CX to display the CX register, DEBUG replies with CX nann (what- ever value iC contains), end you replace it with the program size, 7. + Key in W to write the revised program on disk DEBUG displays a message, “Writing 7 bytes.” If tae number is zero, yowhave failed to emer the program lengrly, uy again, Watch out for the size of the program, because the last instruction could be Longer than i byte. USING THE PTR OPERATOR Let’s now examine another program that introduces seme new features. In this example, you move and add databetween registers and memory locations. Here are the assembler in- struciions for that purpose: 100 MOY AK, [214] 303 AD aK, [LIC] 10? ADD AK 25 wR MOV {iE ax. 1D MOY WORD PTR [220].25Executing Computer Insteustions Chap. 3 M3) wOV BYTE PTR [122,30 ma wor. 19 NOP mA 08 4423 uc 9S oo iE 0B 00 00 Zo B90 80 00 ‘An explanation of the instructions is as follows: 100: 193: 107: 10A: 1D: 113: MA: Move the contents of memory locations 11 AH-11 BH to the AX. The square brackets indicate a memory address rather than an immediate valuc. ‘Add the contents of memory locstions 1\CH-11DH to the AX. Add the immediate value 25H tothe AX. Move the contents of the AX to memory locations LIEH—11FH. Move the immediate value 25H to memory locations 120H--12JH. Note the vse of the WORD PTR operator, which tells DEBUG that the 25H is to move into a word in memory, If you were to code the insituetion as MOV [120],25, DEBUG woold have no way of determining whet length is intended and would display an ERROR message. Although you will seldom need to use the PTR operator, it's vital to know when itis needed. Move the immediate value 30H to memory location 122H. This time, we want tw movea byte, and the BYTE PTR operator indicates this length Define the byte values }4H and 23H. DB here means “define byte(s)" and allows you to define data items that your instructins (such as the ane at 100) ate to reference. 11C, 11B, and 120: Define other byte values for use in the program. To key in this program, first type A 100, and then key in each symbolic in- struction (but not the location). At the ead, key in an additional to exit from the A command. To execute the program, begin by entering R to display the registersand the first instruction; then type in sticcessive T commands. Quit execution when you get to the NOP at 118. Key in D 110 to view the changed contents of tit AX (233E) andof locations (EH- 1VFH GE23), 120H-121H (2500), and 122H 30). You've covered a lot of material in this chapter that will become clearer through repetition. KEY POINTS + The DEBUG program is useful for testing and debugging machine language and as- sembly language programs. + DEBUG provides a sct of commands that lets you perform a number of useful apera- ions, such as display, enter, and trace, + Because DEBUG docs not distingnish between lowercase and uppercase letters, you may enter commands either way.Questions ar + DEBUG assumes that all numbers are in hexadecial format, + If youenter an incomect vatue in the deta segment or code seginent, reenter the E.com- mand (© correct it, + Toresume execution at the first instruction, set the instruction pointer (IP) register to 9100. Key inthe R (Register) command, followed by the designated register, as R IP . DEBUG displays the contents of the IP and waits for an entry. Key in the value 0100, followed by . QUESTIONS: 3-1. Explain the purpote of each of the following DEBUG communds: (a) D; YE; () R; (4>Q; (1 T: DAS (8) U; (hy PL 3-2, Provide the DEBUG commands forthe following unrelated requirements, (a) Display the memory beginning at offset 1ASH in the data segment. (b) Display the memory beginaing at location B4OH. (Noke: Separate this address into its segment and offset values.) (©) Key in the hex value 44441 into the data segment beginning at locstion 18AH. (a) Display the coments of all registers. (©) Display the contents of tre IP register only. (f) Unassemble the symbolic code in locations 100H through 11AH. 3-3, Provide the machine code instructions for the following aperations: (a) Move the hex valve 3248 toshe AX register; (4) add die timmediate hex value 0246 tothe AX. 3-4, Assume that you have used DEBUG to enter the following E command: E ¢S:100 88 36 01 05 25 00 “The hex value 36 was supposed (o be 54. Code another E commandte correct only the one byte that is incorrect; that is, change the 36 vo 54 directly. . Assume that you have used DEBUG to enter the following E command: E (5:10 88 06 20 05 00 30 90 (a) What are the three symbolic instructions represented here? (The first peogram in this chapter gives a cluc.) (b) On executing this program, you discover that the AX register ends up with 5006 instead of the expected 0650). What is the error, ard how would you cor- rect it? {e) Having corrected the instrmctions, you now want to Teexecute the program from the first instruction, What DEBUG commands are required” 3-6. Consider the machine language instructions BO 2A 00 FO 83 12 FO E3 30 This program performs the following? » Moves the hex value 2A to the AL register. + Shifts the contents of the AL one bit tothe lefl. (The sesul is 54.) + Moves the hex value {2 to the BL register. ‘+ Mutiplies the AL by the BL.a7 £ 310, sn. BR. Executing Computer Instructions Chap. 3 Use DEBUG's E command to enter the program beginning at CS: 100, Remember that these are hexadecimal values. After entering the program, key in D C3:100 1 ‘view it. Then key in R and enovgh successive T commands to step through the pro- gram until reaching the NOP. Whatiis the final product in the AX register? Use DEBUG's E command to enter die following machine language program: Machine code (at 10H): 80 00 02 00 EO FE 26 02 92 AB 02 02 90 Data (at 200K}: 24 12 09 00 This program performs the following: ‘+ Moves the contents of the one hyte at B5:0200 (2A) to the AL register. + Shifts the AL contents one bit to the left, (The result is 54.) + Multiplies the AL by the one-byte contents at DS:0201 (12), + Moves the product from the AX to the word beginning at DS:0202. After keying in the program. type in D commands to view the code aad the data, ‘Then key in R and enough successive T commands to stcp through the program wn- li] reaching the NOP, At this point, the AX shoukl coniain the product as OSESH. Key in another D DS:0200, and note that the product ot DS:0202is stored as EGOSH. For Question 3-7, code the commands that write the program on disk under the aarue HEXMULT.COM. Use DEBUG's A cornmand to eater the following instructions: WO KB spo IC SM O%02 SUE O%.36 nor Unassemble the instructions and trace their execution through to the NOP, and check the value in the BX after each instruction. ‘What is the purpose of the INTinstruction? Use DEBUG to create and tun a program that displays the phrase “ut to Lunch.” ‘Star with A 100 for entering the instructions and use A 110 for the phrase (and member the § delimiter). Hint: Sec the example in the soction “Using INT to Display.” Use DEBUG (0 create and run program that accepts three: characters from the key- board and displays them. (a) Start with A 100. (b) Use INT 16 to accept a character inw the AL and move the character to location [120]. (6) Use a second INT 16 to accept a character into the AL and move the character to location [121]. (d) Use an- other INT 16 to accept a third character into the AL and move the character to lo- cation [122]. (0) Now use INT 21 to display the characters. (f) Finally. usean E 123 *$° command to define a *$’ at the end of the three stored characters, Hint: See the example in the section “Using INT for Keyboard Input” (but you don’t need the IMP instruction for this exercise).ra REQUIREMENTS ——— FOR CODING IN ASSEMBLY LANGUAGE Objective: To cover the basic requirements for coding an assembly Janguagé program and defining data items. INTRODUCTION Jn Chapter 3, you leamed how to use DEBUG for keying in and executing machine lan ‘guage programs. No doubt yon were very thuch aware of the difficulty in deciphering the machine code. even for asmetl program. Probably no one seriously codes in machine lan- ‘guage other than for the Cniest programs. You also used DEBUG's A command for keying in a small assembly source program, and no doubt you noticed that it was much easier to understand tan machine code. The use of DEBUG’s A command is a mere convenience, because as of this chapter you'll start developing larger programs and you'll need far more capability in docurtenting and revising them. ‘You write an assembly program according toa strict set of rules, use an editor or word processor forkeying itinto the computer as «file, and then use the assembles translator pro- gram to read the file and to convert itinto machine code. Jn this chapler, we explain the basic reqlirements for developing an assembly program: the use of comments. the general coding format, the directives for controlling the assembled program listing. and the requirements for defining segments and proce- dures. We also cover the general organization of a program, including initializing she program and ending its execution. Finally, we cover the requirements for defining data items.50 Requirements for Coding in Assembly Language Chap. 4 ‘ASSEMBLERS AND COMPILERS ‘The two main classes of programming languages are high level and low devel. Program- ners weiting in a high-level language such ai C and BASIC nse powerful commands, cach of which may generate many machine language instructions. Programmers writing in a low-level assembly language, on the other hand, code symbolic instructioas, each of which generates one machine instruction. Despite the fact that coding in a high-level Tanguage is more productive, some advantages to coding in assembly language are that it + Provides more contol over handling particular hantware requiremenss, « May generate smaller, more compact executable modules. + Often results in faster execution. ‘Acommon practiceis 10 combine the benefits of both programming levels: Code the tlk of « project iv a high-level language, and code critical modules (those that cause no- ticeable delays} in assembly language. Regardless of the pgramming language you use, it 1s stil a symbolic language thet has to be translated into a torm the computer caa execute. A high-level language uses a com- ziler program to translate the source code into machine cods (technically, object code). A Tow-level language uses an assembler program to perform the translation. tinker program {for both high and low levels compleics the process by converting the object code into exe- catable machine language. PROGRAM COMMENTS ‘The use of comments throughout a progeam can improve its clarity, especially in essembly language, where the purpose of a se: of instmetions is ofien unclear. For example, it is ob- vious that the instruction MOV AH,10H moves. 10H to the AH register, but the reason for doing this may be unclear. A comment begins witha semicolon (), and wherever you code iy, the assembler assumes that all characlers on the line 10 48 right are comments. A com- ment may contain any printable character, including a blank. ‘Acomment may appear on a line by itself. like this: realculate productivity ratte of on the same Line following an instruction, like this: ADD AK,BX ;Accumilate total -quancity Because a comment appears only on a listing of an assembled source program and generates no machine code, you may include any mumber of comments without affecting the assembled program’s size or execution. In this book, all sssembly instructions are in _appercase letters and ull coraments are in lowercase, only as a convention and to make the programs more readable. Technically, you can freely use either uppercase or lowerdase for instructions and comments.Wentifiers 51 Another way to provide comments is by means of the COMMENT directive, de- sevibed in Chapter 27. RESERVED WORDS Certain names in assembly language are reserved for their own purposes, 10 be used only under special conditions. By category, reserved words include * instructions, such as MOV and ADD, which are operations that the computer can execute: *+ directives, such as END or SEGMENT, which you use to provide information to the assembler, * operators, such us FAR and SIZB, which you use in expressions; and + predefined symbols, such 6 @Daia and @Model, which retuin information to your program. Using a rescrved word for a wrong purpose causes the assembler to generate an error message. See Appendix C for a list of reserved words. IDENTIFIERS. An identifier (or symbol) is a name that you apply to an item in your program that you ex- pect toreference. The two types of identifier are name and label: 1. Name refers to the address of a data item, such as COUNTER m COUNTER DB 0 2. Labelvefers to the address of an instruction, procedure, or segment, such as MAIN in the statement WAIN PROC FAR ‘The same rules apply to both names and labels. An identifier can use the following characters: CATEGORY ALLOWABLE CHARACTERS. Alphabetic letiers: A through Z and athrough z Digits: Ounough 9 {may not be the first character) Special characters: question mark (7) underline (_) dollar (8) n(@) pexiod (.) (may not be the first character) ‘The first character of an identifier must be an alphabetic letter or a special character, except for the period. Because the assembler uses some special wards that begin with the @ sym- ‘bol, you should avoid using it for your own definitions.52 Requirements for Coding in Assembiy Language Chap. 4 By default the assembler treats uppercase and lowercase letters the same. ‘See Ap- pendix D for 2 command line that forces the assembler to be case sensitive.) The maximum length of an identifier is 31 characters (247 since MASM 6.0). Examples of valid names are TOTAL, QTY250,and $P50, Descriptive, meaningful nanes are recommended. The mares of registers, such a8 AH, BX, and DS, are reserved for referencing those regisiers, Conse- quently, in an instruction suet as ADD OX, BK the assembler antematically knows that CX and BX refer to registers. However, in an in- struction suck as Mow RECSAVE. the assembler can recognize the name REGSAVE only if you define it es a data item in the program. ‘STATEMENTS ‘An assembiy program consists of a set of statements. The 0 types of statements are: 1. instructions such as MOV and ADD, which the assembler translates to object code, and. 2. directives, which tell the assembler to perform a specific action, such as define & data Here is the general formatfor a statement, where square brackels indicate an optional entry: [Ceents Fier: [operation] [operand(s)] Tycomnente} | ‘An identifier (if'uny), operation, and operand (if anybare separazed by at least ene blank of tab character. There is a maximum of 132 characters on a line (512 since MASM 6.0). al- though most programmes prefer ta stay within 80 characters because that is the maximum number mest sereens can accommodate. Two examples of statements are the following: WENTIFIER OPERATION OFERAND coment Directive: couNt of 1 rNane. operation, operand Instruction: P30. wv AX.0 Operation, to operands ‘The identifier, operation, and operand may begit in aay column. However, consisienty starting at the same column for these entries makes @ more readable program. Also, most editor programs provide useful tab stops every eight positions to facilitate spacing the fields. [As described earlier under the heading “Icentifiers,” the term name applies to the jame of a defined item or directive, whereas the term dabei applies to the name of an in- struction; we'll use these terms from now on.rootives 53 ‘The operation, which must be coded, is most commonly used for defining data areas and coding instructions. For a data item, an operation such as DB or DW defines a field, ‘work area, or constant. For an instruction. an operation such as MOY ot ADD indicates an action to perform. The operand (if any) provides information for the operution to act on. For a data item, the operand dcfines its initial value. For example, in the following definition of a data item named COUNTER, the operation DB means “define byte,” and the operand initializes its contents with a zero value: MANE OPERATION —OPERAND coOmENT cousrer 8 0 DeFine byte (D8) with 0 vaTue For an instruction, an operand indicates where to perform the action. An instniction’s ‘operand may contain one, two, oF even no entries. Here ate three examples: QPERANGS OPERATION © OPERAND COMMENT None RET inewrn ‘One 1m ax Tnerement 8X register by 1 Twe ADO CK, 25 tad 25 to CX register DIRECTIVES Assembly language supports a number of statements that enable you to control the way in ‘which a program assembles and lists. These statements, called directives, act only during the assembly of a program and generate no machine-executable code. The most common Girectives are explained in the next few sections. Chapter 27 covers all of the directives in detail; you may use that chapter as a reference any time. ‘The PAGE and TITLE Listing Directives ‘The PAGE and TITLE directives help to control the format of a listing of an assembled pro- gram. This is their only purpose, and they have no effect on subsequent execution of the program. PAGE. At the start ofa program, the PAGE directive designates the maximum num- ber of lines to list on a page and the maximum number of characters on a line. Iis geweral format is PAGE {length [width For example, the directive PAGE 60,132 provides 60 lines per page and 132 characters pet line. Under a typical assembler, the number of lines pet page may range from 10 through 255, and the number of characters per line may range from 60 through 132. Omission of a PAGE statement causes the assembler to default 1o PAGE $0,80.Ey Requirements for Coding in Assembly Language Chap. 4 ‘Suppose that a program defines the maximum line count for PAGE as 60. Whes the assemblet is printing the assembled program and has listed 60 lines, it automatically ad- ‘vances to the top of the next page and increments the page count. ‘You may also want 10 force a page to eject st a specific line in the program listing. such as the end of a segment. At the required line, simply code PAGE with 10 operand. On encountering-PAGE, the assembler advances 0 the top of the next page where it resumes he listing, MME. Youcanuse the TITLE directive so cavse a dile fora programto print on line. of each page of the program listing, You may code TITLE once, at the start of the pro- gram, Hts general format is TITLE text [coment] For the text operand, a recommended technique is lo use the name of the program, as cata- Joged on disk. For example, if you named the program ASMSORT, code that name plus a1 optional descriptive comment (a leading *;’s not required}, all upto 60 characters it Length, like this ‘TILE ASMSORT Assembly program to sort customer nanes SEGMENT Directive ‘és described in Chapicr 2, an assembly program in .EXE format consists of one of more segments, A sack segment defines stack storage, « data segment defines data items, and a code sogment provides for executable code. The directives for defining a segment, SEGMENT and ENDS, have the following format: WANE OPERATION OPERAND COMMENT name SECMENT options] Begin segnent name ENDS. iEnd segnent ‘The SEGMENT statement defines the start of a segment. The segment name must be pre- sent. oust be unique, and must follow assembler naming conventions. The ENDS state- rent indicates the exd of the segment and contains the samc name as the SEGMENT sasement, The maximum size of a segment in real mode is 64K, The operand of a SEG- MENT statement may contain three types of options: aligament, combine, and class, coded in this format: name SEGMENT align combine ‘class?Directive 95 Alignment type. The align entry indicates the boundgry on which the segment is (o begin, For the typical requirement, PARA, the segment aligns on a paragraph boundary, so that the starting address is evenly divisibie by 16, or IOH. Omission of the aliga operand causes the awembler to default to PARA. Combine type. The combine cntry indicates whesher to combine the segment with vutber segments when they are linked after assembly (explained later under “Linking the Program”). Combine types are STACK, COMMON, PUBLIC, and AT expression. For ex- ample, the stack segment is commonly defined as name SEGMENT PARA STACK You may use PUBLIC and COMMON where you intend to combine separately as- sembled programs when linking them. Otherwise, where a programs not wo be combined ‘with other prograrss, you may omit this option or code NONE. Class type. ‘The cfass entry, enclosed in apostrophes. is used to group related seg- ments when finking. This book uses the classes “code” for the code segment (recommended ty Microsofi), ‘data’ for the data segment, and stack’ for the stack semen ‘The following example defines a stack segment with alignment (PARA), cowbine sSTACK), and class (‘Stack’) types: ame SEGAENT PARA STACK ‘Stack? ‘The partial program in Figure 4-1 illustrates SECMENT statements with various options. page 60,132 ‘pirie —-ROARSM2 Skeleton of an -zxE Progran SEGMENT PARA “Eaea’ Darasc ENDS 1 2 é @ ’ 10 32 GeoEsG ekomur PARA ‘code’ 13 MAIN PROC. FAR 1 ASSUME $5 :STACKSG, DS DATASG,C5:CODESG 35 36 a la is 20 MOV AX. DARASG ‘Set address of data mov BS 3x Paegment tn DS moe ax, scoun, yond processing wt 3H AIK SDP pend of procedure Za CODESG ENDS [end of wegment 2 END MAI pend Of program Figure 41. Skelton of an EXE Pmgmam38 Requirements for Coding in Assembly Language Chap. 6 PROC Directive The code segment contains the executable code for a program, which consists of one or more procedures, defined with the PROC directive. A sogitent that contains only one pro- cedure would appear us follows: WANE OPERATION OPERA COMMENT secrame SEGMENT PARA precnane PROC aR sone recedure rithin he cade procname —ENDP jsegrent segnane ENDS "The procedure name must be present, must be unique. and raust follow assembler naming conventions. The operand FAR in this case is related to program execution. When you Te- quest execution of a program, the program loader uses this procedure as the entry point for the first instruction to execute, “The ENDP directive indicates the end of a procedure and concains the same name as the PROC statement to enable the assembler to relate de end ro the start. Because a proce- dure must be fully contained within a segmcat, ENDP defines the end of the procedure be- fore ENDS defines the en of the segment, “The code segment may contain any number of procedures used as subroutines, each with its own set of PROC and ENDP statements. Each additional PROC is usually coded with (or defaults to} the NEAR operand, as covered in Chapser 7 ASSUME Directive ‘An EXE progeam uses the SS register to address the stack, the DS register to address the data segment, and the C'S register to address the code segment, To this end, you have to tell the assembler the purpose of each segment in the program, The required directive is ASSUME, coded in the code segment as follows: OPERATION ‘OPERANO ASSUME | SS:stackname,DS:dazasegnane ,(S:codesegname, $S;stackname means that she assembler is ta associate the name of the stack segment with the 5 register, and similarly ‘or the other operands shown. The operands may appear in any se~ quence. ASSUME may also contain an entry forthe ES, such as ES:datasegname: If your pro- ‘gram does not use the ES register, you may omit itsreference or code ES:NCTHING. (Since MASM6 0, the asseimbler antomnatically generates an ASSUME for the codesegment)Instructions for Iniializing a Program 3? Like other directives, ASSUME 1s justa message to help the assembler convert sym- bolic code to machine code; you may still have to code instructions thal physically Inad ad- dresses in segment registers at execute time. END Directive As already mentioned, the ENDS directive ends a segment, and the ENDP directive ends a procedure, An END directive ends the entire program and appears as the lasl statement. Its general format is: OPERATION | OPERAMD END Corocname] ‘The operand may be blank if the program isnot to execute: far example, you may want te assemble only data definitions, or you may want (oTink the program with another module, 3n most programs, the operand contains the name of the first or only PROC designated as FAR, where program execution is to begin. INSTRUCTIONS FOR INITIALIZING A PROGRAM The two basic types of executuble programs are EXE and COM, We'll develop the re- quirements for EXE programs first and leave COM programs for Chapter 7, Figure 4-1 provides a skeleton of an EXE program showing the stack, data, andcode segments, Let's examine the program statements by line nuriber: UNE EXPLANATION 1 ‘The PAGE ditective for this listing establishes 60 lines and 132 columns er page. 2 The TITLE directive identifies the program's ame 95 AD4ASMI 3 Lines 3,7, and 11 ace comunents that clearly set out the three defined segments. 4-6 These statements define the stack segment, STACKSG (but not its contents in iis example) R10 These statements define the data segment, DATASG (but not its coatents). 12-21 These statements define the code segment, CODESG. 13-20 These statements define the code segment’s only procedure, named MAIN in this example. This procedure illustrates common initialization and exit requirements for an EXE program. The wo requirements for initializing are (1) notify the assemblet which segmems to associate with segment registers and (2) load the DS with the address of the data segmenl,6 Requiremonts for Coding in Assembly Language Chap. 4 14 The ASSUME ditective notifies the assembler to associate certain segments with certain segment registers in this case, STACKSG with the SS. DATASG with the DS, and CODESG with the CS: ASSUME 55. STACKSG,05:DATASG, C5 -CODESG By associating segments with segment registers, the assembler can deter- mine offset addresses for items in the stack, for data items in the data seg- ‘meat, and for instructions in the code segment, For example, cach machine instruction in the code segment is a specific length, The first instruction in machine language would be at offset 0 and, if itis 2 bytes long, the second instruction would be at offset 2, and so forth, 15,16 Two instructions initialize the address of the data segment in the DS register: MOV AX,DATASG Get addrocs of daca segment MOY S,AK Stare address in 05 ‘The first MOV loads the address of the data segment into the AX regisler and the second MOV copies the address from the AX into the DS. Two MOVs are required because no instruction can move daw directly from memory to a segment register: youhave to move the address from another register to the segment register. Thus the statement MOV DS.DATASG would be illegal. Chapter 5 discusses initializing segment registers in more deta 18, 19 These two instructions request an end to program execution and areturn (0 the operating system, A later section discusses them in more deta 22 The END statement tells the assembler that this is the end of the program, andthe MAIN operand provides the entry point for subsequent program ¢x- ecution. MAIN could be any other name acceptable to the assembler. The sequence ia which you define segments is usually unimportant. Figure 4-1 de- fines them as follows, STACKSG SEGHENT PARA STACK ‘Stack’ DATASG SEGHENT PARA ‘Data’ CODES SECHENT PARA ‘Code” Note that the program in the figure is caded in symbolic fanguage, To execute i, You have touse an assembler and a linker totranslate itinto executable machine code asan EXE program. ‘As described in Chapter 2, when the program loader reads an EXE program from disk into memory for execution, it constructs a 256-byte (1D0H) PSP on a paragraph bound- ary in avaiable internal memory and stores the program immediately following the bound ary. The leader then + soads the address of the code segnsent in the CS; * Joads the address of the slack in the SS; and « loads the address of the PSP in the DS and ES registers.Example of a Source Program 59 “The loader initializes the C5:1P and SS:SP registers, but not the DS and ES registers. However, your program normaily needs the address of the data segment io she DS (and of (en in the ES as wel. Asa consequence, you have to initialize the DS with the address of the data segment, as shown by the two MOV instroctions in Figure +t ‘Now. even if this initialization is not clear at this point. take heart: Every EXE pro- gram has virtually identical initatization steps thar you can duplicate each time you code sn assembly program. INSTRUCTIONS FOR ENDING PROGRAM EXECUTION INT 21H is a common DOS interrupt that uses a fonction code in the AH register to spec- ify an action to be performed. The many fonctions of INT 211 include keybourd input, screen handling, disk /O, and printer output. The function that concerns ws here is ACH. which INT 21H recognizes as a request t9 end program execution. You can also.use this op- eration to pass a retom code in the AL for subsequent testing in a batch file (via the IF ERRORLEVEL statement), as follows: WOW AH. 4c Hequest end processing MOV AL,retcoce — jopEional return code INT 218 iCall interrupt service ‘The return code for normal completion of a program is usually 0 izero), You mey also code the two MOVs as one statement (as shown in Figure 4-1): MOY AX,4COOH ;Request normal exit INT 211 fanction 4CH has superseded operations INT 20H and INT 214 function OOH originally used to end processing. EXAMPLE OF A SOURCE PROGRAM Figure 4-2 combines the preceding information into a simple but complete assembly source program that adds 1wo data items in the AX register. The segments are defined in this way: «+ STACKSG contains one entry, DW (Define Word), that défines 32 words initialized to zero, an adequate size for small programs. + DATASG defines three words named FLDD (initialized with 175), FLDE (initialized. with 150), and FLDF (aninitialized) + CODESG contains the executable instructions for the program, although the first statement, ASSUME, generates no executable code. ‘The ASSUME directive performs these operations: + Assigns STACKSG to the SS register, so that the system uses the address in the SS register for addressing STACKSG.0 Requirements for Coding in Assembly Language Chap. 4 ok 32 DUP CO} 175 150 7 PRR 99: STACKS, D8 -DATASG, CS -CODRSG. Mov AX,DATASG "jet address of data mov DB.AK P segmarc in Da mov Ax, FLDD rMove 0175 to 2X xDD AK, FIDE phd 0250 to AE mov FLD, AN score gun in FLOF Mov AX, 4C00H Bnd processing ger aaa maw expr ;End of procedeze CovESG ENDS. end of sequent =D MAIN Bnd of program Figure 42 EXE Program with Conventional Segsnents + Assigns DATASG to the DS register, so that the system uses the address in the DS register for addressing DATASG. + Assigns CODESG to the CS register, so that the system uses the address in the CS register for addressing CODESG. ‘When loading a program from disk into memory for execution, the program loader sets the actual addresses in the SS and CS registers, but, 2s shown by the first two MOV in- structions, you have to initialize the DS Cand possibly the ES) register. ‘We' lt race the assembly, linkage, and execution of this programm in Chapter 5. INITIALIZING FOR PROTECTED MODE In protected mode wider Ue 80386 and later processors, a program may addross up to 16 megabytes of memory. The use of DWORD to align segments on a doubleword ad- dress speeds up accessing memory for a 32-bit data bus. In the following example, the 486 directive tolls the assembler to accept instructions that are unique to the 80386 and later; the OSE32 use type tells the assembler to generate code appropriate to 32-bit pro- tected mode: 386 ‘Segnane SEGHENT OWORD {SES2 Initialization of the data segment register could took like this, since on these proces- sors the DS register is stl 16 bits in size:Simplified Segment Directives 61 MOV EAK,DATASG —:Get address of data segment pov Ds,Ax stoad 16-bit sortion The STI. CLI, IN, and QUT instructions, availabie in real mode, are not allowed in protected mode. SIMPLIFIED SEGMENT DIRECTIVES ‘The assembier provides some shortcuts in defining segments. To use them, you have to ini- tialize the memory model before defining any segment. The general format (including the leading period) is -MODEL menory-node ‘The memory model may be TINY, SMALL, MEDIUM, COMPACT, or LARGE. (Another model, HUGE, need not concer us here.) The reqoirements for each mode! are: MODEL | NUMBER OF CODE SEGMENTS | NUMBER OF DATA SEGMENTS TINY . ~ smALL 2 ! 1 score More then 2 : a comoact 1 fore than 1 LARGE ere than 1 fore tan 1 ‘You may use any of these models for a stand-alone: program (that is, a program that is not linked to another program). As of MASM 6.0 and TASM 4.0, the TINY model is mended for dhe use of COM programs, which have their data, code, and stack in one 64K segment. The SMALL model requires that code Bits within a 64K segment and data fits within another 64K segment; this model is suitable for most of the examples in the book. ‘The MODEL directive autornatically generates the cequited ASSUME statement, ‘The general format: (including the leading period) for the directives that define the stack, data, and code segments are -STAK [size} —_ <—— Figure 45. Displaying he Dota SegmentThe EQU Directive 6a ‘Assemble and link the program, use DEBUG to load the EXE file, and key in D DS:100 for a display of the data. The right side of the display shows the ASCII cepre- sentation, such as “Computer Processors,” whereas the hexadecimal valneson the left in- dicate the actual stored contents. Your display should be identical to Figure 4-5 for offseis, 0100 through 0190. Expect your segment address (OCA3 in the figure) ané data follow- ing offset O19D to ciffer. ‘The reason you issue DS: 108 for the display is because the loader set the DS with the address of the PSP, but the data segment for this program begins 100 bytes after that ad- dress. Later, when you usc DEBUG for EXE programs that initialize the DS to the address of the data segment, you can use DS:0 10 display it. THE QU DIRECTIVE ‘The BQU directive does not define a data item; instead, it defines a vaiue that the assem- bler canuse to substinte in other instructions. Consider the followiag BQU statement coded in the data segment: FACTOR €QU 12 ‘The name, inthis casc FACTOR, may be any name asceptable to the assembler, Now, when- ever the word FACTOR appears in an instruction or another directive, the atsembler sub- stitutes the value 12. For example, the assembler converts the directive TABLEX 08 FACTOR OUP?) tots equivalent value TABLEX DB 1z OUP?) An instruction may also contain an equated operand, as in the following: LOWE EL 25 Mov cx, LEME ‘The assembler replaces LIMITX in the MOV operand with the value 25, making the ‘operand an immediate value, as if it were coded MOV K,25 ;Assembler substitutes 25 ‘The advantage of EQU is that many statements may use the value defined by LIMIT. If the value has to be changed, you need change only the EQU statement, Need- Jess to Say, you can use an equated Value only where a substitution makes sense to the as- sembler. You can also equate symbolic names, as in the following code: TOISALES DY 0 5 QU TOTSALES wy EQ MULww Requirements for Coding in Assembly Language Chap. 4 ‘The first EQU equates the nickname TS to the defined item TOTSALES. For any instric- {don that contains the operand TS. the assembler replaces it with the addrest of TOTSALES, ‘The second EQU enables a program to use the word MPY in place of the regular symbolic instruction MUL. MASM 6.0 intodaved a TEXTEQU directive for text data with dhe format ame TEXTEQU text> KEY POINTS: » A semicolon precedes a comment on a tine, + Reserved words in assembly language are used for their own purposes, only under special conditions «An identifier is a name that you apply to items in your program. The two types of identifiers are name, which refers to the address of a data item, and label, which refers. to the address of an instruction, + An operation is commonly used for defining data areas and coding instructions. An ‘operand provides information for the operation to act on. = A program consists of one or more segments, eath of which begins on a paragraph. boundary. + The ENDS directive ends each segment, ENDP ends each procedure, and END ends the program. + The ASSUME diroctive associates segment registers CS, DS, and SS with their ap- propriate segment names. + An EXE program should define at least 32 words for the stack. + Foran EXE program, you nonmally initialize the DS register with the address of the data segment. . + For the simplified segment directives, you initialize the memory model before defin- ing any segment. Options are SMALL (one code seginent and one data segment), MEDIUM (any number of code sezments and one data segment), COMPACT (one code segment and any number of data segments), and LARGE (any number of code segments and data segments). » INT 21H function 4CH is the standard instruction for eaiting « program. + Names of data items should be unigue and descriptive. For example, an item for an employee's wage could be named EMPWAGE. « DB is the preferred format for defining character strings, since it permits strings Jonger than to bytes and converts them to normal left-to-right sequence. * Decimal and binary (hex) constants generate different values. Consider the effect of adding decimal 25 versus that of adding hex 25: ADD 0X25 a ADD CK Add 37 » DW, DD. and DQ store numeric values in object code with the bytes in reverse se- quence.Questions n + DB items are used for processing half registers (AL. BL, etc.). DW for full registers (AX. BX, efe.), and DD for extended registers (AX, EBX, etc.). Longer numeric items require special handling QUESTIONS. 4-1. Distingnish between a compiler and an assembler, 4-2. What is a reserved word in assembler language? Give two examples, 4-3, What are the two (ypes of identifiers? 4-4, Determine which of the following names are valid: (a) CX: (b) 25C4; (c) @$_X: (d) $25: (@) ATRT. Tr invalid, explain. 4-5. Explain che difference between 2 directive und n instruction and give two exam ples of each. 4-6. Give the commands that cause the assembler when listing a program (a) to advance to a new page and (h) to print a heading at the top of a page. 41, Explain the purpose of each af the three segments described in this chapter. 4.8, The format for the SEGMENT directive is ame SEGMENT altgn combine “class ‘What is the purpose of (a) align: (b) combine; (e) class"? 49, (a) Explain the purpose of a procedure. (b) How do you define the beginning and the end of a procedure? (c) When do you define a procedure as FAR? (d) When do you define a procedure as NEAR? 4-10, What particular END statements are concerned with ending (a) a procedure; (b) a segment; (c) a program? 4-11. Distinguish between the statement thatends an assembly and the statements that end execution, 4-12. Giventhe names STKSEG, DATSEG. and CDSEG for the stack, data segment, and code segment, respectively, code the required ASSUME statement. 4-13, Consider the insiruciion MOV AX,4COOH used with INT 21. (a) Explain whae the instruction performs. (6) Explain the purpose of the 4COOH. 4-14, For the simplified segment directives, the MODEL ditective provides for TINY, SMALL, MEDIUM, COMPACT, and LARGE models. Under what circumstances ‘would you use each model? ALIS. Give the lengths in bytes penerated by the following data directives: (a) DB, (b) DQ; (©) DT; @ DW: (@) DD. 4.16. Define a character string named CONAME containing “Computer Services” as a constant, 417. Define the following numeric values ia data terns named AREA through AREAS, respectively: (a) A L-byte item containing the binary equivalent to decimal 35. {&) ADW containing the consecutive values 12, 14, 22, 28, 33, and 4. (@) A Z-by'e item containing an undefined value. (@) A Lbyee item containing the hex equivalent to decimal 58. (©) Ad-byte item containing the hex equivalent to decimal 436.ira Requirements for Coding Assembly Language Chap. 4 #18, Show the generated hex object code for (2) DB 34; (b) DB *34, (¢) DB 4 DUP(O). 4-19, Determine the assembled hex object code for (a) DD 4A25B2H; (b) DQ 3BAS3DH: (©) DB 35H; (d) DW 3728H.ASSEMBLING, LINKING, AND EXECUTING A PROGRAM Objective: To coverthe steps in assembling, linking, and executing am assembly language program. INTRODUCTION ‘This chapter explains the procedure for keying in an assembly langvage program and for as- setabling, linking, and executing it. The symbolic instructions that you code in assembly Tan- ‘guage are known as the souree program, You use an assembler program to translate the source program into machine cude, known as the object program, Finally, youuse a linker progran to.complete the machine addressing for the object program, generating an executable module, The sections on assembling explain how to request execution of the assembler pro- ‘gram, which provides diagnostics (including any error messages) antd generates the Object program. Also explained are details of the assembler listing and, in general terms, how the assembler processes a source prograr. ‘The sections on linking explain how to requestexecution of the Tinker program so that you can generate an executable module. Also explained arc details of the generated link imap, as well as the jinkor’s diagnostics. Finelly, a section explains how to request execu- tioa of the exccutable module. PREPARING A PROGRAM FOR EXECUTION Figure 4-2 illustrated only the source code for a program not yet in executable format. For keying in this program, you could use any editor or word processing program that produces B7 ‘Assembling, Linking, and Executing a Program Chap. § a standard ASCII file. In the following examples of commands, substitute the appropriate rive for your system. You canalso gain alot of productivity by loading your programs and files into 4 RAM disk. Call up your editor program. key in the statements for the program in Figure 4-2, and name the resuiting file AOSASM1.ASM. ‘Although spacing is not important co the assembler, a program is more readable if you keep the name, operation, operand, and comments consistently aligned on columns. Most editors have tab stops cvery cight positions to facilitate aligning columns. ‘Once you have keyed in all the statements for the program, examine the code for ac- ‘curacy, Although most editors have a print facility, you may also request the DOS PRINT program: PRINT m:ADSASHI.ASM Ac it stands, this source program is just. text file that cannot execute—you must first assemble and link it 1. The assembly step involves transtating the source code into object code and gener- ating an invermediate OBI (object) fie, or module. (You have already seen examples of machine code and source code in earlier chapters.) One of the assemn- bler's tasks is to calculate the offset for cvery data item in the data segment and for every instruction in the code segment. The assembler also creates a header immediately in front of the generated .OBJ module; part of the header contains information about incomplete addresses. The OBJ module is not quite in exe- cutable form. ‘The link step involves converting the .OBJ module to an EXE (executable) ma- ‘chine code module. The linker's tasks include completing any addresses left open by the assembler and combining separstely assembled programs into one exe- ccutable module. 3. The last step is to Joad the program for execution. Because the loader knows where the program is about to load, it is able t0 resolve any remaining addresses still left in- complete in the header. The loader drops the header and creates a program segment prefix (PSP) immediately before the program loaded in memory. 2 Figure 5-1 provides a chart of the steps involved in assembling, linking, and execut- ing a program, ASSEMBLING A SOURCE PROGRAM ‘The Microsoft assembler program (up to version 5.x) is MASM.EXE, whereas the Borland. ‘Turbo program is TASM.EXE, As of version 60, the Microsoft assembler normally uses the ML command, but also accepts MASM for compatibility with earlier versions. ‘You can key in the command to run MASM of TASM with a command line or by ‘means of prompts. This section shows how to use the command line; see Appendia D for the prompt method. The general format for a command line to assemble a program isAssembling @ Soures Program (=}-—= Soe —) s * es - oe — cy OT cy Es = nd ic - ae —) = oe = ) coe WASH/TASM [options] sourcel ,cbjectIL, 5 Crna me Amombte Seceee Prgr (ASI dese Brocton Spa Prgewm ; 0a) Sait Poxmen, Popa (exe) Lea eat rpc e. EXE Figure s-1 Steps in Assembly, Link, and Poman Execute inglL crossref] ‘+ Options provides for such features as setting levels of warning messages and is ex- plained in Appendix D. The assembler's defautts are adequate for our purposes at his ime, *» Source identities the name of the source program, such as AOSASMIL. The assembler assumes the extension .ASM, so you need not enier it. You can also (ype in a disk drive number if you don’t want to accege the current default drive. = Object provides for a generated OB! file, The drive, subdirectory. and filename may be the same as or different from those in the source. » Listing provides for a generated LST file that contains hoth the source and object code. The drive, subdirectory, and filename may be the same 4s or different fram those in the source.16 Assembling, Linking, and Executing a Program — Chap. 5 + Crossref generates a cross-reference file containing the symbols used in the program, which you can usc for a cross-reference listing. The extension is.CRF for MASM and -XRF for TASM. The drive, subdirectory, and filename may be the same as or differ- ent from those in the source, ‘You always key in the name of the source file, and you usually request an OBJ file, which is required for linking a program into executable form. You'll probably often request an LST file, especially when it contains error diagnostics or you want toexamine the gen- erated machine code. A.CRF file is useful for a large program where you want to see which instructions reference which data items. Also, requesting 2 -CRF file causes the assembler to generate statement rumbers for in the .LST file to which the CRF file refers, Later sec: Hons cover LST and .CRF files in detail. Following arc two examples of assembling AOSASML, Note that if the filename for OBI, .LST, or (CRF isto be the same as the source, you nced not repeat it; a reference to drive number is sufficient to indicate a tequest for the file. Example 1. Specify source file AQSASM11 on drive D and gencrate only an object file. In this case, you omit the reference to the LST and CRF files and simply enter the command NASM/TASH D:AOSASHL,D: Exomple 2. Generate object, listing, and cross-reference files: NASH/TASM D:AOSASHL, D:D: D2 ‘The assembler converts your sourte statements into machine code and displays any ‘error messages on the screen. Typical errors include a name that violates naming couven- tions, an operation that is spetled incorrectly (such as MOVE instead of MOV), and ax ‘operand containing a name that is not defined, Because there are many possible errors (100 or more) and many different assembler versions, you may refer to your assembler manual for alist. The assembler attempts to correct some errors but, in any event, you should re- Toad your editor, correct the .ASM source program, and reassomble it. USING CONVENTIONAL SEGMENT DEFINITIONS: Figwe 5-2 provides the listing that the MASM assembler produced under the name AOSASML.LST. The line width is 132 positions as epecified by the PAGE entry. You can also print this listing if your printer ean cornpress the print line. Many impact printers have. a switch that will force compressed printing, or you could request your editor or word. Processor to print in compresséd mode. Another way is to.use the DOS MODE command, for 132 characters per inch and 6 lines per inch, tum on the printer, key in the command MODE LPT1:132,6, and request the PRINT program. Note at the top of the listing how the assembier has acted on the PAGE and TITLE, directives. None of the directives, including SEGMENT, PROC, ASSUME, and END,Using Conventional Segment Definitions ESAS EXE] Move aa 23 au 14 i 36 Lu ia a 2 22 33 2 26 3 26 23 30 9000 0030 900 9030 093 0004 9006 uve 9030 000 903 005 008 o0ac ‘900 012 dois bans gozat 9000 cone 2095 9000 At 0000 & 93 06 0002 3 gua R Ba acca cpt ‘and add operaciones Page 1-2 Page 60.232 Time Rosas (BKE) Move and add operationa SmAGRaG SRGuENT FARA STACK “Stack bie 32 DUP (O) seen SROMENT BARA ‘Data be 175 FLps Dw ise FLOP Dw 7 Covess — SEGUANT PARA ‘Code’ MAIN PROC FAR ASSUME SG-STACESG, DS -DATASG, CS: CCDESG Mov AX,DATASG Sot addreoo cf data mov BS,AX Paegment. in DS. mov ax, FLD Mave 0178 to AK R ADD ORK/FIDE Add 0150 to AX Moy FLOF,Ax Stora sum in FLDe mov -AX.4C00H © End processing trait wan me Ena of procedure CODESG = ENDS. End of Segment END MAIN Bnd of program Segments end Grows? Name Lengeh Align combine Class. copRSS Dole PARA HONE, "cope Darasa | 0006 = BARR 2 "DATA! STACKSG 040 PARA STACK STACK Symbols Name Type Value Acer ware P'PROe 0020 CORESS Length = 0014 F1oD wos 020 DaTASG. PLbE Como 0012 BATASG FIDF GRO oon exrass. acu. Text 01013 @rILENe L reer aosasm SYERSION Text x0 2) source Linee 37 Total Lines is symbole 0 Warning Errore 0 Severe Figure 5-2 Assernblea Program with Conventional Segmems78 Assembling, Linking, and Executing @ Program Chap. 6 generates machine code, since they are just messages to the assembler, The listing is arranged vertically according to these sections: 1. Atthe extreme left is the number for each line. 2. The second section shows the hex addresses of data fields and instructions. 3. The third section shows the translated mavhine code in hexadecimal format. 4. The section to the right is your original source code. ‘The progeam is organized into three segments. Each segment contains a SEGMENT directive that notifies the assembler to slign the segment on an address that is evenly divisible by hex |O—the SEGMENT statement itself generates no machine code. The program loader stores the contents of each segment in memory and initializes its address in a segment register. The beginning of the segment is offset zero bytes from that address, Note that each segment is a separate area, with its own offset valve for data or in structions, ‘Stack Segment The sack segment vomains a DW (Define Word} directive that defines 32 words, each gen- erating a zero value designated by (0). This definition of 32 words is a realistic size for a stack because a lange program may requite many interrupts for inpuvoutpot and calls to sub- programs, all involving use of the stack. The stack segment ends at offset OO4OH, which is equivalent to decimal value 64 (32 words X 2 bytes). The assembler shows the generated constant to the Heft as 0920{0000]; that is, 20H (32) zero words. If the stack is ton small to contain all the items pushed onto it. neither the assem- ler nor the linker wams you, and the executing program may crash in an unpredictable way. Dato Segment ‘The program defines a data segment, DATASG, comaining three defined values, all in DW (Define Ward) format: 1, FLDD defines a word (2 bytes) initialized with decimal value 175, which the assera- bler has transtated to OOAFH (shown on the left). 2. FLDE defines word initialized with decimal value 150, asscmnbled as (096HT. The actual storage values of these two constants are, respectively, AFOO and 9600, which ‘you can check with DEBUG. 3. PLIDPis coded as a DW with ? in the operand to define a word with an uninitialized constant. The listing shows its contents as 000. ‘The offset addresses of FLDD, FLDE, and FLDF are, respectively, 0000, 0002, and 0004, which relate to their field sizes.Using Conventional Segment Definitions a Code Segment ‘The program defines a code segment, CODESG, which contains the program's executable code. all in one procedure (PROC). ‘Thive statements establish the addressability of the data segment ASSUME. $$: STACKSG,,DS:DATASG, CS:COOESC 9000 EE. R MOV AX, DATASC ons 8e 08 Mov DS.4x E. The ASSUME directive relates DATASG to the DS register. Note that the program does not require the BS tegister, although some programmers initialize it as a stan- dard practice. ASSUME simply prevides information to the assembler, which gener- ates ne machine code for i. 2. The first MOV instruction “stores” DATASG in the AX regisier, Now, an jastruc- tion cannot actuatly store a segment in a register—ihe apsciabler simply reeog- izes an attempt 16 load the address of DATASG. Note the machine code to the left: BB----R. The four hyphens mean that at this point the assembler cannot deter- mine the address of DATASG; the system determines this address only when the object program is Tinked and loaded for execution, Becautse the loader may locate fa program anywhere in memory, the assembler Jeaves the address open and indi- cates the fact with an R; the loader is to replace (or relocate) the incomplete ad- dress with the actual one. ‘The sezond MOV instruction moves the contents of the AX register (o the DS resis: ter, Because there is no valid instruction for a direct move from memory to the DS register, you have to code (wo instnuctions to initialize the DS, Although the loader automatically initiatizes the SS and CS when it loads a program for execulion, itis your responsibility to initialize the DS, and the ES if required. ‘While allthis business may seem unduly involved, at this point you really don't have to understand it, All programs in this buck use a standard definition and inidalivation, and ‘you simply have to reproduce this code for each of your programs, To this end, store a skele- ton assembly program on disk, and for euch new programs that you want (0 create, copy the skeleton program into @ file with its correct name, and use your editor to complete the ad- ditional instructions. “The first instruction after initializing the DS register 1s MOV AX,FLDD, which be. gins at offset location 000S and generates machine code Al OOOO. Tae space in the listing hhetween Al (the operation) and OO00 (the operand) is only for readability, The next in- struction, ADD AX.FLDE, begins at offset location 0008 and generates 4 bytes of machine code. The instruction, MOV FLDF.AC, copies the sum in the AX to PLDF at offset (004 in the data segment. Ta this example, machine insuctions are 2, 3. or 4 bytes ia length “The last statement in the program, END, contains the operand MAIN, which reiales tothe name of the PROC at offse: 0000. This is the location in the code segment where the program loader is to transfer control for execution.80 Assembling, Linking, and Executing aProgram = Chap.5 Following the program listing are a Segments and Groups tuble and 4 Symbols table. Segments and Groups Table “The firs table at the end of the assembled ‘isting shows eny defined segments and groups. Note that segments are not listcdiin the sume sequence as they are coded; the assembler uscd for this example lists them in alphabetic sequence by name. (This program cortains no groups, Which is a later topic.) The table provides the length in bytes of each segrient, the aligament (all are paragraphs), the combine type, and the class. The assembler has con- verted the class names to uppercase Symbols Table ‘The second table provides the names of data fields in the data segment (FLOD. PLDE, and FLDF) and the labels applied to instructions in the code segment. For MAIN (the only ¢v- try in the example), Type F PROC means fat procedure (far because as the entry-poim execution MAIN must be known outside this program), The Value column gives the offset for the beginning of the segment for names, labels, and procedures. The column headed Attr for aribute} provides the segment in which the ilem is defined. ‘Appendix D explains all the options for these tables. To cause the assomblet to omit the tables, code a /N option following the MASMITASM command, For the last three entries: GCPU identifies the processor at the time of assembly according to bit number set om: 0 8086 7 privileged instructions 1 80186 8 8087 numeric processor 2 80286 10 80287 wumesic processor 4 80486 11 80387 mumeris provessor 5 80586 (Pentium) @FILENAME gives the name of the progeem @ VERSION shows the assembler version in the form n.on Turbo Assembler Listing Figure 5-3 provides a lisiing of the same program, this time run under TASM. Differences in the generated code to the left of the listing are the following, by statement number: SOURCE ORZECT COUE WEANING a bu 32 CUPCOD —_—-20%(0003) 20 copies af zero moras a mm? amr ‘An undefined vetue 1s NEV AX.DATASG 88 0000s 0000 = incomplete operand and s = . The SP (Stack Pointer} should contain OO{0H, which is the size of the stack (32 words = 64bytes = 40H) ‘The IP ifasquction Pointer) shoutd be OOD00H. The SS and CS are properly initialized for execution; their values depend on where in memory your program is loaded. “The first instruction MOV AX.xxxx is ready to execute—it and the following MOY are about toinitialize the DS segister. To execute the first MOY, pressT (Jor Trace) followed by and note the effect on the TP. To execute the second MOV, again press T fol lowed by . Check the DS, which is now initialized with a segment address. ‘The third MOV loads the contents of FLDD into the AX. Press T again and nate that the AX now contains OOAF. Now press T to execute the ADD instruction and note that the AX coniains 0145, Press T to store the AX in offset 0004 of the data segment, "To check the contents of the data segment, key in DDS:0. The operation displays the three data items as AF 0096 0045 G1, with the bytes for each word in reverse sequerice. ‘At this point, you can use L-to reload and rerun the program or press Quo quit the DE- BUG session CROSS-REFERENCE LUSTING ‘The assembler generates an optional CRF or “XRF file that you can us€ to produce a crass reference listing oLa_ocosram’s identifiers, or syinbols. However, you still have to convertError Diagnesties a7 ‘TCREF with a command line or by means of prompts, This section uses a command Tine; see Appencix D for using prompts. The command to convert the cross-reference file is PCREFATCREP ret File, ref file j + xreffile idenuties the cross-refencace fike generated by the assembler, The program as- sumes the extension, so you need not enter it You can also specify ¢ disk drive number, «+ reffile provides (or generating a .REF file. The drive, subdirectory, and filename may bbe the same.as or different from those in the source. Figure 5-5 shows the cross-reference listing produced by CREF for the program in Figuse 5-2, The syinbols in the first colurnmart in alphabetic order. The numbers in the see- ond column, shown as nf, indicate the line in the LST file where each symbol is defined. Numbers to the right of this column are lire numbers showing where the symbol is refer- enced. For example, CODESG is defined in tine 17 and is referenced in lines 19 anil 29. FLDF is defined in line 14 and referenced in Line 25-+, where the + ” means its valve s modified ducing, program executicn thy MOV FLOBAX). Assemblinga nomber of psngrams generates lot of unnecessary files. You can safeiy delete OBI, CRE, and LST files, Keep the .ASM source programs in case of further changes anid the .EXE files for executing te programs, ERROR DIAGNOSTICS ‘The assembler provides diagnostics for any programming errors (hit violate its rules, The program in Figure 5-6 is the same as the one in Figure 5-2, cxcept that it hus @ aumber of "ADSASML (EXE) Move and ada operations i . Symbol Crose-Reference ‘# definition, + wodification! ace... : - ot avERSToN uf Nate ce Teh ze 88 cone a eapEe¢ : LH ag 38 PATA. 6... a DREase Vie 2s a9 ae FLpD ack 25 SLDE : isa 26 STACK, STACKES 12 symbols $$ Crs Reference Table88 Assembling, Linking, and Executinga Program Chap. § 1 page 60,132 a TrTLe —-AGERGM3 EXE} T1luatrate aaseniy errors 2 9000 STACKS SEGMENT PARA STACK “tack: oO 5 tuov aezot De 32 DUB(O) 5 00D 3 1 ° 9 av89 STACKS ENDS 10 31 sae SaTase SuGMENT PARA ‘data? 32 000s aoar FLDD oe 375 33 8002 C96 Fobe Dw 150 34 004 FLDF De 0Sinm] ASK (LI) erxor A2O27: Operand expected 33 8004 DaTnsc ENDS 16 : - oo - 27 soan bopgse " SeaMeNT PARA ‘core! ta soon BRGIN. © PROC. PAR 25 ASSUME C§.:COUBSG, Ds :DATASC 29 v0uv AL g0¢0 U Moy AX,DAISEC “Address of data 0Sas3 .ASM(17); erzor a2009: Symbol not defined: DATSEG 313003 am D> mou! DK AE y segment in 2S 33 Moy as.FLoy stove 0275 to At 05sem3-ASMI20!: error A2009: Sytbal no: defined: AS AR, PLCS, VRaA 0150 to AK daeo0s a2 OF 0007 BALD 25 0003 a1 on00 0 Moy _PLDG, AK [Store sue in PLDF ‘beams ASM(22): erxor AZ002: Sywbal os defined: TuDG 26 000c ab 4200 Moy AK. aCo3H 7Bnd processing 27 coger SD 21 mr fan 25 oot ugIN ERDF a0Sasm] ASM(25}: error A20U6: Phase error bevween passes 23 001 ComRse ENDS: 33 xD ESGTN, 9 Marning grrors 3 Severe’ Srrore igure $46 Assembler Diagnosties intentional errors inserted for illustrative purposes The program was nun under MASM: ‘TASM genezates a similarerror listing, Here are the errors: LINE EXPLANATION $4 The definition of FLDF requires ua operand 19 ASSUME dees not relate the $5 (@ STACKSG, although the assembler has not detected this omission. 20 DATSEG should he spelled DATASG. 21 DX should be coded as DS, although the assembler does not know that this is an error, 23 AS should be coded as AX. 25 FLDQ should be coded as PLDP. 28 Correcting die other errors will cause this diagnostic to disappear. “The last error message, "Phase error between passes,” occurs when addresses gener- ated in pass 1 of a ovo-past assembler differ from those of pass 2, Toisoiate an obscure er- ror under MASM (prior to version 6.0), use the JD option ts list both the pass | and the pass ‘files, and compare the offset addresses,Questions 9. THE ASSEMBLER LOCATION COUNTER ‘The assembler maintains a locatian coumer hat it uses to account for each defined item in the data segment. Figures 5=3and 5-5 illustrate its effeci by meansof the three defined data items: 0000... FLO OW 0002... FLOE OW 004... FLOF OW Initially, the lovation counter is set ot 0, where the assembler establishes the first data item, FLDD, Becanse FLD is detined as a word, the assembler advances the location counter by 2, 16.0002, where it establishes FLDE, Because FLDE is also defined as word, the assem bler again advances its location counter by 2. to O04, for the next data item. FLDE. also a ‘word. The location counter is again advanced by 2. t 0006. but there ae no further data items, The assembier provides ¢ number of ways to change the current valus in the Focation, counter, For example, you can usc the EVEN of tic ALIGN directive to facilitate aligning an address on an even-numbered boundary (Chapler 6), or use the ORG directive to begin 2 program at a paricular offset (Chapter 7} or to redefine data items with different names iChapcer 26). KEY POINTS Both MASM und TASM provice a command line for assembling, including tat least) the name of the source program. MASM also provides prompts for entering optons. + The assembler converts a source program (o an .OBJ file and generates an optional isting and cross-reference file. + The Segments and Groups table following an assembler listing shows any segments and groups definedin the program. The Symbols table shows all symbols (daa numes acd instruction labels. The tinker (LINK or TLINK} converts an OBI file to an cxcculable .EXE file ives generute the names DATA for the data segment, and TEXT for the code segment, as well as a num ‘The simplified segment dire STACK for the stack sogmen her of predefined equates. + The CREF (or TCREP) program produces @ useful cross-reference listing, ‘QUESTIONS S+L. Code the command line 9 assemblea source program named SQUEEZE.ASM with files LST. OBJ, and .CRP, Assume that the source program and essembler are in drive BE, 5-2. Code the LINK or TLINK command line to link SQUEEZE.OB) from Question 5-1 5.3. Code the commands for SQUBEZE.EXE fro Question 5-2 for the following; (a) direct execution fron’ DOS: (b) execution through DEBU0 5.4, 55. 56, Se. 54, 59, 5-10. Assembling, Linking, and Executing a Program © Chep.5. Explain the purpose of each of the following files: (a) file. EXE: (b) file.OBK, (2) file. MAP: (a) file. ASM: [e} file. CRF: (£9 file.LST. Axseming conventional segmentdetinitions and DATSEGM as the nme of the data segment, cade the two MOV instructions to initialize the DS register. Write an assembly program using conventional segment definitions for the fallow- ing: (a) Move the immediate vaiue hex 50 to the AL register: {b) shifl the AL con- vents one bit left (SHL AL.1): (c) move immediate value hex 18 to the Ci «d) multiply AL by CL (MUL CL. Remember the instructions required to end pro- gram execution. The program does net need to define or initialize the data seyment Copy a skeleton program and use your editor to develop the program. Assemble, link, and use DEBUG to trace and to check the code segment and registers. Revise the program in Question 5-6 fur simpiified segment directives. Assemble and link it, and compare ihe object code, symbol tables, and link map with those of the origina! prograsn ‘Add a data segment to the program in Questiun 5-6 for the following requicemen:s: * Define a t-byte item (DB) named FIELDX containing hex 50 and another named FIELDY conisiniag hex 18. + Define a 2-byte item (DW) named FIELDZ with 20 constant + Move the contents of FIELDX (o the AL register and shift lefl cne bit. + Muhiply the At. by FIELDY (MUL FIELDY) + Move the product in the AX to FIELDZ. Assemble, lnk, atd use DEBUG 10 fest the program. Revive the program in Question 5-8 for simplified segment directives. Assemble and link it, ané compare the abject code, symbol tables, and link map with those of the original program. Uist DEBUG lo test ihe program. Foreach of the following data items, show the contents of the assemblerslocation sounter 0006 WORDA DW 0 WORDB DW 0 BYTIA DB EvIN WORD DW O BYTES 08 0ra SYMBOLIC —=— INSTRUCTIONS AND ADDRESSING ‘Selpcnens To provide ib buss ofthe axscibly eagutge insiruc- {Si pe nd the Sooner fo aldereing ate, INTRODUCTION ‘This chapter introduces the categories of the processor's instruction set, andthen describes, tho basic addressing formats that are used throughout the rest of the book. The instructions formally covered inthis chapter are MOV, MOWSX, MOVZX, XCHNG, LEA, INC, DEC, and INT, as well as the use of constants in instruction operands as immediate values, Fi fully, the chapter explains address alignment and the segment override. prefix. THE SYMBOUC INSTRUCTION SET ‘The following is a list of the symbolic inscructions for the 80x86 processor family, arranged by category. Although the list scoms formidable, many of the instructions arerarely needed. INC: Increment by 1 MUL: Unsigned Muttiply NEG: Negate SBB: Subtract with Borrow IDIV: Signed (Integer} Divide SUB: Subtract Binary Values IMUL: Signed (Integer) Multiply KADD: Exchange and Add 12 ‘Symbolic Instructions and Addressing Chap. 6 ASCI-BCD Conversion AAA: ASCII Adjust After Addition AAD: ASCII Adjust Before Division AAM: ASCH Adjust After Multiplication AAS: ASCII Adjust After Subtraction DAA: Decimal Adjust After Addition DAS: Decimal Adjust After Subtraction BI Shitting RCL: Rotate Left Through Carry RCR: Rotate Right Through Carry ROL: Rotate Left ROR: Rotate Right SAL: Shift Algebraic Left Comparison BSPYBSR: Bit Scan (80386+) SAR: Shift Algebraic Right SHL: Shift Logical Left SHR: Shift Logical Right SHLD: Shift Left Double (0386+) SHRD: Shift Right Double (80386+) BI/BTC/RTRIBTS: Bit Test (80386~) CMP: Compare CMPSn: Compare String CMPXCHG: Compare and Exchange (486+) CMPXCHGSB: Compare and Exchange (586+) TEST: Test Bits Data Transfor LDS: Load Data Segment Register LEA: Load Effective Address LBS: Load Extra Segment Register LODS: Load String LSS: Load Stack Segment Register MOV: Move Data Flag Operations CLC: Clear Carry Flag CLD: Clear Direction Flag CLI: Clear Interrept Flag MOVS: Move String MOVSK: Move with Sign-Extend MOV2X: Move with Zoro-Extend STOS: Store String XCHG: Pxchange XLAT: Translate PUSHF: Push Flags onto Stack SAHE: Store AH in Flags STC: Set Carry Flag‘The Symbolic Instruction Set CMC: Complement Carry Flag LAHE: Load AH from Flags POPF: Pop Flags off Stack Input/Output IN: Input Byte or Word INSu: Input String (80286-4 ) Logicat Operations AND: Logica! AND NOT: Logical NOT Looping LOOP: Loop Until Complete LOOPE: Loop While Equal LOOPE: Loop While Zero LOOPNE: Loop While Not Equat Processor Control ESC: Bscape HLT: Enter Halt State LOCK: Lack Bus Stack Operstions POP; Pop Word off Stack 93 STD: Set Direction Flag STI: Set Interrupt Flag, OUT: Output Byte ar Word OUTSn: Output String GO286+) OR: Logical OR XOR: Exclusive OR LOOPNZ: Loop While Not Zero LOOPNEW: Loop While Not Equal (80386-+) LOOPNZW: Loop While Not Zero (80386 +) NOP: No Operation WAIT: Put Processor in Wait Stare POPA: Pop All General Registers (B0286+) PUSH: Push Word onte Suick PUSHA: Push All General Registers (80286+) Siting Operations MPS: Compare String LODS: Load Suing MOVS: Move String REP: Repeat String REPE: Repeat While Equal REPZ: Repeat While Zero REPRE: Repeat While Not Equal REPNZ: Repeat While Nos Zero SCAS: Scan Sting STOS: Store Sting4 Symbolic instructions and Addressing, Transfer (Conditional INTO: Interrupt on Overflow JA: Jump Hf Above JAE: Sump If Above/Equel JB: furnp If Below SBE: Jump If Below/Baqual JC: Jump If Cary JCXZ: Jump [CX Is Zero JE: Jump Ef Equal 3G: Jump If Greater IGE: Jump If GrederfEqual JLs Jump If Less ILE: Jump If Less/Equal JNA: Jump If Not Above JNAR: Jump If Not Above/Equal INB: Jump If Not Below INBE: Jump If Not Below/Equal Transter (Unconditional) CALL: Call a Procedure INT: Interrupt IRET: fmerrupi Rew, Type Conversion CBW; Convert Byte to Word Chap. & INC: Jumpif No Cary INE: Jump If Not Equal ING: Jump f Not Greater INGE: Jump If Not Greater/Equal SNL Jumpif Not Less INLE: Jump If Not Less/Equal INO: Jump If No Overflow INP: Jump If No Parity INS: Jump If No Sign INZ: Jumplf Not Zero JO: lump If Overflow IP: Jump TéParity Odd IPE: Jump If Parity Even JPO: Jump if Parity Odd JS: Jump If Sign JZ: lamp Vi Zero IMP: Unconditional Jump RET, Retum RETN/RETF: Return Neat/Return Far CDQ: Convert Doubleword to Quadword (80386+ ) CWD: Convert Word 1o Doubleword CWDE: Convert Word to Extended Doableward (80386+) INSTRUCTION OPERANDS Anogerand provides a source of date for an instraction ta process. Some instructions, such fas CLC and RET, do net require an operand, whereas other inttructions may have oné OF wo operands, Where there are two operands, the second operand ix the source, which con- tains either the data to be delivered (immediate) or the address (of a register or in memory) of the data. The source data is unchanged by the operation. The first operand is the desté-Instruction Operands 95 ation, which cortains data in a cegister or in memory and which is to be processed. Here is the instruction format Thabetet poration pperandtceparond Let's now examine how the operand can affect the addressing of data. Register Operands For this type, the register provides the name of any one of the B+. 6-, of 32-bit registers, Depending on the instruction, the register may appear in the first operand, the second operand. or bath. as the following examples illustrate: woRUA Dw 7 Define a nord WoV Dx, WoRDA grster in First operand MOV WORDA.CX —-:Register in second operand MOY EDX, 0x gisters in beth operands Because processing data between registers involves nv reference to memory, itis the fastest type of operation, Immediate Operands In immediate format, the second opetand vontains 4 constant value or an expression. (The first operand is never an immediate value. )TThe destination field in the first operand defines the length of the data and may be a register or a memory location. Here are some examples: cour on? sDeFine a byte A00 8X,25 jade 25 ww 8K MOV COUNT, 50 jMove 50 to COUT A later section discusses immediate operands in more detail, ect Memory Operands tn this format, one of the operands references a memory location and the ether operand ref- cercuces a register, (The only instructions that allow both operands to address memory di- rectly are MOVS and CMPS.) The DS register isthe defaull segment register for addressing data in memory. Here ate some examples: WORDA Dw 0 jeFine a word BYTEA 0B 0 Define a byte NOs 8X,VORDA, sLoad WORDA into BX ADD BYTEA,DL yAdd Dl te GYTEA96 Symbolic Instructions and Aderessing Chap. € mov €%,08:[380H? iMave word From memory at offset 38804 INC BYTE PTR [280] Increment byte at offset 1BDH The last two examples use square brackets as index specifiers to indicate a reference tomemory. (An offset value like 38BOET is combined with the address in the DS.) The omis- sion of square brackets, as in MOV CX8BOH, indicates an immediate value—note the significant difference. The last example increments the byte in memory at offse: [BOH (ihe offset combined with the BS address), Becanse the operand [1 BOH] indicates only a starting memory loca- tion, you need the BYTE PTR modifier here to define the length In the following exarsples, z data item named CODETBL sets as an offset address in aninstruction operand: CDETBL DB 20 DUP(T) iDefine a table of bytes MOY CL,CODETBLI3] :Get_ byte from CODETBL MOY CL.CODETBL+A Sane operation “The first MOV uses an index specifier to access the fourth byte from CODETBL- (Because CODETBL{O} is the first byte, CODETBL{3] is the fourth.) The second MOV uses a plus +) operator for exactly the same effect Indirect Memory Operands Indirect addressing is a sophisticated technique that takes advantage of the computer's ¢a- pobility for segmentofiset addressing. The registers used for this purpote ate BX, DI, Si, ard BP, coded within square brackets as an index operator. The BX, Dl, and SI are associ- ated with the DS register as DS:BX, DS:DI_ and DS'SI, for processing data in the dita seg- ment. The BP is associated with the SS register as SS:BP, for handling data in the stack, which we'll do in Chapter 23 when calling subprograms and passing parameters. ‘When the first operand contains an indirect address. the second operand references a register or immediate value; when the sccond operand contains an indirect address, the first operand references a register. An indirect address such as [DI] tells the assem- Der that the memory address to use will be in the DT register when the program subse- quently executes. Inthe following example, the first MOV initializes the BX with the offset address of DATAVAL. The second MOV uses the adglress now in the BX to store zero im the memory location to which it points, in this case, DATAVAL: DATAVAL DE 50 Define a byte MOV BK,OFFSET OATAVAL — :Laad BX with offset oy COXS, 25 ‘Move 25 to DATAVAL ‘The effect of the two MOVs is the same as coding MOV DATAVAL.25, although the uses for indexed addressing are usually nol so trivial. The following related instruction moves zero 10 a location two bytes immediately following DATAVAL;The MOV Instruction 7 Mov [6X+2],0 ‘Move 0 to DATAVAL +2 ‘You may also combine registers in anindirest address; for example, (3X +I] means the address in BX plus the address in the DL Note that a reference in syuare brackets to the BP, BX. DL. or ST register implies an. indirect operand, and the processor treats the contents of the register us a1 offset addtess. when the program is executing. Here are a few more examples of indirect operands: 490 cL, TK 2nd opaeand = D§-Bx MOV BYTE PTR [011.25 :Ast operané = DS:0T ADD [8P],CL 54st operand = 95:67 Addiess displacement. This method uses an address displacement fer an operand. The following MOV transfers the contents of the BL to DATATAB (a 40-byte table); exactly where ja DATATAG is determined by the contents of the DI when the program is executing: DATATIR DB 49 OUPCT? sDefine table MOV DATATAB[OI],8L :Move BL to table Incloxing on 60386 and later processors. These processors allow an address to be generated fromany combination of one or more general rosters, an offsct, and a scaling factor (1, 2.4, or 8) associated with the contents of one of the registers, For examplo, the instruction NOV EBX, [ECX*2s€SPe] moves a valucinte the EBX that consists of the contents of ((he ECX times 2) plus the con tonts of (the ESP plus 4). THE MOV INSTRUCTION The MOV instruction transicrs (that is, copies) data referenced by address of the second operand to the auidress of the first operand, The sending ficld is unchanged. The operands that reference memory of registers naust agree in size (2.8., both mast be bytes, both must ‘be words. or both must be doublewords). The general formal for MOV is caper [HOV | restau Fmatory. Zi Here are foar examples of valid MOV operations by category. given the following, data items: vreein on 7 iCafsre a byte WROFLO OW 7 efsne a word 1. Register Moves MOY EDXECK Regi ster-to-registee Now 05,8 .gister-to- segment register98 Symbolic Instructions and Addressing Chap. 6 Nov SYTEFLC.DH — :Register-to-nemory, direct Fev (D15 bx Register -to-nenory, indirect 2. Immediste Moves: MOV CK.40 Inmediate-to-reyister ov BYTEFLO,40 HTmmedlate-ta-menory, direct MOY WORDFLD[OXT40 :Tnmediate-ta-menory, indirect 3. Direct Memory Moves Wov CH.BYTEFLD iMenory-to-register, direct MOY CKMORDFLOCEX] — Manary-to-regi ster, indirect 4, Segment Register Moves Woy cK,OS sSeqnent register-to-ragicter MOV WORDFLE,DS — ;Segnent regi ster-to-menory ‘You can move 10 a regiser a byte (MOV CH.BYTEFLD), a word (MOV CX, WORDELD). ora doubleword (MOV ECX,DWORDFLD). The operand affeets only the por- tion of the referenced register; for example, maving a byte to the CH does not affect the CL. Invalid MOV operations are memory-to-memory (kecp that une in mite), immediate to-segment egisiet, and segment register to-segmeat register. Pesforming these operations roquires more than on¢ instruction, MOVE-AND-FILL INSTRUCTIONS For the MOV instruction, the destination must be the samme length as the source, such as byte-to-byte and word-Lo-word. On the 80386 and later, the MOVSX and MOVZX (move and fill) instructions facilitate transferring data from a byte or word source to a word or doubleword destination. Here is the general format for MOVSX and MOVZX: [Tiaberei | hovsnmcicx | rea MOVSK, for use with signed arithmetic values, moves a byte or word to a word or doubleword destination and fills the sign bit (the lefimos: bit of the source) into leftmast bts of the destination, MOVZX, for use with unsigned numeric values, moves a byte 0° word to a word or doubleword destination and fills zero bits into leftmost bits of the dest ation, As an example, consider moving a byte containing 1011 0000 to a word: the result in the destination word depends on the choice of instruction: reise Foamary tomate ] movsx Cx,10L00008 |; = 11111111 19710000 mov2x Cx, 101200008 ;@X - 09000000 10210000 Here are some other examples of using MOVSX aul MOVZX: eveL oe 25 Byte worl OK 40 Mord WwORDL 09160 jDoublewordIrimediate Operands 9 MOvSX OC AYTEL (Byte to nord MOVZX WORDL.BH Byte to word MOVSK FBX,WORDI word to doubleword MOVZK OMORDL,OX Ward to doubleword Chapters 8 and 13 cover signed and unsigned data in detail. IMMEDIATE OPERANDS fn the following example of an immediate operand, the instruction May AK, 02851 moves the immediate constant 0245H to the AX register, The 3-byte object code for this in struction is 884502, where BS means “move an immediate value to the AX register” and the following two bytes contin the Value itelf (4302H, in reverse-byte sequence). Many instructions provide for two operands; the first operand may be a register or memory Toca tion, and the second operand may be an immediate constant. ‘The use of animmediate operand provides faster processing than defining a nurreric constant in the data segment and referencing it in the operand of the MOV. as, for example. in the following: Data segment aMTC pw 02454 Define ANTC as word Code segment: MOV AX.ANTC Move ANTC to ax Immediate Formats Here are some exaraples of valid immediate constants: Hexadecimal 014g Decimal: 228 (which the zesanblar convarts to 0148H) Binary: 1010010008 (which converts to OLEH) Length of Immediate Operands ‘The length of an immediate constant cannot exceed the length defined by the first operand. In the following invalid example, the immediate operand is 2 byles, but the AL register i¢ only 1 byte: NOV CL,O24SH_ Invalid immediate Tength However, if an immediate operand is shorter than a recerving operand, 28 in ADO Cx.48H atid ienath the assembler expaads the immediate operand to 2 bytes, 0048H, andstores the object code as 4800H.,100 Symbolic Instructions and Addressing Chap. 6 ‘The 80386 and later provessors permit 4-byte (doubleword) immediate operands, such as MOV ECK,12345676H — :Hove deubTeword Figure 61 gives examples of MOV, ADD. and SUB, which are three of many in. structions that allow immediaw operands. The .386 directive allows the assembier to recognize the reference to the EDX register, You don’t need an 80386 or later processor to assemble this statement, but you would need one to execute it page 60,132 rrrur -ADGIMMED' {EXE) Example of somediate operanda CNODEL SHALL STACK 64 Uefine stack DATA ibefine data segment PUK OB aso FLDY oN 300 186 cone warn PROC FAR mov) AK, @date sSet address of date mov Ds,ax } segment. in Ds Mov) Cx)325 ove immediate ADD CK, 350 [Ada immediate mov -EDK,O (Mowe immediate (60396+) ROD DX, 20H, [ada immediate (hes! SUB FIDY.250 Ssobcract immediate Mov ELDY, aH iMowe tnmedtate Mov AX, ac00H pind processing mr 2H WAIN ERDP. SID MAIN Fipie 6-1 Using Invmatiste Operands Processing data items that exceed the capacity af a register involves additional cod~ ing, covered in later chapters. THE XCHG INSTRUCTION ‘The XCHG instruction performs another type of data transfer, but cather than simply copy the data from one location to another, XCHG swaps the 1wo data items, The general format for XCHG fs [[iisbetsy ] 2A6 | ragTererTminory, reatater7oenor | ‘Valid XCHG operations involve exchanging data between two registers and between a reg- ister aad memory. Here are two examples: wor00 OW? Define word CHG <1, 8 rExchange contents of two registers XCHC OC,WORD — Exchange contents of register and memoryExtended Move Operations 101 THE LEA INSTRUCTION “The LEA instruction is usefal for initializing a register with an offset address ‘The. general fommat for LEA is A.common use for LEA is to initialize an offset in the BX. DI, or SI register for indexing an address in remary, which is done lot throughout this book. Here's an example: DATATBL Us 25 DUF (2) BYTEFLD 0B ? LEA BX,DATATEL sLoad offset address HOY BYTEFLD,(5X] jMove First byte of CATATBL Anequivalent operation to LEA is MOV with the OFFSET operator, which generates slightly shorter machine code and is used like this: MOV BX,OFFSET DATATEL, jLoad offset address ‘THE INC AND DEC INSTRUCTIONS INC and DEC are convenient instructions for incrementing and decrementing the contents of registers and memory locations by 1. The general format for INC and DEC is [Ruaweri [anc/oec | resister/menory | Note that INC and DEC require only ote operand. Depending on the result, the operations clear or set the OF, SF, and ZF flags, which conditional jump instructions may test for mi- favs, zero, or plus, EXTENDED MOVE OPERATIONS. ‘The programs to this point involved moving immediate data into a register, moving data from defined memory to 2 tegister. moving fegister contents to memory, ad moving the ‘contents of one register 6o another. In all cases, the length af the data was limited to one or two bytes, and no operation moved data from one memory area directly to another mem- ‘ory area, This section explains how to move ¢ata that oxceeds 2 bytes. Another method, the use of siting instruciions, is covered in Chapter 12. in the program in Figure 6-2, the data segment contains two 9-byte fields defined as HEADG1 and HEADG2. The object of the program is to move the contents of HEADGI to HEADG2: Meal: LaterTe dleguie HeADG?: Laserco102 Symbolic Instructions and Addressing Chap. 6 page 60.137 rome Roemoue (XE) axtendad move operations "MODEL SMALL [STACK €@ MOV AX, edata Initialize sequent mov BS.ax' reyiatara mov BELA MOV XK, 09. initialize to sove 9 chars TRA ST RRANGI Hinitiatize adéreas cf HEADGL TBA Or, NEADG2 Vand HEADG 20 Moy Ab, (st) iGet character trom #HADG2. Moy (DTT, AL move it €5 HEADG? Inc gr inner next char im HEADGL mmc 0 Hiner ext pon’n in HRADG? Dec x crenent Count for Loop gue x20 iCount nor zero? Yes. Loop sPintahed. mov AH, 03H pRequeast display LEA (DX, HEADG? 1 OF HEADGZ int 2a Mov AK, acoort ind peocesving mr 7ait A1owarN BNIP END MowArE Figure 6-2 Exended Move Operations Because these fields are each 9 bytes Jong, more than.a simple MOV instruction is required. ‘The program contains a number of new features. In orderto step through HEADG! and HEADG2, the program initializes the CX regis- ter to 9 (the length of buth fields) and uscs the ST and DI registers for indexing. Two LEA in- structions load the offset addresses of HEADGI and HEADG2 into the ST and DI as follows: LEA SE,HEADGL Load offset addresses TEA DEMEADG. | of HEADC and HEADG? ‘The program uses the addressesin the SE and Di registers to move the first byte of HEADGI to the first bye of HEADG2. The square brackets arourd Si and DIin the MOV operands sean hat the instructions sre 10 use the offset address in the given register for accessing the memory location. Thus ov AL, [ST] means “Use the offset uldress in SI (HEADG1 +0} 10 move the referenced bytoto the AL register,” And the instruction mov [T] AL‘The INT Instruction 108 means “Move the contents of the AL to the offset address referenced by Dl (HEADG2+0).”” ‘The program has to repeat these two MOV instructions ninc times, once for exch character in the respactive fields. To this end, it uses a conditional jump instmuction that we have not yelexplained: INZ, Gump if Not Zero). ‘Two §NC instrctions increment the $1 and Dby i and DEC decremerts the CX by 1. DBC alsa sets or clears the Zern flag, depending on the result iz the CX; if the restnit is hot zero. there are still more characters to move, and JNZ jumps back ta the labek A20 to repeat the move instructions. And because the SI and DI have been incremerted by }, te next MOVs reference HEADGI+1 and HEADG2+1. The loop continues in this fashion uml i¢ has moved nine characters in all, up to HEADG2+8. As well, the program introduces the instructions needed ta display the contents of HEADG? at the end of provegsing. The requited instructions are: 1, Initialize Function O9M in the ATT register to request a display. 2, tokialize the address of HEADG? in the DX. 3. Execute the instruction INT 21H. ‘The INT operation displays all the characters beginning with the firs byte of HEADG2up (0 the terminating “S' sign, which is defined immediately following HEADG2, Chapter 9 covers this operaiion in detail, You may wantio key in this program. assembic and tink it, and use DEBUG to trace it, Note the cffce! on the stack, the registers, and the IP (particularly after INZ executes}. Use D DS:0 to view the changes © HEADG2. THE INT INSTRUCTION On execution, an INT instruction interrupts processing and accesses the interrupt vector table ia low memory to determine the axidress of the requested routine, The operation then ‘Wansfers to DOS or to BIOS for specified zetion and returns to your program to resume pro- cessing. Mostoften, the interrupt has to perform the complex steps of an input or autpet op- eration, Interrupts require a trail that facilizates exiting a programm and, on successful completion, retuming to it. For this purpose, INT perforras the following: + Pushes the contents of the flags register onte the stack. (Push First decrements the stack ponnter by 2) + Clears the inte-rupt and trap flags. + Pushes the CS register onto the stack, + Pushes the instruciion pointer (contai stack. + Causes the required operation to be performed. ing. the address of the nextinstruction) onto he ‘To retum froman interrupt. the routine issues an FRET (interrupt Return), which pops the registers off the stuck and thus causes a return to the instruction immediately following the INT104 Symbolic Instructions and Addressing Chap. 6 Because the preceding process is eatirely automatic, your only concerns are to define a stack large enough for the necessary pushing and popping and to use the appropriate INT ‘operations. ALIGNING DATA ADDRESSES Because the 8086 and 80286 have a 16-bit (word) data bus, they exeaute faster if accessed ‘words begin on an even-numbered (Word) address. Consider a situation in which offsets 0012H and 001 3H contain the word 63 A7H. The processor can access the full word at off set 0012H directly into a register. But the word could begin on an odd-numbered address, such as 0013H: Menory contents:[ xx | 6 | a7 | os ahs In this case, the processor has to perform two nevesses. First, it accesses the hytes at 0012 and 0013H and delivers the byte from 0013H (63) 0 the AL register. Then, il accesses the bytes at OO14H and OO1SH and delivers the byte from O014H (A) ta the AH register The AX now contains A763H. ‘You don't have to pexform ary special programming for even or odd locations. nor 49 you have to know whether an address is even or odd. The accessing operaiton automatically reverses a word from memory into a register so that it resumes its currect sequence. ‘The 80386 and later processors have a 32-bit data bus and, accordingly, prefer alignment of referenced items on addresses evenly divisible by four (x doubleword ad- dress). (Technically, the 486 and later processors prefer slignment on a 16-nyte (para- graph) boundary. ‘You can use the ALIGN ar EVEN directive to force alignment of items on bound- aries, For example, either ALIGN 2 or EVEN aligns on 3 word boundary. and ALIGN 4 aligns on a doubleword boundary. When the assembler adjusis the address of an item ac- cording to a boundary, it also advances its locauon coumer accordingly. ‘Because the data segment wher defined with PARA begins on a paragraph boundary, you could organize your data first with daubleword values, then with werd values, and, fi nally, with byte values. However, modern processors execute at such rapid speed thet you'll probsbly never notice the effects of forcing alignment. NEAR AND FAR ADDRESSES ‘An address in a program may be near or far. A near address consists of only the offset por tion of an address. An instruction that references a near address assumes the current s¢g- ment—namely, the DS for the data segment and the CS for the code segment.Key Paints 105 A far address consists of both the segment and offset portions, in the form sey- meatcoffsel, An instruction may reference a far address from within the current segment or in another segment. Almostall essembly programming makes use of near addresses, which the assembler generates unless instructed otherwise Larger programs that consist of many segments would require far addresses THE SEGMENT OVERRIDE PREFIX Fot mest purposes, a reference to a data area in a program is to locations in che data seg ‘ment, handled via the DS register. There are ozcasiors. however —especially for ‘arg pro- grams—witen you may have to handle data that is subject toanother segment register, such 4s the BS or, on the 386 and later, the FS or G3. An example would be a large table of data Joaded from disk into a seperate segment in memory. ‘You can use any instniction to proczss the data in the other segment, but you must identify the appropriate segment register. Let’s say that the address of the other segment is in the ES register, and the BX contains an offset address within that segment. Suppose the requirement is to move 2 bytes (a word) from that location to the DX register. HOY Dx, ES: [Bd Mave to OK from ES: (8x1 ‘The coding of “ES-" indicates an override operator that means "Replace the normal ase of the DS segment register with that of the ES." The next example moves a byie value from the CLinto this other segment, at an offset formed by the value in the SI plus 36. NOY €S:[51#36],ck Move to ES: [5T+36] ‘rom CL ‘The assembler generates the machine language code with the override operator inserted as a Lbyte prefix (26H immediately preceding the instruction, just as if you had coded the insructions as ES: MOY OX,[BX] Mave to OX from £5: [8X] ES: MOV [ST+36].CL zMove to £5: [$1436] from CL KEY POINTS + An operand provides a source of data for an instruction. An instruction may have one, $0, or no operands, + Where there are two operands, the second operand is the source, which references ei- ther immediate data or the address (of a register or of memory) of the data, The first operand is the destination, whick references data to be processed in a register or in memory. + In immediate formal, he second operand contains a consiant value or an expression Immediate operands should match the size of the destination, that is, both byte, word, or doubleword106 Symbolic Instructions and Adér ing Chap. 6 + In direct memory format, one of the operands references a memory location, and the ‘other operand references a register. + Indirect addressing takes advantage of the processor's capability for segment-offset addressing. The registers used are BP: BX, DI. and SI, coded within square brackets as an index operator. The BP is associated with the SS as SS:BP. for handling data in the stack. The BX, DI. and SI are associated with the DS as DS:BX, DS:D1. and DS:SI, respectively, for processing data in the data segment, + You may combine registers in an indirect address as [BX+DIl, which means the ad- dress in BX plus the address in the DI. «The MOV instruction transfers (or copies) data referenced by the address in the sec- ‘ond operand to the address in the first operand. + The LEA instruction is aseful for initializing a register with an offset address. «INC and DEC are convenient instructions for incrementing and decrementing by 1 the contents of registers and memory locations. + The INT instruction interrupts processing of your program, transfers to DOS or BIOS for specified action, and IRET returns to your program (o resume processing. QUESTONS 6-1. Foran instruction with two operands, which operand is the source and which is the destination? 6-2, (a) In what significant way do the following instructions differ in execution? ‘ADD CX,2548H ADO CK, [2548H1 (b) For the second ADD, one operand is in square brackets. Whatis the name of chis feature? 6-3, (a) In what significant way do the following ADD instructions differ im execution? ADD BX. 25, AO {8x],25 «b) For the secand ADD, what sort of addressing is involved withthe first operand? 64, Explain the operation of the instruction ‘ADO OX, {BX4ST+8} 6.5. Consider the following statement, which contains an error, ADO {8x}, [D1] 4) Identify the error: (b) expiain bow to correct the exror. 6-6 Given the following data definitions. find the errors in the staterents, and cade the ingtractions necessary to correct them: BYIEX 08 56 BYTEY DG 27 woRD2 OW 148 (a) 0 BYTEX,evTEY {b) «ADD AL,WORDZ—-sOperand 24s correct (©) SUB -BL.CA7BH_Operand 2 4¢ correctQuestions 107 7. Code the following as instructions with immediate operands: (a) Add hex 48 to CX; (6) subtract tex 48 from DX: (c) shift he DH one bit to the cight; (d) shift BYTEX ‘one bit to the left: (¢) store 248 in the CX; (fi compare BYTEX to zero, 8. Code one instruction that swaps the contents of a word named WORDZ with the BX. 69. Code the instruction to set the BX with the (offset) address of an item named CODETRL. 610. Explain in general terms the purpose of the INT instruction. 6-11. Explain how (a) the INT insteuction affects the stack, and {b) the IRET instruction affects the stick, 612. Code, assemile, link, and use DEBUG to test the following program: + Define byte items named BYTEX and BYTEY (containing any valves} and a word item named WORDZ (containing zero). + Move the contents of BYTEX to the AL. + Add the contents of BYTEY to the AL. ‘+ Move the immediate value 34H tothe CL. + Exchange the contents of the AL and CL. » Multiply the contents of the AL by the CL (MUL CL). + Transfer the product from the AX to WORDZ.| 7 | WRITING .COM == PROGRAMS (Objective: To explain the purpose and uses of COM programs and hhow to prepare an assembly language program for COM format. INTRODUCTION Up to this chapter, we have written, assembled, and executed only .EXE programs. For an EXE program, the linker automatically generates a particular format and, when storing it on disk, precedes it with a special header block that is 512 bytes or more Jong. (Chapter 24 provides details of header blocks.) You can also wrile COM programs for execution. One example of a commonly used COM program is COMMAND COM. The advantages of COM programs are that they are smailer than comparable .EXE programs and are more easily adapted to act as resident programs. The COM format has its cuts in earlier days of microcomputers, when program size was limited 10 64K. DIFFERENCES BETWEEN AN .EXE AND A COM PROGRAM Significam differences berween a program dat is to execute as LEXE and one that is to ex- ceute as .COM involve the progeam’s size, segmentation, and initialization. Program sie An EXE program may be virtually any size, whereas a COM program is restricted to one segment anda maximum of 64K, including the PSP. The PSPis a 256-byte (100H) block 108Converting Into .COM Format 108 that the program loader inserts immediately preceding COM and .EXE programs when it Toads them from disk into memory. The 64K limit for a COM program is a general rule; youmay get around it by coding additional SEGMENT AT statements, a feature that is out- side the scope of this chapter A.COM program is always smaller than its counterpart -EXE program: one reason is, that a 512-byte header recox! that precedes an .EXE program on disk does not precede @ COM program. (Don’t confuse the header record, covered in Chapter 24, with the PSP.) A .COM progcam isan absolut: image of the executable program, with no relocarable address information. Segments The use of segments for COM programs is sigmificandly different (and easier) than for EXE programs. Stack segment. You define a stack segment for an EXE program, whereas the «- sembler automatically generates a stack for a COM program. Thus, when you write an 2s- sembly program that is to be converted to COM format, you omit defining the stack. If the 64K program size ts not largt enough, the assembler establishes the stack after the program. in higher memory. Dota segment. For an EXE ptogram, you ususily define a data segment and int tialize the DS register with the address of that segment. Fora.COM program, you don't de- fine a data segment; :nstead, you define the data within wie code segment. AS you'll see, there are simple ways 10 handle this situation, Code segment. A full COM program combines the PSP. stack, data segment, and code segment into ore code segment, in a maximum of 64K bytes. Initialtzation ‘When the program leader loads a.COM proguim forexecution, x autematically initializes alt four segment registers with the address of the PSP. Because the CS and DS registers now contain the correct initial segment address at execution time, your program does not have to initialize them. Because uddreising begins at an offset of 100H bytes from the beginning of the PSP, you have to code a directive, ORG 10H, immediately following the code segmen's SEGMENT o: CONE statement. The ORG atrective tells the zssembler to set its location counter at 100H. The assembter then begins generating the object codcat en offset of 10H bytes past the start of the PSP, where your coding for the COM program begins. CONVERTING INTO COM FORMAT If your source program is already written in EXE format, you can use an editor w convert the instructions into. COM format. MASM and TASM coding formats for COM programs are identical, although their methods for conversion diffes.10 Writing .COM Programs Chep. 7 Microsoft Conversion For both EXE and COM programs under Microsoft MASM, you assemble and produce anOB) file and then link the OBJ file to produce an .EXE program. {f you wrote the pro- ‘gram to cun as an EXE program, you can now executeit. If you wrote the program io nan asa .COM program, the linker produces « message: Warning: No STACK Segment. You ‘may ignore this warning, because there is supposed to be no defined stack. You use a pro- ‘gram nated EXE2BIN to convert the Microsofi .BXE program to a COM program. (Ac- tually, it converts EXE programs to a BIN (binary) file; the program name means “convert EXE-to-BIN,” but you should name your output file extension COM) Assuming that BXE2BIN is in the defautt drive. and that a linked file named SENTINEL.EXFis in drive Dtype EXE2BIN 0: SENTINEL, D:SENTTHEL.COM ‘The first operand of the command always references an EXE file, s0 do not code the EXE extension. The second operand may be any valid filename with 4 .COM extension. If you omit the COM extension, EXEQBIN assumes BIN, which you have to rename subse- quently 2s COM in order to execute the program. (Someone, somewhere, must have thonght this was a good idea.) Borland Conversion ‘As long as your TASM source program is coded according to COM requirements, you can convert your object program directly into COM program. Use the /T option for TLINK: TLD 77 Ds SENTINEL When conversion to COM format is complete, you may delete the generated OBJ and .EXE files. EXAMPLE OF A COM PROGRAM ‘The program in Figure 7-1, named AO7COML, is similar to the one in Figure 5-2, but naw revised to conform to .COM requirements. Note the following changes from Figure'5-2: *# There is no defined stack or data segment. + The ASSUME statement tells the assembler to expect that all segment registers will ‘contain the starting address of the code segment (where the PSP begins) when the pro- gram is loaded for execution. + The directive ORG 100H tells the assembler to advance its location counter 100H bytes from she beginning of the PSP. The program loader stores the 100H in the in- struction pointer (IP) register when it loads the COM program. + AJMP instruction transfers control of execution around the defined data. Some pro- grammers code data items following the instructions, so that no initial JMP instruc- ton is required, Coding data items fiest may speed up the assembly process stighitly.The .COM Stack ” ‘page 60,132 TITLE Ra7coML COM program te move and add CODESG SeqmENT PARA code” ASQUME CS: CODES, nS: CODESG, 9S. DERG, BB: CODESG ara 30a istace at end of PS? De De De PROC mov AK, FLOD Move 0175 ro AX AID RY FLDE Tadd 0130 £0 AX mov FLOF, at Pstore sun in FLOP Mov AK, $COOH nd. processing mer 21k ALOWATH EKDP SDE EROS EXD BRGIN Figure 7-1 .COM Source Program with Conventions! Segments but provides no other advantage. Examples in this book define the data first only as a programming convention. + Tne names BEGIN and AIOMAIN are merely descriptive and are not otherwise meaningful to the assembler. You can use any valid names for these labels. + The conventional INT 21H function 4CH ends processing and exits to the operating system. Youmtay also use the RET instruction to exit from a COM program. (It trans- fers to.an INT 20H, an old exit, in byte 01 of the PSP.) Here are the steps for MASM and TASM to convent the program to COM format: HASH TASH PASH D: AO7COML, D: TASH D:407CDML,0: Link 0:407¢oen, 0: TLINK /7 0:A07C0H1,.D: EXEZBIN :A07COWL, 1 :RO7COML. COM ‘The EXE and COM peograms are 792 bytes and 24 bytes in size, respectively. The difference is largely cansed by the 512-byte header block stored at he beginning of EXE. modules. Type DEBUG D:AD7COMI_.COM and trace execution of the COM program up. (o (but not incinding) the last instruction, You may also use simplified segment directives when coding a COM progratm, as shown in Figure 7-2. Once azain, define only a code segment, not astack or data segment. ‘THE COM STACK Fora COM program, the system automatically defines the stack and sets the same segment address in all four segment registers. Ifthe 64K segment for the program is iarge enough, the program loader sets the stack at the end of the segment and sets the SP register with the address of the top of the stack.112 Writing COM Programs Chap.7 page 60,322 TITUE AO7GOM2' COM program te move and add data MODEL SMALL “cone oR aon potart at ead of PEP BEGIN: GME ALOMATN foump pase data jnats de€icicioas pene Be 350 PuDr ALOMAIN PROC) NEAR Mov ake, PLD imove 0275 to AX aD AK eDe. jada. 0150 to At mov FLDPLAK :Store sum in PLP Woy AX, 4Co0H find precesairg wr ALOMAIN BXDP END Fignre 7.2. COM Source Program with Simplificé Segment Diveotives ithe 64K segment does not contain enongh space for a stack, the loader sets the sack at the end of memory, Tn either case, dhe loader then pushes a zero Word onto the stack. which acts 4s un offsct for the IPif you use RET te end execution of a COM program. Uf your program is large, or if meraory is restricted, you may have to lake cae push- ing words onto the stack, The DIR command indicates the size of a file and gives you an idea as 19 the space available fora stack, Many of the smalfer programs in this book arc in COM format, und are easily distingsished from .EXE format DEBUGGING TIPS ‘The omission of only one COM requirement may cause a program to fail. {f EXE2BIN finds an error. it simply notifies you that it cannot conver: the file, but doe: not provide a reason. Check the SEGMENT. ASSUME. snd END statements. If yau amit ORG 100H, the executing program incorrectly references data in the PSP, with naprediciahle results If you run a COM program under DEBUG, use D CS:100 to view the data and in- structions. Do not follow the program. through its termination; instead, use DEBUG'S Q command. An attempt to execute the EXE module of ¢ program written as COM will fail; be sure 10 delete this file KEY POINTS + A.COM program is restricted to one 64K vegment and is smaller thanits couaterpart EXE program + Aprogram written as COM does not define a stack or data segment, nor docs it ini tialize the DS register.Questions 13 + Aprogramn written as COM requires ORG 100H immediately following the code seg- ment’s SEGMENT statement. The statement sets the offset address to the beginning of execution following the PSP. + For Microsoft MASM, the EXE2BIN program converts au .EXE file to .COM for mat. Borland’s TLINK can convert an object program directly into COM format. + The system installs the stack for a COM program at the end of the program. QUESTIONS: Fel. 9-2. 2.3. 74. 25. 7-6. ‘What is the maximum size of a.COM program? For a source program ta be converted to.COM format, what segments) can you de fine? Explain why you code ORG 100H at the beginning ofa program to be converted to .COM format: Why is it not necessary to define a stack for a COM program? Assource program is named PRESSURE.ASM. Provide the commands fo convirt it (0 .COM format under (3) MASM; tb) TASM. Revise the program in Question 6-12 for COM format. Assemble, link, convert it (© .COM, and execute it under DEBUG.| 8 PROGRAMMING REQUIREMENTS FOR LOGIC AND CONTROL Objectives: To cover the requirements for program control (loop ing and jumping), for logical comparisons, for logical bit “opera tions, and for program organization. ih INTRODUCTION Up to this chapter. most of our programs have execured in a straighe Line, withone instruction sequenttelty following anosher. Seldom, however, isa programmable problemithat simple. In- stead, most programs consist of various tests io determine which of several actions to take and ampmnber of loops in which a series of steps tepeats until a specific requirement is ceached. A common practice, for example, isto test whether a program is to end execution Requicements such os these involve a transfer of control t the address of an instruction that does not immediately follow the one currently executing. A transfer of control may be forward, 10 execute a new series of steps, or backward. to reexecute the same steps, Insiruc- tions that can transfer control outside the normal sequential flow do so by adding an offset value to the IP. Following are the instructions introduced in this chapter, by category: Compare Toaster Lagat tana Opeations Operations penton Rate oF au a0 SAL/St0 mest Me Nor sAL/sat Inna? om RCH/ROR scar xoR Binal ham means all Conditions] jemp instructions such as INE ent JL, 4Instrustion Lobels nS SHORT, NEAR, AND FAR ADDRESSES ‘The assenibler supports thre types of addresses that ore distinguisted by their distance from the currently executing address: 1. A short address, limited to a distance of -128 to 127 bytes. 2 A near address, Yimiled to a distance of ~ 32,768 to 32.767 bytes within the same sogmient 3. Ajar address, which may be at ¢ distance aver 32K or in another segment. A jump operation reaches 4 short address by a I-byte offset and reaches a ncar ad- dress by a 1- or 2-word offset. A far address is reached by a segment address and an offset: CALL is the normal instruction for this purpose because it facilitates linking to the re- quested address and the subsequent rerum. “The following table lists the males on distances for JMB, LOOP, and CALL operations. ‘There is tittle need tomemorize these rules, because normal use of these instructions rarely causes problems. Short New ve TDF Shee waeseT Over Korie Inntvctions Same argment Same segment ——_anvther segment Mp yes yes yes inne yes yes (60386) ne Loop ye 2 ne CALL NA ses yee INSTRUCTION LABELS. ‘The operand of the JMP. Inna (conditional jump), and LOOP instructions refer to the Label ‘of another instuction, The following example jumps 10 P50, which is the label of an INC instruction: MP Psa Psa: WC OX ‘The label of an instruction such as P50: is terminated by a colon, which givesit the near at- tribute—that is, the lebef is inside « procedure in the same code segment. Watch out: Omis- sion of the colon is acommon error, which the assembler signals, Note that an address [abel in an instruction operand (such as IMP P50) does not have a colon. ‘You can also code a label on a separateline as P50: IN xk In both cases, the address of PSO references the first byte of the INC instruction.16 Programming Requirements for Logic andContat Chap. 8 THE JMP INSTRUCTION. Acommonly used instruction for transferring conzol is the JMP(ump) instruction. 4 jump is unconditional, because the operation transfers control under all circumstances. IMP also. Alushes the processor's prefetch instruction queue: thus a program with many jump opert- tions may lose some processing speed. The general format for JMP is [labet:] INP | shert, near, o fay Short and Near Jumps ‘A IMPoperation within the sare segment may be short or aear (or even far if the destina- tion is a procedure with the FAR attribute). On its first yass through a source program, the assembler generates the length of eack instruction. However, a JMPinstruction may be tro, three, vr four bytes long. A IMP operation to a label within ~128 to +127 bytesis a shore jump. The assembler generates one byte For the operation (EB) and one byte for the operand, ‘The operand acts as an offset value that the processor adds to the IPregister when execut- ing the program. The limits are OOH to FFH, of -128 to +127. AIMP that exceeds —128to +127 bytes becomes anear jump (within 32K).for which the assembler generates different machine code (E9) anda 2-byte operand (808680286) or 4-byte operand (80386 and later). For now, we'Ilpass on the far jump. Backward and Forward Jumps A jump may be backward or forward. The assembler may have already encountered the des~ ignated operand (a backward jump) within —L2Bbytes, a8 ia P50: IMP Po In this case, the assembler generates u 2-byte mschine instruction. In a forward jump, the assembler has not yet ercountered the designated operand: IP Pon P90: Because the assembler doesn’t know at this point whether the forward jump is short or newr, some versions assume near and generate a 3-byle instruction. However, provided that the jump really is short, you can use the SHORT operator to force a short jamp and a 2-byte in- struction by coding IMP SHORT 9G P90:The Jmp Instruction "7 Program: Using the JMP instruction ‘The COM program in Figure 8-1 illustrates the use of the JMP instnaction. The program ini- tializes the AX, BX. and CX registers 10 the value of 1. and a loop performs the following: page 69,132 mame — Koagump’ (com) Jeing JMP for looping THODEL SMALL reo o1o¢ oka 1008 e1ne AaoMAIN BROC NEAR e100 £9 ¢oo2 you AX, 02 pnitialize Ax, 102 BB t00: ov Bx, 01 1K, and 106 65 2002 Moy @x;OL 3b cK’ eo on o705 ano: 9189 os s001 oD AK, OF add OL to AR eLoc 03 De app BKK. {aad AK to OX 108 Gl Er BML x,2 {Double cK 0110 BBP? me 2b fgump to A20 Lapel ouaa ALOMAIH END? ERD ALOMAIN Figure 1 » Add 1 te AX + Add the AX to the BX » Double the value in the CX ‘Atthe end of the loop (after the SHL}, the instruction IMP A70 transfers control to the in- struction labeled A20. The effect of repeating the loop causes the AX to increase as I, 2. 3. 4,...; the BX to increase according to the sum of the digits as 2, 3.6,10, ... : and the CX. to double as 1, 2, 4. 8,.... Because this loop has no exit, processing is endless—asually not a desirable practice. In the program, A20‘s —9 bytes from the IMP. You can conficm this distance by ex- wining the object code for the JMP: BBF7. EB is the machine code fer a near JMPand hex. FTis the qwo'scomplementnotation for —9. At this poirt, the IP contains the offset (01 12H) ‘ofthe next instruction to execute. Bocause this is a backward jurap, the operand F7 is nega tive. The IMP operation adds the F7 (cechnically, FFP7, because the IP is a word in size} to the IP, which contains the offset 0112H of the instruction following: the IMP: DECAL HEX Insteuction register: 274 ouz IMP operand: =3 FFF? (two's complement) Jump address: 26s capo109 ‘The assembler calculates the jump address as O!O9H. where the carry out of 1 is ignored (as a check of the program listing for the offset address of A20 shows). The operation changes the offset value in the IP and flushes the instruction queue.18 Programming Requi mente for Logic and Control Chap. 8 As a usefial practice, key im the program, assemble it, link it, and convert it co COM, format. No dala is defined, beczuse immediate operands generale all the required data. Use DEBUG to trace the COM module for a number of ilerations and observe the effect of exe- cation on the AX, BX, CX, and IP. Once the AX contains 08, the BX and CX will be in- ‘cremened 10 24H (decimal 36) and 8OH (decimal 128), respectively. Key in Q w quit DEBUG. THE LOOP INSTRUCTION: As used in Figure 8-1, the JMP instruction canses an endiess loop. But a standard practice is to code a routing thut loops a specified number of times or until it reaches a particular condition. The LOOP instruction, which serves this putpose, requires an initial value in the (CX register. For each iteration, LOOP automatically dedacts 1 from the CX. Once the value in the CX reaches zero, control drops throagh to the following instruction; if the value in the CX is nonzero, control jumps to the operand address. The distance to the operand must be a short jump. within ~128 to +127 bytes. Fot an operation that exceeds chis limit, the assembler issues a message such as “relative jump out of range.” The general format for LOOPis [tuabeTs | Tie J Woet-aarese The program in Figure 8-2 itJustrates dhe use of LOOP. It performs the same opera- tion as the program in Figure 8-1, except thal this one uses a MOV instruction to initialize the CX with the value 10 and ends after 10 loops. Because LOOP requires use of the CX, this program now uses the DX in place of CX far doubling the initial value 1. The LOOP insiruction replaces YMP.A20 and, for faster processing, INC AX (ineretrent the AX by 1) replaces ADD AX,OL. 60,132 rime — Rosuoop’ {eam} TLJuatration of LOOP “HOUEL SPALL “cone x00 RG con 100 Atonare BROC NEAR 2200 Be 9003, Moe Axon 0303 BB Goa sov | BHO 8106 BA O01 yov | BROT OK to 0a 2:03 89 9900 pov GN tnttialise tor o19e azo: i een loopa Choc 49 mca jad: eo At Sion 93 ps ADD OBLAN jada AC to BE Sior BL Ba Bit By: Boubile bx oil Ba 69 zocor ib) [Decranant cx, er bop LE noasere axa Bp 4c00 Mov AL, acooH jend procewsing dire eat ter Di aie icant ENO END ALOMAIN RigureS-2_ Using he LOOP instnucionTha Flags Ragister 18 Just as for JMP, the machine code operand for LOOP contains the distance from the end of the instruction «y the address of A20, which the operauin adds to the IP when the program excoutes, ‘Asa aseful exercise, modify your copy of Figure 8-1 for these changes. and agseen- ble, link, and convert the program to COM. Use DEBUG to trace through the entire 10 loops and observe the effect of execution onthe AX, BX, CX, DX, and IP. Once the CX is reduced (0 22rv, the contents of AX, BX, and DX are, respectively, OOOBH, 0042H, and 00H. Press Q 10 quit DEBUG. ‘There ate two variations on the LOOP instructian, both of whieh also decrement the CX by I LOOPE/LOOPZ (oop while equal or loop white zero) continues looping as long as the value in the CX is zero of the zero condition is set, LOOPNE/LOOPNZ (loop while not equal or loop while not zero) continues looping as Joug as the value in the CX is aut rao o¢ the zero condition is not sc. Neither LOO? nor its LOOPxx variations changes the setting of any Hugs in the flags register, However, because othet instructions within the loop routine do change flags, the program in Figure 8-2 ases LOOP, and not its LOOPxx variations. THE FLAGS REGISTER ‘The remaining material in this chapter requires a more detailed knowledge of the flags rep- ister, This register contains 16 bits, which various instructions set toindicate the status of an operation. In all cases, a flag remains set until another instruction changes it. The flags register for real mode contains the following commonty used bits, described here from right to left: Bitno.: [a5 14 33 12 19 9 8 Flag: o DIT CF (Cony flag). Contains a carry (0 or L) from the ngh-order (lestmost) bit fol- Jowing arithmetic operations and some shift and rotate operations. JC and JNC test his flag, PE (Parity fag). Comiains a check of the low-order 8 hits of data cperations, The parity flag is not tohe confused with the party bit described in Chapler I atd is seldom of concern in conventional progtamming. An odd auniber of L-bits clears the fag 10 0, and an even number of 1-bits sets it @ 1. JP and JPO test this Hag. AF (Auxillary carry fig). Concerned with arithmetic on ASCII and BCD packed fields. This flag is set when a L-byte arithmetic operation causes a carry out of bit 3 (the fourth bit from the night) of a register. ZE Gero fag). Cleared or set as a resuitof an arithmetic or compare operation, Un- expectedly. a nonzera resilt clears the flag to 0, and a zero result sets it to 1, However, the setting, if not apparently correct. is logically correct: 0 means no (the cesult is not equal to zero), and 1 means yes (the result equals zero). JE and JZ. (among other instructions) test this fag.120 Program ing Requirements for Logicand Control Chap.8 SF ign flag). Set according to the sign thigh-order or leftmost bith generated by an arithmetic operation: A positive Value clears the flag, lo O, and negative sets it to |. 1G and JL. (among other instructions) test this flag, TFdraptag). When set. causes the processor toexecute in single-step mode, that is, one justruction at 2 time under user control. DEBUG sets this flag when you type in Ox ‘Teommané, and that’s about the only place where you'dexpect « use it, {Fdnteruptfiag). Disables interrupts when 0, and enables interrupts when I, This flag iy mrely used in conventional programming. DF (Direction flag). Used by string operations to determine the direction of data transfer. When the flag is 0, the string operation performs left-to-right data transfer; whan the flagis 1, the string operation pettorms right-to-left dita transfer. OF (Overflow flag). Indicates carry into and out of ths high-order (leftmost) sign bit following a signed arithmetic opcration. 1G and JNO test this flag. ‘THE CMP INSTRUCTION, ‘The CMP instraction is used to compare two numeric data fields, on€ or both of which ate contained in a register. Its general format is | sraber 7 [ont [reat ster/merory agi stor/memory immedi at ‘Technically, you may use CMP to compare string (character) data, hut CMPS (cov eced in Chapter 12) is the appropriate instruction for thts purpose, The sesult of a CMP op- eration affects the AF. CR. OF. PR. SF, end ZF flags. although you do not have to test these flags individually. The following code tests the DX regisier for a zero value: WP x,00 sCoupare 0X to zere We P50 ETF equal, jump to B50 Caction iF nonzero) pe sdump point iF OX is zere If the DX contains zero, CMP sets the ZF to 1 and may or may not change the settings of ther Nags. The JE (Jump if Equal) instruction tests only the ZF flag. Because ZF contains | (meaning a zero condition}, JE transfers control (jumps) to the address indicated by operand PSO. In effect, the CMP operation compares the first to the second operand: for example, is the value of the first operand higher than. equal to, of lower than the value of the second operand? (CMP acts like SUB withour the storage cycie.) The next section, provides the various ways of transfering control based on tesied conditions.Conditional Jump instructions yaa CONDITIONAL JUMP INSTRUCTIONS ‘The processor supports a variety of conditional jump instructions that transter control de- pending on settings in the flags register. For example, you can compare two fields and then jump conditionally according to Nag values that the compare vets. ‘The general format for ‘the conditional jump is {Mlabe) ann! shoreaddress As explained earlier, the LOOP instruction decrements the CX register; if it is nonzero, control transfers to the operand adress. You could replace the LOOP A20 state- ment in Figure 8-2 with two statemenis-—one that decrements the CX ane another that per- ‘ferms a conditional jump: DECK sEquivatent to Loo? INZ Ate : DEC docrementsthoCX by | and sets orclearsthe zero flagiin the flags register. INZ then tests the seiting of the zero flag; if the CX is nonzero, control jumps 19 A20, and if the CX is zero, control drops through to the next instruction. (A jump operation that branches also flushes the processor's prefetch instruction queue.) Although LOOP has limited uses, in tuts example it enecuies faster and uses fewer bytes than does the use of the DEC and JNZ insiructions. Just as for IMP and LOOP, the machine code operand for INZ contains the distance from the end of the insteuetion to the address of A20, which the operation adds to the [P reg ister. For dhe H086/286, the disiance for a conditional jump must be a short, within —128 co +1127 bytes. If the operation exceeds this Himil, the assembler issues a message “relauve jump out of runge.” The 80386 and later processors also provide for 32-bit (near! offsets that allow reaching any address within 32K. Signed and Unsigned Data Distinguishing the purpose of conditienal jumps should clarify their use. The type of data (un- signed of sigred)on which you ate performing comparisonsor arithmetic can determine which instructing to use. An unsigned numeric item (logical data) teats all bits as data bits; typical examplesare numeric values suchas cus-omernurbers and phone numbers, A signed numeric item (arithmetic dataj teats the leftmost bitas a sign. where Dis positive and | isnegative: typ- ‘cel examples ate quantity, discoure, and distance. which may be either positive or negative. In the neat exumple, assume that the CX contains 11000110 and the DX contains 00010116. The instruction MP 0x, 0x compares the contents of the CX to the contents of the DX. If you treat the data as unsigned, the CX value is larger; if you treat the data as signed, however. the CX value is smaller because of the negative sign. The use here of CMP is valid, and you have 10 select the122 Programming Requiremants for Logic and Control Chap. appropriate conditional jump instruction. such as. JB Gump Below) for unsigned data or JL. (Jump Low) for signed data. Jumps Based on Unsigned (Logica) Data Use the following condiddonal jump instructions for unsigned date: ‘SYMBOL DESCRIPTION FLAGS TESTED JR/3Z Sump Equal or B36 Mey aH, om ‘wre 21u Rr wp END ALONALN, ‘Using sump Table gs! 2Ené proceaaing see XG char bo inke Ab Lear left 5 pies Hove AL to Bx WDoubie value Hump to cust xine ieode routine 2 routine 2 routine 2 rovtine « routine The procedure BLOIUMP accepts a character from the keyboard in the AL and moves M10 the BX. The value is doubled, so that O stays 0, 1 becomes 2, 2 becomes, and so forth. The doubled value provides an offset inte the table: CUSTTAB +0 is the first address, CUSTTAB +2 the second, CUSTIAB +4 is the third, and so forth, The operand of the136 Programming Requirements fer Logic and Contra! Chap. 8 IMPinstruction, [CUSTTAB + BX}, forms zn address based on the star! of the table plus an offset into the table, The operation then jumps directly to the appropriate routine An important constraint in the program is that the codes may be only the hex val- ues 00-04, zaty other value would cause dire results, and the program should check for this possibility. If you run this program, key in valid values (0-4) to check the effect of the logic. ‘The MOVZX instuction used to move the I-byte keyboard character into the BX. works onty for the 80386 and Ister processors. For earlier processors, you could replace it with the following: XOR BH, BH OY BL,AL ‘lear upper BX Move discount code ORGANIZING A PROGRAM The following are recommended steps in writing an assembly program: 1. Have a clear idea of the problem that the programis to solve. 2. Sketch your ideas in general terms, and plan the overall logic. For example, if a prod- Jem is to perform multibyte move operations, start by defining the Fields to be moved. ‘Then plen the strategy for the instructions: soutines for initialization, for usiag a con- ditional jump, and for using a loop. The following, which shows the main logic, 13 pseudocode that many programmers use to plan a program: * Initislize segment registers + Coll the Jump routine + Call the Loop routine : + End processing ‘The Jump roatine could be planned as + Initialize registers for count, addresses of names, Jump! + Meve one character of name + Increment for next cheracters of names + Decrement count: If nonzero, Jump! If zero. Return ‘The Loop routine could he handled ir a similar wey, Orgenize the program into logical units such that related routines follow one another. Procedures that are about 25 Tines (the size of the screen) are easier to debug than pro- cedures that are longer. 4, Use other programs as guides. Attempts to memorize all the technical material and code “off the top of the heud” ofien result in even more program bugs. Use comments to clarify what a procedure is supposed to ascoraplish, what arithmetic and comparison operations are performing, and what a seldom-need instcuctian is éo- ing. (An example of the ‘atter is LOOPNE: Does it loop while not equal or until not equal?) 3Questions 137 6, To facilitate keying in the program, use a saved skeleton program that you can copy into a newly named file. ‘The remaining programs in this text make considerable use of IMP, LOOP, condi: Gonui jumps, CALL, and called procedures. Having covered the basics of assernbly ian guage, you are now in a position (or more advanced and realistic programming. KEY POINTS + Ashon address is reached by an offset andis limited toa distance of -128 te 127 bytes. ‘Ancat address is reached by an offsct and is limited tu a distance of -32,768 to 32,767 bytes within the same segment. A far addressin another segment is reached by a veg- ment address and offset. + An instruction label such as “P50:" requires a colon to indicate that itis a neer label + Labels for conditional jump and LOOP instructions must be short. The operand gen erates 1 byte of object code: O1H to 7FH covers the curge from decimal +1 19 +127, and FF4 to 80H covers the range from — 1 9 ~128, Because machine insiructions vary in length from I to 4 bytes, the range isnot obvious, but about twa screens full of source cad: is a practical guide. + When using LOOP, initialize the CX with positive velue, because LOOP deste: ments the CX and then checks il for zero. + When an instruction sets a flag, the flag remains sc: until another instruction changes it. «+ Select the appropriate conditional jump instruction, depending on whether the opera Goa processes signed or unsigned data, * Use CALL toaccess a procedure, and include RET/RETN at the end of the procedure for returning. A called procedure may call other procedures, andif you follow the gon: ventions, RET causes the correct address in she stack to pop, *# Use left shift to double a value and right shift to halve a value. Be sure to select the appropriate shift instruction for unsigned and for signed data. QUESTIONS 8-1. Explain these types of addresses: (a] short; (b) near; (©) far. 8-2. (a) What is the maximum number of bytes that a neat IMP, a LOOP, and a condi- Conal jump instruction may jump? (b) Waal characteristic of the machine code operand causes this limit? $3. A IMP instruction begins at offset location 0SC8H. Desermine the transfer offset ad- dress. based on the following object code for the IMP operand: (a) 14H: (6) 7DH; (©) ASH. 8-4. Write a program that calculates the Fibonacci series: 1,1,2,3,5,8,13,.... (Except for the first two numbers in the sequenee, eack namber is the sum of the preceding138, &5. a, 87. 89. 8-10. iL. $12. 8-13, B16, Programming Requirements for Lagic and Control! Chap. 8 ‘wo numbers.) Use LOOP ane set the lirnic for 12 tterations. Assemble, link, and use DEBUG to ace through the routine, Assume that AX and BX contain unsigned data and that CX and DX contain signed data. Determine the CMP (where necessary) and conditional jump instructions for the following: (a) Is the AX equal to or smatler than the BX? (b) Is the CX equal to or smaller than the DX? {c) Does the CX value exceed the DX" (d} Does the AX value exceed the BX? (e) Does the DX contain zero? iI) Is there an overflow? In the following, what flags are affected, and what would they contain? {a) Pro- essing is in single-step mode; (b) a transitr of string data is to be right to lett; (6) a result is negative; (d} a resol is zesa; (@) an overflow occurred. Refer (o Figure 8-3 and explain dhe effect on program execution if the procedure B10 docs not contain a RET. Explain the difference beween defining a PROC operand with NEAR and with FAR Meniity three ways in which an executing program can enter @ procedure. Tn an EXE program, F10 calls G10. G10 calls H4, and H10 calls J10. of these calis, how many ackdresses does the stack now contain? ‘Assume that the CH contains O1L1 1001 and thal an item named TESTVAL con- tains 11100011. Determine the effect on the CH for the following unrelated opera- tions: (2) OR CH,TESTVAL: (b) AND CH.TESTVAL: (c} XOR CH.TESTVAL; (a) AND CH,09000000B: (e) XOR CH.111} 12118 Revise the program in Figure #-4 as tollows: Define the contents of CONAME as lowercase lerters and code the instructions that convert lowercase to uppercase, Assume that the BX contains binary 10111001 10111001 and the C1. contains 03, Determine the hex contents of the BX after execution uf the following unrelated in~ structions: (a} SHE BE, |:(b) SHLBX.CL: (¢) SHR BX,CL; (d} SHRBX,1(€) SAL BHI: () ROR BX.CL: (g} ROR DLL, Use shift, move, and add instructions to initialize the CX with 40H and to mukiply iby 10 ‘An example al the end of the section entitled “Rotating Bits” muitiphes the DX:AX by 2. Revise the routine to (a) multiply by 4; (b) divide by 4; (c} muliply the 48 bits in the DX:AX:BX by 2 ‘Transfer the contents of the CX:BX to the EAX and usea shift to multiply the AX by 4. a resultPART C—SCREEN AND KEYBOARD OPERATIONS. LD | INTRODUCTION =—— TO SCREEN == AND KEYBOARD PROCESSING Objective: To introduce the requirements for displaying informa tion on a screen and sccepting input from e keyboard. il INTRODUCTION Up to.this chapter, our programs have defined data items either in the data area or as isn- mediate data within an instruction operand. However, most programs requice input fromm & keyboard, disk, mouse, o modem ard provide output in a useful format on a screen, printer, of disk. This chapter covers the basic requirements for displaying information on a sercen and for accepting input from a keyboard. ‘The INT (interrapt) instruction handles input and output for most purposes. The two interrupts covered in this chapter are INT 10H functions for screen handling and INT 21H functions for displaying screen output and accepting keyboard input. These fianctions (or services) request & particutar action: you inser! 2 function value in the AH register to iden- tify the type of service the interrupt is to perfom. Low-level BIOS operations such as INT 10H transfer control directly to BIOS. How- ever, to facilitate sosne of the more complex operations, INT 21H provides an interrupt ser- vice dhat Gust transfers contol to DOS, Por example, input froma keyboard may involve a count of characters entered and a check against a moximum mumber. The INT 21H opers- tion handles much of this additional high-level processing and then traasfers control auto- matically to BIOS, which handles the low-level part of the operation. ‘Asa convention, this book refers to the value ODH as the Enter character for the key- board and as Carriage Return for the screen and printer. Operations introduced in this chapter are: 139140 Introduction to Sere in andKeyboard Processing Chap. 9 INT 10H FUNCTIONS «= INT 2AM FUNCTIONS O2H Set curser G2 Display cheracter on screen GH Scro1t screen —«OBK_——DF splay strieg en cers, OAH Input fron keyboard 3H Input fron keyboard 40H Display on screen Chapters 10 and 11 cover more advanced features for handling the screen and keyboard. THE SCREEN Atypical video monitor has 25 rows (numbered 0 to 24) and 80 columns (numbered 0 to 79). The rows and columns provide 4 grid of addressable locations at any one of which the ‘cursor can be set. Here are some examples of cursor locations: Devimmal Format Hex Format Seren Locwion Row Colum Row Column Upper left corner 00 00 = 00H OOH Upper eight corner co 79 (OOK FH Center of screen 1238/40 OCH 27H/280 Lower lef comer 24 00 18H OOH Lower right corner 74 79 18H aeH ‘The system provides space in memory for a video display area, or buffer. The mono: chrome display area begins at BIOS location BOOO{O]H and supports 4K bytes of memory, 2K of which are available for characters and 2K for an attribute for each character, such a reverse video, blinking, high intensity, and underlining. The basic color-graphies video dis. play area supports 16K bytes, starting at BIOS location B800{OJH. You can process either in text mode for normal character display or in graphics mode. For text mode, the display zea provides for screen “pages” numbered O through 3 for an 80-cohumn screen, with one byte for each character and one for its attribute (such as color), Pages and attributes are cov- ered in detail in Chapter 10: for this chapter, we'll assume page 0. ‘The imterrupss that handée screen displays transfer your data directly tw a video display area, depending on the type of video adapter insalled, such as VOA or SVGA. Technically, ‘your programs nay transfer data directly (o the video display aree, bue there is no assurance that the memory addresses will be the same on all computer modsls, vo writing data directly to a display area, although fast, can be risky. The recommended practice is to use the appro- prince INT 10H and INT 21H operations that know the location of the video display area. SETTING THE CURSOR Setting the cursor is a common requirement for text mode, because its position determines where the next character is to display. (Graphics mode does not support the cursor.) INT 10H is the BIOS operation for screes handling, and funetion O2H in the AH cells the oper-Clearing The Screen 144 ation to set the cursor. Load the required page (or screen) nurnber, normally 0, in the BH register, the row in the DH, and the columm in the DL. The contents of the other registers are not important, ‘The following example sets the cursor torow O8. column 13: MOY AH.G2H Request set cursor MOY BH.0O Raye mumber 0 HOY DH,G = jRow 8 MOY DLS Cetin 35 int 10H ia) Anterrupt service “To set the row and column inthe DX, you could also use one MOV instruction with aa in- mediate Hex value: Ov DX,0B0FH Row O8, column 15 CLEARING THE SCREEN . SNT 10H function 06H handles screen clearing or scrolling. You can clear all ot part of a display beginning at any screen location and ending atany higher-numbered location, Load these registers: + AH = function 064 + AL ~ number of tines to scroll, or OOH for the full screen. + BH = atuibute value (color, reverse video, blinking) + CX = starting mw:coomn + DX = ending row:eohumo, ‘The CX and DX together define the screen ares (or window) to be scrolled, and the AL provides the number of lines to be scrolled up. To clear the ensire screen. sprcify the starting row:column in the CX as 00:00H and the ending row:coluna inthe DX as 18:4FH. “Attribute 718 in the following example sets the entire screen so white background (attribute 7) with blue foreground (attibute 1): Hoy AX,0600H GAH = 66 CeeraT1), AL ~ 90 (full seraen) Mov BHM nite background (7). blue foreground (1) Mov CX,JO00H —:Upper Tete row:column MOY OX,18AFH Lower right rom:colum INT 10H Mall interrupt service For example, to scroll the screen window from row 05, column 00 through row 12, colsma 79, you load 0500H in the CX and OCSFH in the DX. Be careful of mistakenly setting the lower right screen Location higher than 184FH. ‘The next chapter describes scrolting in more detail, ‘A program often has to display messages 10 user that requests data or an ection the user mast ke. We'll first examine the methods for the original DOS versions, which are useful for exercises and small programs, and Luter examine the methods that invelve file handles. The original operations work ender all versions and in some respects are2 Introduction to Screen and Keyboard Processing Chap. 9 simpler and easier to use, although use of the newer opesations ix recommended for sof- ‘ware development. INT 21H FUNCTION 09H FOR SCREEN DISPLAY ‘The simplicity of the original INT 21H function 09H for displaying stil keeps it in com- mon use. It requires definition of a display string in the data area, immediately followed by a dollar sign ($ or 24H) delimiter, which the operation uses to end the display. The follow. ing example illustrates: CUSTMSG 08 ‘Customer name?’,'S Display string ‘You can code the dollar sign immediately following the display string as just shown, mside the soring as ‘Customer name?S" or on the nextline as DB *S". The disadvantage, however, is that you can’t use this function to display a $ charauter on the screen. Set function 09H in the AH register, use LEA toload the address cf the display string, in the DX, und issue aa INT 21H instruction. roy at,09H quest. display LEA OK,CUSTUSG Load adftfess of prompr mr 2 ral totercupt service The INT operation displays the characters from left to right and recognizes the end ‘of data on encountering the doilar sign (S) delimiter. The operation does not change the eon- tems of the registers. A displayed string that exceeds the rightinost screen cohamn auto- matically continues on the next row and scrolls the sorcen as necessary. If you omit the dollar sign at the end of the string, the operation continues displaying characters from con- secutive memory locations until it encounters one—if there is one. Using Function 09H fo Display ASCII Charocters Most of the 256 ASCH characters are represented by symbots that can be displayed on a video screen. Some values, such as OOH and FFH, have no displayable symbol and appear as blank, although the true ASC blank character is 20H. ‘The COM program in Figure 9-1 displays the entire range of ASCII characters, The procedure AIOMAIN calls three procecures: + BIOSCRN uses ENT 10H function O6H to clear the screen. + CIOCURS uses INT 10H function 02H w initialize the cursor to 00:00H. + DIODISP uses INT 21H function 09H to display the contents of ASCHAR, which is, initialized to OOH and is successively incremented by 1 to display each character uo- til reaching FFH. “The first displayed tine begins with a blank (QOH), two “happy faces” (OH and 02H, and then a heart (03K), diamond (04H), and club (05H). Character OGH would have dis- played a spade, but is crased by later control characters. Character 07H causes the speaker to sound, O8H causes a backspace, OH causes a tab, OAH causes a line feed, and ODMINT 21H Function 09H for Screen Display “3 page 90,132 tums AgaDr3AS (COM) Display ASCII character sec "MODEL SMALL. ‘cope ORS 1008, DEGIN: SMP SHORT, RLOMATN AScHAR = «DR 0D, "5 itheplay character 4 male procedure: ArOMAIN PROC HEAR ALL BLOscRN iGlear sereen CALL Cocuns [set cursor CALL © BLODISE Display characters wou ax, acy ena mer 24H P processing A1oMRIN | -ENDP clear screen: BioscRN «PROC NEAR - mov AX, Os 0cE iScrol1 full screen Actribute: white on black ‘pper left Saeation Mov = Dx,164m Hower right Leeatian aNr 108 igerh Bros RET pReturn to caller mioscRe | ENDP ’ Sot curser be 06,001 CLOCURS PROC REAR Mov 2H, O21 ;Request get cursor Noy BH,co PPage suber © Moy 2X,0000 [Row 0) column 0 Int 10% peal Bros per [Return to caller cxocuRs ENDP : bisplay ASCT? characters: DLODTSP PROC wow ox,256 Finitialize 256 iterations LER -Dx,ASCHAR. [initialize address of ASCHAR va Moy 2H, 9H :Digplay ASCII ASCHAR zee 24 INC ASGHAR iiaceement for next charactar cor Dae [Decrement CX, loop nonzero RET. pReturn to caller Daoprse ENDP END BEGIN Figure 9-1 INT 21H to Display the ASCH Character ct (Cotes) causes: a “carriage return” to the stant of the next line, And. of course, under func Gon O9H, the dollar symbol, 24H, is not displayed at all. (As covered in Chapter 10, you can use BIOS services to display proper symbols for these special characters.) The musical rote is GEH, and 7FH through FFH are extended ASCTI characters. You can revise the program to bypass the altempt .o display the control characters ‘The following instructions bypass all characters betwoen OBH and ODH; you may want 10 experiment with bypassing, say. only 08H (Backspace) and ODH (Carriage Return).18 Introduction to Sereen and Keyboard Processing Chap. 9 m0: CMP ASCHAR,OBH Below O8H7 3B 030 i¥es, accept CMP ASCHAR,ODH —Below.or equa? ODH? IBE 40. Yes, bypass p30: Mov at, 031 yisplay < GI and > OO INT 21H 3CaN1 intorrupt service p40: INC ASCHaR jncresent for next character oor nz0 iRepeat This exercise bypasses displaying the Backspace, Tab, Line Peed, and Carriage Re- cum chatacters. Note that digplaying them is the normal way to perform these operations. ‘Suggestion: Reproduce the preceding program, assemble it, Jink #, and convert it to a.COM file for execution INT 21H FUNCTION DAH FOR KEYBOARD INPUT INT 21H function OAH for accepting data from the keyboard is particularly powerful. The ‘input area for keyed-in characters requites @ parameter list containing specified fields that the INT operation is to process. (IF you've worked in a high-level language, you may be used 10 the term record o¢ siructere.) First, the operation needs to know the maxisnum Iength of the input data. The purpose is to prevent users from keying in too many charac- fers; the operation sounds the speaker and docs not accept additional characters. Second, the operation delivers to the parameter fist the number of bytes actually entered, The para meter list consists of these elements: 1. The first ontry provides the name of the parimeter list in the form LABEL BYTE. LABEL is a directive with the type ettribute of BYTE, which simply causes align- ment on a byte boundary. Because that's the normal alignment, the assembler does not advance its location counter. The use of LABEL enables you to assign 2 name to the parameter list. 2. The first byte of the parameter lis contains your limit for the maximum number of input characters, The minimurp is 0 and, because this iS a I-byte field, the maximum is FFH, or 255. You decide an the maximum, based on the kind of cata you expect users to enter. 3. The second byte is for the operation to store the actual number of characters typed as, a binary vale |. The third byte begirs a field that is to contaie the typed characters, from left to Tight. ‘The following example defines a parameter list for an input area PARALST LABEL BYTE sStart of parameter 1ist WAXLEN 0B 20 Maxtmun umber of input characters ACTLEN 08? ‘ctual nunber of input charactecs KBOATA pa 20 UR" *) aractars anterad fron keybeardProgram: Accepting and Displaying Namos 145 In the parameter list, the LABEL directive tells the assembler to align on a byte boundary and gives the location the name PARALST. Because LABEL takes no space, PARALST. and MAXLEN refer to the same memory location, MAXLEN defines the maximum mum- ber of keyboard characters, ACTLEN provides a space for the operation to insert the accuz) nurnber of characters eatered, and KBDATA reserves 20 spaces for te characters. You may use any valid names for these fields. To request input, set function DAH in the AH, Joad the address of the pararteter list {PARALST in the example) into the DX, ard issue INT 21H: fos AH oaM aquest keyboard input LER DX.PARALST — ;Load address of parameter 11st INT 21H s€aNl tnterrupe service ‘The INT operation waits for a uset to type characters and checks that they do not exceed the maximum of 20. The operation echoes each typed charucler onto the screen and ad- vances the cursor. The user presses lo signal the end of an entry. The operation also ‘ransfers the Entercharacter (ODH) to the iaput field KBDATA, butdoes sat count its en- try in the actual length. If you key ina name such as Porter+. the parameter list ap- pears like this: efol[r]ec]e] rj] « so | er | 22 | m4 | 5 | 72 | | 20| 20 ‘The operation delivers the length of the input name, 06H, into the second byte of the pa- sameter lis, named ACTLEN in the example. The Enter character (DH) is at KB- DATA+6, (The # symbol here indicates this character, because ODH hus no printable symbol.) Given that the maximum length of 20 includes the ODH, the user may type up to only 19 characters. “This operation accepts and acts on the Backspace character, but docsn’t add it tw the court, Other than Backspace, the operation does not accept more chan the maximum tumber of characters. In the preceding example, if a uscr koys in 20 cheracters without pressing , the operation causes the speakerto beep; at this point, it accepts only the Enter charactet, ‘The operation bypasses extended function keys such as F1, Home, PgUp, and Ariows, If you expeztauser to press any of them, use INT 16H or INT 2FH functionO+H, both cov ered in Chapter 11 eps Hex: | 34 ASCE: 96 20 PROGRAM: ACCEPTING AND DISPLAYING NAMES: ‘The program in Figure 9-2 requests a user to key in a name, and then displays the name at the center of the screen ond sounds the speaker. If the user types, for example, the ame Dana Porter, the program performs the following: 1. Divides the length 11 by 2: 11/2 = 5, with the remainder ignored. 2. Subtracts this value from 40: 40 — 5 = 35.1468 Introduction to Screen and Keyboard Processing Chap. 9 page 60,123 THT —_Avsorma [5B} ‘Accept namea, center on ccrson (MODEL SMALL STACK 64 ane parameter List: Meet oR 20 Paaximm Length of nana ActuLEN DD? } no. of charackers entered ImUME DB 22 DORK po entered name ALONAIN ROC FAR, wor AX, @daca rTnitialize segment Moy De, aE P"regtatare MOV ESLAK FALL QLOCLR :Clear screen A201008 mov px, 0000 {Sat cursor to 00,00 cau gzocms. CAuL BLopaMer Display prompt cal clomyer Provide for input of nave ome giocin j€lesr screen MP RCTULEN, 00 [Mane entered? Je aso fo, exte cain. Biocon bset beri ard 18! cALL BLoceNr ieenver, dteplay name OMP—-AZOLOOP a0: wov ax, acno# 78nd processing ier aa Monn END : Display BiopnMpr Roc NEAR MOY AH, 09H pRequeat display TSA DK, PRoMeT Inn aH RET B.oreMeT NDP c1omMe7 PROC nov, pRequest keyboord BA, tapue INT ner caommer BNDE Figure 9.20 Accepting and Displaying Names In the procedure BIOCENT, the SHR instruction shifts the length 11 one bit to the right, effectively dividing the length by 2: Bits 00001011 become 00000103 .or 5. The NEG instruction reverses the sign, changing +5 % —5. ADD adds the value 40, giving the start- ing position for the colurtn, 35. in the DL segistes, With the cursor set at row 12, column 35, the name appears on the screen like this: Rew 12; Oana Porter iy Colm: 3540Program: Accepting and Displaying Names 7 ‘B1000DE rRaplace 00H with 078 sSet display delimiter piocone 2 Center and diapiay nane EioceNT PROC RAR wntar column: BIR DusL j divide length by 2, NEG DL : ADD DL.40 : Moy ayia ieenter row CALL 20cm iSet cursor rey Au, os TEA DX, KENAME, :Display name RED LOCENT END. giocLR PROG Moy AK, 06008 sRequest_ scroll screen ey Ba, 30 #Color attribute wor CK, 0000 Prem: 00,00 Moy DK;i6¢rH ite 24,75 INT 108 RED quocen = EDP : Set cursor row/eoluam: O20CURS PROC DX set or entry Mov) aM, O2H FRequeat sat cursor mov BH, 00 PPege #0 zee 10H ker Q2ocuRs EXDP en Atom igure 9.2) Accepting and Displaying Names Note the instructions in DIOCODE that insert the Belt (O74) character in the input area imunediately following the name: WOWZx BK, ACTULEN sReplace ODM with O7H in BY Woy KENAMEEBX] 0701 ‘The MOVSX sets the BX with the number of characters. In the MOV. the index specifier in square brackets means that the BX is to act as a special index register co facilitate ex- tended addressing. The MOY combines the tength in the BX with the address of KBNAME, and moves the 07H to the calculated address, For a length off 1, the instruction inserts 07H at KBNAME ¢ I (replacing the Enter character) following the name. The last instruction. in DIOCODE inserts a ‘$” delimiter following the O7H so that INT 21H function 09H can display the aame end sound the speaker.148 Introduction to Soreen and Keyboard Processing Chop. 9 Replying with Only the Enter Key ‘The program continues accepting and displaying names uatil the user presses only as a reply to a prompt, INT 21H fonction O9H accepts it and inserts a length of OOH im the parameter list, like this: Parameter Vist chex): [i4]oo]0o] Ifthe length is zero, the program determines that input isended, as shown by the insiruc- tion CMP ACTLEN.00 in A2LOOP. Clearing the Enter Character ‘You cap use input characters for various purposes, such ¢8 prituing on report, storing 1" atable, or writing on disk. For these purposes, you may have to replace the Enter chuar- acter (ODH) wherever it is in KBNAME with a blank (20H), The field comtaining the ac- tual length of the input data, ACTLEN, provides the relative position of the Enter character, For example, if ACTLEN contains 11, then the Enter character is at KB- NAME+ 11. You can move this length into the BX register for indexing the address of KBNAME as follows: WOVEX BX,ACTULEN Set BK to 00 08 CD Ov -KBNAMEEBK].20H —;Clear Enter character ‘The MOVZX instruction sets the BX with the length 11. The MOV moves a blank (20H) 10 the address specified in the first operand: the address of KBNAME plus the contents of BX—ineffect, KBNAME+ 11. Cleating the Input Area Each character keyed in replaces the previous contents in the input area and remain there ‘nul other characters replace them. Consider the following successive iapus: Inget PARLIST hex? 1. Panne 114108150] 61/6916€ 165100 201201201... 1201 2. Franklin — 114108]4Gi72| 61 16E|6816C,69/8E|00] ... 120] 3. Adams (24105 |41164] 62160173 }00'6915E1001 1201 ‘The first name, Paine, requires only 5 bytes. The second name, Franklin, fully replaces the shoner name Paine. But because the third name, Adams, is shorter than Frankia, it replaces only Frank and the Fnter character replaces the |. The remaining (wo letters in’) still follow Adams, You could also clear KBNAME prior tw prompiing for a name like this: initialize for 29 Toors iStart position for nameUsing Contro! Characters in a Screan Display 9 MOV KENAME[ST],20H jMove one blank to ame INC ST increment for rext character Loop B30 sRepeat 20 times Unstead of the SI register, you couid use DI or BX. Also, if the routine moves a word of two blanks, it would require only 10 loops. However, because KBNAME is defined as DB (byte), you would have to override its leagth with a WORD and PTR (pointer) operand, as the following indicates: wor cx,10. eInivialize for 10 loops LEAST, KBNAME HInitialize the start of name MO WORD PTREST],2020H ;Move two blanks to awe IN ST Increment two positions IN Sr in name Lou 830 sRepeat 10 tines, Interpret the MOY at B30: as “Moye a blank word to che memory location where Une ad- dress in the SI register points." This example uses LEA 10 initialize the clearing of KBNAME and uscs a slightly different method for the MOV at B30 because you cannot code an instruction such as mov WORD PTR[KANAME],20201 ;First oper is invalid ‘Clearing the input area solves the problem of short names being followed by previ- pus data. For faster processing, you cauld clear only positions to the right of the most re cently entered name, USING CONTROL CHARACTERS IN A SCREEN DISSLAY One way to make more effective use of displays is to use the Carriage Return, Line Feed, snd Tab control characters. You can code them as ASCIV or hex values, like this: CONTROL CHARACTER | ASCII | mex EFFECT OW CURSOR Garr'age ceturn | 43 | OO | Resees eo left position of screen Line feed to | oan | advances to next lire Tab oa | 09m | advances to next tab stop You can use these contra) characters for handling the cursor whenever you display output or accept inpuu. Here's an example that displays the contents of a characier string named REPTITL, followed by Carriage Return (13) an¢ Line Feed (10) to set the cursor on the next Tine: REPTITL 3 09, ‘Intertech Corp Annual Report’, 13, 20, ‘S160 Introduction to Seren and Keyboard Processing Chap. 9 Mav AH,094 sRequest display LEA OX,REPTITL {Load address of title INT 21H 1aNl interrupt service ‘Using EQU to redefine the control characters may make 2 program more readable: cr FQU 13 Cor OU ODN) LF EQU 10 Cor EQU OAH) Tak EQU 09 Cor EQU OM) REPTIT( 08 TAB, ‘Tntertech Corp Annual Report’, CR, LF, ‘S! INT21H FUNCTION 02H FOR SCREEN DISPLAY ‘You may find INT 21H function 02H useful for displaying single characters. Load in the DL the character that is to display at the current cursor position, and request INT 2iH. The ‘Tab. Carriuge Return, and Line Feed characters act normally, and the operation automati- cally advances the cursoy. The instructions are: MOV AH,O7H — ;Request display character MOY DL.char —jCharacter to display mer 2a all interrupt service ‘The following example shows how tousso dhis service to display a sting of charac- ters. The string to display is defined in COTITLE, The program loads the address of COTITLE in the DI register and its length in the CX. ‘The loop inveives incrementing the DI (by INC) for each auccessive character and decrementing the CX (by LOOP) for the nomber of characters to display. Here are the instructions: COTTTLE OB “Intertech Corp.*, 13, 19 Mov AH O21 iRequest display character mov ¢x,17 tlength of character string LEA DI,COTITLE — ;Address of character string so: MOV OLD] ‘Character to display INT 24H $€a11 ineecupe service INC OT Tnerenent for next character Loo C50 SRepeat 17 times : Finished FILE HANDLES ‘This section examines the use of file handles for soreen and Keyboard operations, which is more in the UNIX znd OS/2 szyle. A file handle is simply a number thatrefersto a spe~ cific device. Because the following standard file handles are preset, you do not have to efine them:INT 21H Function 40H for Screen Display 11 Handle © Input, normally keybyard (CON), but may be redirected on Ourput, normally display (CON), but may be redirected 0 Errar outeet, display (CON), nay not be redirected 03 Auxiliary device (AU) 04 Princer (LPTL or PRN: As shown, the normal file hancles are 00 for keyboard input and O1 for scrcen display. Pile handles for disk deviees {covered in Chapter 17) have to be set by your program, You ean also use these services for redirecting inpul and cutpul to other devices, although this Fea- ture doesn’t concem us here. INT 25H FUNCTION 40H FOR SCREEN DISPLAY INT 21H fonction 40H uses file handles to request display operations. To request this ser- vice, load the following registers AH = Function 406 CX = Number of characters to display BX = File handle 01 DX = Address of the display area A successfi] INT operation delivers to the AX the number of bytes written and clears the carry flag (which you may test) Ap asuccessful INT operation sets the carry flag and retums an ereor code in the AX: 05H = uccess denied (for an invalid or disconnected device) or 06H = invalid handle. Be- eause the AX could contain either a length or an error eade, the only way to determine an error condition is (0 test the camry flag, although display errors are rare: IC error-routine Jest for cisplay error ‘The operation aets upon contml characters 07H (Beep), 08H (Backspace), OAH (Line Feed), and ODH (Carriage Return), just like INT 21H function 09H. The following ia- structions illustrate funcdon 40H: COTITLE 0B 'Entertech Corp.*, 90H, OAH ;Display area wv AN.A0H sRequest cisplay mov ex,or ‘Wie hencle for screen Mow cx,17 yiaplay 17 characters LEA DX,COTITLE {Display area wr 2H sCalll interrupt service Note that the length of COTTTLE includes the ODH and the OAR. Exercise: Displaying on the Screen Let's use DEBUG to examine the-internal effets of using a file handle to display your name. Load DEBUG, and when its prompt appears. type A 100 (0 begin keying in the12 introduction to Screen and Keyboard Processing chap. 9 foltowing assembler statements (Lut not the leftmost mutnbers} at offset location 1001 (re- member that DEBUG assumes that entered numbers are in hexadecimal format}: 309 MOV AH AG 302 Nov BX.O1 105 NOY Kx CTnse~t Tengen o* your name} 108 NOV DK,106 303 INT 24, 30 NOP OE 0B “x (ansert your nane here) The instructions set the AH to request a display and sct offset LEH ir, dre DX—tte loca tion of the DB containing your name. ‘When you have keyed in the instructions, press again. To unassemble the program, use the U command (Li 100,10D) nd, to trace execution, press R and then re- peated T commands, On reaching INT 21. use the P(Proceed) command to execute the in- texrupt through to the NOP insteuction; it should display your name on the screen. Use the Qcommand to quit DEBUG. INT 21H FUNCTION 3FH FOR KEYBOARD INPUT INT 21H function 3FH uses file handles to request keyboard inpul. although it's @ some- what clumsy operation. Load the following registers: AH = Function 3FH (CX = Maximum number of characters t0 accept BX = File handle 04 DX = Address of area for entering characters Asuccessful INT operation clears the carry flag (which you may test) and sels the AX with the number of characters enteted. ‘An unsuccessful [NT operation could occur because of an invalid handle; the opera- tion sets the carry flag andinserts an error code in the AX: 05H = access denied (for an in- valid of disconnected device) or 06H = invalid hancle. Because the AX could contain either a length or an error code, the only way to de:crmine an error condition is to test the certy flag, although keyboard errors are rare. Like INT 21 function OAH, function 3FH alvo acts on Backspace, but ignores extended function keys such ag Fl, Home, and PageUp, setiously limiting its usefulness, ‘The following instructions illustrate the use of function 3FH: KRINPUT DB 20 DUPC' +) put area nov AN, 3FH iRequest keyboard input nov 1,00 ite handle for keyboard mov 0,20 ‘Maximum 20 characters. -EA ON.RBINEUT — ; Input area INT 21H sal interrupt serviceINT 21H Function 3FH for Keyboard Input 153 ‘The INT operation waits for you to enter characters, but unfortunately does not check whether the number of characters exceeds the maximum in the CX register (20 in the ex- ample). Pressing (ODH) sigrals the end of an entry. For example, typing the char- acters “Intertech Corp” delivers the following to KBINPUT: [Thtertech Corp joDH|cAHt ‘The typed characters are immediately followed by Enter (UDH), whieh youtyped, and Line Feed (OAH), which you did nottype. Because of this feature, the maximum mumber and the Jongth of the input area that you define should provide for an additional two characters. If you type fewer characters than the maximum. the locations in memory following the typed characters still contain the previous contents ‘Asucvessful INT operation clears the carry flag and sets the AX with the number of characters delivered. In the preceding example, this render is 14. plus 2 for the Enter and Line Feed characters, or 16. Accordingly. a program can use the value in the AX to deter- ming the actual number of characters typed, Although this feature is irivial for YES and NO type of replies. itis useful for replies with variable length, such as same [Che umber of characters that you key in exceecs the maximum inthe CX register, the operation actually accepis all the charzcters. Consider a situation in which the maxi- mom in the CX is 08 and a user types the characters “PC Exchange”. The operstion sets the first cight characters in the inpui area to “PC Excha” with no Enter and Line Feed follow. ing and sets the AX with a length of 08 Now, waich this—the nextINT operation to exe- cute does, not accept a name directly from the keyboard. because it still has the rest of the previous string in its butter. It delivers *nge” followed by Enter and Line Feed to the mput area and sets the AX to 05. Both operations are “normal” and clear the carry flag First INI: PC Exeha Ax = 08 Second INT! nge, ODH, OAH AX © 05 ‘A program can tell whether a user has keyed in a “valid” number of characters if () the nambet retumed in the AX is Jess than the number in the CX or (bi the number e2- ‘armed in the AX is equal to that in the CX, and the last two characters in the iaput area are ODH and OAH. ifneither condition is rue. you'l have (o issne additional INTs to accept the remaining characters. After all this, you may well wonder what is the point of specify- ing a maximum lengt in the CX at all! Exercise: Keying in Data Here’s a DEBUG exercise in which you can view the effect of using INT 21H function 3FHL for keying im data, The program allows you to key in up 10 12 charscters, including achar- acter for Enter and one for Line Feed. Load DEBUG, and when the prompt appears, type ‘A 100 to begin entering the folowing instuctions (but not the numbers) a! locaton 10H: 100. MOV AH, 3F 102 Mov Bx,cO158 Introduction to Screen and Keyooard Processing Chap. 3 105 HOV Cx.0C 108 YOV Ox,30F 108 INT 21 109 IMP 100 OF DB 20 20 20 20 20 20 20 20 26 20 20 20 ‘When you have keyed in the instructions, press sgain, The peogram sets the AH and BX to request keyboard input and inserts the maximum length in the CX. It also sets offset JOFH in the DX—the location of the DB, where the entered characters are ta begin. “Try the U command (LI 100,108) to uaascemble the program. Use & and repeated T commands to frace the execution of the four MOV instructions. At location IOBH, usc P (Proceed) to execute through the interrupt; the operation waits for you to key in cheractess followed by . Check the contents of the AX registcr und the carry fiag, and use D DS:10F to display the cntered characters ia memory. You can continue looping indefinitely. Key in Qto quit DEBUG. KEY POINTS + The basic color display supports 16K bytes and can operate in color or monochrome ‘You can process either in text mode for normal character display arin graphics mode. * Monochrome display supports 4K bytes of memory, 2K of which are available for characters and 2K for an attribute for each character. «+ "The IINT 0H instruction transfers contsol to BIOS for display operations. Two com- ‘mon operations are function 02H (set cursor) and O6H (scroll screen). + INT 21H provides special fonctions to handle some of the complexity of inpuYoupat + When using INT2H fonction 09H for displaying, define a delimiter ($) immediately following the display area. A missing delimiter can cause spectacular effects on the sereen «INT 21H function OAH for keyboard input expects the first byte uo comain « maxi- roum value ad automatically inserts an actual value in the second byte. = Afile handle is a number that refers to a specific device. The numbers for file tare les 00 through 04 are preset. whereas others cart be set by your program. + For INT 21H fonction 40H to display, use handle 01 in the BX. + For INT 21H function 3FH for keyboard input, use handle 00 in the BX. The opera tion inserts Enter and Line Feed following the typed churacters in the input area, but does nol check for characters that exceed your specified maximum, QUESTIONS 9-1. On an 80-column screen, what are the locations as hex values for (a) the bottom rightmost location and (b) the top leftmost location? 9-2, Write the instructions to set the cursor to row 16,colamn 20.‘Questions 155 9-3. a4, oS, 9-6, 9-7, o-8. a9. 9-10, DL. Write the instructions to clear the screen, beginning at row 06, column 0, with any color attribute. Define data items and use [NT 21H function 09H 1 display a message “What is tae date (mmdd/yy)?” Follow the tnensage with a beep, Define data items and use INT 21H function DAH to accep! input from the keyboard according to the format in Question 9-4. The section titled “Cleanng the Inpul Area” shows how to clear to blank the entire keyboard input area, defined as KBNAME, Change the example so that it clears only the characters imumediately to de vight of the most recemsly entered name. Key in the program in Figure 9-2 with the following changes: (a) Instead of 10w 12, set the center at row 09: (b) instead of clearing the entire screen, clear only rows 0 through 98; use artribute 17H in the operation, Assemble, Jink,and test the program. Identity the standard file handles for (a) the printer; (b) keyboard input; (c) nonnal screen display. Define data items and use INT 21H funetion 40H to display the message “What is the date (mm/dd/yy)?” Follow the message with a beep, Define data items and use INT 21H function 3FH to accept input from the keyboard ‘according to the format in Question 9.9 Revise Question 9-7 for use with INT 21H, functions 3FH and 40H, for input and display. Assemble, link, and test the revised program.ADVANCED FEATURES OF SCREEN PROCESSING Objective: To cover advanced features of screen handling, includ ing éerolling, reverse Video, blinking, and the usé of color graphics. INTRODUCTION Chapter 9 introduced the basic feanes concerned with screen handling and keyboard in- put. This chapter provides advanced features related to video adapters. sening modes (Iext or graphics), and other screen handling features. ‘The fist section describes the common video adapters and their associated video dis- play areas. The sections on text mode explain the use of the attribute byte for color, blink- ing, and high intensity, as well as the instructions to sct the cursor size and location, to scroll up or down the screea, and to display characters. The last fow sections explain the use of graphics mode, together with the various funetions used for displaying graphics. ‘This chapter covers the following services offered by BIOS INT 10H: 00H Set video mode O84 Set colar palette O1H Set cursor size OCH write prxe! doe 02H Set cursor position ODI Read pixel dot O31 Read curser position OFM write teletype 04H Read Tight pen position OFM Get current video mode OSH Select active page LK Character generator Osh Scrat up screen 42H Select alternative routine O71! Serol down screen 1H Display character string 84 Read attribute/character 18H Return state informationSetting the Video Mode 157 09H Display attriture/characte: 10H Save/restore video stare aK Di splay character VIDEO ADAPTERS ‘The common (or once-common) video adapters include: MDA — Monochrome display adapter CGA Color gmphics adapter EGA — Enhanced graphics adapter 'MCGA Multicolor graphics array (PS/2 models 25 and 30) VGA ‘Video gtapines array The VGA and 1s superVGA successors rezlaced the CGA and EGA video adapters. Soft- ware written for aCGA or an EGA usually can ron on a VGA system, although sofoware written specifically for a VGA doesn’t run on @ CGA or an EGA. Avideo display consists of three basic components: the video controlicr, video BIOS, and video display wea, 1. The video controlter, the: workhorse unit, generates the monitor's scan signals for the. selected text or graphics mode, The computer's processor sends insiructions to the controlle:’s registers and reads stelus information from them, 2, The video BIOS, which acts as an interface to the video adapter, contains such rou- tines as selting the cursor and displaying characters. 3. The video display area in memory comains the information that the monitor is to dis- play. The interrupisthat handle screen displays transfer your data dircctiy 10 this ares. The various video modes reside in different areas of the video display are. Foilow- ing are the beginning segment addresses for major video adapters! + AD00:[0] Used for font descriptors when in text mode and for high-resolution gtaphies for VGA, EGA, and MCGA + B000:{0] Monochrome text mode for VGA. BGA. and MDA. + BBOO:[0]‘Text anc graphics modes for VGA, EGA, MCGA. and CGA. ‘SETTING THE VIDEO MODE The video mode is determined by such factors as text or graphics, color ct monochrome, sercen resolution, and the number of colors. You ase BIOS INT 10H funciion 00H to ini- Galize the mode for the currently executing program or to switch between texl and graph- ice, Setting the mode also clears the screen. As an example, mode 03 provides text mode, 25 cows X 80 columns, color, and 720 X 400 screen resolution fora VGA monitor. ‘To set the mode, request INT 10H with function OOH in the AH register and the mode, in the AL. The following example sets the video mode for standard color ext on any type of color monitor (it is also a fast way w clear ihe screen)Bs Advanced Features of Screen Processing Chap. 10 OY AH,OGH — ;Request set aode MOY AL,OM {80 x 25 standard color vext INT 10H i€all interrupt service If you write software for unknown video monitors, you can use INT 10H function OFH (covered later), which retums the current video mode in the AL. Another approach is (o use BIOS INT | H to determine the device artiched to the system. although the infor- mation delivered is rather primitive. The operation retorns a value to the AX. with bits 5 and 4 indicating video mode: * Of: 40 columns x 25 rows, using a color adapter + 1: 80 columns % 25 rows, using a calor adapter «11; 80 colemns % 25 rows, using a monochrome adapter ‘You can test the AX for the type of monitor and then set the mode accordingly. USING TEXT MODE “Text mode is used for the normal display of the full extended ASCII 256-character set on the screen. Processing is similar for both color and monochrome, except that color does not support the underline attribute, Figme {0-1 shows cammon text modes, withthe made mim- ter on the left. Mode Size Type Adapter degolution Colors 90 (25 rows, Mono. CGR 320 x 200 40 cole) oA 320 x 380 meg, 4320 = 400 GA 360 x 400 01 (25 vows, Color GGA jap = 200 ie 39 cols) BOA 520 2350 16 of Gt MCGA «420 40018 OF 262,148 GR 360 x 40D 16 of 262,144 02 (25 rows, Mono CGA a0 x 200 0 cols) ESA, sao x 350 MGGA £40 x 800 GN 730% 400 clor cuA §a9 x 200 16 Bc cole) EGR £40 x 350 16 of 64 WEG «$40 40016 of 262.146 var 120 too 16 of 362,244 07 (28 rows, Mono MDA 720 x 350 #0 cal EGA 720 % 350, oR 720 x 400 Nove: adapter GOR: Color graphics adapter NCGR: Multicolor graphice array eA, Video gyaphion array Fignre 10.4 Text Modes for Video DigaysUsing Text Mode 169 + Text modes 00 (mono) and UI (color). Provide 40-column fermat; although origi- nally designed for We CGA, aiso work on VGA and EGA systems. + Text moces 02 (mono) and 03 {color}. Provide conventional &0-column formal: al- though originally designed for the CGA. also work on VGA and EGA systems, * Text mode 07 (mono). The standard monochrome mode for VGA, EGA, and MDA, ‘with respectable screen resolutions. Attribute Byte ‘An attribute byte in text (not graphics) mode determines the characteristics of each dis- Played character. When a program sets an attribute, it remains set; that is, all subsequent displayed charnclers have the same attribute until another operation changes it, You can use INT 10H functions to generate a screen atribute and perform such actions as seroll up, scroll down, read attribute or character, or display attribute or character. If you use DEBUG to view the video display area of your system, you can see each L-byte character, immedi- ately followed by its T-byte attribute ‘The attribute byte has the following format, according to bit position: Backeround | roregraund actributer [al] ek ¢ B ]t oR ¢ 6 Bit numer: | 7| 6 s 4 [3 2 1 0 ‘The letters R, G. and B indicate hit positions for red, green. and blue, respectively, foreact: of the three primary additive colors. + Bit 7 (BL) seus blinking 4 Bits 6-4 determine the screen background + Bit 3 (0) setshigh intensity + Bits 2-0 determine the foreground (for the character being displayed), ‘The common RGB color graphics monitor accepts input signals that are sent to threo sepuratc electron guns—red. greon, and blue—foreach of the primary additive colors, The RGB bits define acolor;on both color and monochrome, 000 is black and 111 is white. For example, an attribute set with the value 00000111 meansblack background with white foreground Color Dispiay For most color monitors, the background can display 1 of 8 colors and the foreground char~ acters can display | of 16 colors. Blinking and intensity apply only to the foreground, You can also seiget 1 of 16 colors for the border. Color monitors do not provide underlinings in- swead, setting bit Oselects the blue color as foreground. The three basic colors are red (R), green (G), and blue (B). You can combi these in the attribute byte to forma total of 8 colors (including black and white) and can set high in- tensity (I in the following chart), For a total of 16 colors:160 Advanced Featur coon | AG B| COLOR 1RGB Back 0 0 0 0| Gray 1000 Bue 0.0.0.1 | Light blue 1aos Gwen 0.0 1 0 { Lightgreen Loe Cyan 00-1 1| Lighteyan foul Red = 01:0 0 | Lighted 1100 Magenta 0 1 1 | Light magenta 1101 Brown 01 1 0| Yellow 1110 White 0-1 1 1 | High-imtensity white 11 4 1 of Sereen Processing Chap. 10 Ifthe background and foreground colors are the same, the displayed character is in- visible. You can also use the attribute byte to cause a foreground character to blink. Here ate some typical atributes, where BL means blinking: BACK. FORE- BACKGROUND FOREGROUND GROUND GROUND BLA GB | AGB HEX Black Black D0 60 0000 Black Blue bo 00 0005 OF Blue Red oo 01 O100 Green Cyan loo 10 corr 2 White Lightmagenta 0 ( 1 1 11 Ot 7D Green Gray (blivkingy|| 0 1 0 1000 As Monochrome Display Fora monochrote moniter, the attribute byie is used the sume way as was shown for a color monitor, except that bit 0 sets the underline attribute. To specify attributes, you may set ‘combinations of bits as follows: BACK. FORE BACKGROUND FOREGROUND GROUND GROUND FEATURE BLRGB IAGE HEX Black Black —_Nondisplay 0000 60009 OO Black White Normal 0000 O111 Om Black White Blinking $000 0111 3H Black White —_ Intense 0000 1111 OF White Black Reverse video @i 11 0000 70H White Black Reverse blinking | 1111 0000 FOH Underline 0000 0001 olUsing INT 10H for Text Made 161 Setting the Attribute You can use INT 11H to determine the type of monitor installed. Then, for color, use any of the color combinations described or, for monochrome, use O7H to set the normal attribute (black background, white foreground). The attribute remains set until another operation changes il. Text mode also supports screen pages 0-3, where page Ois the normal screen ‘Asan example, the fallowing INT 10H operation (explained later) uses function 09H (o display 12 brown, blinking (1110) asterisks on a blue (0001) background: MOV AH.O9H iRequest display Mov AL. terisk MOV BH,00H — ;Page number 0 Mov BL.1EH Jor attribute (o0or 1110) MOV Ox,12 successive characters Der 10H 36011 interrupt service You can use DEBUG to check out this example, as well as trying other color combinations, SCREEN PAGES The vorious seat modes allow you to slore data in video memory in pages. A page stores a screenful of data aad is numbered 0 through 3 for normal 80-column mode (and 0 through 7 fot the rarely used 40-columr. screen). In 80-column mode, page number 0 is the default and begins in the video display area at BBOU[O), page | begins at B9OD{O], page 2 at BAOOIO), and page 3 at BBOO(@} ‘You may format any of the pages in memory, although you candisplay only one page at a time. Each character to be displayed on the sercen requires two bytes of memory—one byte for the character anda second for its attribute. In this way, a full page of characters for 80 columns and 25 rows requires 80 X 25 X 2 = 4,000 bytes. The amount of memory ac- tually allocated for each page is 4K, or 4.096 bytes. so that 96 unused bytes immeditely follow each page. USING INT 10H FOR TEXT MODE Earlier, we used INT 10H function OOH for setting the display mode. INT 10H supports other services (available through function codes inthe AH) to facilitate full screen handling. ‘The INT operation preserves the contents of the BX, CX. DX. Dl, SI, and BP segisters, but not the AX—a point to remember if you use INT 10H in a Joop, The following sections de- seribe each function, INT 10H Function QOH: Set Video Mode As desctibed carlicr, you use this operation by setting the mode in the AL, such as 03 for color or 07 for monochrome. (See Figure 10-1.)12 Advanced Features of Screen Processing Chap. 10 INT 10H Function O1H: Set Cursor Size ‘The cursor is not part of the ASCII character set andexists only in fexl mode. The computer maintains its own hardware for cursor control, with special INT operations for its use. The normal cursor symbol is similar to an undettine or break character, but youcan adjust the cursor size vertically by means of function O1H, Set these registers: » CH (bite 4-0) = top of cursor (“start scan line") + CL (bits 4-0) = bottom of cursor (“end scan line”) ‘You can adjust the cursor size between the top and bottorn scan lines0:14 for VGA, 0:13 for monochrome and EGA, and 0:7 for CGA. The following code enlarges the cursor for = VGA to its maximum size: ov AH,OIN —:Request set cursor size Moy CH.GO Start scan Tine MOY CLy1d End scan Tine It 20H ical) snterrupe service ‘The cursor now blinks as a solid rectangle. You een adjustits size anywhere between the stated limits, such a8 04:08, 03:10, and so forth. The cursor retains these attributes until an- other operation changes them. Using 0:14 (VGA), 12:13 (moncchtome or EGA), oF 6:7 (CGA) resets the curser to normal. If yon are unsure of the cursor’s bounds on your moni- ter, try executing function 03H while in DEBUG. INT 10H Function 62H: Set Cursor Pasition “This useful operation sets the cursor anywhere on a screen, according to row:colurm covr- dinates, Set these registers: + BH = Page number, can be 0 (default), 1, 2,0r 3 for 80-column text mode + DH = Row + DL = Column “The cursor location on each page is independent of ts Location on the other pages. This ex- ample sets row 12, column 30, for page 0: MN AK.O2H — :Request set cursor MOV BH,0O Page number 0 MOY DH,12 Row 12 Wo DL,30 Column 30 NT 108 iCal} interrupt service INT 10H Function 03H: Read Cursor Position ‘A progeam can use function 03H to determine the present row, column, and size of the cur sor, particularly in situations where the program has to use the screen temporarily and has to save and resel the original screen. Set the page aunber in the BH, just as for function 02H:Using INT 10H for Text Mode 163 MOV AH,OIH — :Request cursor location MOY BH,00— :Page number 0 (normal) INT 10H iCall interrupt service ‘The operation returns these values: AX = Unchanged CL = Ending sean Tine BX = Unchanged DH = Row CH = Starting scan iine DL = Column ‘The following example uses function 03H to read the cursor and determine its location ‘and size; it then uses function 02H to advance the cursor to the next cchamn on the screen: Mov AH,O3H — jRequest cursor position Mov BH,OS Page 0 INT 10H Returns column in BL Oy AL,02H — jRequest set cursor INC OL at next column INT 10H :CaNl interrupt service INT 10H Function OSH: Select Active Page Fonction 05H for text modes 0-3 and 13-16 lets you select the page thal is to be displayed. ‘You can create different pages and request alternating between pages 0-3 fin 80-column mode). The operation is simply a request that returus no values: OY AHOSH Request active page MOV ALjpaget — ;Page number TNT 10H 11 interrupt service INT 10H Function 06H; Scroll Up Screen Tia program displays textdown the screen past the bottom, the next Jine wraps around to start at the top. But cven if the INT operation specifies colurun 0, the new lines are in- properly indented, and succeeding lines may be badly skewed, The solution is (0 scrol] fhe screen, $0 that displayed lines scroll off at the top and blank lines appear at the bottorn. ‘You already used function O6H in Chapter 9 1a clear the screen, where setting a.er0 value in the AL caused the entire screen to sczoll up. effectively clearing it. Setting a donzero value in the AL causes that number of lines to scroll up. Load the following registets: AL = Number of rows (00 for full screen) CX = Staring row:column BH = Atwibute DX = Ending row:column The following example sets a color attribute and scrolls the full screen one line: Mov Ax,0601H Request scroll up one lint Mov BH,o1H Brown background, blue Foreground Mor and stored in fields in the data segment under the ames LINE1, LINE2, ete. You could use a variely of colors, reverse video, or beeping as an experiment. Then set the cursor to a row 18 and column 10, and display the entered data on one row for each line of stored data. The program is ta accept any number of sets of data and ends when the user presses with ne dai, Wrie the program with a short main logic routine and a series of called subroutines. Include some concise comments. 10-8. Revise the program in Question 10-7 sou INT 30H function 09H for the displyy. 10.9. Explain how the common attribute hyte limits the mimber of available colors. 10-10, Write the instructions to set graphics mode for these resolutions: (a) 320 X 200; {b) 640 X 200, (e) 640 % 480. 30-11, Write the instructions for selecting the background color green in graphics mode. 10-12, Write the instructions to read a dot from rew 44. column 120, in graphics mode. it uses INT 16H for keyboard input and184 Advanced Features of Screan Processing Chap. 10 10-13, Revise the program in Figure 10-7 so thal it provides for the following: (a) back- ‘ground color blue; (b) row beginning at 50.and ending a 400; (c) column beginning at 72 and ending at 568, 10-14, Based on the changes you made in Question 10-13, revise the program w display ‘Braphics dots one column (instead of row) at a time. That is, display dots down the screen, then advance to the nest column, and 30 forth.ADVANCED FEATURES OF KEYBOARD PROCESSING Objectives: To cover all the keyboard operations and artvanced fea- tores of keytoard input, inclucing theshift status, keyboard buffer, and scan codes. INTRODUCTION This chapter describes the many different operations tor handling keyboard input, some of which have specialized uses. Of these opesations, INT 21H function GAH (covered in Chap- ter9),and INT 16H {covered in this chapter) should provide almost all the keyboard opera- tions you'll require. Other topics in the chapter include the keyboard shift status bytes, scan codes, anid the ‘keyboard buffer aree. The shift status bytes in the BIOS data area enables a program ta de- termine. for example, whether the Ctrl>, , ar keyshave been pressed. The scant cade is a onique numer assigned to each key on the Keyboard that enables the system to iden lify the source of a pressed key and enables a program to check if the user has pressed an €x- tended function key such as , , or . And the keyboard buijfer area provides space in memory for you to type ahead befure @ program actually requests input, ‘Operations introduced in this chapter are the following; INT 21H FUNCTIONS INT 16H FUNCTIONS. OH Keyboard input with echo 0H Read a character 6H Disect console YO O1H Determine if character present O7H Direct keyboard inpnt without echo 02H Return current stilt status O8H Keyboard input without echo OSH Keyboard write 185196 Advanced Foatures of Keyboard Processing Chap. 11 OAH Battered keyboard input 10H Read keyboard character OBH Check keyboard status 1H Determine if character present OCH Clear buffer & invoke function «2H Return current shift staeus THE KEYBOARD. ‘The keyboard provides ibree basic types of koys 1. Standard characters, which consist of the letters A through Z, numbers 0 through 9, and such characters as %, $, and #. 2. Extended function keys, which consist of: + Program function keys, such as and + Numeric keypad keys with NumLock toggled off:, , , , , , and , and the duplicate keys for them on the ex- tended keyboard » salphatetics and +pmngran-fimction keys. 3. Control keys , , and . , , , , , and , with the key toggling between them, Te overcome problems caused by thislayout, de- signers produced an enlanced keyboard with 101 keys and subsequently 104 keys for Win dows, Of the LB new keys, only and provide 6 new fusctiony the rest duplicate the function of keys.on the original keyboard. If your programs allow sets to press , , o asy of the fancy new key combinations, the users must have an enhanced key- board and « computer with a BIOS that can provess them. For most other keyboard opera: ‘ions, your programs need not be concerned with the type of keyboard that is installed. KEYBOARD SHIFT STATUS. ‘The BIOS data area at segment 40(6]H in low memory contains a sumber of useful dats items, These inelode the first byte of the current keyboard shift status at 40:17H, where the hits set to 1 indicate the following: BIT ACTION Gir AGTION 7 Insert active 3 pressed 6 — CapsLock state active | 2 pressed 5 Numbockstate active [1 pressed 4 Sorolt Lock state active | @ _ pressedThe Keyboard Buffer 187 ‘You may use INT 16H function 02H (covered later), to check these values. Note that “active” means that the user is currently holding down the key; releasing the key causes, BIOS to clear the bit value. The 83-key keyboard uses only this shift status byte ‘The enhanced keyboards have duplicate (left and right) and keys. $0 that additional information is needed! to test for them. The second byte of the keyboard sta- tus needed for the enhanced keyboard is at 40:18H. where a I-bit indicates the following: BIT _AGTON BIT ACTION 7 Insert pressed 3 CtdNumLock (pause) active 6 CapsLock pressed |2 — SysReq pressed 5 NumLock pressed | 1 LeftAlt pressed 4 Scroll Lock pressed | 0 _LeftCtrl pressed Bits 0, 1, and 2 are associated with the enhanced keyboard. You can test, for example. whether either or is pressed, or bath. ‘Another keyboard status byte resides at 40:96H. The item of interest 10 us bere is bit 4; when on, it indicates that an enhanced keyboard is installed. Exercise: Examining the Shift Stotus "To see the effect onthe shift status bytes of pressing , , and , load DE- BUG for execution. Type D 40:17 to view the contents of the status bytes. Press , . and , aud type D 40:17 again to see the resulc on both status bytes. The byte at 40:17H should show 70H (0111 0OGOB), and the byte at 40:18H is probably OOH. The byte at 40:96H should show the presence (or absence) of an en- hanced keyboard. "Try changing the contents of the statusbyte at 40:1 7H—type F 40:17 00. If your key- board Lock keys have indicator lights, they should tum off. Now try typing E 40:17 70 to tum them on again. ‘You could try various combinations, although it's difficult 10 type a valid DEBUG command while holding down the aid + request”,means that the system will terminate the program if the user presses the +- or keys together, INT 21H Function 01H: Keyboard Input with Echo ‘This operation accepts a character from the keyboard bufter or, if none is present, waits for keyboard entry. The operation tetums cne of two status codes: » AL = anonzero value means thata standard ASCII character (such asa letter or nurn- ber) is present, which the operation echoes on the screen «AL = zero means that the user has pressed aa extended function key such as , Fl>, of , and the AH stil conteins the original function, The operation han- dies extended fanctions churnsily, attempting 10 echo them on the screen, And to get the sean code for the function key in the AL, you immediately have « repeat the INT 21H operation, The operation also responds to a + request. ‘The following example illustrates this function: Mov AH,OIN, — jRequest keyboard input INT 214 3Ca11 interrupt service cAMP AL,00- tended function key pressed? Mz } no, ASCIT character WT 2H 5 yes, repeat operation + for scan code INT 21H Function 06H: Direct Console 1/0 “This rather bizarre operation caa trausler uny character of control code with no imlerference from DOS. There are two versions, for input and for output, For input, load OFFH into the DL. Iino character is in the keyboard buffer, the operation sets the zero flag and docs not swait for input, If acharacter js waiting in the huffer, the operation stores the character in the ‘AL and clears the zero flag. The operation does not echo the character on the soreen and does not check for of -+PriSc. Anonzero velucin the AL represents a standard ASC) character, such 8 a letter or number, Zero in the AL means thet the user has pressed an extended function key such as , , or . To get its sean code in the AL, immediately repeat the INT 21H operation. For screen output, load the ASCII character (not OFFH) into the DL.USING II Using INT 16H for Keyboard Input 189 INT 21H Function 07H: Diract Keyboard Input Without Echo ‘This operation works like function 01H. except that the entered character doesnt ectio an the screen and the operation does notrespond 19 a+ request. You could use the op- eationtokey ina password thatis 0 be invisible or where you don't want to diswrb the screen. INT 21H Function 08H: Keyboard Input Without Echo: ‘This operation works like function 01H, except that the entered character does not echo on the sereen. INT 21H Function OAH: Buffered Keyboard Input This useful keyboard operation is Covered in detail in Chapter 9. However, its inability to accept extended function keys limits its capability. INT 21H Function OBM: Check Keyboard Status ‘This operation retums FFE in the ALif an input characteris available in the keyboard buffer and COH if nocharacteris available. Note that the operation does not expect the user to press akey; rather, it simply checks the buffer. The function is relatcd to those others that do not ‘wait for keyboard input. INT 21H Function OCH: Clear Keyboard Butter and Invoke Function ‘You may use this operation in association with function 01H, 06H. O7H, 08H, or GAH. Load the required funetion into the AL Mov aH.ocH jRequest. keyboard Saput MOV AL,function {Required function (Keyboard input area WT 21H sCal] interrupt service ‘The operation clears the keyboard buffer, executes the function in the AL, and accepts (or waits for) a character, according to the function request in the AL. You could use this opera- tion for a program that does not allow a use! to type ahead. INT 16H FOR KEYBOARD INPUT INT 16H, the basic BIOS keyboard operation used extensively by sofware developers, pro- vides the following services according, to the function code in the AHL. INT 16H Function 00H: Read a Character ‘This keyboard operation handles the keys on the 63-key keyboard, but doos not accept in- put from the additional keys on the enhanced keyboard, such as and , See fonction 101, which is the same as this one but also handles the enhanced keyboard.190 Advanced Features of Keyboard Processing Chap. 11, INT 16H Function 01H: Determine Whether a Character (6 Present ‘This operation handles the keys on the 83-key keyboard, but does not recognize input from ‘he additional keys on the enhanced keyboard, See function L1H, which is the same as this ‘one but also tlandies the enhanced Keyboard. INT 16H Function 02H: Return the Current Status ‘This uperetion returns to the AL the status of the keyboard shift from the BIOS data area at location 417H (40:17H). (Sec the earlier section, “Keyboard Shift Status.”) The following example tests whether the (bit 1) or or , the character in the AI. is OOH. On the enhanced Keyboant, and also retum (OH fo the AL, but the other new (duplicate) control keys, such a < ani . return OH. Here are the three possibilities: KEY PRESSED an AL Regular ASCH character Scan code | ASCII character Extersled function key Scan code | 00H Extended duplicate control key | Sean code | EOH “You can test the AL for 00H or EQH te determine whether the user has pressed an extended function key:Extended Function Keys and Scan Codes, 101 MOY AN,10H ——-Request 610S keyboard input INT 16H sCa1l interrupt service CHP AL,0OH Extended function key? JE KIO 1 yes, exit OM AL,CEOH Extended function key? DE KI 1 yen, exit, Because the operation does nor echo the character on the screen, you have to request a soreen display operation for that purpose, INT 16H Function 11H; Determine Whether a Character Is Present This operation is the same as function 01H, except that this one accepts the additional ex- tended functions ‘rors the entunced keybcard. Ty anentered character is present in the keyboard buffer, the cperation clears the ceo flag and delivers the character to the AL and its scan code (o the AH; the entered character remains in the buffer. If no character is present, the operation sets the zero flag and dovs not wait. Note that the operation provides a look-ahead feature, because the character remains, in the keyboard baffer until function LOH reads it INT 16H Function 12H: Retum the Curent Keyboard Shitt Status ‘This operation is similar to function O2H. which delivers to the AL the status of the key- board shift from the BIOS data area at location 417H (40:17H|. Tae operation also eliv- ers the extended shift status to the AH, where a 1-bit means the following: aT KEY er KEY 7 SysReq pressed | 3 Right Alt pressed 6 — Caps Lock pressed | 2 Right Cisl pressed. 3 NamLock pressed | 1 Left Alt pressed 4 Scroll Lock pressed 0. Left Ctrl pressed EXTENDED FUNCTION KEYS AND SCAN CODES Anextended function key such as or requests an action rather than delivers a character. There is nothing in the system design that compels these Keys to perform a specific action: As the programmer, you determine, for example, that pressing isto set the cor- sor at the top left comer of the screen or that pressing scts the cursor at the end of text on the screen. You could as easily program hese keys to perform wholly unrelated operations. Each key has a designated sean code, beginning with 01 for . (See Appendix F fora complete list of these codes.) By means of the scan codes, « program may determine the sousce of any keystroke, For example, a program could issue INT 16H function 10H to2 Advanced Features of Keyboard Processing Chap. 11 request input of one character. The operation responds in one of two ways, depending on ‘whether you press a character key or an extended function key. For a character, such as the letter A. the operation delivers these two items: 1. Inthe AH register, the scan code for the letter A, HEH. 2, Wo the AL register, the ASCII character A (41H). The keyboard contains two keys each for such characters as ~, +, and *. Pressing the astetisk key, for example, sets the character code 2AH in the AL and one of two scan codes in the AH, depending on which key was pressed: 09H for the asterisk above the nun- ber 8, or 29H for the asterisk by the mmeric keypad. The following example tests the scan code to determine which asterisk was pressed: OW AL.2AH Asterisk? INE 130 5 ono, exit CHP ANOSH — jScan code on #8 key? JE WO byes, exit Ifyou press an extended function key, such as . the operation delivers these two items: " 1. Inthe AH register: The scan code for , 52H. 2. Inthe AL. register: Zero, ot EOH for a new contro! key om the enhanced keyboard. ‘After an INT 16H operation (and some INT 21H operations), you can test the AL. Ifiteon- tuins OCH or EOH, the request is for an extended function; otherwise, the operation has de- livered a character The following example tests for an extended function key: NOY AH.1OH Request keyboard Input INT 16H 11 interrupt service ce AL,00H tended function? Je K20 yes. exit Ce AL,OEOH Extended function? Je k20 byes, exit Inthe next example, if a uscr presscs the key (scan code 47H), the cursor is set to row 0, column 0: Moy AH,10H equest keyboard input INT 16H all interrupt service GHP AL,00F extended function? JE 30 yes, hypass HP AL,OECH Extended function? INE K90 a, exit K30: MF AH,47H Scan code For Home>? INE &90 ma, exit WOv AH,O2N aquest wow Bi.00 set cursor MOV 0X00 ‘to 00:00 INT 10H iCall interrupt serviceProgram: Selecting from a Menu 193 Function keys generate scan codes 3BH—44H, respectively, and aud generate 85H and 86H. The following example tests for function key : HP AM,aail Function key 7 36 H80 tyes, exit ‘At H50, the program coulé perform any required artion. Keybourd Exercise ‘The following DEBUG exercise examines the effects of keying in verious characters. Use the command A 10040 key in these instructions: WoV AH,10 Cor 00 for on Bt INT 16 aMP 100 ray keyboard) Use the P (Proceed) command to execute the INT operation. Key in various normal cha- acters with and to select an item from the menu. ‘acd recoras Delete records Figure +t Menu with Drop Shadow “The programis Usted in Figure 11-2. The procedures and wha! actions they perform are follows: + AIOMALN calls QIOCLEAR to clear the scteen, calls BIOMENU to display the menu and toset the first item to severse video, and calls CIOINPT to secept keyboard input « BIOMENU displays the full set of menu selections. It first uses INT 10H funetion 09H 10 disptuy the shadow box: 8 rows of 19 shadow characters ()DBE). The pro- cedure then displays Uhe menu defined in the data segment as MENU on top of the Shadow box but offset | row and column. * CIOINPT uses INT 16H for inpot to move down the menu, t0 move up the menu, to accept a menu item, and to quit All other keyboard entries are ignored, The routine wraps the cursor around, se that194. Advanced Features of Keyboard Processing “(MODEL SMALL ISTACK 64 ZoPReN = EQU 09 BOTROK = -EQU (0? EFCOL © EQU 16 Chap. 11 MASRIMU (EXE) Se'ect item free rime Yop rev of wom jObtom ro GF menu ileft colunn of nema col, t= 90. Hereen colums ROW m0 jfereen row cour oa? (characters per Line ACTRIB. «GR? jSereen ateribuce iwrgEN ce 19 iviath of nenu v0 DB © «OtSH, 47 DUP (ocDK), OBBH DB «(OERH, "Ade records‘, osntt DR «OBR. ' ‘Delete records '; OBA Da «COBAM, * Cater oreare Da OEAM, * Print report vB CORAM, * Upuate accounts: DB OCORAH, * View records *. Be «OCeH, 17 DU (orDH) , ORCH raw? BRO, To aelect an item, use .* DB 13, 10, 03, ‘Prees «Bsc» to exit. 2c08 286 HLOMMIN, PROC PAR Mov AZ, adate itniviatize seament MOY DS, AK t "negistero MOV BS, AK CALL OLOCLEAR pCleae screen hoy Row, SOTROM. 2 Het rev Moy COL, 60 pind colum cal 920cms bo for curaor Nov AE, AGH pRequeet atepiay ov BY;Or Handle for screen wou aya HMunber of characters. IBA DK, PROMPT faddrews of promt wet 2iit [eal nverrupe service saot0o: ca, Riommw (Piepley weno HOV COL, uEFoOLes CALL, G20CURE 1300 cursor HOV ROW, TOROW.2 jeet row Eo top item yOX —-ATTREB, Lett pact reverwe video KALL biowise Mighhight current mena line XM, CLOTNPUT Provide for menu selection cHP) AL, OD8 ienter key pressed? Je ALOLDOP } yee, continue HOY AK, 05002 iRet pressed (indicates end! CALL QLOCLEAR F mov RK, ACDOH ar 2ui Monn END Figure it-2a Seherting an fea from a MenaProgram: Selecting from a Menu : Display full senu: Brome PROC NEAR Mov ROW, TOPROW: 1 #20: Moy con, LEFCORa Chu, g2ocuKs Mov AM, 09H Noy AL ODBH Moy BH,00 MOY Bu, 60H wor CK,1a Int isk NC ROW CMP ROW, BOTRONS 2 wwe B20 Novy Bow, nopROW LEA Sr, ewU Moy RTTRIS, 71H B30 Moy COL, LBFCDI, Mov cour, 19 Bao CALL GZOCURS Moy A.OoR mov AL, IST] mov BHO Mov Gls aH Moy CxcoL myn 10H mc o3u inc | Sr Dee | CoURT. uz Bea mc Row HP ROW, BOTRDWeL SNe B30 rer pioMey END : Accept input for euommprr PROC MBAR mov AH, 1011 Ns 26H oe BH, 50H ye cae ow At,aetl we G0 Mp AL, ope Se G86 cur aL, 1DH eau Me Clorwect 205 Mov XETRIA, 718 CALL DODTSP im ow GW ROW,BOTROW-2. See 40 Mav Row, ToPRoW+. ome ca) 195, sSet top Tow of shadow (Gee left aolunn of shadow feet cursor text column iRequest. display Shadow character Hage & Black on Drown }19 characters. ext row AL] xows Qleplayed? NO, zepeat [Set top row of nenu [Aderens oF neu. jelue on white ‘Set left column of ment iN. ef cola to dieploy Set curser eext column iRequest. display [Get character from menu iPage © pelte on white Jone character jSet for next column, p ext character Phaet character? ike, rapa iene row ‘ALL roo ddeplayed? 1 Ro, Tepeat. + Yea, retam request: iRequest keyhoard Pape ioown arrow? {0p arrow? her key? sBacepe key? rWone, ret fine’ on waite iBet’ ofa fine to normal video HEnexeaant for sex 20 Past bottom ror? to. ok 1 yea, reset Figure 11-2 Setcting an Hem from a Mena195, 30: can: CLoINFuT Daeprse p20: pLUDISe Q12cLBAR Quoc EAR gaccuns azecurs Roc ovax saan proc ENDP PROC mov Mov Mov NT rer, mane Advancad Features of Keyboard Processing Chap. 11 ATTRIB, 72 yBlue on white tioprsP iget old Line to nezaal video row ROW. TOPROWL. +Below top row? cao pa, ok OW, BORON 1 byes, reser zocuns, ise chreor ATTRIB, 174 white on blue fonray ;Har naw Line to zeverse video cLomweor Set maou ling to normel/highlight: coy fella which line to set Mulciply by length of line "tor eelected went line sCharacters co display 1Bet cursor next column pRequest dieplay Ge! character from menu age 0. iNee averibute fone Character :Next column [Bet for next character jhast character? n20 } “a0, repeat ©Ob, LEFCOL iReset column to left ga0cuns, [Set curser Clear scree EAR 3X, 06008 1H, 618 rBlog on brown ex, 9009 TRoll sereer DX, 1e4PH, 10H peal Bros Set cursor row:colunn: AI OMAZN Figure 1-2e Selecting an Team froma MenuBios INT 09H and the Keyboard Butter 197 trying (© move the cursot above the first menu ling sets it to the fast line, and view versa, The rentine also calls DIGDISPto reset the previous menu line io normal video and the new (selected) menu line io reverse video. + DLODISP displays the curently selected line according to an attribute (normal o¢ re- verse video) that hes been provided. + QUOCLEAR clears the entire screen and sets it to blue foreground and brown background. ‘The program illustrates menu selection in a simple manner; » full program would ¢x- ceute a routine for each selected item. You'll get abetter understanding of this program by ‘yping il in as en ASM file, assembling it, and testing it BIOS INTO9H AND THE KEYBOARD BUFFER ‘When you press a key, the keyboard's processor generates the key’s scan code and requests INT OSH. This interrupt (at location 36 of the intermipt services table) points to an interrupt: handling routine it ROM BIOS. The routine issues a request for input from port 96 (OOH): IN AL.SOH. The BIOS routine reads the scan code and compares it with entries in a scan code table for the associated ASCU character (if any), The routine combines tke scant code ‘with it associated ASCH character and delivers the ewo bytes to the keyboard buffer. Fig- ure 11-3 illustrates this procedure. nS lo Report @ scan cose ® @ Sear aetna tee mere _ NT raters mca stn oe oat 3 erp tees entrar el 0 O32 Worm sees ew oaeern onsen sen code Te Figore 1-3. Keyboard Rufter198, Advanced Features of Keyboard Processing Chap. 71 Note thet INT OSH handles the Keyboard sutus bytes ac 40:(7H, 40:18H and 40:96H for , , and . Altiough pressing thesc keys generates INT O9H, the interrupt routine scts the appropriate bits in the status bytes, but doesn’t deliver any characters to the keyboard buffer. Also, INT 09H ignares undefined key- stroke combinations. Whea you press a key, the keyboard processor automatically generates a scan code and issues INT OOH. When you release the key within one-talf second, it generates & se2- ‘ond scan code (the value of the first code plus 100) 00008. which sets the leftmost bit) ard issues another INT O9H. The second scan code tells the interrupt routine that you have re- leased the key. If you hold the key for more than one-half second, the keyboard process be- comes typematic; that is, i aucomatically repeats ihe key operation. ‘The keyboard buffer requires onc address ithe head of the buffer) to tell INT 16H from where to read the next character and another address (the tail of the buffer) to tell INT 091] where 10 store the next character. The two addresses are offsets within segment 40[0)H. The following describes ihe contenis of the buffer: ADDRESS EXPLANATION SLAH Address of the current head of the buffer, the next position, for INT 16H to read a character 4ICH Address of the current tail of the buffer, the next position. for INT O9H to stote an entered character. AIEH ——Acklress of the beginning of the keyboard buffer itself, The buffer contains 16 words (32 bytes), although it can be longer, and holds keyboard characters and their assoeiated scan codes as entered. Subsequently, INT 16H will read each character and its scan code and deliver them to the program. Two bytes are required for each character and scan code, When you type a character, INT 0H advances the tail. When INT 16H reads achar- acter, it advances the head, In this way. the process is circular, with the head cuorimually chasing the tail, ‘When the buffer is empty (INT 16H has reat all the stored characters), the head and tail are at the same address. In the following example, a user bas keyed ahead the charac ters ‘abecEnter>” INT 09H stoves the characters as follows: + ‘a’ in the buffer at 41 BEL and its scan code LEH at 41TH + ‘b’ an the buffer at 420H and its scan code 30H at 421H; © ‘c" in the buffer at 4274 and its sean code 2BH at 423H: + the at 424H andits sean code EOH a1 425H. At this point, (NT 09H has advanced the tail 10 426H: a 1EH Bb 30M 26H fats FOH Porobo oto bobo tory ALE 4UF 420 421 422 423 424 425 426Bios (NT 09H and the Keyboard Buffer 198 ‘Once the program has issued INT 16H five times, it has read all the characters and ad~ vanced the tail to 426H: because the tail has the Same address as the head. the buffer is now empty. When dhe user keys in 15 characters, the buffer is full, and the tail is immediatcly ‘behind the lead. Tu see this, suppose the user now types ahead ‘fghijkimnopgrs’. INT O9H stores the characters beginning with the tail at 426H and cireles around 19 store the *’ ut 422H. The tail is now advanced to 424H, immediately before the head at 26H: eos fate" f g ho i j & 1m ao pa Prot tio ta ta bo ea rs ALE 420422 424 426 428 42K 42C 42E 430 432 434 436 438 434 43C Altthis point, INT 09H does not accept any more typed characters and, indeed, accepts only 15.at most, although the buffer holds 16, (Can you tell why? IfINT O9H were to accept an- other character, it would advance the tail to the same address as the head, and INT 16H. would incorrectly suppose that the buffer is empty.) The Shit, Chi, and Alt Keys INT 09H also handles the keyboard status byte at 40;17H in the BIOS data area, as well as ‘at 40:18H and 40:96H for the enhanced keyboard. When you press , , or . the BIOS routine sets the appropriate bit to I, and when you release the key. itclears, the bit to 0, Your program may test whether any of the control keys are pressed by means of INT L6H function 02H or by direct reference to the status byte. The COM program in Figure 11-4 iTustrates the use of direct reference te the status byte at 40:1 7H, The program uses the SEGMENT AT feature to define the BIOS data arca as, in effect, a dummy segment. KBSTATE idemifies the location of the keyboard status byte at 40:17, The code segment contains the following procedures: + ALOMAIN initializes the address of BIOSDATA in the BS (because the DS contains the segment address af the defined data), waits for the user to press a key and, if other than , calls BLOTEST. Pressing tells the program to end. + BIOTEST transfers the keyboard status byte to the BL: MOV BL ES:KBSTATE ‘The ES: segment override operation telts the assembler to relate the offset address of KBSTATE with the segment address inthe ES. If any of the , ,or keys were pressed, the procedure cafls C1 ODISP. Note that just pressing a conrol key alone does not satisfy INT 16H: you have to press it in common with another key that causes a valid Keyboard entry, such as +, +, ete. (See Ap- pendix F) The keyboard status byte reflects the ation of the control key. + CLODISP displays the appropriate message for the pressed control key. ‘You could modify this program to test as well for the enhanced keyboard status bytes at 40,1811 and 40:96,rime BIODATA kearare BIODATA copese ECL ALTER Con RY SHTPREY ‘ALOMAIN x0. Alo pioresT B20: 90: RLOTEST c10p1sP cLopisP Conese Advanced Features of Keyboard Processing = Chap. 11 ALANBSTA (Com) Testing Alt, Shift, & Ctr1 SEGMENT AT 40 rlocate BIOs data area ORG 17H Para. co po otatue byee ENDS SEGMENT PARA ASSIME CB. COCESG,DS:BICDATA ond 1008 wee ALOMALN De ‘Alt kay pressed! , DB ‘Ctrl key pressed? DE Shift key preseed’, PRoe MOY AK, BIODATA Hint: adéve ov BS, AK p "of BIODATA in @B Hoy AN, 19H iRequest keyboard entry in Lei GME AL, con rUser requests ond? SE AsO po yes, exit cALL BLOTEST Me) AID :Repeat mov AK, scooH }fnd processing INT 2 OV BL, Rs -mng7aTE iGet keyboard status byte ‘TEST BL, 030000118 fmither Shift pressed? Sz B20 Poe, bypass LEA DX, SAIEREY cau, c1oorse 2a? ‘TROT BL, cos90300n iStrl key pre: az. B30 ne, bypass LBA DK, CrRLKEY tant CLaDise TES? Bu, 00002c00B, jALt key preased? az. 290 P ne, Dypae: LEA DX, ALTREY GALL CLOPIBE RET. END? Woy AH, Dow praquest dleplay nr 21H Ren END? ENDS END BGIN Figure 1-4 Coecking the Keyboard Status ByieKey Points. 201 KEYING IN THE FULL ASCN CHARACTER SET ‘The entire ASCII set consists of 256 characters numbered 0 Cuough 255 (FFH), Many of these are standard displayable characters, from ASCII 20H (space) through ASCIL TEH (the tilde character ~). Because of keyboard limitations, most of the 256 ASCII characters are thot represented on it. You ean, however, key in any of the ASCII codes Ol through 255 by holding dowa and keying in the appropriate code as a decimal value on the numeric keypad. The system stores your entered value as two bytes in the keyboard buffer: The first isthe generated ASCII character and the second is zero, For example, -+255 delivers FFH. You could use INT 16H with DEBUG’s A command to examine the effect of entering various values: 300 Woy At,10 aez INT 16 164 1 100 See Appendix B for a complete table of ASC values. KEY POINTS: » The shift stars bytes in the BIOS dats arca st 40:17H and 40:13H indicate the cur- ert status of such keys as , , , and . + INT 21H keyboard operations provide a viticty of scrvices to echo or not echo characters on the strecn, to recognize or ignore +, and to accept sean codes, « ENT 6H provides the basic BIOS keyboard operations for accepting characters from the keyboard buifes. For a character key, the aperetion delivers the character to the AL and the key's scan code to the AH, For an extended function key, the operation delivers OOH or EOK to the AL and the key’s sean code to the AH. » The scan code is a unique number assigned to each key that enables the system to identify the source of a pressed key and enables a program to check forextended fimc- tion keys such as , , and . + BIOS data arca at 40:1BH contains the keyboard buffer, which sllows you to !ype ahead up to 15 characters befote a program requests input. ‘+ When you press a key, the keyboard’s processor generates the key’s scan code andre ‘quests INT 09H. When you release the key, it generates a second scan code (the First Code plus 10000000B, which sets the leftmost bit) co tell INT O9H that the key is released. » BIOS INT 0H reuicves a scan code irom the keyboard. The operation uses the sean code lo generate an associated ASCII charscter, which it debvers to the keyboard buffer arca. BIOS may also ser the status for , , or .202 QUESTIONS Ibe. ilo 113. 1-4. us. 116, m7. 11-8, 1.9. 11-20. 11, 112, Advanced Features of Kayboard Processing Chp. 11 (a) Whats the location of the first byte of the keyboard shift status in the BIOS dala area? (b) What do the contents 00000010 mean? (c} What da the contents 00001100 mean? Describe the feewures of the following functions for INT 21H Keyboard input: (a) O1H; (b) 07H; (c} O81: (4) OAH. Explain the ditferences among INT 16H functions OOH. (OH, and 11H. Provide the scan codes for the following keyboard functions: (a) Home; (b) PgDn; (c) Down Arrow; (d) function key F8, Use DEBUG to examine the effects of entered keystrokes, To request entry of as- sembly statements, type A 100.and Key in the following instructions: MOM AH.10 (or AH.00) INT 16 IMP 100 Use U 100,104 to unassemble the program, and use the P command to get DEBUG to execute through the INT: Execution stops, waiting for your input. Press any key and examine the AH and Al. registers. Continue typing 4 variety of keys. Press Qto quit DEBUG. Code the instructions for INT 16H to accept a keystroke; if , set the cursor to column 0, row 0. Revise Fignre [1-2 to provide for the following features: (a) Revisethe drop shadow from full shadow to three-querter dots on(B2H). (b) After the initial cleating of the screen, display 2 prompt that asks users to press for a menu screen. (c) When <> is pressed, display the menu. (1) Allow users 10 select menu items also by pressing the first character (upper- of lowercase) af each item. (e) On request of an item, display a message for that particular selection, such as “Procedure to Delete Records.” (1) Adi a last line to the menu containing the item “Exit from program” that allows users to end processing. You'll also have to revise the procedure BIOMENU to handle the display of another row. Under what circumstances docs an INT 09H occur? Explain in simple terms how INT O9H handles and differently from the way it handles the standard keyboard keys. {a} Where is the BIOS memory iocation of the keyboard buffer? (b) What is the bbuffer’s size. in bytes? ic) How many Keyboard characters can it contain? Euplain the effect of the following occurrences in the keyboard buffer: (a) The ad- rest of the head and tail are the same: (b} the address of the tail immediately fol- ows the head. Revise Figure 11-4 for the following requirements: (8) Test also for and : (b} transfer the contents of the second byte of the keyboard shift status 10 the BH: (c} test also for pressed, and display an appropriate message.PART. P-—DATA MANIPULATION iP PROCESSING === STRING DATA Pit ip pec tp INTRODUCTION Up so this chapter, the instructions presented have handled data defined as only oue byte, ‘word, or doubleword. It is often necessary, however, to move or compare data fields that exceed thesc lengths. For example, you may want to compare descriptions or names in or- der to sort them into ascending sequence. Items of this type are known as siring data and ‘ay be in either cheracter or numeric format, Assembly language provides these string in- structions for processing suring data internally: MOVS LODS sTOS MPS SCAS Moves one byte, word, or doubleword from one location in memory to another. Loads from memory a byte into the AL. a word into the AX, or a doubleword into the BAX, ‘Stores the contents of the AL, AX, or EAX registers into memory. ‘Compares byte, word, or doubleword memory locations. ‘Compares the contents of the AL, AX, or BAX with the contents of a memory location,208 Processing String Data Chap. 12 ‘Two other string operations, INS ond OUTS, are covered in Chapter 21. An asso- ciated instruction, the REP prefix, canses a string instruction to perform repetitively 0 that it may process any number of bytes, words, or doublewords a specified number of times, FEATURES OF STRING OPERATIONS. A strieg instrvction can specify the repetitive processing of one byte, word, or deubleword. (80386 and jater) at a time. ‘Thus you couid select a byte operation for a string with an odd. number of bytes anda word operation for a sting with an even umber of bytes. Each string instruction has a byte, word, and doubleword version and assumes use of the ES:DI or DS:SI pair of registers. ‘The string instructions expect that the DI and SI contain valid offset addiesses that reference bytes in memory. The SI register is normally associated with the DS (data seg- ment) register as DS:S1, The DI register is always associnted with the ES {extra segenent) register as ES:D1. For this reason, MOVS, STOS, CMPS, and SCAS require that an EXE program initialize the ES regiscer, usually. but not necessarily, with the same address as that in the DS register: WOV AX.@data —:Get address of data seonent, Nov DS,ax E store it in os. HOV ES. AX t and in €S Figure 12-1 shows the registers associated with each string instruction There are basically two ways to code siring instructions: 1. In Figure 12-1, the second column shows the basic format for each operation, which uses the implied operands listed in the third column (if you code an instroction at MOVS, you incinde the operands—for example, a MOVS BYTEI.BYTE2. where the definition of the operands indicates the length of the move}. A later section, “Alternative Coding for String Instructions,” describes this format in more detail. 2. The second way 10 code string instructions is the standard practice, as shown in the fourth, fifth, and sixth columns of Figure 12-1. You load the addresses of the operands in dhe Db and $1 registers and code the instruction without operands, for example, as: (EA DI,BYTE2 address of BYTEZ LEA SI,BYTEI —; Address of BYTEL MOvsR ve BYTEL to BYTEZ REP: REPEAT STRING PREFIX ‘The REP prefix immediately before a string instruction, such as REP MOVSB, provides for repeated execution based on an initial count that you set in the CX register. REP executes the string instruction, decrements the CX, and repeats this operation until the count in the CX iszero. In this way, you can process strings of virtually any length.MOVS: Move String Instruction 206 Dper. Basie implied eyre Word Toubleword eliea rhatruction Operanda Operation Operation Operation Move OVS 'S:Dx,05:5I MOVSB MOVEW movsD Load Laps AXDS'ST Lonss 00S ons Store 70S, ES{DI,AK aToss = STOsH sras Compare cHPS, DS381/85 CMSB CuPoW Meso Sean AS ES:DI AX ScasB_SCASW {CAS Figure 12-1 Formas forthe Strng Instructions ‘The direction flag (DF) determines the direction of a repeated operation: + For processing from left to right (the sormal practice), use CLD to clear the DF to zero. « For processing from right to iefl, use STID 10 set the DF to 1. Tn the following example, assurne that the DS and ES are both initialized with the ad- dress of the data segment, as shown eather. A REP MOVSB operation moves (or rather, copies) the 25 bytes of DATASTRI to DATASTR2: DATASTRI 0B 25 DUPC'*') Sending field DATASTR2 DB 25 DUPC' *) — Recetving field ao iClear direction fag Mov x25 HInitialize For 25 bytes LEA DI,DATASTR2 Initialize receiving address LEA SI,DATASTRI REP MOVSB itialize sending address ‘apy DATASTR1 to DATASTRZ During execution, CMPS and SCAS also set status flags, so thal the operations can end immediately on finding a specified condition. The variations of REP for this purpose are the following: « REP Repeat the operation until the CX is decremented to zero. + REPE or REPZ Repeat the operation while the zero flag (ZF) indicates equal/zero. Stop when the ZF indicates not equal/zero or when the CX is decremented to zero. « REPNE or REPNZ, Repeat the operation while the ZF indicates not equal/ecro. Stop when the ZF indicates equal or zero ar when the CX is decremented to zero, ‘Theuse of word and doubleword operations can provide faster processing. We'Tl now examine each string operation in detail MOVS: MOVE STRING INSTRUCTION MOVSB, MOVSW, and MOVSD combined with aREP prefix and a length in the CX can move any number of characters. Although you don’t code the operands, the instruction looks like this:206 Processing String Daa Chap, 12 Chabers]]REP woven] {ES:0t 08:51] For the receiving string, the segment:offset registers are the ES:DI; for the-sending string, the segnent:ofiset registers are the DS-SE. As a result, at the start of an EXE pro- eam. initialize the ES register along with the DS register, and prior wexecuking the MOVS, ise LEA to initialize the DI and S] registers. Depending on the direction Nag, MOYS in- crements or decrements the D1 and SI registers by 1 for byte, 2 for word, and 4 for double- word, The following example iusirates moving 10 words: My cta9 sMusbac of words LEA DE,STRING? Address of STRING2 LEA SL,STRINGL — Address of STRINGL REP MavSw Move, 10 wards The instructions equivaleat to the REP MOVSW operation ure: OZ 390 Bypass if OC initially zero 330: MOY 8x, [ST] :Get word From STRINGL Mov [DII.AX —;Store word in STRINGZ AOD 01,2 HIncrement “or next word ADD 5T,2 : Loop 230 rDecrenent OX and repeat 190: Earlier, Figure 6-2 illustrated moving 4 9-byte field. The program could alsohave used. MOVSB for this purpose. in Figure 12-2, the procedure BIOMVSB uses MOVSB to move a 1O-Dyte field, HEADG), 1} byte at a time to HEADG2. The first instruction, CLD, clears the direction flag to zero s0 thar the MOVSB processes data from left co right. The direction flag is normally zero at the start of execution, but CLD is coded here as a precaution. ‘The two LEA instructions load the ST and DI registers with the offset addresses of HEADGI and HEADG?, respectively. Because the loader for a COM program automati- cally initializes the DS and ES registers, the segment.ofiset addresses are correct for ES:D] and DS:SI. A MOV instruction initializes the CX with 10 (the length of HEADG) and of HEADG2). The instruction REP MOVSB naw performs the following: » Moves the leftmost byte of HEADG I (adiressed by DS:S1) to the leftmost byte of HEADG2 (addressed by ES:DIj, « Increments the 11 and SI by | for the next bytes to the right; + Decrements the CX by 1: + Repeats this operation, 10 loops in all. until the CX becomes zero. Because the direction flag is zero and MOVSB increments DI and SI, each iteration processes 1 byte fartherto the rigbt, as HEADG!+ | (0 HEADG2+ 1, andsoon.At the end of execution, the CX contains 00. the DI comtains the address of HEADG2+10, and the ST contains the address of HEADG! + 1¢—both 1 byte past the end of the name,LODS: Load String instruction 207 TITER —-ALDMOVST (COM) Uae of MOVEn tring operations “MODEL SMALE [cobE es 1008 EOIN: SMP EHORT ALOMAZE heADG: OB ‘cybernaute: HEADG? = DR 10 DPT enbG3 ba 30 weet“) ALOWALS PROC NEAR imaia proceaure CALL bLowvss HmvSa subroutine ALL CLoNVsW favs subroutine Mov Ax, 4cooH (Bad of processing ist 7H ALOMAIN EXP. : tae of MOVs: Siomvss PRO” NEAR cp flett to right wou cx.a0 Move 13 Bytom. LER 0x, WBADG2 HEADOL to HEADCD LEA sr, READGL REP NOvsB RET biomes BMDP : use of Most Diomvee proc WRAP cw BY x05 EEA DL, EADG3 TER S1/HEADG? REP NovEH HEADS? to HEADGS clomvs# ENDP END BEGIN Figure 12-2 Using MOVS String Operations If the direction flag is 1. MOVSB would decrement DI and Si, causing processing 1o ‘occar from right to left. But in that case. to move the contents correctly, you would have to initialize the SI with HEADG! +9 and the DI wit HEADG2+9. The next procedure in Figare 12-2, CLOMVSW, uses MOVSW to move five words from HEADG2 to HEADGS. At the end of execution, the CX coniains 00, the DI contains the address of HEADG3+10, and the SI contains the address of HEADG2+10. Because MOVSW increments the Dland SIregisters by 2, the operation requires only 5 loops. For processing right to left, set the direction flag and initialize the SI with HEADG1 +8 andthe D! with HEADG2+8. LODS: LOAD STRING INSTRUCTION LODS simply toads the AL with a byte, the AX with « word, or the EAX with a doubleword from memory. The memory address is subject to the DS:SI registers, although youcan over-208 Processing String Data Chap. 12 tide the SI. Depending on the direction flag, the operation also increments or decrements the SI by 1 for byte, 2 for word, and 4 for doubleword. Because one LODS operation fills the register, there is ac practical reason touse the REP prefix with it. For mest purposes, « simple MOV instruction is adequate. But MOV generates 3 bytes of machine code, whereas LODS generates only 1, although it requires that you initialize the S1 register. You could use LODS to step through string | byte, word, ordoubleword at a time, examining successively for a panicutar character. ‘The instructions equivalent to LODSB are Moy AL,[1] Transfer byte to AL Iw ST Hncrenent SI for next byte (sets flags) In Figure 12-3, the data area defines a 10-byte field named HEADG1 containing the value “Cybernauts” and another 10-byte field named HEADG2. The objective is to itansfer the bytes from HEADG1 to HEADG? in reverse sequence, so that HEADG? contains “stuanrebyC.” LODSB is used to access | byte at a time from HEADGI into the AL. and the instruction MOV [DI].AL transfers the bytes 10 HEADG2, from right toler. STOS: STORE STRING INSTRUCTION 'STOS stores the contents of the AL, AX, or EAX register intoa byte, wond, or double word inmemory. The memory address is always subject to the ES:DI registers, Depending on the direction flag, STOS aiso increments or decrements the DI register by | for byte, 2 for word, and 4 for doubleword, ‘TITLE ALZLODET (COM) Uve of LEDSB string operation “MODEL SHALL CODE ora 10cH BEOIN: JMP_— SHORT A1OMAIN ‘HEAEGL = DB * cyemnaue pnata items. 10 DUP (20H ALORAIN -BROC NEAR jnain procedure ao Fett to eighe wo c.20 Tex BE MEapoy fload address of HEADGL TEA DI,HEADO?+2 Toad addrees of HRADO?9 220: iopss eet character in Al, wOV (071 AL PP arore in HEAEGZ, tec OF } righ to left voor ze [ren characters? Ra acaon P yen, ete Figure 12-2 Using LODSR String OperationProgeam: Using LODS and STOS to Transfer Data 208 A practical use of STOS with a REP prefix is to initialize a data area to any specified value, such as clearing 4 display area to blanks. You set the number of bytes, words. or doublewords in the CX, The insiructions equivaient to REP STOSB are: oe 730 “Dump. 1. zero. P20: WOv [DT},AL Store AL in memory INC/DEC OT sTneramant or dacranent (sets flags) oe P29 sDecrement CX and repeat PIO: iOperation complete ‘The STOSW instruction in Figure 12-4 repeatedly stores a word comaining 2020H. (blanks) five times through HEADG |. The operation stores the AL in the first byte and the AH in the next byte (that is, reversed). Acthe end, all of HEADGI is blank, the CX coa- tains 00, and the DE contains the address of HEADG1+10. TITLE AL2STOST (COM) Use of STOSW atring operation ‘MODEL SMALL ‘CODE ORG 00H pesIN, TWP SHORT ALeMADN x, 20208 yoy CK,5 TRA OT;HRADGI Woy AX, acoow 1Bnd of processing qr 21k ALOMAIN — ENDP 8D BEOIN Figure 12-4. Using STOSW String Operstion| PROGRAM: USING LODS AND STOSTO TRANSFER DATA ‘The program in Figure 12-5 illustrates the use of both the LODS and STOS instructions, ‘The example is similar to the program in Figue 10-4, which transfers characters and auributes directly to the video display area, except that Figure 12-5 contains these uifferences: + For the video area, the program uscs video page 2 rather than page 1. + In BIOPROC, ituses STOSW to store characters and associated attributes in the video area, instead of this instruction and its acvompanying ¢wo DEC instructions that decrement the Di: WOV WORD FTR [VIDAREADIJ,Ax + Itdefines anitem named PROMPT in the deta segment, prompting the user to “Press any key ...”, to be used at the end of processing. + On completion of processing, the procedure C10PROMPT transfers the defined prompt to the video display arca. To this end, it uses LODSB to access characters one210 Processing String Data Chap. 12 TITLE ALZUKVID (8XB) Direct video display MODEL, SMALL VIDSEG SEGMENT AT OBAOOR Hage 2 of video area VIDAREA DB 10008 DUP(?) PrOMPE Da 'Preas any key... ALOMAIN, PROC) EAR Woy AX. edata pAddressability tor mov Bax. Po data manent woy AK, VIDSEG i and for Moy BS ,AK } Video area JASSOME. BS‘ VIDSRG ov RH, oFR FRaquett get mr: 10k p and pave PUSH Ax current pode Pige BX and page Moy AH, 00H [Request gat Mov AL,03 "neds 03, clear soreen Tar lot Moy AH, OSK iRequest set. Moy AL, 02H page 802 Dr Loh CALL B1oRROC ‘Process display aren CALL | CLOPROMPT iDieplay user prompt CALE | DioINPT, iPrevide for input. Moy AH, OSH jReavere, poe Bx joriginaa ov AL.BH jpage number pr iit POP A WReatore video mov AHL OH jmode (in ALI Tet 108 mv AX, 4C008 ind ef procensing er 218 ALOMALN END? ? Stere character 6 attribute in video area: BLopRoc FROG ‘ mov AL, Clear name Buoctam proc cup puert to rigue Mov AX,20208 mov cxl1s :Clear 15 words LEA Dy, NAMEOIS REP STOSW RET eiocum ENDP : SeroLl screen! droscr proc 1AX set_or entsy Moy aHt,30 ieoler attribute Mov cx,00 Moy Dx, igarH ier oH euescr ENDP : Set cursor row/col: Gavcuns PROC BX set on entry Mov. ANo2H SUR -BH,BH INT 10H @zocuns NDP END ALOMAIN Figure 12-4 Right Adjusing on the Seren218 Processing String Data Chap. 12 + BIOINPT Acceprs a name up to 30 characters in length and displays it at the top of the screen, + CIOSCAS Uses SCASB to scan the name and bypasses any input containing an as- terisk. + DIORGHT Uses thefength in ACTINLEN in the inpat parameter list o calculate the rightmost character of the name, and MOVSB right adjusts each entered name to the Tight of the screen, one under the other, as follows: Witie Keys Hickey Mantle Frank Robinson + EIOCLNM Uses STOSW to clear the keyboard input area, KEY POINTS «+ For the string instructions MOVS, STOS, CMPS, and SCAS, be sure that your .EXE programs initialize the ES register + For string instructions, use the suffixes B, W, or D for handling byte, word, or double- word strings. + Initialize the direction flag for the required direction of processing: Clear (CLD) for left to right o set (STD) for right to left. + Doublecheck your initalizatiori of the DI and SI regiaters. For example, MOVS im- plies operands DI,SI, whereas CMPS implies operands SIDI. « Initialize the CX register for REP to process the required number of bytes, words, o doublewords. «For normal processing, use REP with MOVS and STOS, and use a conditional REP (REPE ot REPNE) with CMPS and SCAS. + CMPSW and SCASW reverse the bytes in words that are compared. + To process right to left, addressing begins at the rightmost byte of the field, For ex- ample, ifa field named COTTTLEis 10 byteslong, then for processing bytes, the load address for LEA is COTITLE+9. For processing werds, however, the address is COTTTLE+8 because the string operation initially accesses COTITLE+8 and COTITLE+9. ‘QUESTIONS 12.1. The string operations assume that the operands relate to the ES:D1 or DS'SL repis- fers. Identify the regisiers for the following: (x) CMPS (operands 1 and 2); (b) MOVS (operands 1 and 2); (c) LODS (operand 1). 12-2, For siring operations using REP, (3) how do you set the number of repetitions that are to occur? (b) How do you sct processing right to left?Questions 219 12-3, 124, 125, 126, 127, 128, The elapter gives the instructions equivetent m (a) MGVSB, (b) LODSB, and {c) STOSB, cach with a REP prefix. For exch case. provide the equivalent cade for processing words Revise the program in Figure 12-2. (a) Canvert the program from COM to EXE format; (b) initialize che ES register; (c) change the MOVSB and MOVSW opera- tions to move data from right to left, Use DEBUG to tare through the procedures, and note the contents of the data segment and registers. For what conditions do each of the following instructions test? (a) REPE COMPSB: (b) REPNE SCASB, Use the following data definitions and for parts (@)-(1) code the unretated string op- erations: DESCRIP 06 “ata Tech Advisors’ OUTAREA 0B 16 DUPC* “) (a) Move DESCRIP to OUTAREA, from left to right. (b) Move DESCRIP to QUTAREA, from right to left. (©) Load the fifth and sixth bytes of DESCRIP into the AX. (d) Store the AX beginning at OUTAREA +10. «e} Compare DESCRIP with OUTAREA (they will be unequal), (f) Scan DESCRIP for the first blank character and, if found, move it to the BH. Revise Figure 12-7 so that the operation seans HEADG! for “aa” as a pair of char- acters. A check of HEADGI discloses that “na” does not appear as aword, as shown by the following: (Cy/be/rnv/anns/. Two possible solutions are: (a) Use SCASW twice. The first SCASW begins at HEADG! and the second SCASW begins at HEADGI + 1, (b) or use SCASB and on finding an “n,” compare the byte that fol- Jows it foran “a.” Definea 5-byte field convaining the hex value C3CDCDCDBB. Use MOVSB to du- picate this field 20 times into a 100-byte area, and display the result,GE} ARITHMETIC I: PROCESSING BINARY DATA Objective: To cover the requirements for addition, subtraction, multiplication, and division of binary data. INTRODUCTION This chapter covers addition, subtraction, multiplication, and division and the use of unsigned and signed numeric data, The chapter also provides many examples and wam- ings of various pitfalls for the unwary traveler inthe realm of computer arithmetic, Chap- ter 14 covers special requirements involved with conversion betweea binary and ASCII data formats. Although we are accustomed to performing arithmetic in decimal (base 10) format, a microcomputer performs its acithmetic only in binary (base 2). Purther, the Limitaion of 16-bit registers on pre-80386 processors involves special treatment for large valuss. Instructions described in this chapter are: ADC ADD with carry IDIV Divide signed ADD Add IMUL Multiply signed CBW Convert byte to word MUL Multiply unsigned CDQ — Convert doubleword to quadword NEG — Negate CWD Convert word to doubleword SBB Subtract wit CWDE Convert word!o extended doubleword SUB Subtract DIY _ Divide unsigned 220Processing Unsigned and Signed Data 221 PROCESSING UNSIGNED AND SIGNED DATA Some numeric fields—for example, a customer number and the day of the month—are un- signed. Some signed numeric fickds—for example, customer's balance owing and an alge- braic number—may contain positive or negative valuos. Other signed numense fields—for example, en employee rate of pay and the value of pi—are supposed to be always positive. For unsigned data, where all bits are intended to be data bits, a 16-bit register can con- tain a maximum of 65.535. For signed data, where (he leftmost bit isa sign bi, the register can contain a maximum of 32.767. Butnote that the ADD and SUB instructions do nol dis- ‘inguish berween unsigned and signed data and, indeed, simply add and subtract bits. ‘The following exampie illustrates the addition of two binary numbers, with the val- ues shown as both unsigned and signed. The top number contaies a L-bit to the left; for un- signed data, the bits represent 249, whereas for signed data, the bits represent —7. The addition does not set the overflow or carry flags: UNSIONED —STCNED BINARY DECIMAL DECINAL OF CF vu. 28 7 0000010 #2, # amnion 251 o 0 In this example, the binary result of the addition is the same for both unsigned and signed data. However, the bits in the unsigned field represent decimal 251, whereas the bits in the signed field represent decimal ~5. In effet, the contents of a field mean whatever you in- tend them to mean and you handle them accordingly. Arithmetic Comy An arithmetic operation transfers the cesulting sign bit (0 or 1) 10 the carry Hag. Hf the sign. bitisa 1, then, ineffect, che carry flag is set. Where a carry oocurs on unsigned data, the re- sult is invalid. The following example of addition causes a carry WisINED — STORD BDURY DECIMAL DECIMAL OF CF ui 282 - —smoomuoL +5 ‘oon, a Gmvaliay Gali ‘The operation onthe unsigned data is invalid because of the carry out of a data bit, whereas the operation on the signed data is valid. Arithmetic Overfiow ‘An arithmetic operation sets the overflow flag when a carry into the sign bit docs not carry ‘OL, oF a carry ou OccuTS with a0 carTy in If'an overflow occurs on signed data, the result is invalid (because of an overflow inio the sign bit). The following exemple of addition causes an overflow:222 Arithmet Processing Binary Data Chap. 13, UNSIGNED SIGNED BENARY DECIMAL “DECIMAL OF CF ono = zt pert 9001011 AL +a oor = Taz ia 2 8 (valid) Cimvalid) An add operation may set toth the carry and the overflow flag. in the next example, the carry makes the operation on unsigned data invalid, and the overflow makes the oper- ation on signed dat invalid: WwsI@eD — STCNED BINARY DECIMAL DECIMAL OF CF uiio1s 246 -10 ingoiom +137, “19, apo = a7 way oat Gavatig) invalid) ‘The upshot of all this is that you must have a good idea as to the magnitude of the numbers that your progeats will handle, and you define and process fields azcordingly, ADDITION AND SUBTRACTION ‘Tne ADD and SUB instructions perform simple addition and subtraction of binary data, The general formats for ADD and SUB ave: Clanet:3 | ano7sun | recieter register (1aver:1 | aD0/sus | memory, register aber] | ano/sve | register snenory (abel:3 | ADD Sue | regs eter, immediate Habel} | an0/5u6 | nesory, immediate ‘An ADD or SUB operation sets or cleats the overflow ang carry flags, as described in the previous section. As with other instructions, there are no direct memory-to-memory operations. The following exuntple uses the AX tegister to add CURRAIN RAINFALL: GIRRAIN, OW 123, sBefine CORRATN RAINFALL DM 25 HDeFine RATHFALL NOY AXCURRAIN — jMove CURRAIW £9 AX ADD AXARAENFALL —:Add RAINFALL to AX Wov RATHFALL.AX — :Move AX to RATNFALL ‘As described in Chupter 1, a negative binary number is represented in two's comple- ment form by reversing the bits of the positive number and adding 1. Figure 13-1 providesAddition end Subtraction 223 TITLE ALSADD (Cov) ADD and SUB oparations ‘MODEL SMALL (COoE RG oc BEGIN: UMP SHORT ALOMAIN eyrea be ak [peta iene BYTE2 = pB40K ayes LK worn] = tw com worD2 bw 206cH WORDS = DW «1 00GH PROC NEAR iain procedure: CALL BacaDD ptall ADD routine cut caosun peal! 60 routine voy AX, 4cooH fend pracesmiag % 21k Alomare | ENDE kxanpies of ADD Bytes: BioaDD proc rv 1 DD AD, BL PRagister-to-register BoD AL avrey iMevory-toraginter 30D BYTEL,BL pRegloter-to-menory DD BL, 10H Hymmediace:co-regaerer aD BYTE, 25x Finmediaze-to-memory, rer Bioac> END. Exanplea of SUB words: Grosus koe XOV" AX, WORDL you BX, WORD? su AK BX sRegietar-fron-regiater U3 AX, WORD2 iMenory-fzon-register Sua WORD, BX jegiater -Fron-menory SUR RK, 7000H rmadiate- from-regiBter SUB WORD2, 256 [Tameddate- £xom-mamory, cioswe NDP END REGIN Figare 13:1. Bramples of ADD and SLE examples of ADD and SUB for processing byte and word values. The procedure B1VADD uses ADD to provess bytes and the procedure CI0SUB uses SUB to process words. Overfiows Be alert for overflows in arithmetic operations, especially for signed data, Because a byte provides for only a sign bit and 7 data bits (from —128 to +127). an arithmetic operation ‘can easily exceed the capacity of a L-byte register. And a sum in the AL register that cx- ceeds its capacity may cause unexpected results. For example, if the AL-contains 60H, then the instruction2a Arithmetic |: Processing Binary Data Chap. 13 AOD AL, 20K generates a sum of 80H in the AL. Having added two positive values, we expect the sum co be positive, but the operation sets the overflow flag ta overflow and the sign flag to nega- tive, The reason? The valne 80H, oF binary 10000000, is a negative number: instead of +128, che sum is —128. The problem is that the AL register is too small for te sum, whict: stould be in the full AX register, as shown in the next section, EXTENDING VALUES IN A REGISTER In the previous section, we saw how adding 20H to the value 60H in the AL caused an in- correct sum. A better solution would be for the AX to represent the full sum. The instruc- tion for this purpose is CBW (Coavert Byte to Word), which automatically propagates the sign bit of the AL (Or 1) through the AH. Note that CBW is restricted to the use of che AX Inthe next example, CBW extends the sign (0) in the AL through the AH, which gen- erates O060H in the AX. The example then adds 20H. wo the AX (rather than wo the AL} and generates the correct resuit in the AX: OOOH, or +128: ah AL nx 60H Extend AL sin inte AH 00 60 ADD AX,Z0H to ax wo 80 ‘This example has the same numeric result as the one in the previous section, but the addition in the AX does no treat it as an overflow ar as negative. Still, adiough a full word inthe AX allows fora sign bit and 15 data bits, the AX is limited to values ftom ~32.768 to +32,767. Extending Words and Doublewords ‘The CWD (Convert Word to Doublewort) instruction is used to extend I-word signed value toa doubleword by duplicating the sign bit of the AX through the DX. Here is an example: OW AX, WORD, ‘Tho CWDE (Convert Word to Extended Doubleword, for the 80386 and Ister) in- struction is used to extend a I-word signed value t9 a doubleword by duplicating the sign bit of the AX through the EAX. Here is an exampte: HOV AXWORD: —jMove word to AX DE jExtend word to EAX ‘The CDQ (Convert Doubleword vw Quatword, for the 80386 and later) instruction is used 10 extend a doubleword sigued value to a quadword by duplicating the sign bit of the EAX through the EDX, Here is an examplc: WO EAX,DBWORD —:Nove doubleword ta EAX oa rfxtend doubleword te FAX: EDXPerforming Arithmetic On Doubleword Values. 225 PERFORMING ARITHMETIC ON DOUBLEWORD VALUES ‘As we have seen, large aumeric values may exceed the capacity of 2 word, in effect re- quiring multivord capacity. A major requirement in nmultiword arithmetic is reverse-byte and reverse-wned sequerce. Recall that the assembler automatically converts the contents of defined numeric words into reverse-byte sequence, so that, for example, a definition of 0134H becomes 3401H. The simplest way of defining 2 doubleword is as a DD. The fol- lowing example adds and stores doubleword values: BYORI 00 o1228C62H iDeFine doublewords DAWORDZ 09 OO12553AH ; peWoRD3 09 0 MOV AX, DBWORDL, id and ADD EAX,DBWOHD2 —; store NOY DBWORDZ.EAX — : doublevarcs ‘The assembler automatically arranges the defined data in reverse-byte (and word) se- quence. For some applications, however, the data may be defined as word values. For DBWORDI éefised in the previous example, you have lo define the words as adjacent but in reverse order: bw once# Da 01234 ‘The assembler then converts these definitions into reverse-byte sequence as 162 BCIZ3 04, suitable for doubleword arithmetic. Let's ecamine two ways to perform arithmetic on these values. The fist is simple and specific, whereas the second is more sophisticated and general. In Figure 13-2, the procedure Bi0DWD illustrates adding ont pair of words (WORDIA and WORDIB} to a second pair (WORD2A and WORD2B) and storing the sum ina third pair (WORD3A and WORDSB). Ineffect, the operation is to add values, such as the following: Initial value: 0123 5C624 Add: oon? $534H Total: 0136 11904 Because of the reverse-byte sequence in memory, the program defines the values w:th the ‘words adjacent but reversed: BC62 0123 and 553A 0012, respectively. The assembler then stores these doubleword values in memory in proper reverse-byte sequence: WORDIA and WORDIB; — 62BC 2302 WORDZA and WORD2B: 3455. 1200 ‘The procedure BIODWD first adds WORD2A to WORDIA in the AX (the Jow-order por- Gons) and stores the sum in WORD3A. [t next adds WORDZB to WORDIB (the bigh- order portions) is the AX, along with the carry from the previous addition. Ir then stores the gum in WORDSB. Let's examine the operations in detail. The first MOW and ADD opera- tions reverse the byes in the AX and add the Lefmost words:226 Arithmetic I: Processing Binary Data THLE, BEGIN, fonoia soRD18 WORDZA WORDZE SRDS. WORDSD ‘ALOMALN atomare Bropwo LDH Ex00n0 20 cxooKe ESDSADD (CoM) Adding Goublewords WODE., SMALL copa, ons. OMe awe KOC me ne ine ine Ine auc avon SHORT ALOMAIN oncean oagaa S53AH o012H NEAR iNain proceaure Bi opK«D ieall ist ADD cx opKD i¢alt 2nd 26D AX, ACO0H, find processing 2k Example of ADD doublewords AX, WORDT, pkdd leftmost word AK WORD2A, WORDSA, AX AX,MORDIB sada rigntnos: word 3 WORD 2B. wih carry woRDaD, AX Geraralized add operation: jedear carry flag &x.02 Set loop count ST; WORDIA, Lefemost word DI, WORD2A, efenost word BX, ORDIA, word of mun Mave word to AX Add with carey to Ax (BL AK {Store word sr Adjuat addresses tor next word to right 2a sRepeat for next word RESIN Total: igure 13-2 Adding Muliword Values BC62H 45534H Goii9aH cociaH ie tered in WoRDRAD Chap. 13 Bacause the sum of WORDLA plus WORD2A exceeds the capacity of the AX, a carry o- ors, and the carry flag is set to 1. Next, the example ads the words at the cight, but this.Performing Arithmetic On Doudleword Values 227 time using ADC (Add with Carry) instead of ADD. ADC adds the (wo values and, becaase he carry flag is set, adds | to the sum: wonoae oan wornze = s0012H Plus carry + 1M Total ‘DABEH Catored in WORDIA as 36021) By usiug DEBUG to trace the arithmetic, you cam see the sunt O136H in the AX and the re- versed values 9CIH in WORD3A and 3601 H in WORD3B. Also in Figur 13-2, the more sophisticated procedure CLODWD provides an spprac to adding values of any length, although here it adds the same pairs of words as before, WORD1A:WORDIB and WORD2A:WORD28. The procedure uses the 51, DL. and BX as base registers for the addresses of WORDIA, WORD2A, anid WORD3A, respectively. oops once through the instructions for each pair of wordsto be added—in this case, two times. The first loop adds the leftmost words, and the second loop adds the rightmost words. Becaus: the second [sop is 10 process the words ta the right, the addresses in the SI, DI, and BX registers are incremented by 2. Two INC insiructions perform this operation for each register. INC {rather than ADD) is used for a good reason: The instruction ADD reg,02 would clearthe carry flag and would cause an incorrect answer, whereas INC does not affect the carry Flag. Because of the loop, there is only one add insttuction, ADC. Atthe start, aCLC (Clear Cary) instruction ensures that the carry flag is initially cleat, The results in WORD3A, WORD3B. and the AX are the same as the previous example, “To make this method work, be sure to (I) define the words adjacent to each other, (2) ini- alize the CX to the number of words to be added, and (3} process words from left to right, For multiword subraction, the instruction equivalent to ADC is SBB (Subtract with Borrow). Simply replace ADC with SBB in the procedure CLODWD. Here are the general formats for ADC and SBB: Chaber:) | anc/sep | register, regi ster Diabet: | 40¢/s88 | menory, reatster Capel nc/s6e| register menory {aber :3 | ancysan! ragister,imediate [labe}:] | ADC/SBB_ memory, imedi ate You could add quadwords using the technique covered earlier for adding multiwords; that is, define two pairs of adjacent doublewords and use the EAX register. MULTIPLICATION Formultiplication, the MUL instruction handles unsigned data, and the IMUL (Integer Mul tiplication) instruction handles signed data. Both instructions affect the camy and overflow228 Arithmetic Processing Binary Data Chap. 13 ‘lags. As programmer, you have contro! over the format of the data you process, und you have the responsibility of selecting the appropriate multiply instmction. The general format fot MUL and IMUL is Tabet [WATE [ raiceTeenony | ‘The basic mubiplication operations are byte times byte, word times word, and (80386 and later processors) doubleward times doubleword., byte Times Byte For multiplying two I-byte values, the multiplicand is in the AL register. and the multiplier is a byte in memory or another segister. For the instruction MUL DL, the operation muld- alics the contents of the.AL by the contents of the DL. The gcucrated product is in the AX register. The operation ignores and erases any data that may already be in the AH, Before multiplication: {AW ‘aL (Tgrored) | ul eiplicand After aultiplication: x — Produce —> Word Times Word For multiplying two 1-word values, the multiplicaad is in the AX register and the multiplier {is a word in memory oF another register. For the instruction MUL DX, the operation multi- plies the contents of the AX by the contents of the DX. The generated prochict is 1 double- word that requires tvo registers: the high-order (leftmost) portion in the DX and.the low-onder trightmmost} portion inthe AX. The operation ignores and erases any datathat may already be in the DX. ox aX Before muteiplicarion: | Cignered) | wultipticand After miltiptication: [#ian proauct | Low product Doubleword Times Doubleword For multiplying two doubleword values, the mutiplicand is in the BAX register and the ‘multiplier is a doubleword in memory or another register. The product is goneraied in the EDX:EAX pair. The operation ignores and crascs any data already in the EDX. Ex FAK Before multiplication: | Cgnored) | Nultiolicand After multiplication: [High product | Low product.Mottiptication 29 Fold Ses ‘The operand of MUL or IMUL references only the multiplier, which determines the field sizes. The instruction sssumes that the multiplicand is in the AL. AX, or BAX, depending on the size of the multiplier. In the following examples, the multiplier is in a register: INSTRUCTION MULTIPLIER MULEIPLICAND PRODUCT MUL CL byte aL »~ MUL ex word a Ox:AK Mu eax — doubleword eK FDX:EAX In the next examples, the multipliers are defined in memory: BYTEL 08 ? WORD DW? OWORDL DD > INSTRUCTEON MULTIPLIER MULTEFLICAND PRODUCT MIL’ BYTE pyTer AL »~ Man WORDT worn a Dx: WIL OWORDL = DWORDZ AK EDX:EAK Unsigned Muttiplication; MUL ‘The purpose of the MUL instruction is to multiply unsigned data, In Figure 13-2, BIOMUL gives three examples of the usc of MUL: byte times byte, word times word, and word times byte. The first example multiplies 80H (128) by 40H (64), The product in the AX is 2000H (6,192). The second example generates 10000000H in the DX:AX registers. “The third example involves word times byte and requires extending BYTE! to a word, Because the values are supposed to be nnsigned, the example assumes that bits in the AH register ste to be zero. (The problem with using CBW here is that the leftmost bit of the AL couid be I, and propagating {-bits in the AH would result in a larger unsigned value.) ‘The product in the DX:AX is 0040 0000H. The fourtt example uses the EAX fordoubleword muliplication Signed Muttiptication: IMUL ‘The purpose of the MUL (Integer Multiplication) instruction is to multiply signed data, In Figure 13-3, CLOIMUL gives the same three examples as BLOMUL, but replacing MUL with IMUL. ‘The first example multiplies 80H (a negative number) by 40H (a positive qumber). ‘The produc: in the AX register is FOOOH. Using the same data, MUL generates a product of 2000H, so you can see the difference between using MUL and using IMUL. MUL teats 80H as +128, whereas IMUL teats 80H as — 128. The product of —128 times +64 is —8192H, which equals E0OOH. (Try converting EOQOH te bits, reverse the bits, add 1, and add up the bit vatues.)230 Arithmetic | Processing Binary Data = Chap. 13 TITLE ALQMULT (COM) MUL and IMUL cpaystions MODEL SMALL Looe ORG 120K REGIN: FMP SADR ATOMAIN BYTE: B08 BrmE2 DB 40H wou: = Bw BRDOH, Word (OW = 33008, BHERDA «© BD BaD00R0 GMGRD2 DD 20000000 ALOMAIN PROS NEAR main procedure LL BloRL yall MOL routine Sat CLOT MOL Hall IMG» routine wou Xx, cool Jend processing AL eyres, wayte x byte BYTB2 } product in AX Mov Ax, woRD Mord x word wu = woRD2 Pe product tm pr.aK Mov AL, BYTE Byte x word, SUD ANAK fextend maltiplisand in 3 UL RORDT Po product ia DK:Ax Moy BAY, DwORDL pPoubleword MUL DNORD2 PP doublewoed er LOM, END? 1 Rramples of THUL: i roma, proc MOV AL, BYTE) te x byte mo. Bye PY preduet fo aX Moy AK, wokn Mord x word Twtn, WORD? P promuer in Dx:aX Mov Ay BYTEL ppyte © word car extend maltiplicand in ML TMI, WORDS Breduct in DK:AK Mov AK, DwORDa (moubleword x TMuL — UWORD2 P doubleword Ret c1omu, ADP END agar igre 13.9 Mukiplying Unsigned andi Signed Values ‘The second example multiplies 8000H (a negative value) by 2000H (a positive value}. The product in the DX:AX is F000 0000H, which is the negative of the product that MUL generaced.Performing Doubleword Multiplication a The third example exends BYTE! to a word in the AX. Because the values are sup- posed to be signed, the example uses CBW to extend the leftmost sign bit into the AH reg- ister: 80H in the AL becomes FFSGH in the AX. Because the multiplier, WORD 1. is also negative. the product should be positive. And indeed itis: 0040 O000H in the DX:AX—he sane result as MUL, which multiplied two unsigneé numbers. ‘The fourth example uses the BAX for doubleword multiplication, In cffect, if the multiplicand and multiplier have the same sign bit, MUL and [MUL generate the same product. But if the moltipticand nd multiplier have different sign bits, MUL produces a positive product and IMUL produces a negative product. The upshot is ‘hot your program must know the format of the data and usc the appropriate instructions, You may find it orthwvkile to use DEBUG ta trace through these examples, PERFORMING DOUBLEWORD MULTIPLICATION Conventional multiplication involves multiplying byte by byte, word by word, or doubleword by dovbleword. As we have already scen, the maximum signed value in a word is +32,767. Multiplying larger values on pre-80386 processors involves additional steps. The apprasch ‘on these processors is to multiply each word separately and then add each product together. ‘The following example mukiplies a four-digitdecimal nuaber by 2 two-digit aumber: 1,365 x2 16,380 What if you could multiply only two-digit numbers? Then you could multiply the 13 and the 65 by 12 separatly, like this: a 6 xaz xe se 700 ‘Next, add the wo poxtucts; but remember uct is actwally 15,600: ince the (3 is in the hundreds position, its prod- 15,600 (3 x 12 x 100) + 760 (65x 12) 16, 380 ‘An assembly program can use this same technique, cxcept tha the data consists of words 4 digits) in kexadecienal format, Let’s now exainine the requirements for multiply- ing double word by word and doubleword by doubleword. Goubleword by Word In Figure (3-4, BLOXMUL multiplies a doubkword by a word. The multiplicand, MULTCAN, consists of two words containing 3206H and 25214, respectively. The reason for defining two DWs instead of a DD isto facifitate addressing for MOV instructions that232 Acithmatie I: Proces ing Binary Dato Chap. +3 TITLE AL3DAMUL_ (COM) Multiplication of doublewords “MODEL SMALL cove. RG 00H BEGIN: UMP SHORT ALCMATN foureas Dw 3523H tH -3206H MOUTPLR «Dw OBE te = geooH PRooucr Dw m8 m8 wo AVOMAIN PROG HEAR Mein procedure CALL BLOXMOL peal Tet multipsy CALL CLOCLEAR iClear product CALE DLOXMer. iCall 2nd multiply voy’ Ax, coor jind preceeaiag.” agt 21k ALOMAIN ENDP. : Devubleword x word: BroxMyL PROC yov AX WeuTCAN sMultipzy left word MOL MULTFLR«2 pot mule iplicand Mov PRODUCT, A pSrere product mov PRDUCT$2,Dx Mov AR, MULTCANE2 mMoleipty right word MOL MULTEER+2 + of mltapiicana AOD PRODUCT+2,aK ined co stored product: ADC PRODUCT+4,Dx ner Diexem, mDP : Clear product area Grocuran proc Nev PRODUCT, C000 iClear words Moy PRODUCT ¢z,0000 © left to Tight Mov PRODUCT 44/0000 Mov PRODLET+e, 0000 : Doubleword x doublewera DrowmL —PRoe mov AX, MULTCAN imuletpiicand word 1 Mo = MULTPLR x multiplier word 1 Woy PREDUCTsO, Ax re product, Nov PReDUCT+2,0x oy AX. MULTCAN Multiplicand vord 2 vor. MULTPL RS? se mltipiier word 2 ADD PRODUCE+2,ax jada to stored product AUC PRODUCT+4. Dx ADC PRADUCT4E,00 sAdd any carry igure 13-42 Muliplying Muldword Values. move words to the AX register. The values are defined in reverse-word sequence, and the assembler stores each word in reverse-byte sequence. Thus MULTCAN, which has a de- fined vaive of 32062521H, is stored as 21250632H.Performing Doubleword Multiplication 233 Moy RX, MULTCAN2 juitiplicand word 2 MUL MOLTPLR xemaleiplier word 1 ABO PROSUGT+2.Ax, ingd Co scored, product ADC PRODUCT #4.DK ABC PROUCT+E;00 :Add any carry oy Ax, MuLTean 2 mMuleiplicand word 2 Mun | MULTELRS? ia maltipiior word 2 ADO PRODUCTS AX iaad ta produce Abe | PRODUCT#S‘DX REE PROKNUL © ENOF END BEGIN Figure 13-4b Mutiplying Mutiword Values ‘The multiplier, MULTPLR+2, contains 6400H. The field for the generated prod- uct, PRODUCT, provides for three words. The fitst MUL operation multiplies MULT- PLR +2 and the left word of MULTCAN; the product is hex OB80 E400H, stored in PRODUCT +2 and PRODUCT+4, The seccnd MUL multiplies MULFPLR-+2 and the right word of MULTCAN; the product is 138A 5800H. The routine then adds the wo producis, like this: Product 1: 0600 OFf0 taco Product 2: +238 5800 Total: “L3BA 6680 F400 Because the first ADD may cause a carty, the second add is ADC (Add with Carry), Be- cause numeric data isstored in reversed byte format, PRODUCT will actually contain OOE4 8066 813, The routine requixes that the first word of PRODUCT initially contains zere. Daubleword by Doubleword Multiplying two doublewords on pre-80386 processors involves four mohiplivations MUCTIPUCAND MULTIPLIER word 2 x word? word 2 x word! word | X word 2 Mord 1 % word | Youndd each productin the DX and AX to the appropriate word in the final product. In Fig ure 13-4, DIOXMUL gives an example. MLLTCAN contains 3206 2521H, MULTPLR contains 64000A26H, and PRODUCT provides for four words. Althouga the logic is similar to muliplying doubleword by word, this problem 1e- ‘auires an additional feature. Following the ADD/ADC pair is another ADC that adds 0 to PRODUCT. The first ADC itself could cause carry, which subsequent instructions would clear. The second ADC, therefore, adds 0 if there is no carry and adds 1 if there is a carry. The final ADD/ADC pair dots not require an additional ADC: Because PRODUCT is Large enough for the final generated answer, there is no camry.2 Arithmotic |: Processing Binary Data Chap. 13 ‘The final productis 138A 687C KESC CCEB, stored in PRODUCT with the bytes re- versed, Try using DEBUG to wace ttuough this example. SPECIAL MULTIPLICATION INSTRUCTIONS ‘The 80286 and later processors have additional [MUL formats that provide for immediate ‘operands and allow for generating products in registers other than the AX. You can use these insteuctions for either signed of unsigned multipication, since the results are the same. The valnes must be all the same length: 16 or (for the 80386 and later) 32 bits. 16-Bit IMUL Operation For the 16-bit IMUL, the first operand (a register} contains the multiplicand, and the sec~ ‘ond! operand (an immediate value) is the muitiplier. The product is generated in the first ‘operand. A product thal exceeds the registcr causes the carry aud overflow flags to be set. ‘The general format for this 16-biIMUL operation is 32-Bit IMUL Operation ‘The 32-bit IMUL has: three operands: The second operand (memory) contains the multiplicand. and the third operand (an immediate value) coniains the multiplies. The product is generated in the first operand (a register). The general format for the 32-bit IMUL is. [Hiab] tai [reat wenoryievediate 16/32-Bit IMUL Operettion “The 80386 and later processors provide yet another IMUL format for 16- or 32-bit operations. The first operand fa register) contains the multiplicand, and the second operand (egister/memory) contains the multiplies. The product is generated in the first operand. Here are examples of these three IMUL instructions: Size Instruction ult ipTicand Multiplier Product Rebit TAL DK.25 0x 35 ps 32-pit —TMUL ECK,MULTCAND, 25 NULTCAND a FO 16/32 TUL Bx,Cx eK a aMutiptication By Shitting 238 MULTIPLICATION BY SHIFTING For multiplying by a power of 2 (2. 4, 8, etc.), you may gain faster processing simply by shifting tef the necessary number of bits. For the 8088/8086, a shift greater than 1 requires: that you foad the shift value in the CE register, In dhe following examples, the multiplicand is im the AX: huttiply by 2 Gohife Tefe 2): SHL AKOL Multiply by B (shift left 3: MOV C.,03 — : 6088/8086 SHL AKC. Multiply by 8 Ghife left 3): SHLAK,O3 80286 and Tater ‘The following routine for an 80286 or latcr processor could be uscfui for left shifting adoubleword produetin the DX:AX registers. Although specific to.a 4-bit shift, it could be adapted to other values: sa. Dx,08 to YoFr 4 bits wov Lat jn si AK,O8 to Teft 4 bits SUR BLO ;Shife OL co right 4 bits OR DL.BL —Insere 4 bits from BL én OL. Be especially careful not to shift off a significant digit. DIVISION For division, the DIV (Divide) instruction handles unsignes data and 1DIV-Cinieger Divide) handles signed data, You aré responsible for selecting the appropriate divide instruction. ‘The general format for DIVIIDIV is [fiat | Dany | eatzeerzoeory | ‘The basic divide operations are byte into word, word into doubleward, anc {80386 andTater) doubleword into quatword. Byte into Word Here, the dividend isin the AX and the divisor is a byte in memory or another register, The operation stores che remainder in the AH and the quoticat in the AL. Becanse a J-byte quo- tient és very small—1 maximum of +255 (FFH) if uasigned and +127 (7FH) if signed— this operation has limited use. Before division: <——dividera—>| a aL Remainder | Quotient After division236 Arithmetic |: Processing Binary Deta Chap. 12 Wold into Doubleword For this operation, the dividend is in the DX:AX pair and the divisor is a Word in memory coraaother register. The operation stores the remainder in the DX and the quotient in the AX. ‘The 1Dword quotient lows a maximum of +32,767 (FFFFH) if uasiged and + $6,383 (JEFFHD if signed, o« ax Before division: [High dividend | Low dividend After division: Remainder Quotient Doubleward into Quadword For dividing a doubleword into aquadwotd, the dividend is in the BDX BAX pair and the divisor is a doubleword in memory or another register. The operation stores the remainder in the EDX and the quotient in tho BAX. eDt FAX Before division: | tigh dividend | Low cividend After division: Renard Quotient: Fieid Sizos ‘The operand of DIVADIV references the divisor, which determines the field sizes. in the Fol. lowing DIY examples, the divitors are in a register, which determines the type of operation: OPFRATION —DTVESOR —OTVIDEND QUOTIENT REMAINDER. ov byte a aL a Dv word DK:AX aK ox DIV EX —doubleword EDX:EAX AK Dx In the following DIV examples, the divisors are defined in memory: eyrel pa? woRD1 OW 7 aOR. OD 7 wee OTVISOR DIVIDEND QUOTIENT REMATHDER DIV ere, «BYTE. aK aL an DIV WRDL © WORDL.OK:AK aK Ox DEV OMORDL GMORDL—EDX:tAX AK OX Remainder, If you divide 13 by 3, the result is 41/3, where the quotient is 4 and the true remainder is 1, Note that a calculator (and a high-level programming lan- ghage) would deliver a quotient of 4.333. .., which consists of an integer portion (4) and 4 fraction portion ¢.333). The values 1/3 and .333 are Fractions, whereas the. 1 is a remainder.Division TITLE AL3DSY (COM) DIV and IDIV operations MODEL ‘SMALL. CODE ox 100K SME SHORT ALOMATN De = Gone pe asoctt PROC NEAR in procedure CALL | BieDIV Healt Drv routine CALL cuorory Gald IDIV routine ON AK, scaoH HBna processing INT 2i8 RLONKIN. NDE BLonIy PROC of DIV, Ov" AZ.woRDI Word / byte Div Byte Fomde:qiot Ln AI:AL MOV AL, BITE ipyte / byte SUB AHA ‘extend dividend 1n AK Sty Byres ) eaderquot in AE-AL Noy px, woRD2 pDowbleword / word MOY AX, RORDS PV aividend tn DAVAX Bry woRD: bo eae rquot in DAK HOV AK,woRDL Wend / word Su Bx,De pvextand dividend in Dx BIv WORDS $ mede:quet in DX:AX rer piopry NDP mxamples of 19IV: c1omry Roc Mov Ax, woRDL Mord / byte apiv Briss [Eur quot in AB:AL Mov AL,SYTBL ayte / byte ca fextend dividend in Al Iv. Byres ender squat de AWAL wow iDowbleword { wore MOY -RE/HORDS Po dtwidend in’ DX:ax IDIVWORDL fo mdeiquot in DEAX Mov AX, MORDL word / word wD } exterd divicend in Dx DIY worD3 } inde quet in DAK REZ cLoIDty ENDP. END BEGIN Figure 13-5 Dividing Unwigaed aue Signed Values Using DIV for Unsigned Division ‘The purpose of the DIV instruction is to divide unsigned data. in Figure 13-- 237 the proce: dure B LODIV gives four examples of DIY: byteinto word, byte into byte, word into double~ word, and word into word. The first example divides 200011 (8092) by 80H (128). The remainder ia the AH is OOH, and the quotient in the AL is 40H (64),238 Atithmetic |: Processing Binary Data Chap. 13 The second cxampic requires extending BYTE to a word. Because the value is sup- posed to be unsigned, the example assumes that bits in the AH register are to be zero. The remainder in the AH is 12H. and the quotient ia the AL is OSH. Inthe third example, the remainder in the DX is L000H, and the quotient in the AX is O080H. ‘The fourth DIV requires extending WORD | toa doubieword in the DX register. Af ter the division, the remainder in the DX is OOOH and the quotient in the AX is 0002H. Using {DIV for Signed Division The purpose of the IDIV instruction is to divide signed data. In Figure 13-5. C1OIDIV gives the same four examples as B1ODIV, but replacing DIV with IDIV. The first example divides 2000H (positive) by 80H (negative). The remainder in the AH is OOH, and the quotient in the AL is CDH (~64). (Using the same data, DIV resulted in a quotient of +64.) The results, in hex, of the remaining three examples of IDEV are: TOIV EXAWLE —-RERATNDER (OX) QUOTIENT (AX) 2 FE C-18) Fe C8) 3 1900 (4096) 0080 (128) 4 0000 oor ‘Only Bxample 4 produces the same answer as did DIV. In effect, if the dividend and divi- sor have the same sign bit, DIV an [DIV generate the same result, Butif the dividend and. divisor have different sign bits, DIV generates a positive quotient and IDIV generules a negative quotient. ‘You may find it worthwhile to use DEBUG to trace through these examples. Overfiows and interrupts DIV and IDIV operations assume that the quotient is significantly smaller than the original dividend. As a consequence. the operation can easily cause an overflow; when it does, an interrupt occurs, with unpredictable results. Dividing by zero always causes an interrupt. But dividing by 1 generates a quotient that is the sameas the dividend and could also cause an interrupt. Herc’s a useful rule: Ifthe divisor is a byte, its contents must be greater than the lett byte (AH) of the dividend: if the divisor is a word. its contents must be greater than the Je Word (DX) of the dividend; if the divisor is a doubleword, its contents must be greater than the left doubleword (EDX) of the dividend. The following illustration uses a divisor of |, although other values could serve: DIVIDE OPERATION DIVIDEND OIVISOR —QUOTZEXT word by byre: m2. ah aaa Doubleword by word: 0002 4026 0001 (14028 In both cases, the generated quotient exceeds its available space, You may be wise to in- clude a test priot to a DIV or IDIV operation, as shewn in the next two examples. In the first, DIVBYTE is a I-byte divisor, and the dividends already in the AX:Division 238 CMP AH DIVBYTE —;Conpare AA to divisor ONB £50 pass Sf not snaler ory oiverre jorvide word vy byte In the second example, DIVWORD is @ 1-word divisor, and the dividend is in the DX:AX: CMP DK,DIVWORD — ;Conpara OX to givisor awa £70 HBypass if not smaller OV orvwoRD {Divide doublewerd by ward For IDIV, the logic should account for the fact that either dividend or divisor could, be negative. Because the absolute value of the divisor must be the smaller of the wo, you could usc the NEG instruction to sot a xcgative value temporarily to positive and restore the sign after the division, Division by Subtraction Ifa quotient is too large for the divisor, you could perform division by means of succescive subtraction. That is, subtract the divisor ftom the dividend, increment a quotient value by L. and continue subtracting until the dividend is less then the divisor. In the following example, the dividend is in the AX, the civisor is in the BX, ard the quotient is developed in the CX: Sub CK.OX Clear quotient 020; CV AK\BX TF dividend < diviscr, 3a 020 bade SuB-AX.BX — :Subtract divisor fran dividend Inc CK padd 1 to quotient IMP DRO Repeat pags HQuorient in Cx, ransinder in AX At the end of the rontine, the CX contains the quotent and the AX contains the remainder. ‘The example is intentionally primitive to demonstrate the technique. Ifthe quotient is in the DX-AX pair, inclnce these two operations: 1. AtD20, compare AX to BX only if DX is 2010, 2. After the SUB instruction, insert SAB DX,00. Note that a very large quotient and a small diviser may cause thousands of loops at 2 cost of processing time. DIMSION BY SHIFTING For division by 4 power of 2 (2, 4. 8, and so on), you may gain faster processing simply by shifting righ the required number of bits, Forthe 8038/8086, a shift greater than | requires 2 shift value in the CL register, The following examples assutne that the dividend is in the AX: Divide by 2. Cehift wight 1): SHR Ax,OT Divide by 8 (shift right 1): MOV CL,03 78085 /800520 Arithmetic k Prac ing Binary Osta Chap. 13 SHR AX,CL Divide by & Canife right 3): SHR CL,O3 80286 and Tater ‘The following routine for an 80286 oF later processor could be useful for right shift- ing a doubleword produc: in the DX:AX pair. Although specific ta a 4-bit shift, {could be, adapted to other valucs: SHR AK.O4 to right 4 bits MOY. BL,OL in SHR OX,08 to right 4 bits SHL BL.O4 to eft 4 bits on La bits from BL in DL REVERSING THE SIGN ‘The NEG (negate) instruction reverses the sign of a binary Value, from positive to negative ‘nd vice versa. In effect. NEG reverses the its, just like NOT, and then adds 1 for proper ‘two's complement notation. The general format for NEG is {Prasevs3 WES | regi ster/nemory | Here are some unrelated examples: weg cL bits NEG Bx bits NEC EOx 32 bite NEG BINVAL —:Byte oF word in nemry Reversing the sign of a 32-bit {or larger) value involves more steps, Assume that the DX:AX pair contains a 32-bit binary number, NEG cannot act on the DX:AX pair concur- rently. and using it on both registers would inyalidly add 1 te both. Instead, usc NOT to flip the bi, and use ADD and ADC to add the | for two's complement: Nor 0K ADD AX, ADC DX,0 — jAdid carry vo DX ‘One minor problem remains: It is all very well to petform arithmetic on binary data thot thc program itself defines or on data already in binary form on 2 ditk file, However, «ata that cntets a program froma 2 keyboatd is in ASCH format. Although ASCII datas suit- able for displaying and printing, it requires special adjusting for arithmette—a topic dis- cussed in the next chapter. ‘THE NUMERIC DATA PROCESSOR ‘This section provides a general introduction to the numeric data processor. 8 full discussion is outside the scape of the book. The system hoard contains a socket for an Intel NumericReversing The Sign at Data Processor, known asa coprocessor. The BO87 coprocessor operates in conjunction with an 8OR8/86, the 80287 with an BO286, the BO387 with an $0386, and so fort, ‘The coprocessor has its own instruction set and floating-point hardware for perform- ing such operations as exponentiation and logarithnic and trigonometric operations. The ccight 80-bit floasing- point registers can represent numeric values up to 10 to the 400th power, The coprocessor's mathematical processing is rated about 100 times faster than « regular processor. ‘The 8087 consists of eight 80-bit registers, RI-R8, in the following format: s ‘exponent signi Ficand ra [78 ot | 93 0 Each register has an associated 2-bit (ag that indicates its status: 00 Contains a valid number O1 Contains a zero value 10° Contains an invalid number 1 Isempty ‘The coprocessor recognizes seven types of numeric data: 1. Word integer: 16 bits of binary data. s nunber as [14 0 2. Short integer: 32 bits of binary data, 5 unber aa [30 0 3. Long integer: 64 bits of binary data, s umber 62 [62 4. Short real: 32 bite of floating-point data, aa [se v3 | ez 0 s | exponent siarificand |2az Arithmetic i: Processing Binary Data. Chap. 13 Long, real: 64 bits of floating-point data, s exponent signi ficand 93 [sz se [ot 6. Temporary reat: 80 bits of fleating-point data. Ps] exponese signifi Types 1, 2, ond 3 are common binary two's-complement formats. Types 4. 5. and 6 repre- sent floating-point numbers. Type 7 contains 8 4Dbit decimal digits, You ean load any of these tormats from memory into a coprocessor register and can stove the register contents mnto memory. However, for its calculations, the coprocessor converts all forrnats in its weg- 15 into temporary real. Data is stored in memory in reversc-byte sequence. The processor requesls a specific operation und delivers numeric data to tke co- processor, which performs the operation aad retums the result, For assembling, use the ap- propriate 80x86 directive, ‘The INT LH instruction can help determine the presence of a coprocessor. The op- eration delivers the equipment status to the AX, where bit 1 on means thata coprocessor Is present, KEY POINTS + The macimum signed values for |-byte accumulators are +227 and — 128. + For maltiword addition, use ADC (o account for any carry from a previous ADD. If the operation is performed in a loop, use CLC to mitialize the carry flag to zero. + Use MUL for unsigned data and IMUL forsigned data, + With MUL, if a multiplier is defined as a byte, the multiplicund is AL; if the multi~ plicr is a word, the multiplicund is AX: if the multiplier is a doubleword, the multi- plicand is BAX. + Shift left (SHL or SAL) for multiplying by powers of 2. + Use DIV for unsigned data and (DIV for signed data «+ For division, be especially careful of ovestiows, The divisor toust be greater than the: contents of the AH if the divisor is a byte, DX if ihe divisor is a word, or EDX if the divisor is a doubleword.Questions 243, + With DIV. ifa divisor iy defined as a byte, the dividend is AX, if the civisor is a word, the dividend is DX:AX; if the divisor is a doubleword, the dividend is EDX:EAX. + Shift right for dividing by powers of SHR for unsigned elds and SAR for signed fields. QUESTIONS 13:1, For both unsigned and signed data, (a) what are the maximum values in a byt and (b) what is the maximum value in a word? 13-2, Distinguish between a carry and an overtlow as a result of an arithmetic operation. 13-3, For the following binary additions, show the sums as binary numbery and s un- signed and signed decimal numbers, plus show the settings of the overflow and carry flags (@) OOLWO1L {b> OLMONI Ce) At010120 soooni0da 00012001 -—-+01012002 13-4, Revise Figure 13-2 so that the routine adds three pairs of words instead of two. De- fine the idditional words as WORD3A and WORD3B and change the old WORD3A and WORD3E to WORDAA and WORDSB. For Questions 5-8, refer to the following dala, with words properly defined in reverse sequence: VALVEL bw O153H De 16244 VALUE? td 0328H bw 3caaHt RESULT DM 0 bw 0 mo 13-5. Code the instructions 10 add the following: (a) the word VALLE! othe word VALUED; (b) the doubleword beginning at VALUEL w the doubleword at VALUED. 13-6, Explain the effect of the following related instructions: ste NOY OX, VALUED ADE OX, VALUE? 13-7, Code the instructions to muhiply (MUL) the following; (a) the word VALUE! by the word VALUEZ: (b) the doubleword beginning at VALUES by the word VALUEZ. Store the product in RESULT. 13-8, Code the instructions to divide (DIV) the following: (a) the word VALUE] by 36; (b) the doubleword beginning at VALUE] by the word VALUE2. 13.9, What divisors other than zero cause overflow errors? 13-10, Refer to the section “Multiplication by Shifting.” which Upscrates shifting left 4 bits, Revise the example for a left shitt of 2 bits.14 ARITHMETIC II: =—— PROCESSING == ASCll AND BCD DATA Objective: To examine ASCH and BCD data fofinate, 10 performs arithmetic in these fortuatz, and to cover conversions berwoet these formats and binary. ll INTRODUCTION ‘The natural data format for arithmetic on a computer is binary. As.seen in Chapter 13, bi- rary format causes no major problems, as fong as the program itself defines the data. However, much of the numeric data that ¢ progrem must process is in a form other than. tinary. For example, numerie data enters 2 program from a keyboard az ASCII charac- ters, in base-10 format. Similarly, the display of numeric values on a screen is in ASCIL formal. Arelated numeric format, binary-coded decimal (BCD), hes occasional uses and appears as unpacked andas packed. The PC provides a number of instructions that facil- itate simple arithmetic and conversion between formats. This chapter also covers tech- Diques for converting ASCII data into binary format to perform arithmetic, as well as techniques for converting the binary results beck into ASCII format for viewing, The pro- gram at the end of the chapter combines much of the material covered in Chapters 1 ‘through 13. Tf you have programmed in a high-fevel fanguage suchas C, you are used fo the com- piler accounting for the radix (decimal or binary) pout. However, the computer does not recognize a radix point in an arithmetic field, so thet you as an assembly language pro- grammer have to account for its position. aeData in Decimal Format 245 Instructions introduced ia this chapter are: AAA — ASCIIAdjust After Addition AAS ASCII Adjust After Subtraction AAM — ASCILAd{ust After Muttiplication AAD —— ASCIVAduust for Division DAA _Decimal Adjustment After Addition DAS —_Decimal Adjustment After Subtraction DATA IN DECIMAL FORMAT ‘To this point, we have handled mumeric values in binary and ASCU formits. The PC § tem also supports binary-coded decimal (BCD} format, which allows for some limited ariuhinetic operations. Two uses for BCD format are: 1. BCD permits proper rounding of numbers with no loss of precision, a feawre that is, particularly useful for handling dollars and cents. (Rounding of binary numbers that represent dollars and cents may cause a toss of precision.) 2. BCD is often a simpler format for performing arithmetic on small values envered from a keyboard or for output on the screen or printer. ABCD digit consists of four bits that represent the decimal digits O through 9: Binary BCD digit | Binary BCD digit 000 ° ‘o1or 5 0002 au ano 6 e010 z ona, r 012 3 +1000 8 e100 4 2001 9 ‘You can store BCD digits as unpacked or as packed: 1. Unpacked BCD contains a single BCD digitin the lower (rightmost) Four bits of each. byte, with zeros in the upper four bits. Note that although ASCII format is also in a sense “unpacked,” it isn't called that 2. Packed BCD contains two BCD digits, one in the upper four bits and one in the lower four bits. This format is commonly sed for arithmetic using the mumeric coproces- sor, defined by the DT directive as 10 bytes. Let's examine the representation of the decimal number 1,527 in the three decimal formats: Format Contents Length ASCII 31.35 32.37 Four bytes Unaacked CD 01.05 02 07 Four bytes, Packed BCD «15 27 Two bytes246 Arithmetic i: Procassing ASCII and BCO Data Chap. 14 ‘The processor perfomns arithmetic on ASCT and BCD values one digit ata time. You Ihave to use special instructions for converting between the (wo formats. PROCESSING ASCII DATA ‘Because data that you enter from a keyboard is in ASCIL format, the representation in mexn- ‘ory of an entered decimal value such as 1234 is 31323334H. But performing arithmetic on the ASCII value involves the AAA and AAS instruction: Tlabet:} | amd | :ASCEE Adjust After Addition Cabel:) | Aas | :ASCTT Adjust After Subtraction ‘These instructions sre coded without operands and automatically adjust an ASCII value in the AX register. The adjustment occurs because an ASCII value represents an unpacked base-10 number, whereas the processor performs base-2 arithmetic. Adding ASCII Numbers Consider the effect of adding the ASCIE numbers 8 (38H) and 4 (34H): 38H san Tou) 6H ‘Tne sum 6CH is neither a correct ASCH nor a correct binary value, However, ignore the leftmost 6, and add 6 to the rightmost hex C: Hex C plus 6 = hex 12, the correct arswer in terms of decimal numbers, Why add 6’? Becanse that’s the difference hetween hexafecimal (16) and deeimsl (10). Athougha little oversimplified. it does indicate the way io which AAA performs its adjustment. The AAA operation checks the rightmost hes digit (4 bits) of the AL register. Ef the digicis berween A und F or the auxiliary carry Day (AF) is 1, the operation adds 6 to the AL register, adds 1 (o the AH register, and sets the carry (CF) and auxiliary carry flags to 1. [a all cases, AAA clears the leftmost hex digit of the AL to zero, As anexample, assume that the AX contains 0038H and the BX contains 0034H. The 38 in the AL and the 24 inthe BL represent two ASCII bytes that are to be added. The sum of the 8 and the 4 should be 12. Addition and adjustment are as follows: ADD AL.BL — 1Add 34H to 38H, equals COECH aA pAgqust for ASCII Add, equals OL0zH Because after the ADD the rightmoss hex digit of the AL is C, AAA adds 6 to the AL, adds Vito the AH, ses dhe CF and AP flags, and clears to zero the lefimost hex digit of the AL. “The sesult in the AX is now O102E. “Yo restore the ASCi representation, simply insert 35 in the lefttaost hex digits of the AH and AL to get 3132H, ot decimal 12: RAX,3030H— :RasuiTt is non 31328Processing ASCII Data ar In the next example, the AF flag affects the result. This time, the ADD operation adds 30H to 39H in the AL., giving 72H and setting the AF (becanse of the carry from bit 3 to bit 4), Although the value in the rightmost fou hits is only 2, because the APIs se1. AAA adds ‘6 to the AL. adds | to the AH. and clears the ieftmost hex digit of the AL. The resultin the AX is now corrected as 0108 (or 18). All that is very well for adding I-byte ASCII numbers. Adding mukibyte ASCIT nunibers, however, requires a loup that processes from right to left (Low ordcr (o high or der) and accounts for carries. The example in Figure 14-1 adds two 3-byte ASCH] num- bers, ASCVALI and ASCVAL2, and produces a 4-byte sum, ASCTOT, Note the following points: + ACLC instruction at the start of BIOADD zeros the CF flag, + Following 820, ADC is used for addition because an ADD may cause a cary that should be sdded to the next (left) byte. + A MOVZX instruction clears the AH on each leap because cach AAA may add 1 40 the AH, ADC, however, accounts for any carry, Note that the use of XOR or SUB to clear the AH would change the CF flag. + When looping is complete. the procedure moves te AH (contaiaing either a final OO or 01) to the leftrnost byte of ASCTOT. + Atthe end, ASCTOT contains 01020702H, To insect ASCIL3 in cach byte, the pro~ gram calls C1OCONY, which loops through ASCTOT in memory aid ORs cach byte with 30H, The result is 31323732H.or decimal 1272, which the program displays be- Fore ending. The routine did not use OR after AAA to insert leftmost 3s, becuase OR sets the surry flag and changes the effect forthe ADC instructions. A solution thet saver the (lag settings is to pash (PUSHF) the flags register, execute the OR, and then pop (POPE) the flags tore- store them: ADC ALDI] cA with carry AM jaijust for ASC PSHE iSave Mags 08 ALIOH i Tasere ASCIT 3 PoPr restore Flags Mov [BX],AL —;Store sum Subtracting ASCII Numbers ‘The AAS instruction works like AAA. AAS checks the rightmost hex digit (4 bits) of the AL, Irthe digit is between A and F or the auxiliasy carry is 1. the operation subtracts 6 from the AL, subtiacts 1 from the AH, and sets the cuxiliry (AF) and carry (CP) flags. In all cases, AAS clears the leftmost hex digit of the AL to zer0. “The next two examples assume chat ASCVAL I contains 39H and ASCVAL2 contains 35H. The first exampte subtracts ASCVAL2 (35H) from ASCVAL1 (39H). AAS does not needto make an adjustment, hecause the rightniost hex digits tees than ben A,248 Arithmetic Il: Processing ASCII and BCD Data Chop. 14 TITLR ALAASCAD (20M) Adding ASCIT numbera "MODEL MALE, [cope 386 Reqvar? DR 24" AScroT. DB "0000", "6+ mov! AM, 03H, iRequest deplay TRA DX,ASCTOr total ier 218 Wo ax, 4cooH pia procenaing ier 21H ALOMAIN. NDP. : Add ASCTI values Baoapp Proc TER ST,ASCVALLS2 EER DY /ASCVALZ62 LEA BX,ASCTOT3, » Mov x,03 pini¢ialize 7 eope 20: movex ax, [2] plond ASCIT byte in AK apc) AL, [or] [xa (eith carry? Max Paajuee for ASCE mov BH}. AL istere mum Bee | ar Dec at DEC aK top 320 Hoop 3 tines mov (BX) ,AH pet and, wtore carry RET pieapD —-EXDP. 2 convert binsry to ASCII: Cxocony PROC NEAR EBA aX,ASCTOT+3 Convert _ASCTOT MOY CK!04 ‘te ASCIE 20: Ok BYTE FIR (ax) ,300 Dec BX 1o6P C20 sIpep 4 times Rear crocouy ENDP 88D BEGIN Figure 141 Adding ASCII Numbors mE wo AL.ASCvaL1 0029 SUR AL,ASCVAL2 ;0006 0 as. youre 00 OR AL, 30H 50014 ‘The second example subtracts ASCVALI (9H) from ASCVAL2 (35H). Because the rightmost digit of the result is hex C, AAS subtracts 6 from the AL, subtracts 1 from the AU, and sets the AF and CF flags:Processing Unpacked Bed Data 2a9 ax OF MOV AL,ASCYAL? 70035 SUB AL,ASCYALL §—300FC 17 AAS iOS 2 ‘The answer, which should be —4, is FFO6H, its ten’s complement, which has Little value. PROCESSING UNPACKED BCD DATA ‘Multiplication and division of ASCII numbers require that you fitst convert the numbers into unpacked BCD forest. You can use the AAM and AAD instructions to perform arith- metic directly on unpacked BCD numbers: [label] | aam | :ASCIT Adjust After Multiplication Caper] | aap | jascr adjust sefore otvision Multiplying ASCil Numbers ‘The AAM instruction cortects the result of multiplying ASCH dava in the AX register, How- ever, you must first clear the 3 in the leftmost hea digit of cach byte, thus converting the value to unpacked BCD. For example, the ASCIL number 31323334 becomes 01020364 as unpacked SCD. Alo, becavse the adjustment is only one byte at a time, you can multiply only I-byte fields and have to perform the operation repetitively iz a loop. Use only the MUL (unsigned multiplication). not the IMUL, operation. ‘AAM divides the AL by 10 (OAH) and stores the quotient in the AH and the restain- der in the AL. For exarnple, suppose that the AL contains 3SH and the CL contains 39H. ‘The following code multiplies the contents of the AL by the CL and converts the result to ASCH format: INSTRUCTION crwenT xo AND CL,OFH Convert CL to 09 0035 09 AND AL,OFH Convert AL to 05 0095 09 mul, cL multiply AL by CL op 09 aa HConvert AX to unpacked BCD 0405 OR AX,3030H Convert AY te ASCIT 3435 ‘The MUL operation generates 45 (002DH) in the AX. AAM dividesthis value by 10, gen- crating a quoticat of 04 in the AH and a remsindot of 05 inthe AL, The OR instruction then converts the unpacked BCD value to ASCII format. Figure 14-2 depicts multiplying a 4.bye ASC multiplicand bya L-byte ASCII mul plier. Because AM can accommodate only 1-byte operations, the procedure BLOMULT eps through the multiplicand one byte at a time. from right to left. At the end, the unpacked RED product is 0108090105, which a toop in CLOCONY converts te true ASCT format as 3138393135, or decims] 18,915, The program displays the product before it eads processing. If the multiplier is greater than one byte, you have to provide yet another loop that steps through the auliplier. In that case, it may be simpler to convert the ASCIL data to bi- nary format, as covered in a later section,Chap. 14 Arithmetic I: Processing ASCH and BCD Data TETLE —-RL4AgeMU (com) Multiplying ASCIT numbers MODEL SMALL {CODE &G 100K pesee, Gee ALCMAIN MUuTcAN 3783" rascrt Leena NULTSER «TR '5* ASCPROD TB 5 DUPIO), *S* ALOMAIN PROC NEAR LL mow GL crocomy yoy AH,O9 jRequeet display IBA DX, ASCPROD wr 218 mov AX aceon 22nd processing Ter Bait : Multiply ASCII numbers Biomeut Roc NEAR, nov Cx,0¢ ithitielice 4 leope TEA ST,MSLTCANe? Tea BT ,ASCPRODss ND MUUTOLR, PH = Clear SCTE 2 20: wov AL, [8] iuoad ASCIT character aD RL, OF iClear ASCII 3 WL = MuLTeLR fitole ply aM pnajust for ascar zDD AL, (21) Add 60 nov [orl,an | prodact TEC DL ov (DT).an iStore preauct carry fae | ST Toor 520, jueep 4 kines eer BLOMULT —ENDP. > Convare product to aSCIT @10cony PROC NEAR, LEA BK,ASCPRODes ©; Right tc 2ert, mov exl05. S'bytes 20: OR BYR ETRIBXI, 30% pec BX wor C20 Hhogp 4 times aeT, cxpcury — ENDE ND BEGIN Figure 14-2. Muliplying ASCIE Numbers Dividing ASCIi Numbers The AAD instruction provides 2 correction of an ASCTI dividend prior to dividing. Sast as ‘with AAM, you first clear the leftmost 33 from tho ASCH bytes to create unpacked BCD format. AAD allows for a 2-byte dividend in the AX. The divisor can be only a singte byte containing 05 to 09.Processing Packed 8CD Data 281 Assume that the AX contains the ASCIT value 28 (3238H) and the CL. contains the divisor, ASCH 7 (37H). The following instructions perform the adjustment and devision: INSTRUCTION coemenT a AMD CL,OFH Convert to unpacked ECD 3238 OF AND AX,OFOFH Convert to unpacked BCD 0208, AD iConvart to binary core on ok Divide by 7 00 AAD moltiplies the AH by 10 (OAHD, adds the product 20 (14H) to the AL. and clears the AH. The result, OO1CH, is the hex representation of decimal 28. Figure 14-3 allows for dividing a 1-byte divisor iaco a 4-bytedividend. The proce- dure BIODIY steps through the dividend from left to right. LODSB gets byte from DIVDND into the AL (via the SI), and STOSB stotes bytes fram the AL into QUOTNT (via the DI). The remainder stays in the AH register so that AAD can adjust it in the AL. At the end, the quotient, in unpacked BCD format, is 00090204, and the remainder in the AH is (2. The procedure CLOCONY converts the quotient to ASCII format as 30393234 (From left to right this time). The program displays the ASCII quo:tent, 0924, before ending. IC the divisor is greater than one byte, you bave tw provide yet another loop to step dough the divisor. Better yet, see the laier section "Conversion of ASCII w Binary Forma.” PROCESSING PACKED BCD DATA 4n the preceding example of ASCII division. che quotient was 00090204. If you compress this value, keepingonly the right digit of each byte, the resull is 0924, now in packed BCD format, You can also perform addition and subtraction on packed BCD data. For this pur pose, there are two adjustment instructions, DAA and DAS; Tlabet:] | Daa | cOecinal Adjustment After Addition [labels] | DAS | :Decinal Adjustnant After Subtraction DAA comerts the result of adding (wo packed BCD values in the AL., and DAS corrects the result of subtracting them. Once again, you have to process the BCD fields one byte (two digits} at a time, ‘The BCD sum in the AL consists of two 4-bil digits. If the value of the righunost digit, exceods 9 or the AF flag is set, DAA adds 6 to the AL and sots the AF. If the value in the AL now exceeds 99H or the CF is sci, DAA aukds GOH to the AL and sts the CP. Otherwise, it clears the AF and CF. The following example should clarify this procedure. Consider adding the BCD values 057836 and 060427, Wich the CF flag clesred to 0, art the addition with the rightmost pair of digits.Arithmetic Il: Processing ASCII and BCD Data Chap. 14 TITLE -ALAASCDY (COM) Dividing ASCTT numbers ok L00H BEGIN; JNP SHC AIOMAIN Div DB 3638" jAScIT itewo OIVSOR DB ASCQUCT DB AGADT PROG WEAR CALL B1o0ry ALL c1ocoMy Mov! AH, 09H, LEA _Dx,ASCQUOT brah MOV AX. ¢cooH Bad processing mer 3a AuoMauN EDP bivide aScxY aunbers BiepIv PROC NEAR Mov Od pInitietize 4 loope Sup AAR lear ieft byte of dividend Aub DrVsor. ory Velear divisor of ASCIL 3 Lea 31, DrvbRD LEA (DI AscDUOT 2a: yopss ilgad ASCII byte AED AL, OF idlesr 3010 3 bap indjuce for divide DIV DIVsOR joivide STOs3 jStore quotient Loop aac pRour times? Rar’ Pye, ete BLeDIy—-BHDE > Convert. product te ASCE Gascony proc EAR LER BX,ASCQCOT Leftmose byte, Mov X’d4 _ 2 & byes cao: OR BYTE PTRIBK],20H Clear AgCTZ 3 avg Bx pRexe byte 10 20 Heep 4 times RET rmae c1ocony ENDP END BEGIN pre 14-3. Dividing ASCII Numbers eco HEX emuny ‘STORED BCD Firse AOC, clears CF 36 26 e011 110 27 a gguo 112 3 Ey oxo: 1103, DAA adds 06H, sets AF 000 0130 0130-0012, 8 Second ADC, sets CF 78 8 SLL 1000 9a 3a 1991 0100 72 (1)0€ 40000 1100 DAA adds OSH, sets AF 000 0110Converting ASCII Data to Binary Format 253 002 sore DAA adds 60H Checause 21:0 ooo CF set), sets CF ou ons z Third A0C) ads 2 0 ‘000-102 Ghecause CF set), os 0000 oaae clears cF of 2000 000% R ‘900-1400 DAA ads 06H, sets AF 9000 o120 en oct, 2 ‘The BCD sum is now correctly stored as 127263. The program in Figure 14-4 illustrates the foregoing example of BCD addition. ‘The procedure BIOCONV converts the ASCII values ASCVALI and ASCVAL2 to the packed BCD values BCDVALI and BCDVAL2, respectively. Processing, which is from right to left, could just as easily be from left to right, Also, processing words is cas- ier than processing bytes because you need wo ASCII bytes 10 generate one packed BCD byte, However, the use of words docs require an even number of bytes in the ASCH field. ‘The procedure CIOADD performs a loop three timos to add the packed BCD num. bers to BCDSUM. The final total is 00127263H, which you can confirm with DEBUC— use DDS:114. CONVERTING ASCII DATA TO BINARY FORMAT Perfouning arithmetic in ASCTL or BCD format is suitable oniy for short fields. For most arithmetic purposes, it is moxe practical to convert such numbers into binary format. In fact, it is easier to convert from ASCII ditectly to binary than to convert from ASCII to BCD. « tobinary. Conversion from ASCII to binary is based on the fact that an ASCIT number is in base. 10 and the computer performs arithmetic in base 2, Here is the procedure: 1. Start with the rightmost byte of the ASCII field and process from tight to lett 2, Strip the 3 from the left hex digil of each ASCII byte, thereby forming a packed BCD number. 3. Multiply the first BCD digit by 1, the second by 10 (GAH), the third by 100 (64H}, and so forth, and sum the products. The following example converts ASCII number 1234 to binary: Decieat Hexadecimal Step Product Step Product 4x1e 4 4x Oe aH 3x 10« 300043 X (OAH = EH 2%100- 200 2X SMe CH 1X 1000» 10001: X BEB = 3tBH Total: 38 oaD2H386 BEGIN: Arithmetic I Processing ASC and BCD Data Chap. 14 ALQBODAD (COM) convert ASCII to BCD and add MODEL SMALL Aacvata, ASCVALE SCDvALL acount, BcpauM, cope ORS 1008 SMP SHORT R10MAIH Dp ras7ee! TABCTT daca itone be ‘agsaat De +009! #Bop data icons De ‘bon DB DuPtay ALOMAIN A1OMAIH Brocony B10CORV Gzonp0 cLoano EAR ST, ASCVALI+4 pInitialize for ASCVALL DY, scpvara-a 3a0coRY #€al_ convert: routine SE, ASCVALA-4 pIpitaalize for ASCVALZ DECRCDUAL “2 sidcomv pCal2 convert routing ca 0aDD {eall add routine 3X, COOH ;and processing nw ox.03 iNo, of words to convert 1, (31) jaet ABCKr pote A AL . :Shift off 16,04 PRSCIT 36 Dt) yam ietore BOD digite Add PCD nonbers: BRE EEEDEE GIGHEGREQEDY GuRQgEUEER F an, a iClear aw ST; BCDVAL1+2 HInitialize Dr, BCDvALa«2 p BED 5X, BCDSUMe3 i. addresses x03 pa-byte flelde au, {sr] iGet RCDVALI (or 19nea) aL! [BT] Tadd ecovate PDeetmal adjust Moy [BK], [store te BCESIM bac St DEC | DT Dee Bx esr 20 Hoop 3 time: RT, eNDP EVD. BEGIN Figure id-d. Converting and Adding ACD Numbers ‘Try checking that the sam O4D2H really equals decimal 1234. In Figure 14-5, the proce- dure BIOCONY converts ASCII mumber 1244 to its biuary equivalent. Ax LEA instruction‘Converting Binary Data to ASCII Format 255 TITLE -RAAASCBI (CoM) Conyert ASCII to binary format MODEL SMALL cone: ORG 1608 DR by ww bw MOLEACT ALOMAIN proc NEAR, iain procedure, cA B10coNy mov AX ACOOK est) Bnd processing ALOMAIN _ENDE BLOCONY PROC NEAR MOY BY.10 Mult factor Mov cxLoa iGount for loop A = ST|ASCVAL+3 © jAddrese af ASCVAL azo Mov AL. (61) Select ASCIT character AND AK, ocOrx iRosove 3-zoae. mun MULFACT. Heuteip:y by 10 factor ADD -BIRVAL, 2X, Rod to binary Moy RQ MULReT @alculate next, MUL Bx 110 factor MOV MULPACT,AX DEC St Last ASCIT character? Woop B20 no, continue. rer 1 yea, return’ END BEGIN Figure 84-5 Converting ASCII Numbers to Binary Permat initializes the adress of the rightmost byte of the ASCII field, ASCVAL$3, in the SI reg ‘ster The instruction at B20 that moves the ASCII byle to the ALis Mew at, [ST] ‘The operation uses the address of ASCVAL43 ta cepy the rightmost byte af ASCVAL into the AL. Each iteration of the loop decrements (he SI by 1 and reterences the next byte (0 the left. The loop repeats for each of the four bytes of ASCVAL. Also, each iteration mul- liplies MULFACT by 10(AH), giving maipliers of 1, LO(OAH), 100 (64H), and so forth, At the end, BINVAL contains the correct binary value, D204H1, in reverse-byte sequence, whic you can confirm with DEBUG. ‘The coutine is coded for clarity; for faster processing, the multiplier could be stored: in the Dl register, CONVERTING BINARY DATA TO ASCII FORMAT ‘To print or display the result of binary arithmetic, you have to convert it into ASCTI Format ‘The operation involves reversing the previous step: Instead of multiplying, continue divid- ing the binary number by LO (CAF) until the quotient is less than 10. Each remaindes. which256 Arithmetic th Provessing ASCIl and BCD Data Chap. 14 can be only 6 through 9, successively generates the ASCII mumber. As an cxample, let's convert 4D2H back into decimal format: Divide by 19 Quotient Remainder aT a0z 7 4 alte c 3 ale 1 2 Because the quolient (1) is now Jess than the divisor (OAH), the operation is complete. The. remainders frorn right to left, along with the last quotient, form the BCD resull: 1234. All thar remains is to store these digitsin memory with ASCII 3s, 2s 31323334. ‘The program in Figure 14-6 converts binary number 04D2H to ASCII format. The procedure BIOCONY divides the binary number successively by 10, until the remaining, ‘quotient is less than 10 (OAH), and stores the generated hex digits in ASCH format ax 31323334, which the program displays before ending, You may find ituseful, if not down- right entesaining, to reproduce this program and trace its execution step by step. ‘TITsR AL@BTNAS (CoM) Convert binary data to ASCII MODEL SHALL coe ORG 00H ALOWAIN PROG WEAR. IMain procadure ALL BiocoNv MOV AN, O9H sDisphey GRA -BELASCVAL PRBcIT value int aii Mov AK, acon iBed processing INE 2aH ALoMAEN BNE BLOCONY PROC NEAR Mov Cx,go2a TRA SEASCVRE+3 ——Addreas of ASCYAL Mov ax; DINVAL, ier binary amount B20. cwP AKC 307 ya B30 byes, exit KOR DX,DK ielaa? upper quotient, Div x [Davide By a9 OR DL, 36H Moy [St1.5t iStore ASCIT character : bec sr IME B20 BO: OR AL, 30H, ‘Store lage quotient mov [SH] ,AL PSge ASCII character Figure 14-6 Converting Binary Nusers o ASCII FormatProgram: Converting ASCH Data a7 SHIFTING AND ROUNDING A PRODUCT Suppose your product contains three decimal places and you have to round it and reduce it 0 two decimal places. As an example, if the product is 17.385, add 5 to the rightmost (un- wanted) decimal position, and shift right one digit Product: 17.385 adc 5: + 9.005 Founded product: 17.390 = 17.39 If (a) the product is 17.3855, add 50 and shift two digits, and if (b} the product is 17.38555, add 500 and shift three digits: (a) 17.3855 (by 27.38555, + 0.0050 +.0.00500 17.3905 = 27.38 7.39055 = 27.39 Further, a number with six decimal places requires adding 5,000 and shifting four dig- jis, and so forth, Now, because a compater normally processes binary data, 17.385 appears ¢5 43E9H, Adding 5 to 43E9H gives 43BEH, or 17390 in decimal format, So far, so good. But shifting one binary digit results in 21F7H, or 8695— indeed, the shift simply halves the value. You require a shift that is equivalent to shifting righ? one decimal digit. You can accomplish this shift by dividing the rounded binary vaine by 10, or hex A: Hex 43EE divided by hex A = 6CBH, Conversion of 6CBH toa decimal number gives 1739. Now justinsert a decimal point in the correct position. and you can display the rounded, shifted value as 17.39. By this method, you can round and shift any binary nurabes. For three decimal places, add 5 and divide by 10; for four decimal places, add 50 and divide by 100. Perhaps you have noticed a pattern: The rounding factor (5, $0,500, etc) is always one-half of the value of the shift factor (10, 100, 1,000, etc.). OF course, the radix point in a binary number is implied and is not actually present. PROGRAM: CONVERTING ASCII DATA ‘The program in Figure 14-7 allows users (c enter the number of hours worked and the rate of pay for employees and displays the calculated wage. For brevity, the program omits tome srror checking thal would otherwise be included, The procedures are as follows: AOMAIN Handles initialization and invokes the procedures for entering data and calculating wage. BIOINPT Accepts hours and rate of pay in ASCII format from the Keyboard, These values may contain a decimal point CIOHOUR Initializes conversion of ASCII hours to binary. DIORATE Initializes conversion of ASCII rate to binary. EIOMULT Performs the multiplication, rounding, and shifiing. A ‘wage with zero, one, or two decimal places does not require rounding or shifting,reTLe {386 aLOMATN az0L00P aon Arithmetic I: Processing ASCII and BCD Data Chap. 1 MASCREMP (mz) Eter houre ard rate, dieploy wage “HODAL SMALL USTACK 64 ATR xu 28 sBquates for sex BOU 52 } lecations zou 10 Bor 14 LABEL BYTE Hours parameter List: BB 6 : BF Ds & DUR) : Lape BYTE BE pS Dé UPI?) 03 'Houre ¥orked? « D3 tRate af pay? * Ds tage = ba 10 DUP(30K0, 13, 20 D3 "Press any key Go continue or gec to quit’ be? poate items oe 99 Dw 0 bw 80 DB. DB bo DM OL oe 0 DB oe oe? Oe oa CODE Ror FAR Mov AX, aaa sInitialize 05 Mov Ds,an po and BS tegiateze Mov ES, AX CALL O108cR iClear acreen CALL gsowne peleaz window CALL quocuxs isee cursor CALL | BLOINPT HRecept hours & rate CALL C2OKOUR Feonvert hours to binary CALL DIORATE ieonvert rate te binary LL EIOMULT. Halculare wage, round CALL PLOWAGE hConvert wage tO ASCIL cure ci0ntsp [Display wage CALL Mi0PRUS Flenee fox user MEAL, BH cue az 0100R cALL . quoscR Woy “Ax, acoon Int 2H NEP Figure 147a Displaying Employee WagesProgram: Converting ASCII Data : Imput hours & water BLOINET PROC EAR MoV. HOW, TOPROW+1 Mov COL, LEFCOL:3 CALL g20cuRS Inc | tow MoV AH. 40n Mov -Bx!OL mov cx tena) MESSGL INT 21H Mov AH, oaH LA DK, HROPAR tr 74H Moy COL, LEFCOL+3 CALL q20cuRS io ROW Mov AH, 40H Mov BK.O. Moy xa sa Dx, MEssGa mr zit Mov AH, can TRA DXCRATEPAR we 238 Rr BROINET — ENDP xonoUR PROC cicHouR NDP Dioram: © reoc MOVEX CX, ACTRLEN TER ST, RATRFLD-1 MD ST CK au 2 0RsB1 mov AX, BINYAL Mov BIARATS. AX Dicrame DP 7 Multiply, round, and shift RiomuuT FROG NEAR Moy cx,05 1A DL,ASCWAGE wov AK, 30308 REP STOSW 18a cursor sRegpest dieplay [File handle (Mo. of chazacters PPrompt for houre jResept hours }Set column iRequeat aisploy fetie panaie No. of characters fPrompt for rate rhesape rate tat right position fat houre iconvert to pinazy 1Set right position Pog vate ieenvert to binezy Set ASCET wage to 308 Figure 147 Displaying Employer WegeeB20: B30: Bao: Eso E70: eioMuer F50 Feo: FA oHAGE ov ox on. bec Dee ENDP Lea ADD or mB bee on oz, one Dw oR Dee sua HP on wov RET BuDe Arithmetic rocessing ASCH and SCD Data Chap. 14 siurT, 10 Abus, 00 cx, NODSC. Ch,oe W1f mere than ¢ eae } decimals, exxor 9 & E30 rBypase if 0, 1, 2 dace RODEC, D2 KO =D sCaleulate shift factor 20 SHIFT, AX AKL Calculate rourd value aDausT, Ax Ax, AINERS arkeare Calculate wage AX, ADJUST jReund wage Dx!80 Dx. SHIFT Progect eco Large E50 } for BIve 2a Clear ax 270 apgusT, 00 Shift required? Boa Fone, bypese BRIT fenre wage Bx. Dx iClear remainder Convert wage to ASCII: ST,AScuRGES7 BYTE PIR(ST},".* j8et decimal point SI,NODEC :Set right start pos'n BYTE PrRISTI,«.* Fao sBypase if ac dec posta ar Dx,00 TIE DK-AK © 20, #50 Ax, 0070 2 operation finished Ped reno ;Remainder ia aSCI1 Giglt 3L, 308 (eh) ot iStore RACIY character ar Bx,ox ;Clear remainder F30 At, 308 jstore Leat AgctT 181}, AL po cbaracter Figure 14-7e Displaying Employer WagesProgram: Converting ASCII Deta 264 2 Dioplay wage: q@optsP PROC NEAR Mav” Con, 1BFCOL3 2Sat column cA q20cRS, Nov Cx, 09 IRR ST, ASCWRGE, 20: plea leading xaroo om pyre Prefer) ,200 ame G30 + to blanks Mov BYTE PrEIsT] ,20H te ST wor G20, aor Nov AE, 40H HHequest dieplay Nov RK;OL ivile handle Nov cx) 29 Hie. of charncters LEA DK,RKSSG3 Hvage INr 21H her croprep ENED ; wause for ue fopans poe sov see cursor ov CALL mov’ kk, aan sRegnest display nov Bx,or irite handle oy CxO No. of cheracters BA DX. MEgsa4 Display pause er FR mov AH, 20lL Request reply TNT 168 ier Huopays ENP 1 Convert AECIT to binary: Auoassr PROC NEAR kev MUTT10,0¢01 Moy BWAL,, 60 mov KOR shear Bx vow dat ASCII character Me LE dec point Swe a0 mov DECTND, 01 MP EO AND AX, 0007 MOL MOLTO stuleiply by Eactor ADO GGNYAL, ax jada to binary MOV AKpqULTLO ieelculace next mo, TEIN P"gactor «20 MOY MULTI0, AK eMP DEIN, 29, iRenched decimal peint? gz 350 we | Bx 4 yea, add to count Figure 14.7 Displesing Employee Wiges262 30: aso: Arithmotic Processing ASCII end BCD Data Chap. 14 Dec SE LOOP 320 Gr DECIND, co sEnd of Loop uz” e830 [Any decinal point? ODEC, Bx } Yea, ada co total ADD eer Stoasor ENDP DLOSCR PROC RAR Mov Ax, c6ooH Mov Bi. Somt sAteribute wo x00 ov Dx, 18aFA ter ii =r quose ENP. Geocurs PROC mov yoy BHLO0 sage 0 wou DHLROW [get row, yoy Tb,coL cok unt ier 40H ner ozocurs ENDP. Gsowzn = pROC NAR Mov aR, o605H pPive rows wou BWL16H iAtrtbute wor GH: TOPEOW Mov cL LBPCOL Mov THSBOTROW wov DL,RITCOL ier 10K sr, ovowrnt —ENDe END ALOMATE Figere 14-76 Displaying Employee Wages FIOWAGE Inserts the decimal point, determines the rightmost position to begin storing ASCII characters, and converts the binary wage to ASCIL GLODISP Clears leading zemos of wage to blanks and displays the wage. HUOPAUS Displays the calculated wage until the user presses a key. Pressing tells the program to discontinue processing, JIOASBI Converts ASCII to binary (a common routine for hours aud for mate) and determines the nuxitber of decimal places in the entered value. QIOSCR Scrolls the whole screen and sets it to black on cyan, Q30WIN — Scrollsa window in the middle of the screen where hours, rate, and wage are displayed as brown on blue,Key Points 263 Umitations. A limitation of this program is that the total number of decimal places inboursard inate of pay must be 6 of less. Another Limization is the magnitude of the wage itself. [hours and rate contain a (otal that exceeds six decimal places, or if the wage ox- cocds about 6,553.50, tbe program clears the wage to zero. In practice, 2 program would Print a waming message or would contain precedurss (o overcome these limiations, Eno checking. A program designed for users other than the programmer act only should produce warning messages, but also should validate hours and rate of pay. The only valid characters are the numbers 0 through 9 and one decireal point, For any other charae~ ter, the program should display a message. and redisplay the inpat prompt, A useful in- struction for validating is XLAT, which Chapter 15 covers, In practice, test your program thoronghiy for alt possible conditiens, such as 2210 val- ues, extremely high and Jow values. and negative values. Negative values. Some applications involve negative amounts. especially Zor re- versingand correcting eniries, Youcould allow aminussign foilowinga valve, such as 12.34~, orpreceding the value, as — 12.34. The program could then check for a minus sign during con- version to binary. Cn the otter hand, you say want to leave dhe binary aumbe: pesitive ancl simnply set an indicator to revord the Fact that the mount is negative. When the atithnctic is complete. the program, ifrequired, can insert a minus sign tothe Icft or right of the ASCH field. If you want the binary nuruber to be negative, convert the ASCTE inpu: to binary as usual. (Sce the section “Reversing the Sign” in Chapter 13 for changing the sign of a binary field.) And watch out for using IMUL and IDIV to handle signed data, For rounding a neg- ative amount, subtract 5 instead of adding 5. KEY POINTS + An ASCII field requires one byte for each character. For a aumneric field, the right. most bli-byte contains the digit, and the leftmast half-byte contains 3. + Clearing the leftrnost 3s of an ASCTL number to Os converts it to unpacked binary- coded decimal (BCD} forme, + Compressing ASCTT characters (o (wo digits per byte converts the fieldro packed bi- nary-coded decimal (BCD) data, + Afer an ASClLadd. use AAA to adjust the answer; after an ASCH subvact, use AAS so adjust the answer, + Before an ASCH mubiplication, conven the muhipticand and nultiphicr to wnpacked BCD by clearing the lettmost hex 3s to 0s. After the multiplication, use AAM to ad- Just the produc: + Before an ASCII division, convert the dividend and divisor to unpacked BCD by clearing the leftmost hex 3s, and use AAD 10 adjust the dividend. + For mast arithmetic purposes, convert ASC] numbers to binary, When converting feom ASCII to binary format, check that the ASCU characters are valid: 30 though 34,0 decimal point, and possibly a minus sign264 Auithmetic Il: Processing ASCII and BCD Data = Chap. 14 (QUESTIONS 14-1 Assume that the AX contains ASCIE 8 (0038H) and the DX contains ASCII 6 (0036H). Explain the results of each of the following unrelated operations: (a) ADO AX, EH (b> ADD AK, DK Aa AM Ce) SUE AX,DX Cd) SUB AX. 08H Aas, AAS 14.2. Use hex nolation 10 show the decimal value 3796 in the following formats: (2) ASCIL: (b) unpacked BCD; (c) packed BCD. 14.3, An packed BCD field eamed BCDAMT contains G2050904H. Code a loop that causes its contents to be proper ASCID 323539341, 14.4, A field named ASCAMT' contains the ASCU decimal value 215, and another field named ASCAMT?2 contains ASCH 4, Cade the instructions to multiply the ASCII numbers and to stote the product in ASCPROD, 14-5. Use the same fields as in Question 14-4 to divide ASCAMT! by ASCAMT?2 and store the quotient in ASCQUOT. 14.46, Provide the manual calculations for the following: (a) Convert ASCH decimal value 30846 to binary, and show the result in hex format; (b) convert the hex value back to ASCIL 14-7, Code and run a program that determines 4 computer's memory size (see INT 12H in Chapter 3), converts the size to ASCII format, and displays it on the screenas follows Memory size is rrn bytesiF DEFINING AND PROCESSING TABLES 5 Objective: To cover the requirements for defining tables, pertoer- ing searches of tables, and sorting table entries, INTRODUCTION Many program applications require fables or arrays comtaining such data as names, de- scriptions, quantities, and sates, This chapter begins by defining some conventional tables and then covers methods for searching through them. Techniques for searching tables are subject to the way in which the tables are defined, and many methods of defining and searching other thar those given here are porsible. The definition and use of tables largely involves applying what you have already learned, Other commonly used features are the use of sorting, which rearranges the sequence of data in « table, anc tie use of Inked lists, which use pointers to locate items in a table. The only instmction introduced in this chapter is NLAT (Franslate’ DEFINING TABLES: To facilitate searching through them, most tables are atrangod in a consistcut manner, with each entry defined with the same format (character or mumeric), with the same length, and in either ascending or descending order. A table that you have been using throughout this book is the definition of the stack, which in the following is a table of 64 uninitialized words, where the name STACK refers to the first word of the able: 268266 Defining and Processing Tables Chap. 15 STACK OW 54 DUPC7) ‘The following two tables, MONTABL and CUSTABL. initialize character and mu metic values, respectively. MONTABL cefines alphabetic abbreviations of the months, whereas CUSTABL defines a table of customer numbers: WONTAEL DB ‘Jan’, “Fab", Mar’, ..., ‘Dect CUSTAL DB 205, 208, 209, 212, 215, 224 Alleniries in MONTABL are three characters, and all entries in CUSTABL are three dig- its, But note that the assembler converts the deeimal numbers to binary format and, pro- vided that they don't exceed the value 255, stores them each in one byte. A table may also contain a mixture of numeric and character values, provided: that their definitions are consistent. In the following table of stock itemts, each numeric en- try (stock number) is 2 digits (1 byte), and each character entry (stock description) is, 9 bytes, STOKTBL 08 12, ‘Computers’, 14, ‘Paper....', 17, ‘Dissettes’, ‘The four dots following the description “Paper” are to show that sgaces should be pre sent; that is, spaces, not dots, are to be keyed in the description, For added clarity, you may code cach table entry ona separate line: STOKTBL 0B 12, ‘Computers’ DB 14. ‘Paper...’ DE 17, "Diskertes’ The next exampte defines a table with 100 entries, each initialized to 15 blanks (1500 bytes): STORETEL pe 200 OUPCES DUPC* +3) ‘A program: could use this lable to store np tn 100 values that i( has genersted internally, oF it could use the table to store the contents of up to 100 entries that it accepts from a key- board or reads from a disk file. Tables on Disk In real-world situations, many programs are table driven. Tables are stored as disk files, which any number of programs may require for processing. To this end, a program can read a table file from disk into an “empty” table defined for that purpose. The reason for this practice is because the coments of tables change over ime. {cach program defines its own tables, any changes require thet all programs redefine the tables and be reassembled. With table files on disk, a change to a table simply involves changing the contents of the file. Chapter 17 gives an example of a table file. Now let's examine different ways to use tables in programs.Direct Addressing of Table Entries 267 DIRECT ADDRESSING OF TABLE ENTRIES. ‘Suppose that a user enters a numeric month such as 03 and that a program is to convert it twalphabetic format—in this case, March, The routine to perform this conversion involves defining a table of alphabetic mouths, all of equal Jength. The length of cach entry should bbe that of the longest name, September, in this format: WONTEL OB "January..? DR "February. * DB ‘March... DE ‘Decenber.* The entry ‘January’ is at MONTBL+00, ‘February’ is at MONTBL+09, ‘March’ is at MONTBL +18, and so fort. To locate month 03, the program has to perform the fallow- fing steps: 1. Convert the entered month frotn ASCII 33 to binary 3. 2, Deduct 1 from this number: 3 — | = 2 (because month 01 is at MONTBL +00) 3. Multiply the new number by (the length of each entry): 2% 9 = 18. 4. Add this product (16) to the address of MONTBL,; the result is the address of the re quired description: MONTBL+ 18, where the entry “March” begins. This technique is known as direct table addressing. Because the algorithm calculates the required table address directly, the program does not have to search successively through cach enixy in the table. Direct Addressing, Example 1: Table of Months “The program in Figure 15-1 provides an example of s direct access of a table with the naines of the months. The procedure B1OCONV assumes 11 (November) as input and converts the month from ASCILto binary format like this: Load ASCII month in AX = 323 Use 3030 for XOR Cungacked month) = O1¢iH If Tefenose byze nonzero, clear = 0061H and add OMH (decimal 10) © — QUUBH (dec ™nal 12), ‘The procedure CLOLOC determmes the actual location of entries in the table: Deduct 1 from month in the AX = ODOAH (cecinaT 10) Huleiply by 9 Clength of extries) - OSAH Céecinat 90) Add address of table (HONTL) = HONTBLYSAH ‘The procedure transfers the description (“November”) fiom the table to ALFMON, where DIODISP displays it. One way to improve this program is to accept numeric months from the keyboard and to verify that the values azz berween 0! and 12, inclusive.268 Defining and Processing Tables THTLE ——-ALSOEREC (UN) Direct table addressing “MODEL SMALL (cone ena” 00H Sue SHORT ALOMAIN srebruary ‘, ‘March tray el tdane saugust '; ‘September’ ‘Tovenber "| "Decenber * Roc dimin procedure CALL B1ocony jConvert ¢o binary CALL CloLec huocate month cart BLoprsP: iDteplay alpha month Nov. AK,4cool, pend processing ar 2am ‘AIOMAIN, | ENDP Convert ASCTE to binary: ; Display alpas ronth: Diopise = PROT play apn OVA, don jRequant display Woy BX, 03 [pite handle my CK.08 [9 characters TRA OX’ ALPMOR yr 2a viopsse mNDF iND BEGIN faecery — eroc nov | AM, MomEN i8et up month fov AL,MONINGD HOR AK, 30207 2Clear ASCIT 38 OP AOD Porch 02-097 B20. Po yee, bypa: WOR HAH io, elmer A, mp ALD 2 correct for binary a0: per BIOGUNY — ENDP. 2 Locate month ia tabh @ionee ye 12, ST, MONTBL Tec AL, igowrect for takle XUL NI¥E iMaleiply AL by 9 apo ST.AK wove CX, NINE HHinitialiee 9-char move up IEA D1, ALEMON JEP wovSR iMove 9 charactera ier ciowoc | ENDE Chap. 15 Figure 15-1 Direct Table Addressing: Example 1 Direct Addressing, Example 2: Tables of Months and Days. ‘The program in Figure 15-2 retrieves today’s date from the system and displays it. INT 21H fonction 2AH delivers the following binary values:Direct Addressing of Table Entries TITLE ALSD:SDA_(KXE) Display day of week and month "MODEL. SMBLT. ISTACK 64 DATA savEORY «DBO? BAVENOR DB? ren DB ie ELavEN DBE ne DR 12 DAYSTEL DB ‘Gunday, § ', : DB Tweeday, e DB ‘Thureday, 9, ‘friday. 8 * DB ‘Saturday, § * momma, pp danuary'$', "February $ +, ‘March $ De ‘April 3 May & ‘Jase $ be ‘July § ‘augue: $ sBeptenber § DB ‘October $_', ‘November $ ‘, ‘December $ RIOMAIN PROC EAR MOY AK edata Initialize Woy DS. ax 2 aegment registers Moy ES,Ax Moy AX, o600H CAEL QLOSCR. iClear’ screen CALL | QzOcuRS Hast cursor Mov! AN, 3AHT 1Oet feday'a dace int ae Mov SAVRMOW.DH :Save month Wow SAVEDAY,DL;Sava day of month CALL BADDAYNK © ;Diuplay day of week CALL CLOMONTR = ;Digplay month CALE «DIDDAYWO =: Display day Gur ELorNPT gwale for input cau guoser: Mov’ ax, ¢coon INT. 21H NDP Clee screen End processing Anca BAcnAYWK PROC MUL THELVE, pOay (in AL) 12 LEA DX, DaYsTBL, of table ADD K,AK plus offece Mov BH,D9H fnueat. cigplay mr 2a RET BLODAYWK HBP : Dieplay month! exonormn PROC NEAR MOV AL,SAVEMON Get month Dae | AL, [Decrement by 1 wUL | ELEVEN Dealeiply by ertry length EBA OX, MORTHL © Addreea of tabla ADD OXLAK, p plus offwct Moy AHLOSE iRequest deploy rer aa RST choMowTH “ENDP Figure 15-28, Diroct Table Addtescing: Exsanple 2270 36 buepayno p1oRYHO, exe:wer Qnoecr qioscr Qzocurs ozocurs, PROC Mov I er NDE Roc Tet END Defining and Processing Tables Chap. 15 Dioplay day of moxth EAR. AXSAVEDAY :Get day rat iconvet_ from binary AK,3030H te ABCIT 8X, AX isave ASCII ai AH, O2H HRaguest display DL,aL PO terae digit, BK,oZH jRequeat diaplay Du. BH [decend digit aul Wait for keyboard input: AK, LOH pRequest_ input 16H iCall BIOs Ax, o600H iRequest seroll BA, 27H Panite on blue 330 ker BIOTABLE ENDP Rigure U5-4a Searching a Table Using CMPSB 48 High. check next entry 102 Wigh, check next entry 12a Equal, entry found28 Dafining and Processing Tables Chap. 15 ooze CLODISPL PROC NEAR BO2F Ro cons Mov cx,05 Het to moves words 0032 BF? Mov 51,04 9032 SD 48 o0se F BA DI,DESCRN ;Addy of deper'n 0038 F3/ as RSP Movsi feet daser'n ona Ba a0 Mov AH, 40H iRequest dteplay Dose BB 002 Mov Bx,02, pelle bendie 003E BS oacA Mov Cx.10 FAG charactera 5042 AD 16 dose R LEA BXcDRsem — Sétack deeeription voas cp aa IND ait 0049 ca RET. poss CLpDISP: KNDP END ALOMATS Figure 154 Searching a Table Using CMPSB “The procedure B1OTABLE initializes the Dlto the offset address of STOKTBL (O03), the CX te the length (03) of cach stock item, and the $110 the offset of STOKNIN (000). AAs long as the bytes contain cqual values, the CMPSB operation compares byte for byte, and automatically increments the DI and SI registers for the next bytes. A comparison with the first table entry (123:035) ends with a high comparison after the first byte: the DI con- tains 004, the 1 contains OOL, and the CX contains 02. For the second comparison, the DI should contain 010 and the SE should contain 000. Correcting the 81 simply involves reloading the address of STOKNIN. To correct the ad- dress of the table entry that should be inthe DI, however, the inerement depends on whether the comparison ended after one, two, or three bytes. The CX contains the number of the re- maining uncompared bytes, in this case, 02, Adding the CX value plus the length of the stock description (that of the previously compared stock item) gives the offset of the next table item, as follows: Address in DI after OHPSB: 04H Add remaining length in Oc: + 02H Add Tength of stock description: +aH Next offsec aiiiress in table: 10H Because the CX contains the nutaber of the temaising uncompared bytes (if any), the arith- netic works for all cases and onds efter one, two, or three comparisons. Ou ar equal com- parison, the CX contains 00, and the Dl is already incremented to the address of the required description. The procedure calls CIODISPL, whee a REP MOVSW operation copies the description into DESCRN in order to display it. Tables with Variable-Length Entries Itis possible to definea table wilh variable-length entries. A special delimiter character such 38 00H could follow each entry, and FFH could distinguish the end of the table, The SCAS. instruction i¢ suitable for scanning for the delimiters. However, you must be sure that no byte within an entry contains the bit configuration of a delimiter: for example, an arithmetic binary emcunt may contain any possible bit configuration.‘The XLAT (Iranstate) Instruction an THE XLAT (TRANSLATE) INSTRUCTION ‘The XLAT instruction translates the contents of a byte into another predefined valuc. You could usc XLAT, for example, to validate the contents of data items or, if you transfer data between @ PC and an IBM mainframe computer, to translate data between ASCII and EBCDIC formets. The general format for XLAT is (abet) | xtat | :No operand XLAT expects thatthe address of the table is in the BX register and the byte to be transiated is in the AL. The following example converts ASCII numbers 0-9 into EBCDIC format. Because the representation of (-9 in ASCH is 30-39 and in FBCDIC is FO-F9, you could use an OR operation to make the change. However. let's also convertall other characters ta ablank. which is 40H in EBCDIC, Touse XLAT, you define a ranslaion table that accounts for all 256 possible characters, with EBCDIC codes inserted in the ASCII positions; that is, Ge EBCDIC characters in the ASCII locations and EBCDIC blanks in all other locations. Because the number 0 is ASCII 30H, the EBCDIC numbers begin in the table at location 30, of decimal 48: MLTABLE 0B 48 DUPCAGH) sE6COIC blanks DB OFOH.OFIH,OF2H.OFIH, ....OFH :EBCDIC 0-9 DB 198 DUP car) JEBCDIC blanks ‘The following example perfarms the inidalization and translation of an ASCH item, named ASCNO into EBCDIC format: LeA GK, XLTABLE Adress of tale ir ox WV ALLASOD Character to translate in Al SAT sTranslate to EBCDIC X1_AT uses the AL value as an offset address; in effect, the BX contains thestarting address of the table, and the AL, contains an offset valve within the able, If the AL value is 00, for example, the table address would be XLTABLE+0 (che first byte of XLTABLE containing 40H). XLAT would replace the 00 in the AL with 40H from the table, ‘Note that the first DB in XLTABLE defines 48 bytes, addressed as XLTABLE+00 through XETABLE+47. The second DB in XLTABLE defines data beginning at XLTABLE+48. If the AL value is 32H (decimal 50), the table address is XLTABLE+50; this tocation contsins F2 (EBCDIC 2), which XLAT would insert in the AL register. ‘The program in Figure 15-5 modifies XLTABLE so that it converts ASCH minus sign (2D) and decimal point (2E) to EBCDIC (60 and 4B, respectively) and now loops through 4 Gbyte ASCII field. Initially, ASCNO contains ~31.5 followed by a blank, or hex 21033312E3520. At the ead of the loop, EBCNO should contain hex 60P3F|4BP540, which youcan verily by means of DEBUG.ae Defining and Processing Tables Chap. 15 page 0,192, rime —-ALSKIATE (COM) Tzanalate ASCII to ERcDIC ‘MODEL SMALL LODE ome 2008 BROIN: GME ALONATN Acro Da 2315" pcre oR BURT) Jeonverted ERCOIG ram KETABLE DB 45 TUP(40N) Translate table DB 60H, aH Da don DB OPOX, OFLH, CPIM, oF aut, OFA Da «PSH, OPGH, CPIM, OPER, OF Ht Da 196 DUPCaon) ALOWAIN PROC RAR LEA SE,A8c¥0 of ABcHO LEA DT, EBGHO ‘9f EBCNO Noy Oxrcé Lensth of items LEAK, xEPARLE Addreps of table 20 Less ieee ASCIL char in AL xia pTranslate character BT0Se pBtore AL i BACND LOOP A29 iRapeat 6 times may 2x, acooH Bed processing re fait ALOMAIN —ENDP BND BEGIN i Figure 15.8 Converting ASCTI Numbersio FRCDIC PROGRAM: DISPLAYING HEX AND ASCII CHARACTERS ‘The program in Figure 15-6 displays all 256 hex values (00-FF), including most of their related ASCII symbols, for example, both the ASCH symbol § and its hex representation, 53, The fall display appears on the screen as a 16-by-16 matrix: DO 01.02 03 04 05 06 07 0B 09 UA 0B CC OD OF OF FO FL F2 F3 FA FS FS F7 F&O FA FB FC FD FE FF As shown in Figure 8-1, displaying ASCII symbols causes no serious problem. How- ever, displaying the hex representation of an ASCH valuc is more involved. For example, you have to convert 00H to 3030H, O11 ta 3031H, and so forth. "The program defines HEXCTR initially with 00 and subsequently increments it by 1 for each of the 256 ASCI characters. The procedure B10HEX splits HEXCTR into its (wo hex digits. For example, if HEXCTR contains 4FH, the routine would extract the hex 4, which XLAT uses for the translation. The value returned to the AL is 34H. The routine then extracts the F and translates it to 46H, The result, 3446H, is displayed on the screen as 4F.Program: Displaying Hox and ASCII Characters 29 TITIE —ALSASCHK (com) Dieplay ASCIT and hex characters CHOOSE SHALL 386 . oxt 2008, BEOIN, SMP SHORT acoMArN DISPRON DB. 16 DOPKS DUPI' 2), 13 eexere «BR a BEATAB «B30, 224,231, 23K, 341, 2511, 36H, 97H, BALE, 29H DR 4H 2H,43H, can janx, aon Anomara “bROG uAR iain procedure, CALE | qlecuR iClear aoroon LEA 51, BISFROW a20100P: CALL BOMEX sTranalate CALL caprere i end aiaplay CMP) HBXCTR, cree Hast hex vatue (PFT JE ASO Pyes, terminate Ine HEYeTR pono, iner next haw gap R2OLOOF as) mov ax, acouE ina processing mer ai ALOWAIN RDP : + Gonvert ASCII te hex BLOWEX PROC NEAR MOUZX AK, HEXCTR idee hex pair in AK BUR ax,0# [shift off right hex digie LER BK, ALATAR iSet table addreaa xLAT rvangiate hex Mov Is). AL tore Left character MO AL, HEXCTR AND_ AL; OF iClear lett hex digit XLAT ‘ranelare hex Moy (81) +2,aL [dtore right character RED ENDP Display = CLODISEL PROC REAR i MOV AL, HEXCTR Iget hex character Mov {gi]-3.at ome AL JAR \BOF character? ye zo 1 yes. bypa cme aL.o7H Lover than 77 ca e208 "yar ok ome AL ,20H MWigherJequal 162 vaE cat i ee, ok c20: lelge force plarke Moy BYTE PER [s1)+3, 208 e308 AWD 81,05 iWext location in row TBA I, DISPROWeBa owe br,sT Filled up row? UNE 40 Pona, bypaee Figure 15-60 Displaying ASCH and Hex Valoes280 Defining and Processing Tables Chap. 15, mov an, a0H Yee, requeat display Hor Brot he Bande Mor Gist 1 entice row TEs Dilprsrnow int hk 3, HRB ossrmow sReest dimlay row CLODrerL END? Z Clear screen: GiocuR = PROC NEAR mov Ar, oscow pRogueat sere] mov Bd, 61H jateribace MOY cx,0900 Mov Dl 1aaFR RET OiccLR © ENDP exp ancrN igure 15-66 Displaying ASCH and Hex Values ‘The procedure C1ODISPL converts non-ASCII characters 10 blanks. Because INT 21H fanedion 40H teats 1AH as an end-of file character, the program also changes it to blank. The procedure displays a full 1ow of 16 characters and ends after diaplaying the 16th row. ‘There are many other ways of converting hex digits to ASCII characters: for example, you could experiment with shifting und comparing. SORTING TABLE ENTRIES Often, an application requires cording data in a table into ascending or descending se- quence. For example, a user may want a list of stock descriptions in ascending sequence, cr alist of sales agents’ total sales in descending sequence, There are a sumber of table sort routines, varying from relatively slow processing but clear, to fast processing but ob- scurc. The routine prescated in this section is faitly efficient and could serve for most able sorting. ‘A genecal approach to sorting’a table is to compare a table entry with the entry im- mediately following it. If the comparison is high, exchange the entries. Continue in this fashion, comparing entry 1 with entry 2. entry 2 with encry 3, and so on to the end of the table, exchanging where secessary. If you made tny exchanges, repeat the entire process {from the start of the table, comparing entry ] with entry 2 again, and so forth, At any point, sf you proceed through the entire table without making an cxchange, you know thatthe table is sorted into sequence. In the following pseudocode, SWAP is an item that indicates whether an exchange ‘was mate (YES) or not saade (NO), 10: Initialize address of Tast entry io the table 720; Set SWAP to MO Initialize address of start of the table V30: Table entry > next entry?Linked Liste zat Yes: Exchange entries Set SWAP to YES, Increment for next eoury in the cable At end of the table? No: ump to T30 Yes: Does SWAP = YES? Yes: Dumo to 120 (repeat sort) Net End of sere ‘The program in Figure 15.7 allows a user t> key in up to 30 names from the key- board, which the program stores in a table named NAMETAB. It contains the following procedures: + AJOMAIN calls BIQENTER to accept a natre from the keyboard. calls C1OSTOR 16 store the name in a table anct, when all the names are keyed in, calls DIOSORT and FIODISP. + BIOENTER prompls the user to key in a name, accepts it. and fills it to the right with blanks. When ali the names ace keyed in, the aser just presses , with no name, + CIOSTOR stores each name successively in the table. + DIOSORT and ESOXCHG sort the table of names into ascending sequence. + FIODISP displays the sorted table, ‘Note that the table entrics are all fixed-teagth 20 bytes; a routine for sorting variable- length data would be more complicated. LINKED LISTS A linked list contains date in what are called cells, ike entries in a table, but in no specified sequence. To facilitate forward searches, each cell contains a pointer that indicates the lo- cation of the next entry in the list. (A zell may slso contain a pointerto the preceding entry so that searching may proceed in either direction.) The method facilitates additions and deletions toa list without the need for expanding and contracting it. Consider a linked list that contains cells with part mimber (4-byte ASCU value), unit price (binary word), and a pointer (binary word) to the next part number in che sequence. Thus each entry is 8 bytes in length. The pointer is an offset from dhe start of the list, The inked list begins at offset 0000, the second item in the series is at 0024, the third is at 0032, and so Forth: OFFSET PART 0, PRICE LOCATION OF NEXT PAKT 10. 0000 = O10R 12.50. ‘oz 0008 = «1720 08.95 016 ole = asz7_ 03.75 9c 24 = 0120S 13.80 0022 032 0e5 25.00 008.282 Defining and Processing Tables Chap. 15 page 50,132 mitta —ATSNMERT (rR) Sort names entered from terrinal CMCDEL SHALL ISTACK 64 mMEPAR LABEL BYTE Hane garaneter Lists MRENLEN DB 21 maximum dength UAMELEN CB > } no. of chars entered MEFLD TR 23 DOPE! }oname cur mB 3, 20 ESDADDR Of essa1 DR Name?” TAMEPAR «TB 30 DUP (20 DUP’ *1) ;Nome table WAMESAY DB 20 DOP(?}, 13, 29 SWAPPED TB 00 lene ROMAIN EKOC FAR Nov Ax @data piigialize DS and Mov DS,Ar MOV ES, AE BS regiatere cD CALL G1ocLR, iflear screen RE quocmRs Het cwrecr LEA DI, WAMETAB AzOLOOP. CALL Bi ORNTER Accept name from KB CHP NAMELEN, 00 Any more names? 52 B30 0, go te sort (Mr -NANECTR, 30 30 nameg entered? ge ARO ‘yaa, go to sort cALL Crostor crore entered name in table SMe -R201D0F a0 ind of inpuc cAI, O10cLR iClear screen cat Q20cmRs 2” and eet ctireor GMP -MAMECTR, 02 Jone o ne name entered? Tey Ato i yes. east CALL © DLDsoRT [sore stored names ALL Flobtse jBleplay sorted nanes pao: mov Ax, AcooH [end processing Int 21H Dioware NDP. 7 Recent name ae input RLOENTER PROC Wor AH, 40H iRequest display wor -BK.OL fie handle wor ck,06 characters EA UX, ¥ESSG2 iPeompt tr 21H mov AM, on CBA DX, RAMEPAR pRecept nama ier zit Figure 15:70 Sorting a Table of NamexLinked Lists B20: BLOENTER éxoatoR piosoRT ELORCHG 283 mov RK, cou pRequest display wou | BX.e2 iPile handle. yoy ox,02 [2 characcers BBA DX, CRLF iReturn/iine feed tweak mov BH, 06 iClear characters after nane yOV BL. NAMBLEN HGet count of chara wou cx!21 sun cay ax )Cale eemaining vength MOV NAMEPLD(BX], 20H Set name to olanK Tee Bx. rose B20 eer SNDP Store name in table: PROC ENC) WANECTR iAdd to nuaber of names con EEA $1, maMErLD mov cxyae iten words REP Movsw [ame (SI? co taple (ci) RET anne Sort names in table: PROC aU 1,40 (et up atop addres MOV ENDADDR,DI Mou swADpED 00 Set up etact LEA ST ,AMETAR [of cable Nov cx,20 Huength of compare ov BIlst sDDz,20 (ox: name For compare yOv ax, DT yov Bx.sz BREE owPER rCompara name to next, OE BAO pone exchange PALL mLOxCHG 2 exchange Moy sTLAK CMP BT, ENDADDR pind of table? ex B30 f no, continue [MP SWAPPED, 00 iany_ ewaps? Suz 0 ‘yea, eontinue RET i Boy ond of wort, Exchange table entries Roc MOY Cx, 10 sNunber of charactera ERA BE NAMESAV Nov atjox REP MVS iMove lower ften te ave Figure 18-7b Sosing s Tble of Namez204 Defining and Protessing Tables Chap, 15 wy ox,20 (Momber of charactors Mov Dr BK REP HOVSW iMove higher item Co lower REP MOVSW’ iMove save to higher item Mov "SWAPPED, ¢2 :Signal exchange made rer : Display sorted nares Pioprsp PROC MEA S1,Nam a0 ima py RaNRSRY jintt’ze mart of table wer xin Jeount for loop Boy Al som Request asap . [Request auapiay tov tas iNT inate tov Gua 120 characters » CR/LE ie ai bec aver pia thie last one? cee ae Pao Loop == i Yee, ent Piociae BND 2 Clear screen Groene PROe soy ax,o600H Nov -BHIG1K sattribute Moy Cx,00 }Pull seceen Mov DK,194FH Int 104 guocta «END Qzocurs— PROc vou sRequest eet cursor yov iPage 0 xv hLeeacxon 09:00 ner Quocuxs ENDE END = ALOMATN Figure 15-7e Sorting a Table of Names The item at offset 0016 contains 0000 as the next address, either 10 indicale the end of the. list of to make the list circular, ‘The program in Figure 15-8 uses the contents of the defined linked list, LINKLST, to locate a specified part number, in this case, 1720. The search begins with the first itera in the tabic. The logic for using CMPSB is simile to that in Figure 15-4, The procedure. BIOLINK compares the part number (1720) with each item in the table, according to the cesults of the comparison: + Low: The item is not in the table.Linked Lists AIONATN ESDP. BLOLINE PROC B20 830 340 890. RET PLOLINE ERDF END TUTLE —AASLNEIS (EXE) Use of a Linked Liat SRODEL SMALL STACK 63 :Defane stack DATA PaARTNO OB 11720" :Port number LINKLS? 0B ‘0103 pbinkea 1iat table pH 1750, 24 Be +1926" DN 0395, 16 De *ag27 OF 0375, 00 pp 0126" De 1300, 22 De "9205" Be 2500, o8 eo} FAR AK, @tata :8eb address of data Besa PP gegmere Ln DS a, aK band BS vegisrer BLOLLNK AK, Ac004 #Bnd processing aa wear, DE, LENRLST pinitialize cable adare: cK, 04 28et to compare 4 bytes er, PARTHO Hinie'se parew addze: cubse Pare# > table 530 ; ecual, exit REO i Tau, ser in table Dr, iadd CK value to of fot bt, oz HGet offset of next item DK! (On DE, LINN.ST DE, Ox. Bx,0D juaet Cable entry? mb BLO eften Found> Bso ALOWAIN Figure 158 Using a Linked List 205, + High: The procedure gets the offset from the table for ihe next item to be compared. If the offset is not zero, the comparison is repeated for the next item; if the offset is zero, the search ends without finding a match. + Equal: The item is found.286 Defining and Processing Tables Chap. 15 Amore complete program could allow a user at a keybourd to emter any part number and could display the price as an ASCII value. THE TYPE, LENGTH, AND SIZE OPERATORS The assembler supplies a number of special operators that you may find useful. For ¢x- ample, the length of a table may change from Usne to time and you may have wo modify program to account for the new definition and add routines that check for the end of the ‘able. The use of the TYPE, LENGTH, and SIZE operaters can help rodues the number of instructions that have to Fe changed. Consider this definition of a table with 12 words: RAINTL DW 22 DUP(7) — Tabla with 12 words ‘The program can use the TYPE operator to determine the definition (DW in this case), the LENGTH operator to determine the DUP factor (12), and the SIZE operatarto éeter- mine the number of bytes (12 X 2 = 24). The following examples illustrate the three operators: MOV AX, TYPE RAINTBL ur2H @ bytes) MOV BK,LENGTH RAIKTBL {BX = OUCH (22 bytes) Nov Ox,SIZE RAINTBL $CK - 018H (24 bytes) ‘You may use the values that LENGTH and SIZE retum to end a search or a sort of table, For example, if the SI register contains the incremented offset address of a search, you may test this offset using Che ST.STZE RATNTBL Chapter 26 describes the TYPE, LENGTH. and SIZE operators in detail. KEY POINTS «+ For most purposes, a fable contains related entries with the same length and data for- mat + Atuble is based on its dala format; for example, entries may be character or numeric. and typically each the ssime length. + ‘The maximum numeric value for a DB is 256 and that numeric DW and DD reverse the bytes. Also, CMP and CMPSW assume that words contsin bytes in reverse sequence. + Ifa table is subject to frequent changes, or if several programs reference the table, store it on disk. An updating program can handle changes to the table. Any program can ther load the table from disk, and the programs need not be changed, + Under direct table addressing, the program catculates the address ofa tableentry and. accesses that entry direct.Questions 287 + When searching a table, a program successively compares a data item against each entry in the table nntil it finds x match. + The XLAT instruction Facilitates ranstating data from one format to another, QUESTIONS 154, 18-2, 15:3, 15-4, 15-5, 15-6. 15-7, 15-8. 15-9. Distinguish between processing a table by direct addressing and by searching. Define a table named TEMPTBL with 365 words, inittalized to (a) zeros for binary data; (b) blanks for character data, Define three separate related tables that contain the following data: (a) ASCII iter sumbers 05, 09, 12, 19, and 23; (b) item descriptions of videctape, receivers, modems. keyboards, and diskettes; (c) item prices 12.50, 93.75, 87.45, 79.35, and 15.95. Revise Question 15-3 s0 that ali the data isin the same table, For the first item, de- fine its number and deseription on the first line and its price an the second line: for the second item, define them on lines three ard four, and so forth. Revise Figure 15-1 so that it accepts the month from the keyboard in numeric (ASCH) format. If the entry is valid (01-12), locale and display the alphabetic month; otherwise, display an error message. Allow for any number of keyhoard entries; end processing when the user replies to the prompt with only . ‘Akko, revise the table so that a 'S" sign follows each entry. Instead of transferring the description (0 ALFMON, the program should display it directly from the table. Code 4 program that allows a user‘ enter item numbers (ITEMIN) and quantities (QTYIN) from the keyboard. Use the table defined in Question 15-4, and include a search routine that uses ITEMIN to locate an item number in the table. Extract the description and price from the table, Calculate the value (quantity X price) of each sale. and display description and value on the screen. ‘Using the description table definedin Question 15-3. writc aprogram that (a) moves the contents of the table to another (empty) table; (by sorts the contents of this mew table into ascending sequence; (ch displays each description on one sow of the sctcen, Provide for scrolling the screen. Revise Figure 15-5 to reverse the process—that is, translate EBCDIC data to ASCII format, The EBCDIC characters to translate are minus sign (60H}, dec imal point (4BH), and numbers 0-9 (FOH-FOH). All other characters are 10 be translated to ASCII blank. For data, use a stting of EBCDIC hex characters containing FOPOP1F24BFSF060 (defined as OFOH, OF 1H, etc), which are to be translated in ASCII format and displayed. The hex result should be 303031322E35302D. Weitea program o provide simple encryption of data. Define an 80-byte dara area named CRYPDATA conteining any ASCII data. Arrange a translation table toDefining and Processing Fabies Chap. 15, convert the data somewhat randomly, for example, A toM,B to R, C10 X, and 50 forth. Provide forall 256 possible byte values. Arrange a second translation table that reverses (decrypts) the data. The program should perform the following ac- tions: (a) Display the original contents of CRYPDATA on a line: (b) encrypt CRYPDATA and display the encrypted data on a second line, (¢) decrypt CRYP- DATA snd display the decrypted data on athird line (which should be identical to the fist Line).i DISK STORAGE |: ORGANIZATION il Objective: To examine the basie formats for baad Hak aced ctsloeent storage. the boot record, disestory, end fie atkication table. INTRODUCTION A serious programmer has to be Familiar vidh the technical dewils of disk organization, particularly for developing utility programs that examine the contents of diskettes and hard disks. ‘This chaptet explains the concepts of tracks, sectors, and cylinders and gives the ca- pacities of some commonly used devices. Also covered is the organization of important data recorded at the beginning of a disk, inclading the boot record (which helps the system load. the operating system ftom disk into memory), the directory (which contains the name, lo- sation, and status of each file on the disk), and the file. allocation table (or FAT, which al- locates disk space for files). Where a reference to a disk or diskette is required, this text uses the general term disk, DISK CHARACTERISTICS For processing records on disks, you need some familiarity with the terms and characteris tics of disk organization. A diskette has two sides (or surfaces), whcreas « hard disk con- tains a number of two-sided disks on a spindle,230 Disk Storage |; Organizatior Chap. 16 Tracks and Sectors Each side of a diskette or hard disk contains a number of concentric racks, numbered be- ging with 00, the outermost irack, Each track is formatted into rectory of 512 bytes, here the data is stored. Both diskettes and hard disk devices are run by a coniroller that handles the place- ment of the read-write hesds on the disk surface and the transfer of data between disk and memory. There is 4 read-write head for each disk surface. For both diskette and hard disk, arequest for a read ora write causes the disk drive controller to move the read-write beads Gf necessary) to the required track. The contolfer then waits fur the required sector on the spinning surface to reach the head, at which pointthe read or write operation takes place. Tora read operation, for exampke, the controller reads each bit from the sector aa it parses the readiwrite head. Figure 16-1 illustrates these features. “There are two maie differences berween a hard disk and a diskette drive. For hard, isk, the read-wrile head rides just above the disk surface without evcr touching it, whereas for disket, the read-write head actually touches the surface. Also, a hard disk device rotates constantly, whereas a diskette device starts and stops for each read/wiite operation. Cylinders A cylinder is a vertical set of all of the tracks with the same number on each surface of a diakette or hard disk. Thus cylinder 0 is the set of all tracks numbered 0 on every side, cylin- der 1 is the set of all racks numbered 1, and so forth. For a diskette, then, cylinder ¢ eon- sists of track on side 1 and track 0 on side 2; cylinder | consists of track } on side } and track 1 on side 2; and so forth. Side number and head are che same; for example, disk head 1 accesses the data on side 1, ‘When writing a file, the controller fills all the tracks on x eylinder and then advances the read-write heads ta the next cylinder. For example, the system fills all of diskette cyltn- der O (ull the sectors on track 0. sides | and 2), and then advances to cylinder 1, side J.Disk Charactaristies 231 ‘AS Scen, a reference to disk sides (heads), tracks, and sectors is by number. Side and track numbers begin with 0, but sectors may be numbered in one of wo ways: 1. Physical secior: Sector numbers on each track begin with (, sc that the first sector oa the disk is addressed as cylinder 0, head/side 0, sector 1, the next as cylinder 0, hhead/side 0, sector 2, and 0 forth. 2, Relarive sector: Sectors may be numbered relative to the start of the disk, 90 that the fitst sector on the disk, on cylinder 0, track 0, is addressed as relative tector 0, the next one as relative sector 1, up to the last sector on the disk. Different disk operations may use one or the other methed, depending on how ac- cessing is to be performed. Disk Contolier ‘The disk controller, which is located between the processor and the disk drive, handles all communication between them. The controller accepts data from the processor and converts the data into.2 form that is usable by the device. For example, the processor may send a request for data from a specific cylinder-head-sector, The role of the coniroller is to provide the appropriate commands to move the access arm to the required cylinder, se- fect the read/write head, and accept the data from the sector when the data reaches the readl-write head. ‘While the controller is performing its work, the progessor is freed for other tasks, Un- der this approach, the controller handtes only one bit at a tine, However, the controller can also perform faster UO by bypassing the processor entirely and transferring data directly 10 and from memory. The methed of transferring a large block of data in this manner is known 438 direct memory access [DMA). To this end, the processor provides the coawller with the read or write command, the address of the UO buffer in memory, the number of sectors to transfer, and the numbers of the cylinder, head, and starting sector. With this method, the processor has ¢o wait until the DMA is complete, because only ome component ata lime can use the memory path. Clusters ‘Acluster isa group of sectors that the system treats as a unit of storage space. A cluster size is always apower of 2, such as |, 2,4, of sectors. On adisk device that supports one sec~ tor per cluster, sector and cluster are the same. A disk with two sectors per cluster would ook like this: sector | sector] [sector | sector] [sector | sector cluster cluster cluster And a disk with four sectors per cluster would look like thi sector | sector | sector [sector] [ sector | sector | sector | sector cluster cluster292 Disk Storage I: Organization Chap. 16 Abhard disk typically has four sectors per cluster. A file begins on a elusler boundary and requires a minimum of one cluster, even sf the file occupies only one of the four sec- (ors. A cluster may also overlap from one track to another. A 100-by*e file (small enough to fit on one sector) stored on disk with four sectors per cluster uses 4 512 = 2,048 bytes of storage, although only one sector wou contain data. For each file, the system stores its clusters in ascending sequence, although a file may be fragmented so that it resides. for example, in clusters 8, 9, 10, 14, 17, and 18. Disk Capacity Here are common diskette storage capacities: Tracks per Sectors Bytes Sectors side per per =Total, «per Capscity (Cylinders) Track Sector Two Sides Custer 5.25" 360KB 0 9 se 368640 5.25 1.2H8 0 ws siz 1,228,800 1 3.5" 720KB 8 3 mz 737,200 2 35" LAMB a a sz 1a7a,se0 1 B.S" 2.88MB 6 512 2,949,170 - For hard disks, capacities vary considerably by device and by partition. Useful opera- tions for determining the number of cylinders, secinrs per track, or read-write heads include INT 21H functions 1FH and 440DH with minor code 60H, both covered in Chapter #8. THE DISK SYSTEM AREA AND DATA AREA Certain sectors are reserved for the purpose of supplying information about the fles on the disk. The organization of diskenes and ard disks varies according 1o their capacity. A tard disk and some diskettes are formatted as sclf-booting—that is, they cnable processing to start when the power is turned on or when a user presses the Ctrl + Alt + Del keys. The gencral organization of a disk consists of a system area, followed by a data area that com- prises the rest of the disk System Area The system area is the first area of a disk, on the outermost track(s) beginning with side 0, track 0, sector 1, The information thal the system stores and maintains in its system area is sed to determine, for example. the starting location of each file stored on the disk. The. three components af the system area are: 1, Boot record, 2. File allocation table (FAT) 3. Directory‘The Disk System Area and Data Arco 293 ‘The system area and the data area are organized like this: Boot Systen| User record | Far |uirectory| ties | Fr1es| <—Systen areas ec Data areas ‘The following list gives the organization of various diskette devices, showing the starting and ending sector numbers for the boot record, FAF, and directory. Sectors are iden tified by relative sector number, whete relative sector 0 is eylinder 0, track 0, sector 1, the fist sector on the device (explained earlier in the section “Cylinders"): Device Soot FAT Directory Sectors/Cluster 5.25" 36008 0 1-4 eal 2 5." 1.8 0 424 as2e 1 BS ROG 86 713 2 29" Lame 0 eas saz 1 For hard disk, the locations of the bootrecordand the FAT arc usually the same as for diskente, whereas the size of the FAT and the location of the directory vary by device. A formatted disketts-contains the following information in terms of beginning physi- cal and relative sectors: 720K (9 sectors/track) LAAMG (18 eectare/erack) Relative Ratatwe Efe |_ Gy. side sector sector | Cyl. Side sector Sector Poot record | oO 0) od 0 oO 1 ° FATT o 0 2 1 o 0 2 1 FaTz o 0 4 4 eo e¢ uw Directory | 0 0 8 7 o 2 2 as Data area eo 1 6 1 eo 1 68 Data files on 720K disketes begin on cylinder 0, side 1, sectors 6 through 9. The system stores records next on cylinder 1, side 0, thea cylinder 1, side 1, then cylinder 2. side O, and so forth, This feature of filling data on opposite sides (in the same cylinder) before pro- ceeding to the next cylinder reduces the motion of the disk head and is the mettiod used on both diskettes and hard disks. Data Area ‘The data area for a bootable disk or diskette begins with two system files aamed [0.8YS and MSDOS,SYS (for MS-DOS) or IBMBIO.COM and IBMDOS.COM (fur IBM PC- DOS). When you use FORMAT /S to format a disk, DOS copies its systet files onto the first sectors of the data area, User files either iramediatcly follow the system files or. if there are no system files, begin at the star of she data area. ‘The next sections explain the boot record, directory, and FAT:294 THE BOOT RECORD Disk Storage |: Organization Chap. 16 ‘The boot record contains the instructions that load (or “buot”) the system files [0.8 YS, MS- DOS.SYS. and COMMAND.COM (if present) fiom disk into memory. All formatted disks contain a boot record even if the sysiem files aronot stored on it. The boot record contains the following information, in order of offset address: OH 03H oBH oDH OER 10H nH 13H 15H 16H 18K 1AH cH EH 20H 24H 25H 26H 2H 2BH 36H Short or far jump to the boottrap routine at offset TEH or 36H in the boot record ‘Moanufocturer’s name and DOS version number wher boot was created Bytes per sector, usually 200H (512) Seciors per cluster (1, 2, 4, or 8) Reserved sectors Nunber of copies of the FAT (1 or 2) Number of root directory entries Totel aumber of sectors if valume is less than 32 MB Media descriptor byte (same as first byte of the PAT, described lates) Number of sectors for the FAT Number of sectors per track Number of read-write heads (sides or surfaces) Number of hidden sectors Boastrap leader routine for DOS versions through 3.3 Total number of sectors if volume is greater than 32 MB Physical drive number (for diskette, A = 0: for hard disk, 80H = drive C. etc.) Reserved by the system Extended boot sector signature (contains 29H) Volume ID ‘Volume label Reserved by the system 3EH-IFFH As of DOS 4.0, the bootstrap loader begins here DOS 40 extended the beot record with additional fields from 20H through IFFH. The original boot record is 20H (32) bytes, whereas the extended version is 20H (512) bytes. THE DIRECTORY All files on a disk begin om a cluster boundary, which is dhe [rst sector of the duster. For each file, DOS creates a 32-byte (20H) directory entry that describes the name of the file,The Directory 296 the date it was created, its size, and the location of its starting cluster. Directory entries have the Following format: BYTE 00H-07H O8H-04H_ OBH OCH-ISEE 1GH-17H 18H-19H 1AH-LBH ICH-IFHL PURPOSE Filename, as defined in the program that created the file, ‘The first byte can also indicate the file status: + 00H File has never been vied + OSH Fist character of filename is actually ESH + 2BH nu is fora subdirectory + ESH File has been deleted Fitenome extension, sich as EXE or ASM File attribute, defining the type of ik (note that a file may have more than one attribute) + 00H Normal file + 01H File that can only be read (read-only) + 02H Hidden file, not displayed by a directory search + G4H System file. not displayed by a directory search + 08H Volume lahe! Gf this is a votume label ecard. the label itself is in the filename and extension fields) + 10H Subdirectory + 20H Archive file, which indicates whether the file was rewritten since the last update. {Asan example, code 07H would mean a system file (04H) that is read only (O1H) and hidien (O2H).} Reserved by the system. Time of day whea the file was created or lust updated; stored as 16 bits in binary format as Ihhhhheamamsmumisss Dare when the file was created or last updated, stored as 16 bie: in binary formatas tyyyyyyymimmmdddddh, The year can be 000-119 (with 1980 as the starting point}, the month can be O1-12, and the day can be 01-31. Starting clusier of the file. The number ie relative to the last two sectors of the directory. Where there are no system files, the first data file begins at relative cluster 002. The actual side, track, and cluster depend on disk capacity. A zero entry means that the file has no space allocated to it. Sive of the file in bytes. When you create a file, the system calculates ard stores ils size in this field. For nometic fields that exceed one byte in the directory, data is stored with the bytes in reverse sequence.296 Disk Storage |: Organization Chap. 16 THE FILE ALLOCATION TABLE The purpose of thc PAT is to allocate disk saace for files. The FAT contains an entry for cach cluster on the disk, When you create a new file or revise an existing file, the system revises the associated FAT entries according to the location of the file on disk. The FAT be- Bins at sector 2, immediately following the buot record. On a disk where a cluster consists of four sectors, the same number of FAT entries can reference four times as much data as disks where a cluster consists of one sector. Consequently, the use of clusters with multiple sectors reduces the number of entries in the FAT'and enables the system to address larger disk storage space The vriginal designers provided for two copies of the FAT (FAT! and FAT2), pre- sumably because FAT2 could be used if FAT! became corrupted. However, although FAT2 is still maintained, is use has never been implemented. The earlier section “Disk System ‘Area and Data Area” includes both FAT! and FAT2 in the FAT storage requirements, All other discussions in this book concern FAT! First Entry in the FAT ‘The first byte of the FAT, the media descriptor, indicates the type of device (see also byte 15H in the boot record), including the following: FOH 3.5", two-sided, 18 sectorstrack (1.44MB) and 3.5". two-sided, 30 sectors/track (2.88MB) FRH Hard disk (including RAM disk) FOH 3.5", two-sided, 9 sectors/track (720KB) and 5.25", two-sided, 15 sectors/track (1.2MB} FCH 5.25", one-sided, 9 sectors/track (I80KB) FDA 5.25", ¢worsided, 9 sectorsitrack (360K B) FFH 5.25". two-sided, 8 sectorsitrack (320KB) Note that FOH and F9H each identify two different disk formats. Second Entry in the FAT ‘The second FAT eanry contains FFFFH fordiskette FATs that support 12-bit PAT entries and EFEYEFH for heed disks that support 16-bit PAT entries. The first two PAT entries look Sike this: ram disteee (OL # >] | tard ask sPelvl*L 1b dL | Asalready described, the first field on a disk is the boot record, followed by the FAT and then the dicectory. The data area is next. The entice picture is as follows:The File Allocation Table 237 luster O[eluctor 1] cluster 2[etuseer 3]... [etuseer a directory area—5|¢———____-data area > ‘You would expect thet the data area would be the stating point for clusters; however, the first (wo cluster numbers (0 and 1) point to the directory, so thal the data area for stored data files begins with cluster number 2. The reason for this odd state of affairs will soon be made clest, Pointer Entrles in the FAT Following the first two FAT entries are pointer entries that telate to every cluster in the dala asva. The directory (at [AH-1BH) contains the location of the first cluster fora file, and the FAT contains a chain of pointer entries for each succeeding cluster ‘The entry length for diskettes is 3 hex digits (1 1/2 bytes, or 12 bits), but for hard disk it is d hex digits (2 bytes, or 16 bits). Each FAT pointer entry indicates the use of a partic- tolar cluster according tn the following formal: s2ers | 1s8mTs EXPLANATION 000 0000 | Refereaced cluster is currently unused pon. anna — | Relative number of next cluster for a file FFO-FF6{FFFO-FYT6 | Resesved cluster FE7 FFF? — | Unusable (bad track) FFF FEEF | Last cluster of a file The first wo entries for 2 |44MB diskette (a 12-bit FAT] look like this: FAT entry: orl]... --E-]---[-- if. Relative ciuste o 1 2s 4 5) 6 ‘The term “telative cluster means the cluster to which the FAT entry points. In a sense, the first (wo BAT entries (© and 1) point to the last two clusers in the directory, which have been assigned as the start of clusters; the directory indicates the size and slart- ing cluster for files. ‘The ditectory contains the starting cluster number for each file and a chain of FAT pointer entries that indicate the location of the next cluster, if any, at which the file.contin- ues. A pointer entry containing (FIFFFH indicates the last cluster for the file, Sampie FAT Entries ‘The following examples should help clarify ihe FAT structure, Suppose a diskette contains only one file, named CUSTOMER.FIL, thatis fully stored on clusters 2, 3, and 4. The di- rectory enury for this file contains the filename CUSTOMER, the extension FIL, OOH to298 Disk Storage |: Organization Chap. 16 indicate a normal Ge, che creation date, 0002H for the location of dhe firs! relative cluster of the file, and an catry for the size of the file in bytes. The 12-bit FAT enty would appear 4s follows, except that peirs of bytes would be reversed: FAT entry: For [rrr [003] on Jrrr[-..[...[--- Relative cluster: o 1 203 8 5 6 16. end For the first wo FAT enures, FO indicates a two-sided nine-sectored (| .44MB) diskette, fol- jowed by FFFFH. For a program to read CUSTOMER-FIL sequentially from disk into memory, the system takes the following steps: + For the first cluster, searches the disk directory for the filename CUSTOMER and ex- tension FIL,extracts from the directory the location of the first relative cluster (2) of the file, and delivers its contents (data from the sectors) to the program inmain memory. + For the next cluster, accésscs the FAT painter entry that represents relative cluster 2. From the diagram, this ontry contains 003, meaning that the file continues on relative cluster 3. The system detivers the contents of this cluster to the program. + For the last cluster, accesses the FAT pointer entry thet represents relative cluster 3. ‘This cntry contains O04, meaning that the file continues on relative cluster 4. The sys~ tem delivers the contents of this cluster to the program ‘The FAT entry for relative cluster 4 contains FFFH to indicate that 1o more clusters. are allccated for the file, The system has now delivered all the file's data, from clusters 2. 3, and 4, ‘We've just seen how KAT entries work in principle: now let’s see how they work in terms of reversed-byte sequence, where a litle more ingenuity is required, Handling 12-Bit FAT Entries In Reversed-Byte Sequence: Following is the same example of FAT cniries for CUSTOMER FIL just covered, but now with pointer entries in reversed-byte sequence. The 12-bit FAT for this file looks like this: sar entry: wore [os [wo [ro Tre J Relative cluster: oor 2 3 4 5 But what's needled now w decfpher the entriesis to represent them according to relative byte rather than cluster: Fat ney: [Le [os bolo Po Ter) Relative byte: o 1 2 3 4 $ € ? Here are the steps used to access the clusters: + To process the first FAT entry, multiply 2 (the file's first cluster as recorded in the di- rectory) by 1.5 (the lengih of FAT entries} to get 3, (For programming, multiply by 3 and shift right one bit ) Access the word at bytes 3and 4 in dhe FAT. These bytes con-Exercise: Examining the FAT 299 tain 03 40. which, when reversed, are 4003, Because cluster 2 was an even number, use the last three digits, so that 003 is the second cluster for the file. + Forthe third cluster, multiply cluster number 3 by 1.5 to get 4, AccessFAT bytes 4 and S. These contain 40 00, which, when reverted, are 0040. Because cluster 3 was an odd nutmber, use the first three digits, so that 00d is the third cluster for the file. + For the fourth cluster, multiply cluster 4 by 1.5 to get 6. Access PAT bytes 6 and 7. ‘These contain FF OF, which, when reversed, are OFFF. Because cluster 4 was an even number, use the last three digits, FFF, which mean that this is the last entry. (Whew!) Handing 16-Bi FAT Entries ‘As mentioned earlier, following the media descriptor for hard disk is FFFFFFH. FAT olnter eniries are 16 bits loag and begin with bytes 3 and 4, which represent cluster 2. The directory entry provides the starting clusters fos files, and pointer entry FFEFH indicates em/-of-file. Determining the cluster number from cach FAT entry is simple, although the’ bytes in each entry are in reverse sequence. ‘Asan example of 16-bit FAT entries, supposethe only file on a particular hard disk ‘occupies four clusters (at 4 sectors pet cluster, or 16 sectors in all}. According to the di- rectory, the file starts at cluster 2. Each FAT pointer entry is a fall word, so that revers- ing the bytes involves only the one entry. Here is the FAT, with pointer entries in reversed-byte sequence: ] Relative clusters ° 2 2 3 4 s ‘The FAT entry for relative cluster 2, 0300, reverses as 0003 for the next cluster. The FAT entry for relative chaster 3, 0400. reverses a8 0004 for the next cluster. Continue with the chin of remaining entries in this fashion through to the entry for cluster number 5. your program has to determine the type of disk that is installed, it can check the me- dia descriptor in the boot sector directiy or, preferably, could use INT 21H function 1BH or 1CH. Fat encry: rarr | reer [ 0300 | 0400 | os00 | frrr |. - EXERCISE: EXAMINING THE FAT Li's use DEBUG ww examine the FAT for 1 disk, For this exercise, you'll need two for- matted blank 3.5" diskettes with 720K and 1.44MB capacities, without the system files copied on them. Copy two files onto each disk. The first file should be larger than 512 bytes and smaller than 1,024 bytes, to fit onto two sectors; AD4ASMI.ASM is suggested. The second file should be larger than 1,536 bytes and smaller than 2,048 bytes to fit onto four sector; ALODRVID.ASM is suggested, You'll see that the FATS for the two diskettes arc similar. but not identical300 Disk Storage i Organization Chap. 16 Procediure forthe 720K Disk First insert the 720K diskene in drive A (or B if necessary). Load DEBUG and key in che (load) command (more fully explained in Appendix E): L 100 6 8-20 100H is the starting offset in DEBUG's segment where the data is to be read in. «+ The first © means use drive A (or 1 for drive B). + The second 0 means reod data beginning, with relative sector 0. + 20 means read 20H (32) sectors. ‘You can now examine the boot record, disectocy, and FAT for this diskewe. To start the display, key in the command D 100, Because the file is stored beginning at offset 100H, you can locate the records this way: 1, The boot record is atthe start, 2t 100H. 2. The. FAT follows the boot sector: 100H + 200H (1 sector 20H or 512 bytes) = 20H. 3. The directory follows the FAT: 300H + {6 sectors > 200H) = OOH. The boot record. Some of the fields on the boot record are: + Segment offset 103H shows the manufacturer's name and D908 version when the FAT ‘was created. + Offset 10BH shows the number of bytes per sector (where 0002H reverses as 0200H, or 512 bytes). + Offsct L1SH is the media descriptor, P9H for this diskette. ‘Check out the other fields. The Directory. For the directory, key in the command D PQQ, where: + Offsct POOH coniains the filename for the first file, AMASMI.ASM. + Offset FAH gives the starting cluster number (0200, or 0002) for this fil + Offset F1CH-FIFH gives the size of the file in bytes. + Offset F20H begins the entry for the second file. A100RVID.ASM. Note that FAH shows its starting cluster as 0300, of 0003, The FAT. For the FAT, key in the command D 300, which should display: FAT entry: ell ele lar lo |e] Relative byte: oa 2 3 4 5 6 7Exerci : Examining the FAT gor + PY is the media descriptor. + FF FF at bytes } and 2s the content of the second field. The pointer enuries beginning at byte 3 can be calculated like this: + For the first file, multiply 2 (its first cluster according to the directory) by 1.5 © get 3, Access offset bytes 3 and 4 in the FAT, which contain FF 4F, and reverse the bytes to get 4FFP. Because duster 2 was an even number, use the last three digits, FFP, which tell you that theae are no mote clusters for this fle. For the second file, muitiply 3 (its first cluster according 10 the directory) by 1.5.10 get 4. Access offset byes 4 and 5 in the FAT, which contain 4F 00, and reverse the byiesto get O04F, Because cluster3 was an odd number, use the frst three digits. 004, which identify the nextcluster in the series. Muiply cluster 4 by 1.5 to gst 6, Access offset bytos 6 und 7 in the FAT, which contain FFOF, and reverse the bytes o get OFFF. Because cluster 4 was an even number, use the frst three digits, FFF, which indicate the end of the data. Procedute forthe 1.44MB Disk Now insert the 1.44MB diskette in drive A, and enter the DEBLIG command L100 00 30. (Load 30H sectors because there's more FAT on 1.44N(B diskettes.} The boot record begins at 100H and the FAT follows at 10UH + 200H = 30H. Because the RAT for a 1.4MB diskette is 18 sectors long, the directory is at 3008 + (12H X 200H) = 2700H. The boot record. Use D 100 10 display the boot record. Note that the media de~ scriptor byte at | L9H is FD and the number of sectors per chuster (at 1ODH) is t The directory. For the directory, key in the command D 2700. The fist file name is at 2700H and its starting cluster (2) is at 271A. The second file name is at 27203 and is starting cluster (4) is at 2734, CThe starting clustet for the second file on the 720K diskette ‘was 3 because that format has two sectors percluster, whereas 2 1.44MB diskette has one sector per cluster.) The FAT. For the FAT, enter the command D 300, which should display: FAT entry ro] re | re | os | eo} FF | os | 60 | 00 | 7 | ro | FF y eo Relative byte: 2 3 4 5 6 7 6 8 10 1 According to the directory, she first file stats at chustet 2, 80 multiply 2 by 1.5 toget rel- stive byte 3. Bytes 3 and 4 contain 03 FD, which reverse as FUO3. Because chuster 2 was an even numuber, use the last three digsts, O03, Chuster 3 X 1.518 4; relative bytes 4 anc 5 concain FO FR, which reverse ¢s FFFO. Becalise cluster 3 was an odd number, use ihe frst three digits, FEE, which indicate the end of the file. We now know that the file cesides on clusters 2 and 3, ‘Use the same technique to trace through the chain for the second file, which begins with cluster 4, or relative byte 6.wz Disk Storage: Organization Chap. 16 INT21H provides some supporting services for programs wo accessinformation about the directory and the FAT, including functions 47H (Get Current Directory) and 1BH and 1CH (Get FAT Information), described in Chapier 18. PROCESSING FILES ON DISK Data on disk is stored in the form of afile, just at you have stored your programs. Although thereis no restriction on the kind of data that youmay keepin afile, a typical user file would consist of records for customers, inventory supplies, or name-and-address lists. Each record contains inforenation atouta particular customer or inventory item. Withia a fle, all records ate usually (but not necessarily) the same length and format, A record contains one or more fields that provide information about the record, Records for a customer file, for example, could contain such fields as cusiomer number, customer same, and amount owing, The records could be in asconding sequence by customer namber, a8 follows: “1 rane [ant] [#2 [ mane [ane] | 3 | name [ame # [name [ant Processing for files on hard disk is similar to that for diskette: for both, you have to supply a path name to access files in subdirectories. Intemupt Services for Disk Input/Output Acqumiber of special interrupt services support disk inpuoutput, A program that writes (or creaies) a file Erst causes the system: to gererale an eatry for it in the directory. Whea all the file’s records have been written, the program clases the file so that the system can com: pletc the directory entry for the size of the file. ‘A program that is to read « file first epens the file 10 ensure that it exists. Once the program has read ull the records, it should close the file, making it available to other pro- grams. Because of the directory’s design, you may process records in a disk file either se- quertially (one record after another, successively) or randomly (records retrieved as requested, throughout the file). “The highest level of disk processing is via INT 21H, which supports disk accessing ‘by means of a directory and “blocking” and “unblocking” of records. This methed performs some preliminary processing before linking to BIOS. Chapter 17 covers the use of DOS op- erations to write and read disk files, and Chapter 18 discusses various operations that sup- port directories and disk files. ‘The lowest level of disk processing is via BIOS interrupt 131, which involves direct addressing of track and sector numbers, and is covered in Chapter 19. KEY POINTS + Bach side of'a diskette or hard disk contains a sumber of concentnc tracks, starting with track number 00. Each track is formatted into sectors of 512 bytes, starting with sector mamber 1 + Acylinder is the set ofall tracks with the same number on each side.Questions 303 + A sector may he referenced hy cylinder-head of by relative sector number, + Acluster isa group of sectors that the system treats ay. unit of storage space. A clus- ter size is always 2 power of 2, suchas 1, 2, 4, or 8 sectors. A file begins on a cluster boundary and requires @ minimum of one cluster, + Regardless of size, ali files begin on a chusier boundary. + The boot record contains the instructions that oad (or “boot"} the system. files TOSYSSYS, MSDOS.COM, and COMMAND.COM from disk into memory. + The directory contains an entry for cach Fike on a disk and indicates the filename, ex- tension. file attribute, time, date, starting sector, and file size, ‘+ The purpose of the file allocation table (FAT) is 10 allocate disk space for (es. The FAT begias at secor 2 immediately following the boot record and contaius one entry for each cluster for cach file in the directory. QUESTIONS 16-1, What is the length in bytes of a standard sector? 16-2. What is acylinger? 16. 16- . What is the purpose of a disk controller? J. (a What is a cluster? (b) What is its purpose? (¢) What is the disk space (in terms of bytes) used for each of cluster sizes 1,2, 4, and 8? 16-8. Show how to calculate the capacity of a diskette, based on the number of eytinders, scciors per track, and bytes per sector, for (a) a 3.5", 1.44MB diskette and (by a 5.25", 360KB diskette, 16-6, What are the thice parts of the disk system area” 16-7. (a) Whats the purpose of the bootrecond? (b) Where is it located? (¢} How can you vse it ( determine the number of sectors per tack? 16-8, How does the directory indicate a deleted file? 16-9, What is the indication in the directory tor (a) a normal file; (b} a read-only file: (¢) a system file? 16-10, What is the additional effect on a diskette or hard disk when you use FORMAT /S to formar? 16-IL, Considera file with a size of 3.165 (decimal) bytes. (a) Where does the system store the size? {b) What is the size in hexadecimal format’? Show the value as the system stores it 16-12. Where and how does the FAT indicate that the device on which it resides is on (a) hard disk; (by a 3.5", 1 44MB diskette; (c) a 5.25", 300KB diskette? 16-13, How docs the FAT indicate 12-bit entries and 16-bit entries”?DISK STORAGE II: WRITING AND READING FILES INTRODUCTION ‘The original services for processing disk files used a method catled file contra blocks (FCRs). This method, although still supported by DOS, can address drives and filenames, at not subdirectories. Succeeding DOS versions introduced anumber of extended services that zre simpler and more capabie than their original counlerperts. Some of these operations involve the use of an ASCIIZ string to initially identify « drive, path, and filenatie; a file handle for subsequent accessing of the file; and special return codes to identify errors. Although no new asiembly language instructions are required, this chapter introduces anumber of INT 21H services for processing disk files. Here they are, arranged by category: OPERATIONS USTMG FILE HAMDLES OPERATIONS USING FORS 30K Craze Fila FH Open File 30K Gpen Ft1e JOH Close tle SEH Close file MH Read record 3°H Read record 25H Write record 40H Write record 16H Create file 42H Move file poiater ZIM Read record randomly OTHER SERVICES. 22H Write record randosly INT 25H Absolute read 2M Read black eandonly IAT 26H Absolute write 28H Weite block randorrlyError Return Codes 305 ‘The chapter covers the services for writing and reading disk files, and Chapter 18 cov- er the various support services required for handling disk drives, directories, and files. As a reminder, the term cluster denotes a group of one or more sectors of dats, depending on the device, ASCII STRINGS ‘When using many of the extonded services for disk procossing, you first provide the sys- tem with the address of an ASCHZ siring containing the filespec: the location of the disk ative, directory path, and filename (all optional ané withm apostrophes), followed by a byte of hex zeros; thus the name ASCIIZ string. The maximum length of the string is |28 bytes. ‘The following example defines a drive and filename: PATROL OB *E:\AIZRANRD.ASM” , 00H ‘This example defines a drive, subJiroctory, and filename: PATHNAMZ DB.“ F:\UTALTTY\AL7RANRD. EXE” ,OOH The backslash, which may also be a forward slash, acts a3 a path separator. A byte of zeros \erminates the string, For interrupts that require an ASCIIZ, string, load its offset address in the DX register, for example, as LEA DX.PATHNAMI FILE HANDLES As discussed in Chapter 9, you may use file handles disectly for certain standard devices: 00 = input.01 = output. 02 = error output, 03 = auxihiary device, and 04 = prister. Other VO services involve the use of a file handle for operations that access files: for these, you have to requesi the file handle number from the systerr. A disk file must first be opened; ‘unlike transferring data trom the keyboard or to the screen, the systema has to addsess disk files through its directory and FAT entries and must update hese entries, During program execution, each file referenced must be assigned its ewa unique file handle, The system delivers a file handle when you open « file for input or create a file for output, The operations involve the vse of an ASCIZ string and INT 21H fanctien 3CH or SDE. The file handle is a unique 1-word number rsturnediin the AX that you save in a word data item and use for all subsequent requests to access the file. Typically. the first file handle retumed is 05, the second is 06, and so fonh. ‘The PSP contains a default file handle table that provides for 20 bandles (thus the rominal limit for opened files), but you canuse INT 21 H function 67H 10 increase the Lint, as explained in Chapter 24, ERROR RETURN CODES The file handle operations for disk deliver a completion status via the carry flag and the AX register. A successful operation clears the carry flag te zero and performs other appropriate206, Disk Storage I: Writing and Reading Files Crap. 17 Functions, An unsuecess/ul operation sets the carry Mlag to 1 and returns an error code in the AX. depending on the operation. Figure 17-1 lists error codes O1~36; other codes are con- cemed with networking 0 Invalia tureeion number 20 Unknown anit 22 Pile noe found 2], Drive not ready 03 Path net found 22, Unlenqwn sommand 04 Too rany files open 23 CRC cate error DS Access tensed 24 aa request structure lengeh 06 Invalid Fandle 25 Seck error 0? Memory ‘control block deatroyad 26 Unknewa media type 08 Insufficient vemory 27 Sector not found 09 Invalid menory Block addresa 26 Printer out Of paper 10 Tnalid envizcrment 29 Write fault LL Thalia formar 40. Read tauie 12 Invalid access code 31 General failuze 13 Invaria cata 42 Sharing violation 15 Thwalid erive specified 43° Lock violation Le Attenpe to rerove directory 34 Invalid disk charge 15 Net same device a5 Fea unavadjable No were cles 36 Sharing buffer overflow Write-protected disk Figure 17-1 Major Dish Eno Return Coaee If these errors aren’: enough, yent ean also use INT 59H for additional information abou errors. (See Chanter 18.) ‘The Tollowing sections cover the requirements for creating, writing, and clowing disk files, FILE POINTERS: "The system maintains a separate file pointer for each file that.» program is processing. The create and open wperations initialize the value of the file pointer lo zet9, the file's starting, bo cation, The file pointer subsequently accounts for the current offset location within the file, Each read/write operation causes the system to increment the file pointer by the mum= ber of bytes iransferred. The file pointer then points to the location of the next record 10 be ancessed. File pointers facititate both sequential and random processing, For random ac- cessing of a reoord, you can use INT 21H function 42H (covered in a later section) 10 set the fle pointer to any location ta dhe Ble, USING FILE HANDLES TO CREATE DISK FILES ‘The procedure for writing a disk fie is the following: 1, Use an ASCUZ string 10 get a file handle from the system: 2, Use INT 21H function 3CH to create the tile, 3. Use INT 21H function 40H to write revordsin the file: 4, Ar ehe end, use INT 21H function 3LH to close the file.Using File Handles te Create Disk Files 307 INT 21H Function 3CH: Create File For creating a new file ot overwriting an ald file with the same name, first use INT 211] function 3CH. Load the CX with the required file attribute (covered in Chupter 16) and the DX with the address of the ASCHZ string (the location on disk of the new file), Here’s sn ‘example that creates a normal file on drive B with attribute O: PATHNAML DB 'E:\ACCOUNTS.FIL* 00H FLLNANDL OW ? sie handle Woy aH, 308 sRequast create file Mav Cx, 00 Normal attribute LEA OX, PaTHNAM — ASCTEZ surlag INT 2H sal jntercupt service a¢ errar sSpecial action if error MOY FILHAWOLAL Save handle 19 word For a valid operation. the system creates a directory entry with the given attribute, clears the carry flag, and sets the bande for the file in the AX, Use this file handle for all subsequent accesses of the file. The named fe is opened with its file pointer se: to zere and is now available for writing. (f2 fite with the given name alrcady exists in the path, the op- eration sets up a zero length [or overwriting the new file on the old one. For error conditions, the operation sets the carry flag and retorns a code in the AX: 03, 04, of 05 (see Figure 17-1). Code OS means that either the directory is full or the xefer- enced filename has the read-only attnbute, Be sure to check the carry flag first. For exam- ple, creating a file probably delivers handle OS to the AX. which could easily be confused with error code 05. access denied, Related services for creating a file are INT 21H func- tions SAH and SBH. covered in Chapter 18, INT 21H Function 40H: Write Record For writing vevords on disk, use INT 21H function 40H, Load the BX with the stored file handle, the CX with the number of bytes to write, and the DX with the address of the out- put area. The following exomple uses the file handle fiom the preceding create operation to write a 256-byie record from OUTAREA: FILMANOL OW 7 File handle QUTAREA DB 756 DUPC* sourput area MOV AH 40H Request write record Mov ax.FTLAANDL File handle Mov 0x, 256 Record Tengen LEA OX,QUTAREA Address of output area IMT 224 iCal interrupe service 3 error? sSpecial action if eeror OP Ax, 256 1 bytes written? INE error not, error308 Disk Storage Il: Writing and Reading Files Chap. 17 ‘A valid operation writes the record onto disk, increments the file pointer, clears the cerry flag, and sets the AX to the number of bytes actually written. A full disk may cause the number written 1o differ from the number requested but, beceuse the system does not re- port this condition as an emor, you have to test the value returned in the AX. An invalid operation sets the catry flag and retumns to the AX error code 05 (access denied) or 06 (in- valid handle). INT 21H Function 3EH: Close File ‘When you have finished writing a file, you have to close it. Load the file handle in the BX. and use INT 21H function 3BH: Mov An, 3EH iRequest close File MOY EX,FILWANOL FA T2 handle IwT 21H 3G] interrupt service A successful close operation writes any remaining records still in the memory buffer and upelates the FAT and the directory with the date and file size, Am unsuecessfuluperation sets the carry flag und returns in the AX the only possible error code, 06 {invalid handle). Program: Creating a Disk File ‘The program in Figure 17-2 creates ¢ file from names that « user keys in. Its major proce- dures are the following: + AI0MAIN Calls BIXCREAT, CIOPROC and, if at the end of input, calis FIOCLSE. BOCREAT Uses INT 21H function 3CH to create the file and saves the handle in» data item named HANDLE. « CIUPROC Accepts input from the keyboard and clears positions from the end of the name 10 the end of the inpu: area DIOSCRL Scrolls the screen when near the botwom row. EIOWRIT Uses INT 21H function 40H to wnte records. FIOCLSE At the end of processing, uses INT 21H function 3EH to close tre file in ‘order to create a proper directory entry. ‘The input area is 30 bytes, followed by 2 bytes for the Enter (ODH) and Line Feed (GAH) characters, for 32 bytes in all. The. program writes the 32 bytes as a fixed-length record. You could amit the Enter/Line Feed characters, but you swuld include them if you want to sort the records in the file, because the DOS SORT program requires these characters to indicate the end of records, For this example, the command to sort the recotds from NAMEFILE DAF into ascending sequence in NAMEPILE.SRT could be SORT F:NAMEFILE. SRT (SORT processes from NAMEFILE DAT to NAMEFILE SRT}Using File Handles to Create Disk Filas sos page 60,232 crme ALICaPIL (Exe) create disk file of names MODEL MALI. STACK G4 DATA MANEPAR TAREL BYTE sParaneter ligt: wAREEN a 30 imascimum Lengeh UANELEN ie : Pactual Jengeh MAMEREC Ca 30 DUP(! '), ODH, UAH Entered name, + CR/LF for writing BRRCDE TB 20 ferror indicator HANDLE Ew ? }Pile bandte YATUNAM 3 See:\NAMEATLE DAT", € PROMPT a tNane? Rom 5 on opmss DB Plee open excex 449", oD, ORNL wRIMSG DB cree wise exror #1", CDM; vA leone mov) AK adata jieietalize daca Mov DS! ax segnent Mov ES.AX Mov AX, 06008 ALL guosce, iolear wereen CAEL Gz0=uRS Het cursor CARe | BIOUREAT fereate file GHP ERRODE, 09 foreate error? a AZOLOD } yes, continee op AO + ono,’ exit cau, © croeRoc He NAMELZN, uD #804 of inpuc? SNE AZ0LOOP. Fone, continue eaLL © F10ctsE i yes, close, 90 mov AK, C001 Bnd processing ir zn ALOMAIN, ENDP : Create disk file: RLOCREAT PROC NEAR mov RH ac PRequast creave mov ex,00 Paemmal aR DX, PATHNAM. IP 2 ae B20 pixrort tov HANDLE, AK pono, gave handle B20 2 yea, TER DX, OPNMSS f. @rror message can. -xlounre BLOCREAT ENDP Figure 17-29, Using a Halle w Create + Fike Note two points. (1) The Bater/Line Foed characters are included efter each record only to facilitate the sort and could otharwise be omitted. (2) Bach record could be in vari- able-length format, only up to the end of the name; this would involve some extra pro- gramming, as you'll see later.210 cropRoe 39: croproc DioscRL. bio: peo: DipsceL HLowRIT F20: exowRET Disk Storage It Writing end Resding Files Chap. 17 Accept input pRoc WRAP mov A, 40H eques: display mov BK,OL pHandle. you exyo6 Tiangth of prompt EEK BR) FROMPT iBteplay prowee Int zn Woy aw, ont PRequeat input. EEA OX, WAMEPAR Hiecepe name ter 2am CHE NAMELEN, 00 ila there a name? 32 c80. ma. exit wv AL, 208 pPlank for acoring S38 sya MOV CL, HAMELEN slepgen, fea BY CHAMEREC xDD DY, CK pAdgraae + Length wee Gk ‘alculate remaining ADD ck, 30 ‘length REP sToSR bse to blank GALL ELOWRIT Hieite dine record CALL DROScRL [heck for 9=selL ‘BNP check for serel} proc WEA cHp | ROW,18 sRottem of screen? GAR (B20 b yee, bypase inc | FOW bBo, aaa to 10H Me 80 Moy AK,«g01# }Seroll one row CALL QLOSCR. Care q2ocums iReset cursor ad ENUP Write disk recora: eRce NEAR mOV AAO Request write wow BX HANDLE wor x37 530 for name, 2 for CR/LP LEA DX, WAMERES iy au Juc B20 puta write? LRA OX, WRIMS. 2 Ro, CALL LEER peal: error routine nov NANCIE, 00 RED exDE Fignre 17-25 Using a Handle to Creste aleUsing File Handles to Read Disk Files Fiockee — FRoC iv ALL roy soy ast mer FiocLss eNrP Muesce ROC Ov Ov roy INT ker piosce —ENDP ReocuRs ROC nov, g2ocrs RIGERR nov ter sow REL ENP xD 1ORPR Clove disk file WEAR MAMEREC, AIL ELOWRET HL 3B BX, HANDLE Bevel) screen NEA BH, 1B ex; 0000 Bx, era 08 Set cureor: NEAR AH, O26 BH,00 DR,ROW Duzoe 10H an i0et BOF mark iRequest. close PAK eat on entry ySer yellow on bine sscroll sRequaet Feotuna, Display disk error sessage BER Al 208, Bx, 01 cx,2t aa BRRCDE, 02 aALoMaTN BX coneaine DP addeeso of mest juandie fox ecreen Hangth joe oxrer code Figure 17-25 Using « Handle to Create a File USING FILE HANDLES TO READ DISK FILES: ‘This section covers the tequizements for opening and reading disk files using file handles. ‘The procedure for reading 4 disk file is the following: 1, Use an ASCIIZ. string to gets file handle from the system, 2. Use INT 21H funetion 3DH to oper the file. 3. Use INT 21H fonction 3FH to readrecords from the file 4 At the end, use INT 21H function 3EH to close the file, INT 21H Function 3DH: Open Fite 1S your program is to read a file, first use INT 21H function 3DH w openiit. This operation checks that a fileby the given name actually exists. Load the DX with the address of the re- quired ASCIIZ. string, and set the AL with an access code:a2 Disk Storage I: Writing and Reading Files Chap. 17 srs, REQUEST YT = Reserved 001 = write only | 4-6 Sharing mode ' 020 - read/write | 7 Taheritance flag Before reading 2 file, be sure to use function 3DH to open the file. nat function 3CH to create it, The following example opens. file for reading, FILHANDZ OW 7 :FiTe handle fOv A, 304 sRequest open fiT@ wow AL.oo ead only LEA DX PATHNL —SASCITZ string INT zt SCNT interrupt service IC arrora iSpacial action 4f error Wov FILHAND2.AX :Save handle in word If afile with the given name exists, the operation sets the record length to 1 (which you can override). assumes the flie’s current attribute, sets the file pointer WO (the start of the fle), clears the eary flag, and regurns a hands for the file in the AX. Use this file bandle for al! subsequent accesses of the file. If the file does not exist, the operation sets the carry flag and retumes an error code in the AX: U2, 03, 04, 0S, or 12 (see Figure 17-1). Be sure to check the carry flag first, For ex- ample, creating a file probably delivers handle 05 to the AX, which could easily be con- fused with error cade O5, access denied. INT 21H Function 3FH: Read Record “Toread records, ase INT 21H function 3FH. Load the file handle in the BX, dhe nuunber of byies to read in the CX. and the address of the input area in the DX, The following example uses the fle handle from dee preceding crarnple to wad a 512-byte record: FELHAND2 ww? TWAREA 6B S42 UP? *} NOV at, 3FA sRequest read record NOV. BX,FILHANO2 File handle nov on si? :Record length LEA OX.INAREA Address of inpuc aree INT 214 3Ca11 interrupt service aC errors FSpeeial action if error owe ax,00 rZera bytes read? Je enafile f yes, end of File ‘A valid operation delivers the record to the program, clears tbe carry flag, and sets the AX tothe number of byies actually read. Zero inthe AX means an attempt to read from the endUsing File Handles for Random Processing a1a of (he Sle; this isa warning, not an error, An invalid read sets the carry flag and retums to the AX error code 05 (access deniec) or 06 (invalid handle}. Because the systein limits the number of files open al one time, a program that suc- cessively reads a number of files should close them as soon as it is through with ther. Program: Reciding « Disk File Sequentially The program in Figure 17-3 reads the file crezted by the program in Figure 17-2 and sorted by the DOS SORT commar. Here ure the main procedures: + ALOMAIN Calls BLOOPEN, CIOREAD, DIODISP and. if at the end, closes the file and ends processing, + BIOOPEN Uses INT 21H function 3DH to opea the file and saves the handle ina dita item named HAND! + CIOREAD Issues INT 21H function 3FH, which uses the handle toread the records. + DIODISP Displays the records and scrolls the sercen, Because Enter and Line Feed. characters tlready follow each record, the program does not have toadvance the cur- sor when displaying records. USING FILE HANDLES FOR RANDOM PROCESSING The preceding discussion on processing disk files sequentially is adequate for creating a file, for printing its contents, and for making changes to small files, Some applications, however, involve accessing a particular record ona file, such as information froma few em- ployees or inventory pans, To update 2 file with new dita, 4 program that is resuicted 10 sequential processing may have to read every record in the file up to the one that is required. For example, 10 ac- cess the 300th record ina file, sequential processing could involve ;eading through the pre ceding 299 records before delivering the 300th (although the system could begin at a specific record number), The generst solution is to use random processing, in which a program can directly access any given record in a file. Although you create a file sequentially, you may access the records sequentially or randomly. When a program first requests a record randomly, the read operation uses the direc- tory to locate the sectorin which the record resides. teads the entire sector from disk inu a ‘buffer, and delivers the required record to the program. In the next example, records are 128 bytes long and four to a sector, A request for random record number 21 causes the following four records to be read from the sector into the buffer: record #20 | racord #21 | record #22 | record #23 ‘When the program requests the neat recor randomly, such as number 23, ihe operation frst checks the baffer. Because the record is already there, it is transferred directly to theaw TIME eNDEDE GPRM, PATHNAK READMSC Age age: Aon B1000RN 2901 BLDOreN cpoRRAD, Disk Storage Ik Writing and Reading Files Chap. 17 ALTRIFIL (EXE| Read disk recorda sequentially MODEL SMALL STACK 64 DATA bE 90 JBnd process indicator Be 2 2 3a pure 1) be Niet pea error s+", OOH, alt DE SBANAMERLLS. Sr", 0 Ba Vere Read error 7°", 00H. OAK DE oa mov AM, @dara vinieialize Moy DS.Ax ‘aegnent Wov BS.AX. registers Mov AX Og00K caur — gubace 1Cleax ecreen CALL gaocuxs Set cursor Gur Bibowen fOpen tite CMP ENDCDS.09 Valid open? mz AS po, elt cau CLoREAD ;Read aick record cab ENDCDA, 09 iNornal reac? gee aD Pena, ate cate pnoprsF } yea, Gleplay name, SMP AgDLOOP ;. Sant inue ov iRequest close file ier ov ind processing wt Bune proc Mov PRogueat open Mov Hormel & TBA ut ae rexvor? mow Pao, gave handle, MP 7 return Moy sNDCDE,o2 > yes LBA DK, OPEINSG > Sdepray: CALL «© X1OERE D error measage Read disk record, PROC NEAR Moy AR, SF pRequaat read MO BXLHANDLE, Mov Chaz ;30 for name, 2 for OR/LP TER DX, TOARBA mr 3W ae eo pperce on rnad? Figure 17-24 Reading Recows SequentiallyUsing File Handies for Random Processing MP ax.00 oe 36 cme TOAREA, an oe, co mp C80 cao: BEA DX, READHEG CALL = XL0ERR a0: Woy! ENOCDR, 91 a0, ner aRDe ‘SLORBAD : Dieplay name Troprge PROC NEAR. HOY AA0H mov EK,OL yoy 32 BRR DX, TOAREA, maa ow | RoW,20 SAE 8D Tee Row neo: wo Ax, 0501 cALL «— Quoser aL © Qa20cuRs END? DIODISE : Scroll screen Q108CR PROT mov cx 0000 oy DE, 1047 INE 108, RET. quesen END : Set cursor: Gzoems PROC HEAR mOY AK, 2 mov BIL 09 MOY DH, RoW Nov DL,00, a) Qzogues — RNDP ans. sRnd of file? j5OF marker? Dyes, exit > Tnvetid read HForce end PRoqueet dioplay jset_ harale and Jensth pBottom of screen? yes, bysaes Bo, Snerament row sSerel1 pet curwor ix set on entry [Set color sequest scroll Recent act > Column : Display disk error message: RLOERR PROC Moy aR, 40 mov BKLoL wow cx20 ter anit RET, XG ORR -ENDP BND ALONATN, 1DK containg addres iHandle for ecreen Haength Pot mecaage igure 17-38 Reading Records Sequeatiallya6 Disk Storage Ht: Writing and Reading Files Chap. 17 program. If the program requests a record aumber that is no in the buffer, the operation uses the directory to locate the se:tor containing the record, reads the entire sector into the buffer, and delivers the record to the program. fp this case, requesting random record nurn- ‘bors that are close together in the file results in fewer disk accesses. INT 21H Function 42H: Move Fite Pointer ‘The open operation initializes the file pointer to zero, and subsequent sequential reads an ‘sites increment it for each recoré processed. You can use function 42H (Move File Pointer) foset the file pointer anywhere within a file and then use other services for random retrieval or updating of records. “To request function 42H, set the file handle in the BX and the required offset as bytes in the CX-DX. For an offset up to 65,535 bytes, set zero in the CX and the offset value in the DX. Also, set a method code in the AL that tells che operation the point from which 10 take the offset: 00 Take the offset from the start of the file. 01 Take the offset from the current location of the file pointer, which could be anywhere within the file, including at the start. 02 Take the offset from the end-of-file, You can use this metbod code for adding recoxs to the end-of-file. Or you can determine the file size by clearing the CX:DX to zero and using method code 02. ‘The following example moves the potter 1,024 bytes from the start of a file: MO AH, A24 Request mova pointer ov AL.00 to start of file Moy BX\HANDLEL © Ser file handle ov ¢x,00 HUaper partion of oFfser Mov 0X, 1074 Hower portion of offser wT 21 HCaN) interrupt service ac error Special action if error A valid operation clears the carry flag and delivers the new pointer location in the DX:AX. ‘Jou may then perform a read or write operation for random processing, An irvalid operation sets the carry flag and retums in the AX code 01 tinvalid method code) or 06 (invalid handle) Program: Reading «i Disk File Randomly "The program in Figure 17-4 reads the file created in Figure 17-2. By keying in a relative record number that is within the hounds of the file,a user can request any record in the file to be displayed on the screen. If the file contains 24 records, then valid record numbers are 61 through 24. A number entered from the keyboard is ia ASCII format end in this ease should be oaly one or two digit.Using File Handlss for Random Processing a7 TLTLB —ALTRDEAN (BKB) Read dex records randowly “MODEL SMALE STACK 4. cATR. wNOLE D7 wPile hondle RECIND) De? iRecora index RRCDE «DRO Read error indicator FEOMDT DE Record nunber? * JOAREA «DE 32 DUP" jDlok recerd area PATHNAR DE TF: \NAMEPTE.SRT* PRIMES DB tet Open error et4', OK, OAH REAIMSC «BR lava Read axear #¥4!) ODM, OAR OL DE 09 RECUPAR LABEL BYTE Hinput parameter List MAKEN” DBD P iaximam Tengeh RCILEN «OB? } actual Lengeh RECENO «BB 3 Dua?) {record number cooE sae owary PROC FAR Moy AK,aanea sTeetalion HOY DavaK 2 segnent mov ES. ax 5 registers Woy Ax, osn0x ALL LOecRN jClear aozeer CALL Q20cuRs: [Set curser CALL © BLOOPEN (pen file Mp RRREDE, 30 Walid open? REOWOOF: CAML SLORECN jRequest racera # Qe RCTERN, 00 [Any more requestal Se 890 ftoy exit CALL DLOKKAD [Read dik record CM BRRCDE. 90 iNermal rear om AiG pone, bypast CALL RLODISP } pea, dlaplay name, a gypA20LO0R contince aso: Mov AK, acooH ‘and processing LOMA ENDE ? Open fil Bioorm eno” NEAR Moy AA, 30H Request open Moy AL,O0 iormal rite TER DK) PRTHNAM i ait oe Bo pBreoxt Moy HANDLS, AK Dono, save tandle RED Figure 174a Reading a Dak File Randomly318 ano Binorent CreREcN cap: 30: cae: ‘orecr ycREAD p29, kor nov Me you wou anD Bee SRE Mov ALS ker END Rec Hoy Disk Storage I: Writing and Reading Files Rene, 01 Dx, Operas XIDERR Ge record aynbe: ame 2a, 40H Bx, 2a x15 BX, PROMPT But a, OF DX, RECBFAR 24 AcTLEN,03 eo 20 ANAS ‘ALL RECENO x4) [AH RRCTNO ‘nL, RECDNOS1 AX, OFOH a Ax, os RECINDK,AX cet, 20 guocuRs Read a rene we 42H ALLO BX, HANDLE cH, 00 ‘Bx RECTEDx ait bao AK, 37H Bx, maNoLe ox,32 EX, TOARER, ik B20 IOAREA, LAM 0 30 Bx, READS XIOERR Chap. 17 2 yee, Ssepiay i error message pRequeat dinplay prom pelle handle 11S tnavacters iRequest input pet record number iCheck lengeh ¢, 1, 2 jtength 0, terminate rhength 2 shencth 2 jelear ascoy te Convert to binary Bajust (ist racord 4: imaleiply by 16 isave index ie record randomly: Request set file, pot7 Setar of file taste Ronee OF Tapper portion of [bbe Boreion of ctf (Brrr condition? i yee, bypess iRequest read 20 for name, 2 for 6 parrer on read? {SOP marker”, F yee, exit rInwalid read [Display te. age Figure 17-4b Reading a Disk File Randomly ‘The program is organized as follows: + AIOMAIN Calls BIDOPEN, C10RECN, DIOREAD, and ELODESP; ends when the user has no more requests.Using File Handles for Random Processing 23a: peg DIGREAD —ENDE BRRCDE, 01 Display name: Hioprsp paoc NEAR Mov AH, 40H Mov BK,OL wy oxlaa LEA DK, TOAREA ast ae mov — CoL.00 ep ROW,20 che a0 We ROW mp BSG ov AR,oseaH CALL G2U5CRN CALE OZ0CURE B90: rer e1opise —aNDE ; sexel] scree: Brogers © proc NEAR rov) an, 1eH roy Cx, noo Woy | DX 18aFH fet eM loscRN ENDE : set curser. Geocuss moc NEAR mov AH.02 voy BH.0a vou DH Row Woy DL, col, inr 108 q2ocurs ENDP : Display disk srror 319 srovee ond iRequest dleplay $5et_hendle Pand length #Seroll rset cursor JAX get on entry ieee "color Request scroll HXORRR PROC NEAR NOU AH 40H nov Bx,on sor CK.20 Int 218 inc Row ner X10HER END. END) ALOMAIN DK gontains address Handle fuength POE message Higure 17-4e Readg a Dist File Randoniy « BIOOPEN Opens the file and gets the file handle. + CIORECN Accepts a record numaber from the keyboard apd checks its Length in the parameter list, There are three possible lengiks: 00 End of processing requested G1 One-digit request, stored in the AL. 02 Twordigit request, stored in the AX “The procedure has to convert the ASCI number to binary. Because the number is in the AX, the AAD instruction works well for this purpose. The system recognizes location320 Disk Storage Il: Writing and Reading Files Chap. 17 O.as the beginning of 4 file. The program deducts 1 from the actual mmber(so that a aser request, for example, for record | becomes record {0}, multiplies the value by 16 {the length of records in the file), and stores the result in a field called RECINDX. For example, if the entered number is ASCII 12, the AX would contain 3132 An AND insiriction converts this value to 0102, AAD further converss it t0 000C (12), and. SHE effectively multiplies the number by 16 w get CO (192). An improve- ment would be for the procedure to validate the input number (02-24) + DIOREAD Uses function 42H and the celative record location from RECINDX to set the file pointer and issues function 3FH to deliver the requiced record to the program in (OAREA + ELODISP Displays the retrieved record. PROGRAM: PROCESSING AN ASCII FILE The preceding examples created files and read them, but you may also want To process ASCII files created by an editor or word processing program. You nced te know the ora- nization of the directory and FAT and the way in which the system stores data in a scctor. The data in an ASM file, for cxample, is stored exactly the way you key itin, incladiag the. characters for Tab (OPH), Entct (ODE), and Linc Feed (GAH). To conserve disk space, the spaces that appear on the screen immediately preceding a Tub character orspaces ona line to the right of an Enter churacter are not stored. The following illustrates an instruction as antered from a keyboard: TabsMOVAN,09. yea, aieplay Elapise ;O.epiay last Line quest display Bx,02 Wendie CHUDISARER iealeutate a Lenach of cx.DE ike Dx,O=SARER 2k Row, 22 :Bottom of sereen? E3¢ pene, eit E30 Figure 17-50 Keading en ASCH PlleProgram: Proceasing en ASCII FilaO. 323 B90: Ei30r8P Guoscr cuoscr e20cms eeocmns: ALORRR, XLOERR Ret ax,occan reroll gadser Rzocuns serell screen: WEAR 1AK set, on entry BH, ak {Ser "eolor attribute x, 00c0 isezell Dx, 184FH 10h Set cursor aH, 028 sRoquest set ‘BH, 00 Pcurwor sRequest display iMandle fhength aieror indicator ‘IOMAIN igure 17-5 Reading a ASCH File ‘The procedure wansfers onc byte at 2 tine from SECTOR to DISAREA, where the characters are tobe displayed. It has to chock for the end of a sector (to read an ‘other sector) and the end of the display area, For conventional ASCII files. such as .ASM files, exch line is relatively short and is sure toend with Enter/line Feed. Non- DASCI files, such as EXE and .OBJ files, do not have lines, s0 the program has to check for the end of DISAREA to avoid moving data into the area that follows. The program is intended to display only ASCII files, but ihe test forthe end of DISAREA is insurance against unexpected file types. ‘These are the steps: 1. Inisialize the address of SECTOR and the address of DISAREA. 2. Ifat the end of SECTOR, read the next sector. If at the end-of-file, exit: other. wise initialize the address of SECTOR. e tialize DISAREA. awe goto step 3. If at the end of DISARBA, force an Enier/Line Feed, display the line. and ini- Get a character from SECTOR and store it in DISAREA. fall the characters have been processed. exit. if the character is Line Feed (CAH), display the line and go to step 2; otherwise324 Disk Storage Il: Writing and Reading Files Chap. 17 + E1ODISP Displays the data in the display Tine up to and including the Line Feod, Be- ‘cause lines in an ASCI file are in variable-length format, you have to sean or the end of each line before displaying it. (The monitor accepts Tab characters (QOH) and au- tomatically sets the cursor on the nect location evenly divisible by eight.) Scrolling cen be a problem. If you perform no special tests to determine whether you have reached the botom of screen, the operation amomatically displays new lines ‘over old and, if the old Tine is longer, old characters still appear to the right. For proper scrolling, you have to count rows and test whether you are atthe botiom of the screen. + XLOERR Displays a message for a disk error. Try running this program under DEBUG with an appropriate drive number and ASCII file, after each disk input, display the contents of the input area and see how your records ate formatted. Enhancements to this program would bs fo prompt a userto key in the file name and extension and to use the fall DX:AX for the file size. ABSOLUTE DISK 1/0 The purpose of the DOS INT 25H and 26H instructions is to cnable absolute reads and writes to process a disk directly, for example, in tecovering a damaged file, In this case, you do not define file handles or FCBs, and you lose the convenience of directory handling and blocking or deblocking of records that INT 21H provides, Note that INT 21H funetion 44H (covered in Chapter 18} provides a simitar service and has superseded INT 25H and 26H. Because these operations treat atl records as if they were the size of a sector, they directly access 2 whole sector or block of sectors. [isk addressing is in terms of relative fecott number (relative sector). To determine a relative record number on two-sided diskettes with 9 sectors per tack, count each sector from ack 0, sector 1, as follows: TRACK SECTOR RELATIVE RECORD WMOER, ° 1 0 (the first sector on the disk) a 2 1 1 1 8 x a ” 2 9 26 ‘A formula for determining the relative record number on diskettes with 9 sectors is Relative record nunber = Cerack ¥ 9) + (sector - 1) ‘Thus the relative record number for track 2, sector 9, is Qn + G-D= 18486 Here is the required ending for disk partitions thet are less than 32 MBs: wv ALdrives —:0 for A, 1 for B, exe. Noy BK, addr jaddress of parameter block NOV CX,sectors — jAunber of suctors to read/write Mov X,sectoré —: Begining relarive sector runber INF 25H or 26H ;AbSOlute read or writeDisk Services Using File Control Blocks 5 3C ferrer] i€F Gs set on an error POP Pop flags INT 25H and 25H destroy alf registers except the segment registers and.use the carry flag to indicate a successful (0) or unsuccessful (1) operation. An unsuccessful operation returns one of the following sonzero codes tothe AL: 10060000 Attachment Talled to respond 91000000 Seek operation failed 90001000 Gad CRC read on diskette 90000100 Requested sector rot found 90000011. Accent to write on write-protected diskette 90000010 Other error ‘The INT operation pushes the flags onto the stack. Because the odginal flags are still on the stack upon returning from the operation, pop them after checking the carry flag. Since DOS 4.0, you can use INT 25H and 26H to access disk partitions that exceed 32 megabytes. The DX is not used, and the BX poinis to a 10-byte parameter block in the following format: OoH-03H 32-bit sector number OtH-GSH Number of secters to read/write OGHAC7H Offset address of buffer O8H-C9H Segwent address of buffer DISK SERVICES USING FILE CONTROL BLOCKS ‘This section briefly covers the FCB services for creating disk files and processing them se- uentially and randomly. These services were introduced by the first version of DOS and are still available under all versions. Disk processing for the FCB services involves defining a file control block (FCB) thatdefines the file and a disk transfer erea (DTA) that defines records. You provide the sys- tom with the DTA address for all disk VO operations. Because the FCB method dors not support file hendles or path names, its use is restricted to processing files in the current di- rectory. Also, FCB operations do not use the error codes listed in Figure 17-1 and they da notclear or set the carry flag to indicate success or failure. (A variation of FCBs existin the ‘program segment prefix (PSP)). Fite Control Block ‘The FCB, which you define in the data atea, contains the following informationabout the file and its records (you initialize bytes 00-15 and 32-36, whereas the system sets bytes 17~31): 0 Disk drive. For most FCB opeiations, OO is the default drive, 01 is drive A, 02 is drive B, and so forth. 1-8 Filenome. The name of the file, left adjusted, with trailing blanks, if any.