Faculty of Information Technology Computer Systems Engineering Department Assembly Lab ENCS 311 EXP. No. 1 Introduction to Assembly Language 1.1 Objectives: The purpose of this experiment is 1. To provide a brief overview of the 8086 programming model. 2. Introducing Debug and TASM programs 3. To define the steps required to create, assemble, link and debug Assembly programs using MS Notepad and the TASM, TLINK and DEBUG,. 1.2 Introduction: Assembly language is an “Intermediate-level” programming language which is higher than machine language and lower than high-level language such as Basic, C, FORTRAN, or Java. It is easier to use (as it uses a human readable instructions) but runs slower than machine language; it is more difficult to use but runs faster and uses much less memory than high-level language. Programs written in assembly language are converted into machine code which is the code understood by the processor by specialized programs called assemblers or compilers. To write assembly program, the programmer must know some details about the processor in use. In this lab, we will write assembly code for the Intel 8086 processor. 1 1.3 8086 Processor Programming Model: The 8086 microprocessor has a total of fourteen registers (shown in Figure.1) that are accessible to the programmer: Figure.1: 8086 Registers • Eight of the registers are known as general purpose registers i.e. they can be used by the programmer for data manipulation. • Each of the registers is 16 bits long i.e. can contain a 16-bit binary number. • The first four registers are sometimes referred to as data registers. They are the AX, BX, CX and DX registers. • The second four are referred to as index/pointer registers. They are the SP, BP, SI and DI registers. • The data registers can be treated as 16-bit registers or they can each be treated as two 8-bit registers. Each 8-bit register can be used independently. For example, the ax register may be accessed as AH and AL (H and L refer to high-order and low-order bytes). • The four index registers (SI and DI) can be used for arithmetic operations but their use is usually concerned with the memory addressing modes of the 8086 microprocessor which we look at later. • Instruction Pointer (IP) register is used to control which instruction the CPU executes. The IP, or program counter, is used to store the memory location of the next instruction to be executed. 2 • The CPU checks the IP to ascertain which instruction to carry out next. It then updates the program counter to point to the next instruction. Thus the program counter will always point to the next instruction to be executed. • Flag Register: Nine individual bits of the flag register are used as control flags (3 of them) and status flags (6 of them). The remaining 7 are not used. A flag can only take on the values 0 and 1. We say a flag is set if it has the value 1. Table.1 shows a list of all 8086 flags and their operations Table.1: 8086 Flags 1.4 Procedure: 1.4.1 Debug Program As you begin to learn assembly language programming, it is useful to learn about the debug program or the debugger. A debugger displays the content of registers and memory and lets you view registers and variables as they changed. Using debugger, we can step through a program one line at a time (called tracing), making it easier to find errors. There are many debugger programs which we can utilize such as Microsoft CodeView, Borland Turbo Debugger, and the debug.exe program that is provided with both DOS and Windows operating systems. 3 To start debug.exe program: Open the command prompt: Start Æ runÆcmd ↓ (Or startÆ all programsÆaccessoriesÆcommand prompt) The DOS command page will appear, then, type the following prompt C :/>Debug ↓ At this moment, the instructions of Debug can be introduced A. Creating basic assembly program 1. To assemble a program on the Debug, the "a" (assemble) command is used; 2. When this command is used, the address where you want the assembling to begin can be given as a parameter, 3. Move the value 2 to the ax register, move the value 4 to the bx register, add the contents of the ax and bx registers, the instruction, no operation (nop), to finish the program a 100[enter] (Debug must start from address 100h) mov ax, 2[enter] mov bx, 4[enter] add ax,bx[enter] nop[enter][enter] 4. Use” t “command to trace each instruction. (Type “t =100”, this mean trace starting at cs:100, then type “t” only to trace the next instructions) 5. Use” r” commands to view the contents of all registers 6. Use rax to view the content of ax 4 B. Debug basic commands 1. Use the “r” command to display the current contents of all of the 80x86's internal registers. List the initial values held in CS, DS, and SS. 2. Calculate the physical address of the next instruction to be executed. 3. What is the instruction held at this address? 4. Calculate the physical address of the current top of the stack. 5. Use “r” command to first display the current contents of CX only, and then change this value to 10H. 6. Use “r” command to display the current contents of IP 7. Use the “r” command to display the current contents of the flag register (type “r f” which used to show the content of the flag register) and then change the state of the parity flag to represent even parity. (Note that PO on the flag register means that the number of ones in the last operation is odd, and PE means that the number of ones is even) The table below shows meaning for each flag symbol: 5 C. Tracing assembly program 1. Using notepad, write the following code and save it as sample.asm file .model small .stack 100H .code mov al,5h mov bl,0ah add al,bl mov cx,10 sub cx,3 mov dl,41h mov ah,2 int 21h mov AH,4cH int 21H end 2. Use TASM to assemble the program, • Place the file in the TASM folder • Using command prompt, go to the TASM (ex: cd c:\TASM) • Then, type • TASM sample • Tlink sample 3. on the command prompt write: debug sample.exe 4. use U command to un- assemble the program 5. use “t “command to trace the program 6 6. What is the length of mov al,5h instruction? 7. What is the length of mov cx,10 instruction? 8. What is the length of add al,bl instruction? 9. What is the length of sub cx,3 instruction? 10. Note that when you execute INT 21h you call INT 21 service routine which is a program stored in somewhere in memory. Can you detect the staring logical address of the INT 21h service routine 11. what is staring physical address of the INT 21h service routine 1.4.2 TASM Program Other option to run an assembly program is using TASM. TASM (Turbo Assembler) is an x86 assembler package developed by Borland and it used to build the object file (.obj) which is the file contains machine code for the assembly program. Turbo Assembler package is bundled with the linker, TLINK (Turbo Linker) used to generate .exe program from .obj file, see Figure.2 Assembly program (.asm) TASM .OBJ file Figure.2 7 TLINK .EXE file TASM first program: 1. Using notepad, write the following code and save it as welcome.asm file .model small .stack 100H .data na db 13,10,"Welcome to ENCS 311 Lab$" .code mov Ax,@data mov ds,Ax mov ah,09h lea dx,na int 21h mov AH,4cH int 21H end 2. Use TASM to assemble the program, • Place the file in the TASM folder • Using command prompt, go to the TASM (ex: cd c:\TASM) • Then, type • TASM welcome • Tlink welcome • welcome • This will show “Welcome to ENCS 311 Lab” on the screen 3. What is the effect of the following to instructions mov Ax,@data mov ds,Ax 4. What the following instruction means: 8 lea dx,na 5. What is the size of the stack in bytes reserved by using “.Stack 100h “directive? 6. Suppose you want to reserve stack of 2 Kbytes, what is the directive you have to use? 7. Modify the above program to view the message as shown below: Welcome To ENCS 311 Lab Homework: Write an assembly program to view Welcome to ENCS 311 Lab on the screen, but with using the corresponding ASCII codes for the characters of the message, i.e. don’t use the string characters when you define the message 9