Introduction to Assembly Language Programming Tools

Total Page:16

File Type:pdf, Size:1020Kb

Introduction to Assembly Language Programming Tools

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

I.1 Entering and exiting DEBUG  To enter the DEBUG program, simply type its name at the DOS level: A :\> DEBUG 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 After the Q and enter key (carriage return) have been entered, DEBUG will return you to the DOS level.

j.tio Lab1 2 I.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 The R command will display all registers unless the optional field is entered, in this case only register named will be displayed and/or altered.

I.3 Coding and running programs in DEBUG  A, the ASSEMBLE command. The assemble command is used to enter assembly language instructions into memory. - A 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 - U If the U command is entered with no addresses after it: “U ”, 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> 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> The trace command functions similarly to GO command in that if no starting address is specified, it starts at CS:IP.

I.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 - D < start address> 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 - F 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

j.tio Lab1 3 - E
For example, - E 100 ‘John Smith’. This example showed how to enter ASCII data, which can be enclosed in either single or double quotes.

I.5 Loading and Writing programs  N, the NAME command. The name command initializes a filename in memory before using the load and write commands.  Its format is : - N

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

B. 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.

Figure 2. NASMIDE environment

Note: Use Help option to explore the environment.

j.tio Lab1 4 NASM Program Structure: ;NASM-IDE ASM Assistant Assembler Project File

BITS 16 ;Set code generation to 16 bit mode Setting of bit mode and IP ORG 0x0100 ;Set code start address to 0100h address

section .text

MAIN: jmp START DISP: mov ah,09h ;String Print int service int 21h ret Main Program START: lea dx,[msg1] ;Display message call DISP END: mov ah, 4ch ;End of program int 21h

section .data Data Assignment msg1 db 13,10,‘Hello World!','$'

C. EMU8086 is an 8086 machine emulator licensed software. It provides a virtual machine to emulate 8086- microprocessor 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 j.tio Lab1 5 IV. Procedure

DEBUG Environment

1. Type DEBUG at the command prompt of DOS then press ENTER key. 2. Command: R 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 COMMAND CONTENTS/OBSERVATION IP - R IP CX -R CX AX - R AX DH -R DH

C. Write the appropriate command to modify the contents of the following registers.

REGISTER NEW CONTENTS COMMAND AX 0001 CX 0021 IP 0100

3. 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 MOV AX,1 MOV BX,2 MOV CX,3 ADD AX,BX ADD AX,CX INT 3

4. 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 MACHINE CODE MOV AX,01 j.tio Lab1 6 MOV CX,3 ADD AX,BX

5. Command: G A.Execute the program in number 3. Type the given command

Command: –g =0100

B. What are the contents of the following registers? AX BX CX

6. Command: T A.Reset the values of AX, BX and CX and set value of IP to 0100. B. Execute program given in number 3 using trace command. C.Type T or t at the DEBUG prompt, then press ENTER key. Repeat this step until all instructions are executed. D.What are the contents of the following registers after executing each instruction?

INSTRUCTION AX BX CX MOV AX,1 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. 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 BLOCK OF MEMORY DATA CONTENTS - F 100 10F FF 100 – 10F

- F 100 L20 00 FF 100 – 11F

j.tio Lab1 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 00,01,10,11 (alternately)

9. 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 MOV CX,05 CS:0103 MOV BX,0200 CS:0106 MOV AL,0 CS:0108 ADD AL,[BX] CS:010A INC BX CS:010B DEC CX CS:010C JNZ 0108 CS:010E MOV [0205],AL CS:0111 INT 3

B. 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 .text lea dx,[msg1] mov ah,09h int 21h

section .data msg1 db ‘Hello World’,13,10,’$’

printf(“%c”, ‘A’) section .text j.tio 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) section .text statements here(TRUE) cmp al,0 ;al is representing x else jz TRUE statements here(FALSE) 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; section .text mov al,0 ;al is equal to i while(i<=5){ next: add al,1 : cmp al,5 jle next 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

BITS 16 ORG 0x0100 12. Write the code below in NASMIDE editor and save it as printf.asm. ; Display String

[section .text]

MAIN: jmp START DISP: mov ah,09h ;String Print INT service int 21h ret START: lea dx,[msg] ;Display message call DISP ;call DISP subroutine program END: mov ah, 4ch ;End of program int 21h j.tio Lab1 9 section .data msg db ‘Welcome to COMSYLA!’,13,10,’$’ 13. Run the program (a shortcut key to run the code is CTRL-F9). Illustrate the output on the space provided.

bits 16 org 0x0100 [section .text] MAIN: jmp START DISP: 14. Create mov another ah, code09h (given below) in NASMIDE editor and save it as scanf.asm. int 21h BITSret 16 ONE:ORG 0x0100 lea; Input dx,[msg2] a number and display call; Author: DISP J. TIO jmp[section START .text] MAIN: TWO: jmp START leaDISP: dx,[msg3] call mov DISP ah,09h ;string print jmp intSTART 21h THREE: ret leaSTART: dx,[msg4] call leaDISP dx,[msg] jmp STARTcall DISP ELSE: mov ah,1 ;input char lea dx,[msg5]int 21h ;char entered placed to al END: call mov DISP ah, 4ch ;End of program jmp intEND 21h

START:section .data msglea dbdx, “Input [msg1] a number: ”,”$” 15. Compile call and DISP run the program. Illustrate the output on the space provided. mov ah, 1 int 21h cmp al,'1' je ONE cmp al,'2' je TWO cmp al,'3' 16. Create another code (given below) in NASMIDE editor and save it as if.asm. je THREE jmp ELSE END: mov ah, 4ch int 21h

section .data msg1 db 'Input numbers from 1 to 3: ','$' msg2 db 13,10,'The number entered is ONE $' j.tio msg3 db 13,10,'The numberLab1 entered is TWO $' 10 msg4 db 13,10,'The number entered is THREE $' msg5 db 13,10,'The number entered is NOT WITHIN THE RANGE $' 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.

#make_COM# MOV AL, 80H ; COM fileMOV isBL, loaded 80H at CS:0100h ORG 100hADD AL, BL

;stringMOV input AH,4CH INT 21H mov buffer, 255 mov dx, offset buffer mov ah, 0ah int 21h

lea dx, [msg] ;display msg mov ah,9 int24. Compile21h and run the code, now is single step. Write you observations on the space provided.

mov bl, byte ptr [buffer+1] ;get length of string input mov byte ptr[buffer+2+bx],'$' ;assign $ after string

lea dx, [buffer+2] ;see discussion below mov ah,9 int 21h

mov ah,4ch ;terminates program int 21h

msg db 13,10, 'Welcome to $' buffer db 257 DUP(?) ;define buffer ;with 255 char + 1 byte for buffer size ;+ 1 byte for actual size of data =257

;The 1st byte of the buffer area contains the maximum number of keyboard char. ;The25. Create2nd byte another of thecode buffer (given below)contains in EMU8086 the count editor of andthe saveactual it as string.asmnumber of. char typed. ;The remaining locations in the buffer contain the ASCII keyboard data. ; ;Source: pg 238 of The Intel Microprocessors by Barry Brey j.tio Lab1 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 AX Flag Register MOV AX,1234h ADD AX,5678h ADC AX,9ABCh INT 3 j.tio Lab1 13 *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: ABCDEFGHI

j.tio Lab1 14

Recommended publications