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
EC—331 Microprocessor and Interfacing Techniques (3+1) Lecture 4 Addressing Modes Dr Hashim Ali Spring — 2017 Department of Computer Science and Engineering HITEC University Taxila Addressing Modes in Microprocessor (8086/88) • Addressing mode provides different ways for access an address to given data to a processor. • When 8086 executes an instruction, it performs the specified function on data. Operated data is stored in the memory location. There are various techniques to specify address of data. These techniques are called Addressing Modes. • Types of Addressing Modes:- Data Addressing Modes - Program Memory Addressing Modes - Stack Memory Addressing Modes 2 • Data-Addressing Modes: - • Program Memory Addressing Modes: - • This mode is related to data transfer operation, that is, data is transferred either from the memory to internal registers of 8086 processors or from one register to another register. ‣ Example: MOV AX, DX This mode involves program memory addresses during various operations. ‣ Example: JMP AX, in this instruction, the code execution control jumps to the current code segment location addressed by the contents of AX register. Stack Memory Addressing Modes: - This mode involves stack registry operations. ‣ Example: PUSH AX, this instruction copies the contents of AX register to the stack. 3 Data-Addressing Modes • An addressing mode means the method by which an operand can be specified in a register or a memory location. • 8086/88 provide seven Addressing Modes: 1. Register Addressing 2. Immediate Addressing 3. Direct Data Addressing 4. Register Indirect Addressing 5. Base-Plus-Index Addressing 6. Register Relative Addressing 7. Base Relative-Plus-Index Addressing 4 Opcode and Operand in Microprocessor • An opcode is a short of “operation code” • An opcode is a singe instruction can be executed by the CPU. In machine language it is a binary or hexadecimal value such as B7 loaded into the instruction register. • In assembly language mnemonic form an opcode is a command such as MOV or ADD or JMP. • Example: MOV AX, 1000H ; MOV is the opcode ; AX (register) is an operand • Operands are manipulated by the opcode. In this example, the operands are the register AX and the value 1000H. 8-bit Opcode 1 Byte 8-bit 8-bit Operand 1 or 2 Byte 5 Data-Addressing Modes • MOV instruction is a common and flexible instruction. - provides a basis for explanation of data-addressing modes • Figure 3–1 illustrates the MOV instruction and defines the direction of data flow. • Source is to the right and destination the left, next to the opcode MOV. - an opcode, or operation code, tells the microprocessor which operation to perform 6 Figure 3–1 The MOV instruction showing the source, destination, and direction of data flow. • The MOV AX, BX instruction transfers the contents of source register (BX) into the destination register (AX). - The source never changes, but the destination always changes. ‣ - MOV copies the source data into the destination. The source and destination are often called operands. 7 Figure 3–2 8086–Core2 data-addressing modes. 8 1. Register Addressing • The most common form of data addressing. - • Once register names learned, easiest to apply. The microprocessor contains these 8-bit register names used with register addressing: AH, AL, BH, BL, CH, CL, DH, and DL. - 16-bit register names: AX, BX, CX, DX, SP, BP, SI, and DI. 9 • Register addressing transfers a copy of a byte or word from the source register or contents of a memory location to the destination register or memory location. - • Ex: MOV CX, DX Important for instructions to use registers that are the same size. - Never mix an 8-bit with a 16-bit register, an 8- or a 16bit register with a 32-bit register - This is not allowed by the microprocessor and results in an error when assembled. 10 • Use of registers to hold the data to be manipulated • Memory is not accessed when this addressing mode is executed • Example: MOV BX, DX ; copy the contents of DX into BX MOV ES, AX ; copy the contents of AX into ES ADD AL, BH ; add the contents of BH to contents of AL • Source and destination registers must have the same size 11 12 2. Immediate Addressing • Transfers the source, an immediate byte or word of data, into the destination register or memory location • The source operand is a constant • The operand comes immediately after the opcode • For this reason, this addressing mode executes quickly • Immediate addressing mode can be used to load information into any of the registers except the segment registers and flag registers. 13 • Example: MOV AX, 2550H ; move 2550H into AX MOV CX, 625 ; load the decimal value 625 into CX MOV BL, 40H ; load 40H into BL • The data must first be moved to a general-purpose register and then to the segment register. • Example: MOV AX, 2550H MOV DS, AX MOV DS, 0123H ; illegal instruction! 14 • In symbolic assembly language, the symbol # precedes immediate data in some assemblers. - • MOV AX,#1234H instruction is an example. Most assemblers do not use the # symbol, but represent immediate data as in the MOV AX,1234H instruction. - An older assembler used with some Hewlett-Packard logic development does, as may others. - In this text, the # is not used for immediate data. 15 • The symbolic assembler portrays immediate data in many ways. • The letter H appends hexadecimal data. • If hexadecimal data begin with a letter, the assembler requires the data start with a 0. - • to represent a hexadecimal F2, 0F2H is used in assembly language. Decimal data are represented as is and require no special codes or adjustments. - An example is the 100 decimal in the MOV AL,100 instruction. 16 • An ASCII-coded character or characters may be depicted in the immediate form if the ASCII data are enclosed in apostrophes. - • Ex: MOV AL,’A’ Binary data are represented if the binary number is followed by the letter B. - in some assemblers, the letter Y - Ex: MOV CL,11001110B 17 18 Parts of Assembly Language • Each statement in an assembly language program consists of four parts or fields. • The leftmost field is called the label. • used to store a symbolic name for the memory location it represents • All labels must begin with a letter or one of the following special characters: @, $, -, or ?. • a label may any length from 1 to 35 characters • The label appears in a program to identify the name of a memory location for storing data and for other purposes. 19 • • • The next field to the right is the opcode field. - designed to hold the instruction, or opcode - the MOV part of the move data instruction is an example of an opcode Right of the opcode field is the operand field. - contains information used by the opcode - the MOV AL,BL instruction has the opcode MOV and operands AL and BL The comment field, the final field, contains a comment about the instruction(s). - comments always begin with a semicolon (;) 20 3. Direct Data Addressing • Moves a byte or word between a memory location and a register. • The data is in some memory location(s) and the address of the data in memory comes immediately after the instruction - usually a 3-byte long instruction • This address is the offset address • Example: MOV AL, [8088] • ; move content of DS:8088 into AL The physical address is calculated by combining the contents of offset location 2400 with DS 21 • Example: Find the physical address of the memory location and its contents after the execution of the following, assuming that DS = 1512H. MOV AL, 3BH MOV [3518], AL • Solution: - First 3BH is copied into AL - Then in line two, the contents of AL are moved to logical address DS: 3518 which is 1512:3518. - Shifting DS left and adding it to the offset gives the physical address of 18638H (15120H + 3518H = 18638H). - After the execution of the second instruction, the memory location with address 18638H will contain the value 3BH. 22 Few Examples 23 4. Register Indirect Addressing • Transfers a byte or word between a register and a memory location addressed by an index or base register. • The address of the memory location where the operand resides is held by a register. • The registers used for this purpose are SI, DI, and BX. • They must be combined with DS (by default) in order to generate the 20-bit physical address. - The register BP uses the stack segment (SS) by default. 24 • • For example: - MOV AL, [BX] - MOV AL, [BP] - MOV AL, [SI] - MOV AL, [DI] Segment override prefix symbols can be used if you wish to access data in different segments. - MOV AL, CS:[BX] - MOV AL, DS:[BP] - MOV AL, SS:[SI] - MOV AL, ES:[DI] 25 • Example: MOV AX, [BX] ; moves into AX the contents of the memory location pointed to by DS:BX, 1000:1234 • The physical address is calculated as 1000x10+1234=11234H • The same rules apply when using register SI or DI. • Example: MOV CL, [SI] ; move contents of DS:SI into CL MOV [DI], AH ; move contents of AH into DS:DI 26 • Example: - • Assume that DS = 1120H, SI = 2498H, and AX = 17FEH Show the contents of memory locations after the execution of MOV [SI], AX ; move contents of AX into DS:SI Solution: - The contents of AX are moved into memory locations with logical address DS:SI and DS:SI + 1 (Two bytes because of AX) - The physical address starts at DS (shifted left) + SI = 13698. According to the little endian convention, - Low address 13698H contains FE, the low byte, - High address 13699H will contain 17, the high byte. 27 Few Examples 28 5. Base-Plus-Index Addressing • Transfers a byte or word between a register and the memory location addressed by a base register (BP or BX) plus an index register (DI or SI). • Combining based and indexed addressing modes. • One base register and one index register are used. • Examples: MOV [BX+DI], CL ; move contents of CL into DS:BX+DI - Physical Address = DSx10 + BX+DI MOV CH, [BX+SI] ; move contents of the DS:BX+SI into CH - Physical Address = DSx10 + BX+SI MOV AH, [BP+DI] ; move contents of the SS:BP+SI into AH - Physical Address = SSx10 + BP+DI MOV [BP+SI], AL ; move contents of AL into SS:BP+SI - Physical Address = SSx10 + BP+SI 29 Few Examples 30 6. Register Relative Addressing • Moves a byte or word between a register and the memory location addressed by an index or base register plus a displacement. • The data in a segment of memory are addressed by adding the displacement to the contents of a base or an index register (BP, BX, DI, or SI). • Examples: MOV AX, [BX+4] - Physical Address = DSx10 + BX+4 MOV CH, [SI+5] - ; move contents of the DS:DI+1 into AH Physical Address = DSx10 + DI+1 MOV [BP+2], AL - ; move contents of the DS:SI+5 into CH Physical Address = DSx10 +SI+5 MOV AH, [DI+1] - ; move contents of DS:BX+4 into AX ; move contents of AL into SS:BP+2 Physical Address = SSx10 + BP+2 31 • Example: • Assume that DS = 4500, SS = 2000, BX = 2100, SI = 1486, DI = 8500, BP= 7814, and AX = 2512. Show the exact physical memory location where AX is stored in each of the following. All values are in hex. 1- MOV [BX+20], AX 2- MOV [SI+10], AX 3- MOV [DI+4], AX 4- MOV [BP+12], AX • Solution: Physical Address = segment reg. x 10 + (offset reg.) + displacement 1- DS:BX+20 2- DS:SI+10 3- DS:DI+4 4- SS:BP+12 location 47120 = (12) and 47121 = (25) location 46496 = (12) and 46497 = (25 ) location 4D504 = (12) and 4D505 = (25) location 27826 = (12) and 27827 = (25) 32 Few Examples 33 7. Base Relative-Plus-Index Addressing • The base relative-plus-index addressing mode is similar to the base-plus-index addressing mode, but adds a displacement besides using a base register and an index register to form the memory address. • This type of addressing mode often addresses a two-dimensional array of memory data. • The data in a segment of memory are addressed by adding the displacement to the contents of a base and an index register (BP, BX, DI, or SI). 34 • Examples: MOV [BX+DI+1], AX - ; move contents of AX into DS:BX+DI+1 Physical Address = DSx10 + BX+DI+1H MOV AX, [BX+SI+10] - ; move contents of the DS:BX+SI+10 into AX Physical Address = DSx10 + BX+SI+10H MOV AH, [BP+DI+3] - - ; move contents of the SS:BP+SI+3 into AH Physical Address = SSx10 + BP+DI+3H MOV AX, FILE[BX+DI] ; move contents of the DS:FILE+BX+DI into AX Physical Address = DSx10 + BX+DI+FILE 35 Few Examples 36 Offset Registers For Various Segments • The following Table provides a summary of the offset registers that can be used with the four segment registers of the 8086/8088. Segment Register CS DS ES SS Offset Register IP SI, DI, BX SI, DI, BX SP, BP 37 Easy Way to Remember 8086/88 Addressing Modes • Total 17 different memory addressing modes:- DISP, [BX], [BP], [SI], [DI], [BX+DISP], [BP+DISP], [SI+DISP], [DI+DISP], [BX+SI], [BX+DI], [BP+SI], [BP+DI],[BX+SI+DISP], [BX+DI+DISP], [BP+SI+DISP], and [BP+DI+DISP] ‣ DISP = Displacement 38 Assembly Language • There is a one-to-one relationship between assembly and machine language instructions. • What is found is that a compiled machine code implementation of a program written in a high-level language results in inefficient code. - • More machine language instructions than an assembled version of an equivalent handwritten assembly language program. Two key benefits of assembly language programming. - It takes up less memory - It executes much faster 39 Languages in-terms of Applications • One of the most beneficial uses of assembly language programming is real-time applications. • Real time means the task required by the application must be completed before any other input to the program that will alter its operation can occur • For example the device service routine which controls the operation of the floppy disk drive is a good example that is usually written in assembly language • Assembly language not only good for controlling hardware devices but also performing pure software operations - searching through a large table of data for a special string of characters - Code translation from ASCII to EBCDIC - Table sort routines - Mathematical routines • Assembly language: perform real-time operations • High-level languages: Those operations mostly not critical in time. 40 Assembly Instructions 41 Assembler Directives • Model: Selects the size of the memory model. - .MODEL SMALL : Reserves 64KB memory for Code Segment (CS) and 64KB for Data Segment (DS). - .MODEL MEDIUM : CS can exceed 64KB but DS must fit in 64KB of memory. - .MODEL COMPACT : CS is of fixed 64KB size but DS can vary. - .MODEL LARGE : Both CS & DS can exceed 64KB but only one of them. - .MODEL HUGE : Both can exceed its 64KB memory size. - .MODEL TINY : Used with compact (com) files in which CS & DS must fit into 64KB. 42 Assembly Code Template .MODEL SMALL .STACK ; beginning of the stack segment .DATA ; beginning of the data segment .CODE ; beginning of the code segment Ex: .DATA DATAW DATAB SUM DW DB DB 213FH 52H ? ;nothing stored but storage is assigned Ex: .CODE ProcedureName PROC Attribute … ProcedureName ENDP END ProcedureName ; name of the procedure ; procedure statements PROC is a statement used to indicate the beginning of a procedure or subroutine. ENDP indicates the end of the procedure. ProcedureName may be any valid identifier. Attribute is NEAR if the Procedure is in the same code segment as the calling program; or FAR if in a different code segment. 43 Sample Program ; This program displays “Hello, World!” .MODEL SMALL .STACK 100H .DATA message DB “Hello, World!”, 0dh, 0ah, ‘$’ ; newline+eoc .CODE MAIN PROC mov ax, @data ; address of DS mov ds, ax mov ah, 9 mov dx, offset message ; displacement message starting at 0 int 21h mov ax, 4C00h ; halt the program and return int 21h main endp end main 44 PROGRAM MEMORY — ADDRESSING MODES • Program memory-addressing modes, used with the JMP and CALL instructions, consist of three distinct forms: 1. Direct Program Memory Addressing 2. Relative Program Memory Addressing 3. Indirect Program Memory Addressing 45 1. Direct Program Memory Addressing • Used for all jumps and calls by early microprocessor; also used in high-level languages, such as BASIC. - GOTO and GOSUB instructions • The microprocessor uses this form, but not as often as relative and indirect program memory addressing. • The instructions for direct program memory addressing store the address with the opcode. 46 Figure 3–14 The 5-byte machine language version of a JMP [10000H] instruction. 47 • This JMP instruction loads CS with 1000H and IP with 0000H to jump to memory location 10000H for the next instruction. - • an intersegment jump is a jump to any memory location within the entire memory system Often called a far jump because it can jump to any memory location for the next instruction. - in real mode, any location within the first 1M byte 48 • The only other instruction using direct program addressing is the intersegment or far CALL instruction. • Usually, the name of a memory address, called a label, refers to the location that is called or jumped to instead of the actual numeric address. • When using a label with the CALL or JMP instruction, most assemblers select the best form of program addressing. 49 2. Relative Program Memory Addressing • Not available in all early microprocessors, but it is available to this family of microprocessors. • The term relative means “relative to the instruction pointer (IP)”. • The JMP instruction is a 1-byte instruction, with a 1-byte or a 2-byte displacement that adds to the instruction pointer. • An example is shown in Figure 3–15. 50 Figure 3–15 A JMP [2] instruction. This instruction skips over the 2 bytes of memory that follow the JMP instruction. 51 3. Indirect Program Memory Addressing • The microprocessor allows several forms of program indirect memory addressing for the JMP and CALL instructions. • In 80386 and above, an extended register can be used to hold the address or indirect address of a relative JMP or CALL. - for example, the JMP EAX jumps to the location address by register EAX 52 • If a relative register holds the address, the jump is considered to be an indirect jump. • For example, JMP [BX] refers to the memory location within the data segment at the offset address contained in BX. • at this offset address is a 16-bit number used as the offset address in the intrasegment jump • this type of jump is sometimes called an indirect-indirect or double-indirect jump • Figure 3–16 shows a jump table that is stored, beginning at memory location TABLE. 53 Figure 3–16 A jump table that stores addresses of various programs. The exact address chosen from the TABLE is determined by an index stored with the jump instruction. 54 3–3 STACK MEMORY-ADDRESSING MODES • The stack plays an important role in all microprocessors. - • holds data temporarily and stores return addresses used by procedures Stack memory is LIFO (last-in, first-out) memory - describes the way data are stored and removed from the stack 55 • Data are placed on the stack with a PUSH instruction; removed with a POP instruction. • Stack memory is maintained by two registers: • - the stack pointer (SP or ESP) - the stack segment register (SS) Whenever a word of data is pushed onto the stack, the high-order 8 bits are placed in the location addressed by SP – 1. - low-order 8 bits are placed in the location addressed by SP – 2 56 • The SP is decremented by 2 so the next word is stored in the next available stack location. - the SP/ESP register always points to an area of memory located within the stack segment. • In protected mode operation, the SS register holds a selector that accesses a descriptor for the base address of the stack segment. • When data are popped from the stack, the low-order 8 bits are removed from the location addressed by SP. - High-order 8 bits are removed; the SP register is incremented by 2. 57 Figure 3–17 The PUSH and POP instructions: (a) PUSH BX places the contents of BX onto the stack; (b) POP CX removes data from the stack and places them into CX. Both instructions are shown after execution. 58 • Note that PUSH and POP store or retrieve words of data —never bytes—in 8086 - 80286. • 80386 and above allow words or doublewords to be transferred to and from the stack. • Data may be pushed onto the stack from any 16-bit register or segment register. - • in 80386 and above, from any 32-bit extended register Data may be popped off the stack into any register or any segment register except CS. 59 • PUSHA and POPA instructions push or pop all except segment registers, on the stack. • Not available on early 8086/8088 processors. • 80386 and above allow extended registers to be pushed or popped. - 64-bit mode for Pentium and Core2 does not contain a PUSHA or POPA instruction 60