[go: up one dir, main page]

0% found this document useful (0 votes)
361 views133 pages

Microprocessors Douglas V Hall

microprocessors interfacing experiements

Uploaded by

Chandan K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
361 views133 pages

Microprocessors Douglas V Hall

microprocessors interfacing experiements

Uploaded by

Chandan K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 133
EXPERIMENTS IN MICROPROCESSORS AND INTERFACING PROGRAMMING AND HARDWARE SECOND EDITION DOUGLAS ¥. HALL Maen {ake Forest, ins Columbus, Chio Mission ils, California Peoria, Minos BMC, 18M PCAC. eM POVAT, 18M P8:2, ae MiroChannel Architecture are registered "admarks ol 1M Corprein. Te alwng are regen racersrks oft Corporation: 480, e801, RMI. Borand, Stdekeck,Turoo Assembler, TASM, Turbo Debugger, and Tuba Gr= re ‘aglteredademarks of Boland Iicmatond ine. Maeosot, MS, MS OOS, Windows 3.0, ode. and MASM sre registered rademares of Me-ceah Corpeaton. Other product names are fogatted trademarks of tho companies assorted wits ts product name flere nth tox or Agu. Copyright © 1902 by the Glanece Ohison of MacmitanMeGrau- Hil Shoe! Punishing Company. Copyiget 2 1886 ay McGraw, a. All rights reserves. Except as permed Lncer the Unites States Copyright Act, no part ofthe pubieaion may be reproduced or Se appear in the rightmost digit of the address field eorex 60 | Go lad ast press oe hey and then ne a Brat Manet nex mse dceee | TS] LS | frmbol for ass ha vt segnend nee aes | [pa the syaibol for a BG) dake man ee at cre eet es Proms end dm Sho | TL oe Nawesm | ax [omar note that the symbols for the last four keys — Se ee 2. To prepare the SDK to receive its next command, oner fae Seles -) —— prem they anes sist Rese SSCs. Whenever n dash i dplayed on the tetas cchoearueesr rm ocmmracmmat | TET oy loom | |p With one of the keys on the keypad. Figure 1-1 eld — inex Shows the fnetons of eats af ans PES LD Pale | ow eerntien pes ow | urout 2S | Osta Segment poe Ei ana ‘SDK-36 ww an a a ack SF BJ fmm |e fo fa we] | RIMES a twee a nom | wa s [om ren | | oi eae eee roitarnac eg teres ezem ole pe pee eee uae i — ie ope ciel Seon = Trousers, Lr SES A ee [Tf me fae | aoe foe =] Sasssaretae ti 2 Ay mp wae Loam Stel omen ce . Zien tay seo gr eg Poa recent das, Nox tween rgb Sonar {cis ors arecsn tae 2 epeRweNT (@) Hexadecimal keypad interpretation. Intel Conporation) that many of the kes have several functions, depending upon whether yout are entering a com. mand, entering addresses and data, or examining 8086 registers. The 0 key. for example, is used te enter the EB command ifs dash is showing. It is Used to enter the number 0 if an address or data Word ts being entered. Tt is used to represent the AX register if registers are heing examined, “The SDK-86 keypad monitor program allows you to execute 10 baste commands. Figure 1-2 summarizes the format for these commands. Worle your way through the following sections. which show you how to use some of these commands. The sections are ented by the command name 50 thet you ean find them easily if you need to relresh your memory at some future tie, EXAMINE BYTE (EB! ‘The examine byte command Is used to inspect and, if desired, change the byte stored in a memory location. ‘This command allows you to easily enter program code bytes or data bytes in successive memory Jocatlons. As shown in Figure 1-2, the format for the command is EB addr {data .1*. ‘The letters “EB” tell you to press the Key labeled EB. The “addr” tells you to enler the address of the byte that you want io look at. Pressing the comma after entering the address causes the address and the contents of that address to be displayed. The square ‘brackets and the * shown in the command format are not entered. Command parts contained tn square brackets are optional. The inner square brackets in ‘his command indicate that the data at that address can be changed by simply typing in the desired hex value, The“ indicates that the part of the command contained in the outer square brackets can be repeated. In other words, by pressing the <> key, you can step to the next address, see lls contents, and change them if you want ‘To see the byte in a memory location, press the key labeled EB, enter the address of the desired byte, and onmane Funcinlsyoe xarioe | Dielownodtis memory bse eaters Be | EBcoaie> a teu Blo Examine | Diplayeeditar emery word eens ors jeoear> fear 21a Examine | Ospaye-notes 8085 asi contets ager | ER T] 1°10) trout | Digaye cea aetna part Sym | gl ala Wed | Bicootser>orai to urpur | outputs aye out pot bye porte cea ) A foupar | Duessedaen wont cup ore Word | Gicoertacdr> Sdn a eds>1 902 60 | Tater conta fom monitor ow rosary Boia (a cend ed Ol | Bl crane seer) Q)*D FIGURE 1-2 SDK-86 boare! moniter command summaty. {Intel Cosporaion) then press the <> key. Addresses are entered in the segmentioffset form, and so in order to see the contents of the memory location 000100H (0000:0100), you enter 0:160 as the address. Note that the SDK does not require you to enter leading zeros and does net display them, 4. To see the byte in memory at 0000:01008, enter the following series of Keys: BBO:100, ‘Afier you press the <> key, the contents of add- ress 0000:0100 should appear on the rightmost ‘ovo digits of the data field LEDs, 2, To insert some new byte value at that address, center the hex digits of the new value with the keypad, To enter the value C4H, press the keys ca As you press the Keys, you should see the ew vahie appear on the data field LEDS. If you make a mistake while entering a value. Just {ype in the two correct digits; they will replace the previously entered value 8. To examine the next memory location, press <>. You should now see the address 101 displayed on the address fefd digits, and the byte stored at that location displayed on the right two data field LEDs. To change the contents of this new address, enter another value with the keypad. Press the <> key fagain if you want to examine and/or change the next sequential memory address. '5, To terminate the command alter you have looked fat all the memory locations you Want, prese the <> Key, When the dash prompt ) appears, you can enter your next command, 4. EXAMINE WORD (EW) ‘The examine word command is used to mspect and, sf desired, change a word stored In (we memory loca- Uons. AS shown in Figure 1-2. the format for the ‘command is EW addr. {[datal.1* ‘The operation of BW is identical to that of the EB command deseribed in the preceding paragraphs, except that a data word (16 bits) will be displayed on the four data field LEDs. The right two digits of the displayed word represent the byte contained in memory at the address specified in the command, The high byte of the word, displayed on the upper two data fleld LEDs. represents the byte contained in the memory location after the address specified im the ‘command, 1. Examine the word contained in addresses (0000-0100 and 0000:0101 by pressing the keys EW0:100, You should see the byte from 0000:0100% displayed on the right evo data field LEDs and the INTRODUCTION TO THE SOK-85 MICROPROCESSOR DEVELOPMENT BOARD 3 byte from address 0000:0201 displayed on the left ‘vo LEDs, 2, Change the displayed word by entering the desired ord. Press the <> key to step to the next word in memory. 3. Write down the value you see for this word, Then press the <> Key to get the dash prompt back, so that you can enter another command. 4. Use the EB command to verify that the low byte of the displayed word came from the low address (0000:0100) and the high byte of the word came from the higher address (0000-0101). This relax ‘Gonship is very important to remetber, EXAMINE REGISTER (ER) To display the contents of an 8086 register, press the key and then the key that contains the letter Initials for that register. 1, Examine the contents of the AX register by pressing the keys BRAK Notice that the key is also the key. The address field of the display will show the letter A ‘to maleate the register that is being displayed, and ‘the data fled will show the word contained i the AX register, 2, Examine the contents of the next 8086 register by pressing the <> key. Each tlme you press the <.> ‘key, the SDK will display the next register in the sequence AX, BX, CX, DX, SP, BP, Sh. DI, OS, DS. $8, BS. IP, FL. FL ts the flag register. In each case the address field will show one or two mnemonie letters to indicate which register Is being displayed. 3, To change the word in any register, enter the four hex digits for the word you want in the register. ‘Then press the <> key to step to the next register. or press the <> key to get the dash prompt back ‘0 that you can enter your next command, NOTE: ‘The ER command operation is not circular, so if you want to loole at a register earlier In the sequence, press the <> key to terminate the command, Then press the key to start the command again and then press. the key that has the name of the register you ‘want 10 see. co ‘The SDK-86 GO command allows you to run programs which you have previously loaded into RAM. Remember, you can load programs, into RAM starting at address 0000:0100H. The format for the ‘command 1s| GO [addr } |, breakpoint addr! ‘The first address spectiied in the command is the Program address at which you want execution to 4. expentwenT a start. The breakpoint address specified in the command is the program address at which you want execution to stop. Whenever you run a program on a microcomputer, you must I some way prevent the processor from fetching and attempting to execute random bytes stored tn memory after your program. One way to solve this problem is te put a jump instruction as the last instruction in the program to make the program execute over and over again in an ‘endless loop, For programs that you don’t want to execute over land over, you can put & breakpoint after the last program instruction. When the 8086 reaches the breakpoint, tt will save the values that were in the £8086 registers afer the last program instrietion was executed and retum control to the SDK-86 monitor program. You can then use the EB or EW command to look at any program results in memory. You can use the ER command to look at any program results sn registers. Using the SDK-86 Commands io Load and Exectite a Program igure 1-8 shows a simple program that you can enter into the RAM on your SDK and run for practice. When run, this prograin causes the SDK to display ‘4-digit hexadecimal count sequence en the data fleld LEDs. The program keeps counting from QO00H to EFFFH, Don't worry tf you haven't the foggiest nation hhow the program works at this point. You can learn to drive a car without knowing how the camshaft opens the valves, To load and run the program in Figure 1-3, follow these instructions. 12. Look at the first coluran from the left in Figure 1- 3 to find the addresses for the code you will be loading into RAM. The addresses shown in the program listing are relative. This means that they show where the bytes for each instruction are relative to the starting address at which you load the program into RAM. You want to load the program starting at address 0000:0100H, and $0 you add 10014 to each of the addresses in Figure 1-3 to find the actual address of each byte. ‘The E command, deseribed previously, 1s used to enter the sequence of data and instruction bytes shown in Figure 1-2 tnta the SDK-86 RAM start- sng at address 0000:0100H. To see the current contents of address 0000:0100 on the data fleld LEDs. press the keys EBO:100, ‘You want to enter the frst instruction code byte of the program into this location 2, The second column to the left in the program listing contains the codes that you wall be loading into RAM, The reason there are different numbers of code bytes on different lines 1s that the code byte(s) required for an instruction are put on the line with the Instruction, Laole at the 184 Personal Coaputer MACRO Asseabler Version 2,00 Page f+! ages 132 9086 Count Progren 5 5 This progran dinpiays an tnsrononting hanadesiaal count on 5 the data Tielé diaplay of the $0686 board. i 000 COE HERE SeaHEMT PUBLIC S1AE C9:C0BE WERE DECORA, MERE Sanitialize data sepeent 0000 98 1300 OV A, DATA PERE 00039 88 HOW D8, ak 0005. 9F coo HOV Dt, 060 4 zero counter 008 8B 000 OV BK, OFFSET SEVEH SEO 5 pointer to seven-seguent codes 0008 ah RFE DISPLAYENOY DY, OFFER {point at 8279 control address 008 E090 HO AL, 38 5 Yoad control werd for data field 0010 OT BY, aL 4 send control werd $0 279 cout BA SOFF av Dx, OFFEBK 5 point at 4279 display mam 0018 6B OF Mov C1, BT ote a4 cL HOV Aly cl 1 get tow byke to be displayed ons a6 OF AND AL, FH 4 mask upper nibble 00a a7 nate 5 translate Lower nibble'to 7-s09 cade 001g FE OUT Diy a F send to 279 display RAN O0ie a6 cs tov AL, 4 get low byte again OO1E BL OF Ov Ck, 04 4 Toad rotate count 0020 m2 00 fou aL, OL 4 Move apper nibble into Iow position 002224 OF BD AL, FH 5 Mask upper nitble ones 7 tan § translate 2nd nitble to 7-s99 code oes gE WT Day a 5 send to O277 display RAK 08S 8A CS HOY Aly | Set high byte to translate 0088 24 OF a AL oF 4 Mask upper nibble 0024 07 su 5 Translate to Tosey code 002) EE Or Di a send $0 8279 display Rh ooec aa os HOV aly 4 get high byte to fix upper nibble coat 2 C9 ROL Aly cL ‘ove upper nibble into low position cog 24 oF PAD AL, FH 4 sash upper nitble ooge 07 ate 4 translate to T-seg code 0039 SE OWT Dky AL 5 Tosog code to S279 display Aa 0094 3 FFFF NY ST, OFFFFH 4 Jond counter to pause 0087 4 Pause: DEC St 9038-75 FD BAZ. PAUSE 0038 47 INCL 5 incrawent counter nga 6B ce AP oIeLay 0030 (CODE HERE BS 000 DATA HERE SESRENT WORD PUBLIC 9000 3F 06 8 4F 66 6D 79 07 SEVER_SEB DB FH) OOH, Sy OFM, 68H, AIH, 7M, OTH Ve ete sare be 0009. TF AF 77-90-39 SE 79 71 Diy Fy 77My POH, Hy SERS PAM 1 a on DATA, HERE EMIS Ey FIGURET-3 8086 counting program, INTRODUCTION TO THE S088 MICROPROCESSOR DEVELOPMENT BOARD. 5 6 2. {op of that column to find the BEM code byte and. fhe other cades that will be put aller {To put the BS code byte into RAM and ta sce address. G1OT on the address Neld and the contents of ‘that address on the data fel, press the keys Bs. ‘The next code byte (to the right of BBR) is 19H, ‘To enter this with the keypad, press the keys 13, ‘The <> Key steps to the next RAM address ‘The next code byte for the program is OOH, so enter this value and press the <> key to step to the next address, ‘The next code byte, found on the next linen the Program printout, is 8EH, Enter this value and Press the <,> key to step to the next RAM. auldress, - Enter each code byte in column 2, followed ty a When you reach the end of the code bytes in column 2, insert the data bytes, starting with SPH in the series of addresses starting with 0000:013E. Note that address 0000:013D is ‘slapped. - Aer you have entered all the program bytes, ress the <> key to return control to the monitor Program. EXPERIMENT 1 9 The next step 1s to check f the program was entered correctly. Press the keys EB 0:10, to see the byte in the first address, If this byte is ot correct, enter the correct value and prese the <> key to get to the next address. if the byte is correct. just press the <,> key to step to the next address. A common error Is to miss a byte: consequently, the bytes after the missed byte eid LP at wrong addresses. If you find an ervor, you can just insert the correct value(s), and proceed, When the program seems correct in RAM, run the ‘program from the starting address (0000:0106H. Press the keys G00:100, + If the program does not operate correctly, press the RESET key and check the codes in RAM again. If the program stil does not work core reetly. get help, If ie permits we suggest that you now go on and do Experiment 2, which shows you how to luse breslspoinis to debug a program that docs? Work and how to modify the operation of the pro- gram, Experiment 2 uses the same program 16 {his experiment, so ifyou leave the power on and do Experiment 2 now, you won't have to Key the Program in again 10. 4, Keypad Monitor REFERENCES: Hall: Chapter 3 URDA oF Intel: SDK-86 User's Manual EQUIPMENT AND MATERIALS SDK-86 microprocessor board INTRODUCTION Hall: Chapter $ discusses the importance of writing mlcrocomputer programs in a etructured manner so that they are more likely to work correctly. Sometimes, however, in spite of careful planning, vriting, and entering, an exror creeps into @ program, ‘Most systems have programs or a series of commands that help you (0 ind the problems) in program, Finding arid correcting the problems in a program 1 commonly referred to as debugging the program, The ‘ovo debugging techniques we want to teach in this exercise are breakpoints and single stepping. It is important to use these techniques as part of a systematic overall debuyging procedure rather than randomly experimenting. Here Is a debugging sequence that we have found effective. 2. Check your algorithm to make sure tt really ‘specifies what you want I 1 do. 2. Cheek that the instructions you have chosen do actually implement the steps of the algorithm. 3. Check that the instructions are correctly coded, if you are hand coding, and that they are correctly entered Into RAM. If these checks don’t turn up any prablems, choose a logleal checkpoint in the program and predict what the contents of key registers and memory locations should be when the program hhas executed to that point. Record your pre- dictions Im some way set a breakpoint at that point in the program and run the program. The program w= tun to the breakpoint and stop, 5. ©. Use the examine memory ané examine register commands to see if memory and register contents agree with your predictions. The prediction part of this Js very important. because it doesn't do you any good to look at memory and register contents Ifyou don't know what the contents are supposed tobe. 7. If the results are correct at that point, you can predict the memory and register contents for a Tater point in the program and move the breakpoint to that point. You can then run the program to the new breakpoint and recheck Key ‘memory locations and registers to see if they are correct at that point. The breakpoint technique allows you to localize the problem area in a rogram. 8 Another debugging technique available. in some ‘systems is the single step. This fonction exeesstes one instruction and stops. You can then examine memory and register contents to sce if they agree ‘with what you predicted they should be at that point. If the results are correct, you can use the single-step function to execute the next instruction and recheck Key memory and register contents. This function allows you to step your way through @ program one instruction at a time, ‘checking the contents of key memory and register Tocations after each instruction. After you have debugged a few programs. you wil get a feeling for which technique Js best suited (o debugging a particular program, OBJECTIVES At the conclusion of this experiment, you should be able ta 1, Use the SDK-86's keypad monitor commands to run a program to a breakpoint and then examine register and memory contents to see if they agree ‘with predictions, 2, Use the SDK-86 keypad single-step command to step through a program, 2. Modify the operation of a program by changing @ value in the prngrars PROCEDURE ‘The Example Program 1, fits not already loaded, use the SDK-86 keypad ‘moniter, EB command to load the program shown, in Figure 1-8 into RAM starting at address (0000:0200H, as described in Experiment 1. 2. Check that ail the instruetion code bytes and data bytes are inserted correctly. 8, Use the SDK-86 GO command with no specified breakpoint to mam the program aa you did in the last section of Experiment 1. If the program does not run correctly, press RESET to get back to the ‘moniter program s0 that you can enter commands again, Use the EB command to make sue that the ‘codes are all in memory correctly. Remember that the top segment on the LEDS is it for a 6 but is not it for ab. When you get the program working correctly, go on to the next step here. Using a Breakpoint As a fst example ofthe use ofa breakpoint you imight want lo see If the het se anstructons of che Program are Inialeing te speted rogters In tHe Say you want You insert a breakpoint ap part of the GO corimand. As shown ia Figuce 1-2, he format ofthe 9 command i GO [ start adér |, breakpoint addr | Remember, the brackets shown in the command format are not entered; they siniply indleate the parts of the command that are optinaal. For our case here, the starting address is 0000:0100 (CS = 0000, IP = 0100; you could also use 0010:0000—that is, CS 0010 and IP = 0000). The instruction at a specified breakpoint address does not get executed. Therefore, the breakpoint address you use is the address of the instruction after the last instruction you want executed, (A breakpoint must always be put at the adress ofthe first byte of an imatrsetion!) i. Predict what should be in the affected registers after these instructions execute, Because yout are hnew at this business, here's what should be in them, Predicted Observed Register Contents Contents AL 0H BX oiscr Dx FFEAH DI 00008 bs 00001 2, For the example here, you want to execute the first sic instructions, so your breakpoint address will be 0000:0110, the address of the seventa, ‘instruction in the program. Enter the command G00:100.0:110 8 BeeuveNT? ‘When execution reaches the brealzpoint address you have specified, the SDK will display a dash in the address field and the letters “br’ in the data field. Ifthe last instruction in your program is INT 3 (eode CCH), the "br’ message wil not appear. ‘The dash indicates that the SDK is ready for you tocenter your next command. 8. Use the ER command to determine the 8086 register contents. Record these contents so that yyou can compare them with those predicted, NOTE: if the SDK “locks up" and does not reach the breakpoint, you can get back to the monitor by pressing the RESET key. In this ease you may have to reenter the Instruction byte at the breakpoint address, because the moniter does the breakpoint operation by temporarily changing this instruction te CCH, Using the Single-Step Command Another technique that fs sometimes useful for debugging a program is single stepping, When the single-step command on the SDK-86 Is used, the ‘monitor program will execute one program instruction and then stop. You can then use the EB, EW, and ER commands to see ifthe program data is correct. Ifthe data is correct, you can then execute the next in- struction and check again. If the data is not correct, you can try to determine why it isn't As shown in Figure 1-2, the format of the SDK. single-step command is, ST start addr, {| start addr} ,1°. ‘What all ofthis means Is that Ifyou press the keypad kkey labeled ST (the <3> key) followed by the address of the instruction from which you want to start stepping and then press the <> key, the SDK will exceute the instruction al that address, ‘The address of the next sequential instruction will be displayed on the address ficid of the display. Each time you! press the <> key, the SDK will execute one instruction and. step to the next. Pressing the <> key terminates the single-step command and returns exocution 10 the ‘monttor program. 1. Execute the first instruction in our example program by pressing the keys sto:100, 2, The offset of the next instruetion will be displayed ‘on the address field LEDs, and the frst byte of the instruction at that address will be displayed om the data field LEDs. Use the program listing in Figure 1-3 to see if A does this correctly for your program, 8. Now press the <> key until you have stepped ‘ough the first six mstructions, then press the <2 key to return to the moniter. 4, Use the ER command of the SDK to see if the register contents agree with those predicted, 5. There is @ problem if you try to use the single-step command to step all the way through the program. to the INC DI instruction al offset OO9AH. Can you see what the problem is? If you can't, try stepping ‘through the program to the INC DI instruction to determine the problem. In the next section we show you one way around this problem, Modifying the Operation of a Program ‘Take a Jock at the instructions starting at offset 003411 in the program. The first instruction here, MOV SLOFFFFH, loads the immediate number FFFFH {nto the St register, The next instruction, PAUSE:DEC SI, decrements the number in the Si register by 1. If the result of this decrement operation does not leave 0 in the SI register, the JNZ PAUSE instruction will send execution back to the PAUSE:DEC SI Instruction, This type of operation is called a program. {oop and will repeat until the number in SI is counted down to 0. 41. Use this fact to help you predict the number that should be in SI when execution reaches the INC Df instruction at offset OAH. Enter a GO command, which wil nan the program to a breakpoint ai the INC DI instruction’s address, . Use the SDK-86 ER command to cheek your prediction, Changing the MOV Si.OFFFFH Instruction so that ft loads a value other than FFFFH into the SI register has an Interesting eflect on the operation, of the program, What SD&-86 command can yon use to change the word that the instruction loads? AL what address in memory is the FFFIH part of the MOV SI. OFFFFH instruction located? |. Use the proper SDK-86 command to change the FFFFH in the MOV SI,OFFFFH instruction to A0FFH, Use the GO command to run the program with no breakpoints in order to see the effect this change hhas om the operation of tie program. . In your report describe the effect produced by the ‘ew value you put in the MOV SI instruction, Experiment with different count values and observe the effects. DEBUGGING PROGRAMS WITH THE SDIC6 KEYPAD MONITOR 9 REFERENCES, Hall: Chapters 2 and 3 Hall: Appendices A and EQUIPMENT AND MATERIALS. ‘SDK-86 microprocessor board INTRODUCTION For your first 8086 programming assignment, we hhave chosen a simple arithmetic example. This exam: pple was chosen because it can be done with just a few Straightforward instructions and because It doesn't require Interaction with any external hardware. In this experiment and the next you will hand cade the Instructions so that you develop a feel for the bit-level Suuucture of microprocessor inatructions. Except for very short programs such as these, hand coding is very tedious and error-prone, so in Experiment 5 and following experiments we show you how to use computer-based tools to develop assembly langage programs. ‘Even though ths program may seem very simple, it 4s important that you follow the program development ‘steps described in Hall: Chapter 3, 80 that these steps are automatic to you when yom write more complex programs. To refresh your memory, we will ‘summarize the steps here. 1, Define very carefully what you want the program odo, 2, Write an algorithm for the program. 8. Choose the instrnetions which will implement each part of the algoritam. 4. Jn some way produce the machine codes for the program. 5. Load the program nto a microcomputer and run rs ©. Debug the program as desestbed in Experiment 2. ‘The more time you spend on the first three steps, the less time you will spend debugging the program and the more likely it Is that the program will ran 0 correctly the frst time. OBJECTIVES At the conclusion of this experiment, you should be abe to : 1. Write the algorithm for a simple arithmetic program. 2. Choose 8085 assembly language instructions that ‘val implement this algorithi. 8. Use 8086 instruction coding templates to produce the machine codes for these Instructions, 4. Enter, run, and debug the program on an SDK-86 ‘ilcrocomputer development beard. PROCEDURE Adding 2 Bytes The develop 2 progran register to a byte in the AL register and copies the ‘sum to the BL register. The contents of the carry flag aiter the addition should be put in the least significant bit of the BH register, The other 7 bits of the BH register shoutd all be mae zeros (inasked) NOTE: Assume that you are going to load the desiced numbers in the specified registers with the SDK-86 ER command before you run the program. 1 programming problem In this exercise i to which adds a byte in the AH Write the algorithm (sequence of steps) you ‘would use to do this addition. Remember, al this Dont just think: of the operations you wait to do im each step, not the instructions you are going to use to implement the algorithm. Mentally step through the algorithm to make sure you havent Jet out any required parts. Use the list of 8086 instructions fa Hall: Chapter 3 to help you choose the appropriate ‘nstruetion(s) for each step of your algorithm, To. help yon with this, think about the following ‘questions. . For the baste addition operation, da you want to use the elmple ADD instruction or the ADC instruction, which adds any carry from a previous operation to the sum? b, What instruction can you use to transfer the sum to BL? © What instruction can you use to rotate the carry flag contents into the Teast significant bit of the BE register? . What logical instruction can be used to mask the upper 7 bits of the BH register without changing the LSB? Put in the INT 3 instruction as the last in- struction of your program. Remember from a dis- ‘cussion in Experiment 1 that Whenever you run ‘2 program, you must in some way make sure that execution stops at the end of Your program land that the processor doesn't go on executing the random bytes that happen to be present in the memory locations after your program. You can do this by inserting a breakpoint as part of the GO command, but in case you forget the breakpoint there, the INT 3 instruction will automatically cause execution to return to the monitor program. 8. When you have written out the ist of assembly Tanguage Instructions you have chosen for this program, refer to Hail: Figure 3-4 to see the ‘overall format you will be using for the program. Don’t wrtte the Ist of insiructions directly on the coding form because you won't know how many bytes each wall require until you code out the in structions. 4, Wilts the mnemonic, operands, and comment for the first instruction on the first line of your ‘coding form. You will be loading the program into ‘memory starting at address 0000:0100H, so put, O100H tn the address column on the paper a3 the offset of the first instruction, 8. Use the coding examples in Hall: Chapter 3 and the coding templates at the end of all: Appendic A or Appendix B to help you determine the binary code for this first instruction. After you have checked it carefully, write the hexadecimal equivalents for the instruction codes in the DATA/CODE column of the program paper. ©. Sequentially write the address offsets, hexadecimal Instruction codes. assembly Janguage mnemonics, operands, and comments for each of the other instructions on the program coding paper 7. Use the EB command to enter the program into ® 10. nL 4, When "be" and/or an RAM starting at address 0000:0100, Use the BB command again to check that you hhave correctly entered the instruction codes. Use the ER command to load 42H into AH and 35H into AL. For future reference, predict the result that these numbers should produce in the BH and BL registers when your program executes. Use the GO command to exeeute the program. shows on the data field LEDs, indicating that the program has run to a breakpoint, use the examine register command to see if the AH, AL, BH, and BL registers contain the predicted numbers. If they do, reolee, and go fon to atep 14. If the registers did not contain the predicted numbers, proceed as follows, . Predict what the contents of the AH and AL registers should be after the first instruction executes. B. Use the single-step command to execute the first instruction. . Use the ER command to see if the actual contents of the BH and BL registers agree ‘vith your predictions, If the contents agree with your predictions, predict the contents that the registers should hhave after the next instruction executes. Then, execute the instruction and cheek the ‘actual contents. Repeat the process until you And the instruction that is not produemg the result you want. ¢. When you find an instruction that is not producing the results you expect, road the etalled description of the instruction in Hall: Chapter 6 to make sure you are using the correct instruction, If you are, cheek the instruction coding to make sure you have coded it correctly. It is usually not too ficult (o find the source of a problem when you narrow it down to one or tvo instructions with the single-step command or a breakpoint ‘When your program works correctly. insert OAH im AH and.78H in AL, Predict the results these ‘numbers should produce in BH and BL. Then run the program again and see if the results agree ‘with your predietions. Did the cary bit get moved correctly into the least significant bit of BE? If st did, you are done with this experiment. If not, ‘debug the program as described in step 18. ASIMPLE 208s ARITHIWETIC PROGRAM. 14 Writing a Program Data in Memory REFERENCES Hal: Chapters 2 and 3 Hall: Appendix EQUIPMENT AND MATERIALS SDK-86 microprocessor board INTRODUCTION For your second programming exercise, we have again chosen an arithmetic example in order to Keep the programming task simple and to make external hardware unnecessary. Experiment 3 worked witi data operands contained exclusively in registers. This Jab works with data operands contained in memory. OBJECTIVES [At the conclusion of this experiment, you should be able to write an assembly language programa to 1. Add a BCD byte frm one memory location to a BCD byte from the next higher memory location. 2, Adjust the result to a legal BCD number, 3. Store.the-BCD.sum in the next higher memory address and any carry from the addition in the following memory location. PROCEDURE Accessing Data in Memory 1. Draw a memory diagram showing the relative locations of the two BCD mumbers to be added, the sum, and the carry in successive memory addresses, 2, Write the algorithm for the program to add the two BCD numbers and save the sum and carry Remember that afler a BCD addition, the sum must be adjusted to make sure that the result is legal BCD number. 8, Choose and write the series of assembly language instructions that will implement your algorithm. To help you choose the correct, instructions, answer the following questions. ‘a. What instructions are required to initialize ‘the DS register so that program instructions ‘can access the data locations tn memory? . Is the ADD or the ADC instruction more reasonable to use for the addition in this program? ‘6 What instruction must be used to adjust the sum to the correct BCD form? 4, What instructions can be used to copy the carry flag into the least significant bit of a byte and leave zeros in the upper 7 bits of the byte? Use a different method from the one you used in Experiment 2 ce, What instruetion should you put at the end fof your program so that the 8086 will not aceldentally execute the random contents in RAM alter your program? . AL the tap of your coding form. set aside <-byte ‘sized memory locations for the two numbers to be added, the sum, and the earry. For an SDK- 186 board, these spaces can start at offset O100H, 50 write “offset O100H next to the first dats byte. For the first run of your proiram, try adding the BCD numbers 46H and 38H. Enter these numbers on the coding form and put zeros fm the sum and carry locations, so that you can. tell if the program puts anything tn these locations when it runs. The instruction codes for your program care be put in memory after the ‘ata bytes. At what offset will you put the first instruction byte for your programm? Write your first assembly language instruction fon the coding form after the data bytes. This frst instruction will probably be MOV AX, (the segment base for the data segment containing numbers to be added). The segment base address Is simply a number, so this instruction ‘can be coded simply as a MOV immediate to register instruction. Find the coding template for {his instruction at the ene of Hall: Appendix A or Appendix B. ‘According to the termplate in Appendix B, the hasie opcode for the MOV immediate word to segister instruction is BSH + the 8-bit code for the destmation register. The S-bit code for the AX. register {s 000, 0 the basie opcode for the ‘instruction fs simply BSH. The 16-bit number to be loaded into the AX register is coded Im as bytes 2 and 3 of the instruction. The least significant byte of the word to be loaded fs put in the first byte after the opcode, and the high byte of the word is put in the next byte. An. Important point to remember ie that the low ‘byte goes in the lower address, ‘To determine the actual 16-bit number you want to code into this instruction, ask yoursell the question, What is the segment base address for the data segment I set up at the top of the coding form? Write this number in as bytes 2 ‘and 3 after the basic opeade for the instruction (on the coding form, . Write the remaining instructions for your pro- gram one at 2 Ume, and work out the codes for each, Use the examples in Hall: Chapter 3 to help you work out the codes for the instructions that access locations in the data segment. Remember, the direct address or displacement required in these instructions is coded th as byins 8 and 4 of the instructions, law byte fist. + When you have coded all your program, recheck the program to make sure that you have used the correct instructions and that you haven't left any displacements o7 offsets out of the instruction codes, Then enter the data and code bytes in the RAM of the SDK-86 board, correct BCD resulk for adding 46H and 38H fs BAH with no carry. Run your program and then check the suni memory location to see if {Your program produced this result. Also check if the carry byte Is zero, as st should be. If the results In memory are not correct, use a breakpoint or single stepping to debug the rogram. 9. When you get the Basie program operating correctly, load the data memory locations with the BCD values 89H and 93H. Prediet the BCD values that these numbers should produce. then run the program and sce if your program produced the predicted sum and carry. If it diel snot. find the probiem, 10. Modify your program so that it subtracts the second BCD number from the first BCD number and puts the correct BCD difference in the next memory location. Any borrow produced by the ‘subtraction should be put in the next memory location after the difference. Again, to help yon choose the correct instruction, answer the following questions 4. What instruction should you use to do the subtraction? b, What instruction is required to adjust the result of the subtraction to BCD form? 21, Load the program and some test values in memory, run the program, and debug tt, if necessary, By now you have probably decided that hand cotling and Keying tn 8086 programs ts a painfu taal. in the next experiment we start showing you how to use a ‘compuier to do these tasks so that you can develop ‘SoIne nore interesting programas. WRITING A PROGRAM WHICH ACCESSES DATA IN MEMORY 13 Using a Computer to Develop Assembly Language Programs REFERENCES Hall: Chapter 3 TBM or Microsoft DOS manual ‘Manual for the text editor you are using Borland Turbo assembler (TASM) manual or ‘Microsoft MASM manual EQUIPMENT AND MATERIALS IBM PC or PS/2-compatible computer Painter Formatted disk to save work files INTRODUCTION [As described in Hall: Chapter 3, assembly language programs can be developed much more easily with a Computer than with the hand-coding and keying method used in Experiments 3 and 4. To develop assembly language programs on a computer, several tools (programs) are used. ‘The first programming tool you use is an editor program. An editor program, when run. allows you to fype in the maemonles, operands, and comments for ‘your program. Characters unll be displayed on the Seren as you ‘ype them in. Sines the characters you type in are initially Just stored in RAM In the computer, you eam easily go back and correct any mistakes you may have made. When you get your program entered into RAM in the form you want, you Save it in a named file on disk. This inital version of your program is ealled the source file. ‘There are a great many available editors that can be used to develop the source files for assembly language programs. One of these is EDLIN, the line oriented editor that comes with DOS. This editor is Somewhat incanventent to use for all bul the very Shortest programs, so we recommend a screen. oriented editor. Examples of this type of editor are the ‘Shareware’ editor PCWRITE, Wordstar™, and the editor that is part of the Borland C++ Integrated Development Environment, which we use in Tater exercises, The main requirement is that the editor be fable to generate pure ASCII text Mes. “The editor we have chosen to use here is the Borland Turbo C++ Integrated Development Bavironment editor, If you are using some other ceditor. you can just replace the editor section of this Exercise with the appropriate sections from the manual for your editor. ‘The next program you use In developing assembly language programs is an assembler. An assembler tanslates the assembly language instructions in your souree program to the equivalent binary codes. which can be executed. Two commonly avatlable 8086 assemblers for the IBM PC are Borland's Turbo ‘Assembler, TASM, and Microsoft's Macro Assembler, MASM. We tse TASM for the discussions here. MASM source file requirements are nearly the same as chose for TASM, 50 if you have MASM, you can easily adept our discussion. We wall point out any diferences as wwe go along, ‘aller you get your program to assemble correctly, you LINK the .OBY file produced by the assembler to produce an executable (EXE) fle. in later exercises lve show you how to load and run the .EXE file. To link @ program you will use the LINK program that | came with the assembler you are using. OBJECTIVES At the conelusion of this experiment, you should be able to 1. Use the simple DOS commands DIR, MODE. ‘TYPE, and PRINT. 2. Use an editor to write and modify an assembly language program. 3. Use an assembler to assemble a program and observe the assembler error messages. 4, Print a program listing using DOS commands. 5. Link a .OBS fle to produce a .EXE file which ean be Toaded and nun. NOTE: The computer commands or file names shown ean be entered as elther upper- or lower: ase letters. Commands are shown elther as Single letters or a8 a group of leters surrounded by <>, Letters contalned in the <> represent a single key on the keyboard, For instance, the carrlage-return key 18 shown as . The key Is always used with another Key, in the same way that the shift key is used with another key. For instance. the command -C, sometimes shown as 2C, means “hold dows the key and then press C. ‘This exercise and the following exercises assume tat the required programming tools are installed on a hard disk Identified as C:, a path command has been inserted in the autoexec.bat file so that a path does not need to be specified as part of each command, and you are using a floppy disk in the A: drive to store your program files, PROCEDURE Starting (Booting) the System and Getting Started 1 Turn on the power to the computer and the power to the CRT moniter. If your computer does not have @ builtin clock, ‘you may be prompted to enter the date. Enter the ‘new date in the form shown on the screen, For example, June 0, 1991, would be entered as 6- 30-81. Likewise, you may be prompted to enter the tme. If so, enter the time sa the format shown on the sereen, 8. The machine will then respond with the Disk Operating System (DOS) command prompt C>. ‘The prompt tells you that you are logged on the C: drive. This means that if you do not specily a drive Jeiier In a command, che computer will assume that you mean the C: drive, When the prompt is present, the machine is ready for you to enter a 2, DOS commend, For reference, Piguce 6-1. shows the functions of some useful DOS commands, ‘AS an example of how to use these ‘commands, enter the command DIR/P You should now see @ DiRectory listing of all the {les and subdirectories on the C: drive. Mf your machine is connected to a printer, you can gel a printout {hard copy) of « directory by entering the command DIR>PRN 4, Insert your formatted work disk into drive A: and gently close the drive door. ‘Type Ai to change the logged drive to A: so that all your work files will automatically be vritten to your Boppy. Using Borland Turbo C++ Editor to Enter a Source Program 11. With the A> prompt showing, type tox to run the program. After a short pause the opening screen should appear. Press the Flo key to put the cursor in the banner menu along the top of the screen. Bach entry in this banner represents a menu of available commands. Use tie arrow Keys to move the cursor box to the leftmost entry in the banner and press the key. A menu of commands should appear. 2. Read through this menu to get an overview of the BOS COMMAND ACTION brR/P List directory of disk in logged drive, pause when ecreen is full. DIR/W A: List wide @irectory of disk in a: drive DIR A:>PRY Send directory list of A: te printer MODE Leri:132 Set printer to 132 characters widen

to run. -s Stop display scrolling, press any key to ‘continue. - sctrl>- Cancel command Copy CRT screen to printer. PRINT A:Pilenane.ST Print hardcopy of Filenane.LST from A: ‘TYPE A:Filenane.ASK List on CRT, Pilename.ASM from At ERASE A:Filenams.BAK Erase Filename.BAK from A: FIGURE 5-1. Useful DOS command USING A COMPUTER TO DEVELOP ASSEMBLY LANGUAGE PROGRAMS 15,

You might also like