Professional Documents
Culture Documents
The addressing mode means where and how the CPU gets the operands when the instruction is executed.
Instruction Queue
AL
80H
Instruction Queue
AH 12
MACHINE CODE
AL 34
B8 34H 34 12H 12
MACHINE CODE
B0H 80H
5
CX
89 C1
Machine code
1) Base Register---BX, BP 2) Index Register---SI, DI 3) Displacement ---constant value Example: 1) add ax,[20h] 3) add ax,[bx+20h] 5) add ax, [bx+si+20h]
A0
64 10
Code Segment
AL
45
20000H
21064H
Data Segment
45
10
MOV [BX], AL
(DS)*10h= 30000H + (BX)= 1000H 31000H M
AX
30000H
AL
30000H
32000H
50 40
31000H
11
64H
40
50
64H
Displacement
For physical address calculation: DS is used for BX,DI,SI; SS is used for BP PA=(DS)*10H+(BX)/(DI)/(SI)+Disp OR PA=(SS)*10H+(BP)+Disp
12
8A 8F
Code Segment
64 10
20000H
21000H 22064H 45
Data Segment
CL
45
13
(DI) (SI)
Base register(bx or bp) determines which segment(data or stack) the operand is stored; if using BX, the operand is defaultly located in Data segment,then: PA=(DS)*10H+(BX)+(DI)/(SI) if using BP, the operand is defaultly located in stack segment,then: PA=(SS)*10H+(BP)+(DI)/(SI)
14
AH
56H 43200H
56H
15
if using BX, the operand is defaultly located in Data segment,then: PA=(DS)*10H+(BX)+(DI)/(SI)+disp if using BP, the operand is defaultly located in stack segment,then: PA=(SS)*10H+(BP)+(DI)/(SI)+disp
16
A1
Code segment
34 12 40000H
Data segment
AH
45 41444H
45
17
1. Direct Addressing
[disp]
DS DS
CS ES SS CS ES SS
(SI)/(DI)/(BX)/(BP)+disp DS
(BX)+disp (BP)+disp (BX)+(SI)/(DI)+disp (BP)+(SI)/(DI)+disp DS SS DS SS
CS ES SS
CS ES SS CS ES DS CS ES SS CS ES DS
18
Instruction
1. MOV AX, [0520H] 2. MOV AX, [BX]
addressing mode
Direct Addressing Register Indirect Addressing
logical address
3000:0520 3000:6000
physical address
30520H 36000H
4. MOV AX, [BP+6060H] Register Relative Addressing 5. MOV AX, ES: [BX+SI+0050H] Based Indexed Relative Addressing
19
20
00 20
segment1
00 10
IP
21
0010
Sub-routine
CS 3000h IP 0010
segment1
0010
Sub-routine
05 00 Sub-routine
Code Segment
23
05 00 Sub-routine
Code Segment
24
Example:
The Contents of different registers are given below. Form effective addresses for different addressing modes Offset(displacement)= 5000H AX=1000H,BX=2000H,SI=3000H,DI=4000H,BP=5000H,SP=6000H,CS=0000H,DS=1000H SS=2000H,IP=7000H Shifting a number four times is equivalent to multiplying it by 16D or 10H
25
26
Programming in 8088/8086
Three levels of languages available to program a microprocessor: Machine Languages, Assembly Languages, and High-level Languages.
Machine Language
A sequence of binary codes for the instruction to be executed by microcomputers. Long binary bits can be simplified by using Hexadecimal format It is difficult to program and error prone. Different uP (micro-processor) uses different machine codes.
27
Programming in 8088/8086 (cont.) Assembly Language To simplify the programming, assembly language (instead of machine language) is used. Assembly language uses 2- to 4-letter mnemonics to represent each instruction type. E.g. Subtraction is represented by SUB Four fields in assembly language statement: Label, OP Code, Operand and Comment fields. Programs will be translated into machine language, by Assembler, so it can be loaded into memory for execution. High-Level Language High level languages, like C, Basic or Pascal, can also be used to program microcomputers. An interpreter or a compiler is used to translate high level language statement to machine code. High level language is easier to read by human and is more suitable when the programs involves complex data structures.
28
Assemblers
Programming the instructions directly in machine code is possible but every machine codes depending on how the data is stored. The process of converting the microprocessor instructions to the binary machine code can be performed automatically by a computer program, called an ASSEMBLER. Popular assemblers include IBM macro Assembler, Microsoft Macro Assembler (MASM) and Borland Turbo Assembler(installed on IE NT Network). Most assemblers accept an input text file containing lines with a rigorously defined syntax split into four fields.
Not all fields need to be present in a line. Eg. A line can be just a comment line if it starts with semicolon;
29
Object code is the binary code obtained after running the assembler (
Or compiler if the source is in a high level language). Modules of a program may be written separately and linked together to form a executable program using a linker. The linker joins the object code of the different modules into one large
31
Fields in Assembler
<label> <Mnemonic or directive> <operands> <;comment> Comment field contains internal program documentation to improve human readability -use meaningful comments
32
Why Program in Assembler? Assembler language instruction has a one-to-one correspondence with the binary machine code: the programmer controls precisely all the operations performed by the processor (a high level language relies on a compiler or interpreter to generate the instructions). Assembler can generate faster and more compact programs Assembler language allows direct access and full control of input/output operations However, high-level language programs are easier to write and develop than assembler language programs
34
Advantages of High-level languages Block structure code: programs are most readable when they are broken into logical blocks that perform specific function. Productivity: easier to program Level of complexity: no need to know the hardware details
Readability
variants) instructions
Instructions belong to one of the following groups: data transfer, arithmetic, logic, string manipulation, control
36
An instruction can be coded with 1 to 6 bytes Byte 1 contains three kinds of information
Opcode field (6 bits) specifies the operation (add, subtract, move) Register Direction Bit (D bit) Tells the register operand in REG field in byte 2 is source or destination operand 1: destination 0: source
-Data Size Bit (W bit) Specifies whether the operation will be performed on 8bit or 16-bit data 0: 8 bits 1: 16 bits
37
38
2-bit MOD field and 3-bit R/M field together specify the second operand
39
Examples
MOV BL,AL (88C316) Opcode for MOV = 100010 D = 0 (AL source operand) W bit = 0 (8-bits) Therefore byte 1 is 100010002=8816 MOD = 11 (register mode) REG = 000 (code for AL) R/M = 011 (destination is BL) Therefore Byte 2 is 110000112=C316
40
Examples:
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
41
42
44
45
The stack
The stack
The stack is a block of memory reserved for temporary storage of data and registers. Access is LAST-IN, FIRST OUT (LIFO)
The last memory location used in the stack is given by the effective
address calculated from the SP register and the SS register:
Example:
46
The stack
Data may be stored onto the stack using the PUSH instruction this automatically decrements SP by 2 (all stack operations involve words). The POP instruction removes data from the stack (and increments SP by 2). The stack may be up to 64K-bytes in length.
47
POPF ; removes a word from the stack and loads it into FLAGS
PUSH may be used with any register to save a word (the register contents) onto the stack. The usual order (e.g. as with MOV) of storing the lower order byte in the lower memory location is used. PUSH may also be used with immediate data, or data in memory. POP is the inverse of the PUSH instruction; it removes a word from the top of the stack. Any memory location or 16-bit register (except CS) may be used as the destination of a POP instruction. PUSHF and POPF saves and loads the FLAGS register to/from the stack,respectively.
48
Exchange Instruction (XCHG) XCHG exchanges the contents of two registers or a register and memory. Both byte and word sized exchanges are possible.
Examples:
XCHG AX,BX; exchange the contents of AX and BX XCHG CL,BL; exchange CL and BL contents
49
50
LEA &LDS
LEA loads the offset of a memory address into a 16-bit register. The offset address may be specified by any of the addressing modes.
Examples (with BP=1000H): LEA AX,[BP+40H];=>SS:[1000H+40H] =SS:[1040H];load 1040H into AX
LES reads two words from memory and is very similar to LDS except that the second word is stored in ES instead of DS.
LES is useful for initializing that DI and ES registers for strings operation.
Example (with DS=1000H):
LES DI, [2222H]; loads DI with contents stored at 12222H and 12223H and loads ES with contents at 12224 and 12225H
52
LAHF, SAHF LAHF (load AH with the low-order byte of the FLAGS register) and SAHF (Store AH into the low-order byte of the FLAG register)
very rarely used instructions -originally present to allow translation of 8085 programs to 8086.
53
IN, OUT
Examples: IN AX, 0C8h IN AL, DX OUT p8 ,AX OUT DL, AX ;reads port address at C8h (8 bit address) and loads into AX ;reads the port address given by DX and loads into AL ;sends the data in AX to port p8 ; sends the data in AX to port given by DL
IN reads data from the specified IO port (8-bit or 16-bit wide) to the accumulator (
AL or AX).
The IO port can be an immediate address (8-bit only) or specified by a variable or register (8 or 16-bit address). (Seems only DX can be used.) OUT sends data from the accumulator register to the specified I/O port. Both byte and word sized data may be sent using IN and OUT.
54
55
56
Addition Binary addition of two bytes or two words are performed using: ADD d,s ADD adds bytes or words in d and s and stores result in d. The operands d and s can use the same addressing modes as in MOV. Addition of double-word is achieved by using the carry bit in the FLAGS register. The instruction ADC d,s automatically includes the carry flag, and is used to add the more significant word in a double-word addition. Addition
Example: addition of two double words stored at [x] and [y] MOV AX, [x] ; Loads AX with the word stored at location [x] MOV DX, [x+2] ; Loads the high order word ADD AX, [y] ; Adds the low order word at [y] ADC DX, [y+2] ; Add including the carry from the low order words
57
Addition (cont.)
Example: addition of two double words stored at [x] and [y]
Addition of binary coded decimal numbers (BCD) can be performed by using ADD or ADC followed by the DAA instruction to convert the number in register AL to a BCD representation. (see example)
Addition of numbers in their ASCII form is achieved by using AAA (ascii adjust after addition).
58
59
Subtraction
60
Multiplication
61
Division
62
63
64
65
66
67
68
List file of 8086 assembly language program to produce packed BCD from two ASCII characters
69
70
72
REP prefix
73
74
75
76
77
78
Unconditional JUMP
79
80
81
82
Conditional Jumps
83
84
85
86
Loops
87
88
89
90
91
Procedures
92
Stack Diagram
93
94
Interrupt Instructions
95
Interrupt Instructions
96
97
98
Arrays
Any consecutive storage locations of the same size can be called an array
X DW 40CH,10B,-13,0 Y DB 'This is an array' Z DD -109236, FFFFFFFFH, -1, 100B Components of X are at X, X+2, X+4, X+8 Components of Y are at Y, Y+1, , Y+15 Components of Z are at Z, Z+4, Z+8, Z+12
99
DUP
Allows a sequence of storage locations to be defined or reserved Only used as an operand of a define directive
DB DW DB db 40 DUP (?) 10h DUP (0) 3 dup ("ABC") 4 dup(3 dup (0,1), 2 dup('$'))
100
Word Storage
Word, doubleword, and quadword data are stored in reverse byte order (in memory)
Directive DW 256 DD 1234567H DQ 10 X DW 35DAh Bytes in Storage 00 01 67 45 23 01 0A 00 00 00 00 00 00 00 DA 35
101
EQU Directive
name EQU expression
expression can be string or numeric Use < and > to specify a string EQU these symbols cannot be redefined later in the program sample EQU 7Fh aString EQU <1.234> message EQU <This is a message>
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Macro definition:
name
MACRO [parameters,...]
<instructions>
ENDM
MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 MOV CX, p3 ENDM ORG 100h MyMacro 1, 2, 3 MyMacro 4, 5, DX RET 119
120
ORG
100h
CALL m1
MOV
RET
AX, 2
; return to Main Program.
; return to caller.
123