Al Balqa Applied University Al-Huson College University Electrical Engineering Department Microprocessor 8086 Lab Manual Eng. Buthayna AL-Sharaa Table of Contents No 1 Lab Title Introduction to Assembly Language Page 3 2 Assembly Language Fundamentals 9 3 Data Transfer & Arithmetic 11 4 Loop Instruction 20 5 Addressing Modes 22 6 7 8 Boolean Instructions & Conditional Structured Shift, Rotate, Multiplication and Division Instructions Stacks and Subroutines 25 31 38 9 Debug Program 43 10 Keyboard Input with int 16h & Macros 47 Appendix ASCII CODE Tables A 50 Lab1 Introduction to Assembly Language 1. Objective: To be familiar with Assembly Language. 2. Introduction Machine language (computer's native language) is a system of impartible instructions executed directly by a computer's central processing unit (CPU). Instructions consist of binary code: 1s and 0s. Machine language can be made directly from java code using interpreter. Assembly Language is a programming language that is very similar to machine language, but uses symbols instead of binary numbers. It is converted by the assembler (e.g. Tasm and Masm) into executable machine-language programs. The difference between compiling and interpreting is as follows. Compiling translates the high-level code into a target language code as a single unit. Interpreting translates the individual steps in a high-level program one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated. C, C++ code is executed faster than Java code, because they transferred to assembly language before machine language. Assembly language is machine-dependent; an assembly program can only be executed on a particular machine. In Our Lab we will learn the assembly language of the 8086intel Microprocessor. To make programs in assembly language, you must know some information about the 8086 microprocessor. The 8086 contains 14 registers. Each register is 16 bits long. See Figure (1) Each register has different usage as shown in Table (1) below. The general purpose registers can be "split". AH contains the high byte of AX and AL contains the low byte. You also have: BH, BL, CH, CL, DL, DH. So if for example, DX contains the value 1234h DH would be 12h and DL would be 34h. And a 16-bit FLAG Register. The FLAGS Register consists of 9 status bits. These bits are also called flags, because they can either be SET (1) or NOT SET (0). All these flags have a name and purpose. op-code destination operand, source operand Segments: The 8086 microprocessor has a memory of size 1MB. This memory is divided into segments, each segment has a size of 64KB. The segments are: Data Segment (DS), Code Segment(CS), Stack Segment (SS), Extra Segment(ES). ❖ the address of any location within a segment consist of 2 parts: a. segment address: 16-bit selector defining the beginning address of any 64KB (216) memory segment b. offset address: selecting any location within the 64KB (216) memory segment 3. Running your first example: Hello Program! 1. Click Start → All Programs → Emu8086 2. Click New → Com Template. Where a white page opens. 3. Enter the code shown in the following screen 4. Click Compile press - Note: . If your program does not contain any errors → save program → . then click from the screen that shows. is used to execute the program one instruction at a time. Analysis of the Hello program: Homework: Write an assembly language program to print all letters as follows: AB......MN Note: To print a character on the screen you have to use the int 21h with the service 2, the character to be printed have to be in dL register. For Example, the following code print A on the screen. mov ah, 2 mov dl, 41h int 21h Note: Don’t use loop in your solution Lab2 Assembly Language Fundamentals Objective: To be familiar with Assembly Language Fundamentals. Our compiler supports two types of data types: Byte, Word Variables A variable is a memory location that has an address and a value. The syntax for variable declaration is: Varname DB value Varname DW value DB : stands for Define Byte DW : stands for Define Word Value: is the numerical value of variable in any of the supported number systems( Hexadecimal, decimal, binary, octal). The letter ? can be used to leave the content of variable not changed. The characters used for each numeric system are : h – hexadecimal d – decimal b – binary o – octal Examples: 30d, 6Ah, 42, 1101b, 777o - Hexadecimal beginning with letter: 0A5h - If no radix is given, the integer constant is decimal - A hexadecimal beginning with a letter must have a leading - the definition of an array is similar to variable definition , except that a list of values are specified. Example: defining array named list1 of 4 elements each element is a byte, with initial values 1,2,3,4 List1 db 1,2,3,4 Character Constants A character constant is a single character enclosed in either single or double quotes. The assembler converts it to the binary ASCII code matching the character. Examples are: 'A' "x" ASCII character = 1 byte. String Constants String constant is a string of characters enclosed in either single or double quotes: 'ABC' "xyz" 'Say "Goodnight," Gracie' -Each character occupies a single byte. -it is preferred to end String with the symbol $ Comments All single line comments start with the symbol ; Lab3 Data Transfer & Arithmetic ❖ Direct-Offset Operands: a direct-offset operand: (also called Direct Addressing Mode ) is adding a displacement( a number ) to the name of a variable or array. Let’s begin with an array of bytes named arrayB: .data arrayB db 10h,20h,30h,40h,50h .code ; the following instruction with will move the first byte in the array into AL: mov AL , arrayB ; AL = 10h ; the following instruction with will move the second byte in the array into AL: Mov AL , [arrayB+1] ; AL = 20h Note: An expression such as arrayB+1 produces what is called an offset address by adding a constant to the variable’s name. Surrounding an offset address with brackets indicates the expression is used to obtain the contents of memory at that address. Lab4 Loop Instruction Example: LEA BX, List ;Load the offset address of List into register Bx Note: The operator Offset performs the same as LEA Example: Mov BX, OFFSET List ; this instruction is the same as the above. This operator specifies the size to which an address points to in memory, as shown: • WORD PTR : address or register points to a word • BYTE PTR : address or register points to a Byte Example: INC WORD PTR var1 ; increment the content of the word in memory addressed by var1 Lab Work: Exercise 1: Write the code to print your name 5 times on screen Exercise 2: Write the code to find the square of the number 4 Exercise 3: Lab5 Addressing Modes Objective: To know more about Assembly language, such as how CPU can access data in memory. Addressing Modes: The addressing modes are different ways to find the memory address (how CPU can access data in memory). There are five addressing modes used in assembly programming with 80x86 family, these modes are: 1. Immediate addressing mode: One of the operands is an immediate number Ex mov al,5 2. Register addressing mode Both operands exist in registers. Ex add cl,bl 3. Direct Addressing : Mode In this mode the 16 bit Offset is taken directly from the instruction. Examples: mov [START+1],CL ; copy CL into location START + 1 mov DX, [200] ; copy the word at address 200 into register DX 4. Register Indirect: [bx], [si], [di], [bp] Addressing data at any location through an offset address held in BP, BX, DI, and SI. Examples : mov ax,[bx] ; copies the word in data segment memory location addressed by bx to ax mov [BP],dl ; copies dl into stack segment memory at the address stored in BP 5. Relative register indirect: Register added to a constant to form the offset MOV AX, [BX+5] ↔ MOV AX,5[BX] ↔ MOV AX, [BX]+5 6. Base-Plus-Index Addressing: In this mode one base register (BP or BX) + 1 of index register (DI or SI) – base register: hold beginning location of a memory array – index register: hold relative position of an element in the array -if base register BP is used➔ data addressed is in stack segment - if base register BX is used➔ data addressed is in data segment Examples: MOV DX, [BX+DI] ; move data(16 Bit ) is data segment at address BX+DI to register DX mov ax,[bp+bx] mov ax,[si+di] ; error , two base registers ; error , two index registers 7. Base-Indexed with displacement (relative base plus index) MOV AX, [bx+si+2] Homework: 1. Write an assembly program that allows the user to enter two strings and determines if they are equal or not. (Each string must end with $). 2. Write an assembly program that allows the user to enter 5-digits Number, and then prints it in reversed-order digits. Lab 6/Boolean Instructions & Conditional Structured OTHER logical instructions: XOR,OR. They have the same syntax a the AND instruction Truth table OR 0 0 0 0 1 1 1 0 1 1 1 1 Example: mov al,0C5H OR al, 0FH Final value of al=0CFh Truth table XOR 0 0 0 0 1 1 1 0 1 1 1 0 note 0 xor A = A 1 xor A = A’ (complement) Example : write code to flip(complement) bit 0 in al and keep the other bits unchanged Xor al,1 Exercise 4 Lab 7/ Shift, Rotate, Multiplication and Division Instructions Lab 8 Stacks and Subroutines Write a program that finds the largest unsigned number in an array of 5 numbers , each number is of one byte. Use subroutines Lab 9 Debug Program Objective: Debugging and writing assembly programs using Debug program under windows. Debug is available on every Windows box through the DOS command prompt we will it to explore the computer organization of the 80x86 machine. To open the program: Click to open Write debug then press enter -enter ? then press enter. The following shows This is a menu that contains the set of all commands that can performed in this program. Some of the Debugger’s Commands: 1. Register ( r ) : to display the content of CPU registers. To change the content of register AX Old value of AX new value of AX 2. Dump (d ): to display the content of data segment. The following example dumps the content of data segment at offset 0000 address Content of memory in hexadecimal Content of memory in ASCII 1. Enter (e) : to fill or change the contents of the data segments. Example: to change the content at ds:0000, enter Old value at address 0000 New entered value at address 0000 - Press Enter to exit this command - Intel uses Little Indian organization to store data in memory( where most significant byte of operand is stored in highest address in memory 2. Assemble (a): to enter our code to the code segment Example: to enter instructions at CS:100 3. Trace ( t ): to execute the code one instruction at a time. The instruction executed is the one whose address is stored in register IP. Enter : -t 4. Go (g ): used to execute the set of instructions between two addresses. The statement g=100 10b runs the instructions between these addresses. 5. HEX (H) : to find the length of our code syntax: H end address of code start address of code The output of this command is: Sum of two address difference between two addresses 6. Name (n): used to specify the name of file 7. Write (w ): to write our code to the file we specified using command ( n). the size of code to write in bytes should be stored in register CX Example: Address of the string To exit Lab 10 Keyboard Input with int 16h & Macros Example: ORG 100h MyMacro2 MACRO LOCAL label1, label2 CMP AX, 2 JE label1 CMP AX, 3 JE label2 label1: INC AX label2: ADD AX, 2 ENDM ;;;;;main program ORG 100h MyMacro2 ; first call to macro MyMacro2 ;second call to macro RET Exercise 2. Write a program that finds the largest unsigned number in an array of 5 numbers , each number is of one byte. First Use subroutines. Second use macros ASCII CODE Table