Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Emulator (Emu8086) Overview Imagine we want to run a program on the MDA-8086 microprocessor training kit. You should first write the program in assembly then – according to the 8086 instruction set formats – convert from assembly to machine code then enter the program through the keyboard on the kit then run your program. If you are not satisfied with the results, there may be an error in your program, which is most probably, or in the format converting. In the first case you should review your code and fix the error then you will reconvert the fixed code in both cases. If after running the program there are more unsatisfied results you should review and fix your code again until you reach your goal. Our emulator (Emu8086) has the advantage of writing with assembly language directly and converting to machine language automatically to write it through the keyboard into the kit. Actually we can generate an EXE file from the emulator and run itt. The program has the capability to show the conditions of the general registers file (AX, BX…), instruction pointer (IP), segment register file (CS, DS…), flags, stack, and memory locations, which simplify assuring the steps of the program. Another advantage in this emulator, the program can be executed step by step to get the error position or can be executed altogether. Example on the emulator to see how it works Open the emulator (ALL programs -> Emu8086 version 3.04-> Emu8086 version 3.04), press continue. 1. Launch the EMU8086 emulator. Choose “New” and specify “COM template”. 2. Using the assembler editor, enter the following codes: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #make_COM# ; COM file is loaded at CS:0100h ORG 100h MOV AL,10001100B ;AL=8C MOV BL,var1 ;BL=[var1]=5 ADD AL,BL ;AL=AL+BL=91 & AF=1(AUXULARY FLAG) ADD BL,0FBH ;BL=BL+FB=00 & AF=1,CF=1,ZF=1 HLT ;stop here var1 DB 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The symbol (;) acts like (//) in C++, writing a comment. After writing the previous program, it should appear like the following: 3. Start emulation by clicking the “emulate” button emulator window will appear. on the toolbar. A new You can see in it the general registers file (AX, BX…), instruction pointer (IP), segment register file (CS, DS…), flags, stack, and memory locations. You can also see the Arithmetic and Logic Unit (ALU) inputs and output. You can see your code in the most right yellow column and the machine code of the program in the next yellow column. As you see this looks a lot like our example, except that variables are replaced with actual memory locations. When compiler makes machine code, it automatically replaces all variable names with their offsets. By default segment is loaded in DS register (when COM files is loaded the value of DS register is set to the same value as CS register - code segment). The offset of var1 is 010Ch, and full address is 0B56:010C. In memory list first row is an offset, second row is a hexadecimal value, third row is decimal value, and last row is an ASCII character value. Q1. What are the values of CS,DS,SP, and IP ? 4. Single-step the program codes by pressing the “single step” button. Q2. Record the values of IP, AL, BL, and the flags (CF,AF,ZF). Hint: to show the contents of flags by pressing the “FLAGS” button. 5. Repeat step 4 until the end of the program. To run the whole program (not step by step), click the Run icon, and then you can see the final result of your program. To re-execute the program click on the Reload icon or write the first IP address in the program – which is here IP=0100 – in the IP field then click on Run icon. Code example 2 1. Open new File. Choose “New” and specify “COM template”. 2. Using the assembler editor, enter the following codes: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #make_COM# ; COM file is loaded at CS:0100h ORG 100h CLD LEA SI, str1 LEA DI, str2 MOV CX, 8 L2: LODSB SCASB JNZ L1 LOOP L2 MOV DL, 'Y' JMP L3 L1: MOV DL, 'N' L3: HLT str1 DB 4, ‘A’, 6,’ 6’, 2, 6, 0, 5, 8 str2 DB 4, ‘A’, 6, 6 , 2, 1, 0, 9, 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Q3. What is the equivalent instructions for LODSB and SCASB ? Hint: you can use the “Complete 8086 instruction set” tutorial from (help->Emu8086 reference-> Complete 8086 instruction set) ,OR from the help folder at the source directory of the emulator,to know how the instructions are executed and what are the flags affected by these instructions. 3. Start emulation by clicking the “emulate” button on the toolbar. A new emulator window will appear. 4. Press Four Single-steps. Q4. What are the values in SI,DI,CX, and DF (Direction Flag)? 5. Each time after single-stepping, observe and record down the contents of the affected registers and memories, answer the following question. Q5. Find the values in AL, SI (after execution LODSB), the contents of [DS:SI] and [ES:DI] for each Loop, ZF and DI (after execution SCASB) .For jump instructions (JNZ L1 and LOOP L2) determine the jump will taken or not, you must use Single step until end of the program to answer this question and record the values in the table. To show the contents of memory A. In the emulator window, select “view” “External memory viewer” . A “External memory viewer” window will appear. B. Type the segment value (DS=CS=SS=0B56, for this example only) and the offset value (according to the values of DI or SI) in the address fields, click the “show the memory at” button and observe the memory contents shown in the “External memory viewer” window. Q6. What is the final values in flags (ZF, CF, AF, OF), AL, CX, and DL? Code example 3 1. Open the samples folder by clicking on Samples tab 2. Choose and open “int21” file. 3. The following will be written on the editor window %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ; This sample gets a string from a user, and prints it out. ; INT 21h is used, thus DOS operating system is required. #make_COM# ORG 100H ; COM file is loaded at 100h ; set data segment: MOV AX, CS MOV DS, AX MOV ES, AX ; input a string: MOV DX, OFFSET s1 MOV AH, 0AH INT 21h ; get one line down, by printing new line characters: MOV DX, offset nl MOV AH, 9 INT 21h ; set '$' to the end of inputed string: MOV DI, offset s1 XOR BX, BX ; second byte of buffer holds the number of inputed characters: MOV BL, [DI+1] MOV BYTE PTR [DI+BX+2], '$' ; print the entered string: MOV DX, offset s1 ; first byte is buffer size, second actual characters entered, we skip these 2 bytes: ADD DX, 2 MOV AH, 9 INT 21h ; exit to operating system: MOV AH, 4Ch INT 21h ; data: s1 DB 30, 30 dup(' ') nl DB 13, 10, '$' ; new line characters END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Hint: you can use the “List of supported interrupts” tutorial from (help->Emu8086 reference-> List of supported interrupts) ,OR from the help folder at the source directory of the emulator,to know how the interrupts are executed. INT 21h / AH=0Ah - input of a string to DS:DX, first byte is buffer size, second byte is number of chars actually read. INT 21h / AH=09h - output of a string at DS:DX. 4. Read the above code with its comments and try to understand it. Hint: '$' represents of the null character. Like C++ programming to end the array string. 5. Start emulation by clicking the “emulate” button on the toolbar. A new emulator window will appear. 6. Run the program codes by pressing the “Run” button. 7. The user screen will appear, type your name on it and press “Enter”. Q7. What is the output on the screen? Q8. What are the memory contents at(in decimal Formats) [DS:012E] and [DS:012F] ? Q9. What are the memory contents (in ASCII Formats) from ([DS:012E+2]=[DS:0130]) to [DS: (130+[DS:012F]) ]. Name(Arabic): Sec: B.N. Answer Sheet Q1. CS= DS= SS= IP= Q2. IP AL BL CF AF ZF Q3. LODSB ≡ SCASB ≡ Q4. SI= Q5. CX AL after LODSB DI= SI after LODSB [DS:SI] CX= [ES:DI] ZF after SCASB DF= DI after SCASB JNZ L1 LOOP L2 8 Q6. AL= H, CX= H, DL= „ „, and ZF= , CF= , AF= , OF= Q7. The output on the screen is: Q8. [DS:012E]= [DS:012F]= Q9. {([DS:012E+2]=[DS:0130]) to [DS: (130+[DS:012F]) ]}=” ”