Week 3 8086/8088 Addressing Modes, Instruction Set & Machine Codes
Addressing Modes
When the 8088 executes an instruction, it performs the specified function on data These data, called operands,
May be a part of the instruction May reside in one of the internal registers of the microprocessor May be stored at an address in memory
Register Addressing Mode
MOV AX, BX MOV ES,AX MOV AL,BH
Immediate Addressing Mode
MOV AL,15h MOV AX,2550h MOV CX,625
Direct Addressing Mode
MOV CX, [address]
Example: MOV AL,[03]
BEED
02003 FF
AL=?
Register Indirect Addressing Mode
MOV AX,
BX DI SI
BEED
Example for Register Indirect Addressing
Assume that DS=1120, SI=2498 and AX=17FE show the memory locations after the execution of: MOV [SI],AX
DS (Shifted Left) + SI = 13698. With little endian convention: Low address 13698 High Address 13699 FE 17
Based-Relative Addressing Mode
MOV AH, [
DS:BX SS:BP
] + 1234h
BX AX
3AH
DS
1234
Indexed Relative Addressing Mode
SI MOV AH, [ DI ] + 1234h
Example: What is the physical address MOV [DI-8],BL if DS=200 & DI=30h ? DS:200 shift left once 2000 + DI + -8 = 2028
Based-Indexed Relative Addressing Mode
Based Relative + Indexed Relative We must calculate the PA (physical address)
PA=
CS SS DS ES
BX SI : BP + DI +
8 bit displacement 16 bit displacement
MOV AH,[BP+SI+29] or MOV AH,[SI+29+BP] or MOV AH,[SI][BP]+29
The register order does not matter
8
Based-Indexed Addressing Mode
MOV BX, 0600h MOV SI, 0010h ; 4 records, 4 elements each. MOV AL, [BX + SI + 3]
OR
MOV BX, 0600h MOV AX, 004h ; MOV CX,04; MUL CX MOV SI, AX MOV AL, [BX + SI + 3]
9
Summary of the addressing modes
Addressing Mode Register Immediate Direct Register Indirect Operand Reg Data [offset] [BX] [SI] [DI] [BX]+disp [BP]+disp [DI]+disp [SI]+disp Default Segment None None DS DS DS DS DS SS DS DS
Based Relative Indexed Relative Based Indexed Relative
[BX][SI or DI]+disp DS [BP][SI or DI]+disp SS
10
16 bit Segment Register Assignments
Segment Registers Offset Register CS IP DS SI,DI,BX ES SI,DI,BX SS SP,BP
Type of Memory Reference
Default Segment
Alternate Segment
none none CS,ES,SS CS,ES,SS None
Offset
Instruction Fetch CS Stack Operations General Data String Source String Destination
Brey
IP SP,BP BX, address SI, DI, address DI
11
SS DS DS ES
Segment override
Instruction Examples Override Segment Used Default Segment
MOV AX,CS:[BP]
CS:BP
SS:BP
MOV DX,SS:[SI]
SS:SI
DS:SI
MOV AX,DS:[BP]
DS:BP
SS:BP
MOV CX,ES:[BX]+12
ES:BX+12
DS:BX+12
MOV SS:[BX][DI]+32,AX
SS:BX+DI+32
DS:BX+DI+32
12
Example for default segments
The following registers are used as offsets. Assuming that the default segment used to get the logical address, give the segment register associated? b)DI c)IP d)SI, e)SP, f) BX
a) BP
Show the contents of the related memory locations after the execution of this instruction MOV [BP][SI]+10,DX if DS=2000, SS=3000,CS=1000,SI=4000,BP=7000,DX=1299 (all hex)
SS(0)=30000 30000+4000+7000+10=3B010
13
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
14
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.
15
Converting Assembly Language Instructions to Machine Code
OPCODE D W MOD REG R/M
An instruction can be coded with 1 to 6 bytes Byte 1 contains three kinds of information:
Opcode field (6 bits) specifies the operation such as add, subtract, or move Register Direction Bit (D bit)
Tells the register operand in REG field in byte 2 is source or destination operand
1:Data flow to the REG field from R/M 0: Data flow from the REG field to the R/M
Data Size Bit (W bit)
Specifies whether the operation will be performed on 8-bit or 16-bit data
0: 8 bits 1: 16 bits
Byte 2 has two fields:
Mode field (MOD) 2 bits Register field (REG) - 3 bits Register/memory field (R/M field) 2 bits
16
Continued
REG field is used to identify the register for the first operand REG
000 001 010 011 100 101 110 111
W=0
AL CL DL BL AH CH DH BH
W=1
AX CX DX BX SP BP SI DI
17
Continued
2-bit MOD field and 3-bit R/M field together specify the second operand
18
Examples
MOV BL,AL Opcode for MOV = 100010 Well encode AL so
D = 0 (AL source operand)
W bit = 0 (8-bits) MOD = 11 (register mode) REG = 000 (code for AL) R/M = 011 OPCODE D W MOD REG R/M
100010
11
000
011
MOV BL,AL => 10001000 11000011 = 88 C3h ADD AX,[SI] => 00000011 00000100 = 03 04 h ADD [BX][DI] + 1234h, AX => 00000001 10000001 __ __ h => 01 81 34 12 h
19
Software
The sequence of commands used to tell a microcomputer what to do is called a program Each command in a program is called an instruction 8088 understands and performs operations for 117 basic instructions The native language of the IBM PC is the machine language of the 8088 A program written in machine code is referred to as machine code In 8088 assembly language, each of the operations is described by alphanumeric symbols instead of just 0s or 1s.
ADD AX, BX Opcode Source operand
Destination operand
20
DEBUG program instruction set (page 825 mzd)
Debug instructions List of commands
a Assemble [address] you can type in code this way c range address ; compare c 100 105 200 d [range] ; Dump d 150 15A e address [list] ; Enter e 100 f Fill range list F 100 500 g Go [=address] addresses runs the program h Value1 Value2 ; addition and subtraction H 1A 10 i Input port I 3F8 r Show & change registers Appears to show the same thing as t, but doesn't cause any code to be executed. t=startaddress Trace either from the starting address or current location. u startaddress UnAssemble
21
Some examples with debug
0100 mov ax,24b6 0103 mov di, 85c2 0106 mov dx,5f93 0109 mov sp,1236 010c push ax 010d push di 010e int 3 Display the stack contents after execution. -D 1230 123F
22
Some examples with DEBUG
0100 mov al,9c 0102 mov dh,64 0104 add al,dh 0109 int 3 trace these three commands and observe the flags T=<start trace location> Saving and Loading a file After the code has been entered with the A command Use CX to store data indicating number of bytes to save. BX is the high word. Use N filename.com Then W command to write to file. L loads this file.
23
Example
Copy the contents of a block of memory (16 bytes) starting at location 20100h to another block of memory starting at 20120h MOV AX,2000 MOV DS,AX MOV SI, 100 MOV DI, 120 MOV CX, 10 MOV AH, [SI] MOV [DI], AH INC SI INC DI DEC CX JNZ NXTPT
100-10f
NXTPT:
120-12f
24