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
GROUP EVALUATION FORM Laboratory 1: Introduction to Assembly Language Programming Tools Group No: Section: LAB SCHEDULE: Group Members: DATE PERFORMED: 1. DATE SUBMITTED: 2. Exercises 3. Prog’g Problems 4. Teamwork (+pts) 5 EVALUATION GRADE: EVALUATION: (for lab instructor use only) Exercises (50%): Prog’g Problems (50%): Lab Performance (10%): Attendance (10%): LABORATORY NO. 1 Introduction to Assembly Language Programming Tools I. Objective To introduce the following assembly language programming tools: DEBUG Netwide Assembler (NASM) Emulator 8086 (EMU8086) II. Components to be borrowed PC power cords (2) Keyboard (1) Mouse (1) III. Conceptual Framework A. DEBUG is a program included in the MS-DOS and PC-DOS operating systems that allows the programmer to monitor program’s execution closely for debugging purposes. Specifically, it can be used to examine and alter the contents of memory to enter and run programs, and to stop programs at certain points in order to check or even change data. Figure 1 shows the DEBUG environment. FLAGS Figure 1. DEBUG Environment Commands in DEBUG 1.1 Entering and exiting DEBUG To enter the DEBUG program, simply type its name at the DOS level: A :\> DEBUG<return> After DEBUG and enter key (carriage return) have been entered, the DEBUG prompt “-“ will appear on the following line. DEBUG is now waiting for you to type in a command. To exit Debug, simply type Q (quit command) after the DEBUG prompt: - Q <return> After the Q and enter key (carriage return) have been entered, DEBUG will return you to the DOS level. 1.2 Examining and altering the contents of registers R, the REGISTER command. The register (R) command allows you to examine and/or alter the contents of the internal registers of the CPU. The R command has the following syntax: - R <register name> The R command will display all registers unless the optional <register name> field is entered, in this case only register named will be displayed and/or altered. 1.3 Coding and running programs in DEBUG j.tio Lab1 2 A, the ASSEMBLE command. The assemble command is used to enter assembly language instructions into memory. - A <starting address> The starting address may be given as an offset number, in which case it is assumed to be an offset into the code segment, or the segment register can be specified explicitly. U, the UNASSEMBLE command. The unassembled command displays the machine code in memory along with their equivalent assembly language instructions. The command can be given in either format shown below: - U <starting address> <ending address> - U <starting address> <L number of bytes> If the U command is entered with no addresses after it: “U <return>”, then DEBUG will display 32 bytes beginning at CS:IP. G, the GO command. The GO command instructs DEBUF to execute the instructions found between the two given addresses. Its format is: - G <=starting address> <stop address(es)> If no addresses are given, DEBUG begins executing instructions at CS:IP until a breakpoint is reached. After a breakpoint is reached, DEBUG displays the register contents and returns you to the command prompt. Up to 10 stop addresses can be entered. DEBUG will stop execution at the first of these breakpoints that it reaches. T, the TRACE command. The trace command allows you to trace through the execution programs one or more instructions at a time to verify the effect of the programs on registers and/or data. Its format is: - T <=starting address> <number of instructions> The trace command functions similarly to GO command in that if no starting address is specified, it starts at CS:IP. 1.4 Data Manipulation in DEBUG D, the DUMP command. The dump command is used to examine the contents of memory. The syntax of the D command is as follows: - D <start address> <end address> - D < start address> <L number of bytes> The D command can also be entered by itself, in which case debug will display 128 consecutive bytes beginning at DS:100. F, the FILL command. The fill command is used to fill an area of memory with a data item. The syntax of the F command is as follows: - F <starting address> <ending address> <data> - F <starting address> <L number of bytes> <data> This command is useful in filling a block of memory with data, for example to initialize an area of memory with zeros. E, the ENTER command. The enter command can be used to enter a list of data into a certain portion of memory. - E <address> <data list> - E <address> For example, - E 100 ‘John Smith’. This example showed how to enter ASCII data, which can be enclosed in either single or double quotes. 1.5 Loading and Writing programs N, the NAME command. The name command initializes a filename in memory before using the load and write commands. j.tio Lab1 3 Its format is : - N <drive name:> <filename> <extension name> B. After the code has been entered with the A command, CX must be set to contain the number of bytes to be saved and register BX must be set to 0. W, the WRITE command. The write command is used to save instructions onto a disk. Its format is: -W L, the LOAD command. The load command performs the opposite function of Write command. It loads from disk into memory starting at the specified address. Its syntax is: -L NASM is an acronym for Netwide Assembler, unlike DEBUG that is specifically for debugging purposes, this software assembles program written in assembly in any platform. NASM Integrated Development Environment (NASMIDE) is complete software that has an editor, assembler and builder. Figure 2 is the NASMIDE environment. NASM was among the first of the Open-Source, freely available, assemblers available for the x86. The project was started in the 1996 time frame as a way of creating a portable x86 assembler. * Source: Webster Figure 2. NASMIDE environment Note: Use Help option to explore the environment. NASM Program Structure: ;NASM-IDE ASM Assistant Assembler Project File BITS 16 ;Set code generation to 16 bit mode ORG 0x0100 ;Set code start address to 0100h Setting of bit mode and IP address section .text j.tio MAIN: jmp START DISP: mov ah,09h Lab1 ;String Print int service 4 Main Program Data Assignment C. EMU8086 is an 8086 machine emulator licensed software. It provides a virtual machine to emulate 8086microprocessor system and provides assembly language programming and simulation. Figure 3 shows the EMU8086 environment. Figure 3. EMU8086 Environment Emu8086 combines an advanced source editor, assembler, disassembler, software emulator (Virtual PC) with debugger. *Source: Help option of Emu8086 IV. Procedure DEBUG Environment 1. 2. j.tio Type DEBUG at the command prompt of DOS then press ENTER key. Command: R Lab1 5 A. You will see a dash “-“ prompt that signifies DEBUG environment. Type R/r on this prompt then ENTER key. Illustrate the output: B. Type the following commands. Write the contents or observation on the space provided: REGISTER IP COMMAND - R IP CX -R CX AX - R AX DH -R DH CONTENTS/OBSERVATION C. Write the appropriate command to modify the contents of the following registers. 3. REGISTER AX NEW CONTENTS 0001 CX 0021 IP 0100 Command: A Assemble the given program at the starting offset address 100h. Type A 100 then press ENTER key. Encode the program written below: CS:0100 4. COMMAND MOV AX,1 MOV BX,2 MOV CX,3 ADD AX,BX ADD AX,CX INT 3 Command: U A. Write the command that will unassemble the program in number 3: Command: __________________________________________ B. What are the equivalent machine codes of the following instructions? INSTRUCTION MOV AX,01 MACHINE CODE MOV CX,3 ADD AX,BX j.tio Lab1 6 5. Command: G A. Execute the program in number 3. Type the given command Command: –g =0100 B. 6. A. B. C. D. What are the contents of the following registers? AX BX CX Command: T Reset the values of AX, BX and CX and set value of IP to 0100. Execute program given in number 3 using trace command. Type T or t at the DEBUG prompt, then press ENTER key. Repeat this step until all instructions are executed. What are the contents of the following registers after executing each instruction? INSTRUCTION MOV AX,1 AX BX CX MOV BX,2 MOV CX,3 ADD AX,BX ADD AX,CX 7. Command: D A. Illustrate or describe the output after executing the following D commands: COMMAND OUTPUT D 100 10F D CS:110 120 B. Type in the command: ___D_______ No. of bytes displayed: ___________ Beginning Address: ______________ Ending Address: ________________ 8. j.tio Command: F A. Determine the contents of the following blocks of memory, after executing the F commands. You may use the D command to display the contents: COMMAND - F 100 10F FF BLOCK OF MEMORY 100 – 10F - F 100 L20 00 FF 100 – 11F Lab1 DATA CONTENTS 7 B. Fill the following blocks of memory with the specified data. Write the appropriate command. BLOCK OF MEMORY DATA COMMAND 100 - 110 00 11F – 130 9. 00,01,10,11 (alternately) Command: E A. Enter the data ‘John Snith’ at starting address 100h: Command: ______________________________________ B. Modify the data ‘John Snith’ to ‘John Smith’ (ASCII code of m=6D) Command: ______________________________________ 10. Command: N, W, L A. Assemble the given program at starting address 100h. Write the command on the space provided. Command: ____________________________________ CS:0100 CS:0103 CS:0106 CS:0108 CS:010A CS:010B CS:010C CS:010E CS:0111 B. MOV MOV MOV ADD INC DEC JNZ MOV INT CX,05 BX,0200 AL,0 AL,[BX] BX CX 0108 [0205],AL 3 Name the file ”LAB1.com” and save it in drive C. Set the value of CX with the total number of bytes of the program and set BX to 0 before saving. Write the commands for naming and saving the file. Command (Naming): ____________________________ Command (Saving): _____________________________ Note: Check the file in drive C. C. Exit from DEBUG and load the saved file (LAB1.com) by typing DEBUG LAB1.com from the DOS prompt. Use Unassemble command; do you see the program code LAB1.com? ________ NASMIDE Environment TABLE 1: Translation of C program statements to Assembly codes: C program statements Assembly Equivalent codes printf(“Hello World! /n”) section lea mov int .text dx,[msg1] ah,09h 21h section .data msg1 db ‘Hello World’,13,10,’$’ printf(“%c”, ‘A’) j.tio section .text Lab1 8 mov ah,02h mov dl,’A’ int 21h scanf(“%d”, num) section .text mov ah,01h int 21h Note: value of num will be stored at AL if (x <> 0) statements here(TRUE) else statements here(FALSE) section .text cmp al,0 jz TRUE jmp FALSE for (i=5;i<>0; i--){ section .text mov CX,5 ;CX is equal to i next: nop : loop next ;loop instruction decrements CX by 1 then checks if CX is equal to 0 : } i=0; while(i<=5){ : i++; section .text mov next: add cmp jle ;al is representing x al,0 al,1 al,5 next ;al is equal to i } main(){ : section .text mov ah,4ch int 21h } //exit of C 11. Run the NASMIDE software located at c:\NASM. Type NASMIDE at the prompt C:\NASM\> nasmide 12. Write the code below in NASMIDE editor and save it as printf.asm. BITS 16 ORG 0x0100 ; Display String [section .text] j.tio MAIN: jmp START DISP: mov ah,09h int 21h ret START: lea dx,[msg] call DISP END: mov ah, 4ch int 21h ;String Print INT service ;Display Lab1message ;call DISP subroutine program ;End of program 9 13. Run the program (a shortcut key to run the code is CTRL-F9). Illustrate the output on the space provided. 14. Create another code (given below) in NASMIDE editor and save it as scanf.asm. BITS 16 ORG 0x0100 ; Input a number and display ; Author: J. TIO [section .text] MAIN: jmp START DISP: mov ah,09h ;string print int 21h ret START: lea dx,[msg] call DISP mov ah,1 ;input char int 21h ;char entered placed to al END: mov ah, 4ch ;End of program int 21h section .data msg db “Input a number: ”,”$” 15. Compile and run the program. Illustrate the output on the space provided. 16. Create another code (given below) in NASMIDE editor and save it as if.asm. j.tio bits 16 org 0x0100 [section .text] MAIN: jmp START DISP: mov ah, 09h int 21h ret ONE: lea dx,[msg2] call DISP Lab1 10 c:\NASM\> printf 17. Compile and run the program. 18. Input number 1 then press ENTER key. Repeat this step then input the numbers 2,3 and 4 for each run. 19. Illustrate the outputs on the space provided. j.tio Lab1 11 EMU8086 Environment 20. Invoke EMU8086 software. 21. Click the Samples Icon then choose Hello file. 22. Click Emulate icon, then choose Run icon. 23. Illustrate the output. 24. Close the Emulator, then repeat steps 21 and 22 now choose traffic_lights file. Click Stop icon if you want to stop running the code. Describe the output. 22. Close the Emulator, then create a new file, choose COM template. 23. Write the given code below and save it as samplecode.asm. MOV AL, 80H MOV BL, 80H ADD AL, BL MOV AH,4CH INT 21H 24. Compile and run the code, now is single step. Write you observations on the space provided. 25. Create another code (given below) in EMU8086 editor and save it as string.asm. #make_COM# j.tio ; COM file is loaded at CS:0100h Lab1 ORG 100h ;string input 12 26. Compile and run the program. 27. Input the string “COMSYLA!”. Illustrate the output on the space provided. V. Programming Problems 1. Trace the given code below using DEBUG. Write the register and flag’s content on the space provided. Verify the result by computing it manually. INSTRUCTION MOV AX,1234h AX Flag Register ADD AX,5678h ADC AX,9ABCh j.tio Lab1 13 INT 3 *Flag Register Content – refer to figure 1 2. Make an assembly program that will translate decimal values (0 to 15) to its equivalent hexadecimal code. Use EMU8086. Example: Input a number (0 to 15): 15 Equivalent Hex code: F 3. Make an assembly program that will display the equivalent alphanumeric characters of ASCII codes 41h to 49h using loop. Use NASMIDE. Example: Alphanumeric Characters of ASCII values 41h to 49h: j.tio Lab1 ABCDEFGHI 14