Transcript Document

Now we begin our
exploration
in the
assembly language
川大软件学院左航
Chapter 4
川大软件学院左航
Chapter 4:
Assembly LanguageAddressing Modes(寻址方式)
Chapter 4:
• 4.1 Prerequisite knowledge about
instructions
• 4.2 Data Addressing Modes
• 4.3 Program Memory-Addressing
Modes
• 4.4 Stack Memory-Addressing Modes
川大软件学院左航
4.1 Prerequisite knowledge
about instructions
川大软件学院左航
4.1 Prerequisite knowledge about
instructions
• Default expression
– Segment register ---- stored segment base
address
– imm/im ---- immediate data
– DST ---- destination operand
– SRC ---- source operand
– R8/R16/R32----register of 8/16/32 bits
– mm ---- memory data
川大软件学院左航
4.1 Prerequisite knowledge about
instructions
•
MOV AX, BX
opcode
operand
operand
CS
1,2,3 operand
川大软件学院左航
SS/DS
4.1 Prerequisite knowledge about
instructions
• We will use the most common
instruction “MOV” to illustrate data
Copy
addressing modes.
DST
MOV
SRC
content
operand1 operand2
川大软件学院左航
4.1 Prerequisite knowledge about instructions
•
MOV X,Y
X
opcode
2.register
3.memory
AX
BX
CX
DX
=
operand1
00100101
11001011
11110000
10111001
10101110
川大软件学院左航
Y
operand2
immediate data
MOV AX, 23H
1.immediate data
4.1 Prerequisite knowledge about
instructions
• 1. Operand is directly in the instructions.
• ----immediately addressing
• 2. Operand is stored in a register in CPU.
• ----register addressing
• 3. Operand is stored in the memory.
• ----the address is combined by segment
address and offset address.
川大软件学院左航
4.2 Data Addressing Modes
川大软件学院左航
4.2 Data Addressing Modes
•
•
•
•
•
•
•
4.2.2 Immediate addressing
4.2.3 Direct data addressing
4.2.1 Register addressing
4.2.4 Register indirect addressing
4.2.6 Register relative addressing
4.2.5 Base-plus-index addressing
4.2.7 Base relative-plus-index
addressing
• 4.2.8 Scaled-index addressing
• 4.2.9 Data Structures
川大软件学院左航
4.2.2 Immediate addressing
川大软件学院左航
4.2.2 Immediate addressing
•
MOV AX,1234H AX
opcode
AX
cpu BX
CX
DX
=
operand1
00100101
mem 11001011
11110000
10111001
10101110
川大软件学院左航
AX + 1234H
operand2
immediate
data
1234H
FFFFFH
9FFFFH
CS
CS + IP
90000H
3FFFFH
DS
12H
34H
Opecode
MEMMORY 1M
30000H
1FFFFH
SS
10000H
00000H
川大软件学院左航
4.2.2 Immediate addressing
• Example:
– MOV AX,1234H
AX AH
AL
00105
00104
00103
12H
34H
opcode
川大软件学院左航
Code
segment
4.2.2 Immediate addressing
• Definition:
– Operand2 is a immediate data or an
expression whose value can be figure out.
It’s a constant data.
• Example:
– MOV
– MOV
– MOV
– MOV
AX,100H
AX ← 100H
EAX,0A5FH EAX ←0A5FH
AH,1101B AH ←1101B
AL,’A’---------AL ← 41H
川大软件学院左航
4.2.2 Immediate addressing
•
A simple program
.MODEL TINY (only cs)
0000
0100
0103
0106
0109
B8
B8
B9
8B
0000
0000
0000
F0
.CODE
.STARTUP
MOV AX,0
MOV BX,0000H
MOV CX,0
MOV SI,AX
.EXIT
(return to DOS)
END
川大软件学院左航
4.2.3 Direct data addressing
川大软件学院左航
4.1 Prerequisite knowledge about
instructions
•
MOV AX,NUM AX
opcode
AX
BX
CX
DX
=
AX + NUM
operand1
operand2
00100101
11001011
11110000
10111001
10101110
川大软件学院左航
immediate
data
1234H
FFFFFH
9FFFFH
CS
Physical address
Address
of NUM
90000H
3FFFFH
DS
20H
00H
Opecode
30000H
=DS + 2000H
30
50
MEMMORY 1M
Content
of NUM
1FFFFH
SS
10000H
00000H
= segment base
address + offset
川大软件学院左航
4.2.3 Direct data addressing
• A. Direct addressing
• B. Displacement Addressing
川大软件学院左航
4.2.3 Direct data addressing
• A. Direct addressing
– Definition: Transfer data between a
memory location, located within the data
segment and the AX register. P79
– Example:
• MOV AX, NUM
AX ←DS:[NUM]
• MOV TWO,AX
DS:[TWO] ←AX
• MOV ES:[2000H],AL ES:[2000] ←AL
川大软件学院左航
4.2.3 Direct data addressing
NUM DW 3050H
MOV AX,NUM
30
0
30000
2000
+
2
AX
00
DS
20
1
00
opcode
NUM
Code
segment
32000
AH AL
4
30002 3
32001
30
32000
50
川大软件学院左航
data
segment
4.2.3 Direct data addressing
• SS+SP/ESP or SS+BP/EBP
• DS+ memory offset
• MOV ES:[2000H],AL
ES:[2000] ←AL
川大软件学院左航
4.2.3 Direct data addressing
• B. Displacement Addressing
– It is almost identical to direct addressing,
except that the instruction is four bytes
wide instead of three.
– And the registers used aren’t AX.
• MOV AX, NUM
AX ←DS:[NUM]
– We need not consider about the instruction
bytes now, so ignore it.
• MOV CX,NUM
川大软件学院左航
4.2.1 Register addressing
川大软件学院左航
4.2.1 Register addressing
• Definition:
– All the operands are in registers(8/16/32b).
It’s a variable data.
• Example:
– MOV
– MOV
– MOV
– MOV
AL,BL (8)
AL ← BL
DS,AX (16)
DS ← AX
SP,BP (16)
SP ← BP
ECX,EDX
ECX ←EDX
川大软件学院左航
4.2.1 Register addressing
• Special Example:
– MOV EAX,BX
– Wrong, mixed size in MOV (32 ,16)
– MOV DS,AX
– Right
– MOV ES,DS
– Wrong, segment-to-segment
– MOV CS,BX or MOV IP,BX
– Wrong, CS or IP Register can’t be DST
川大软件学院左航
4.2.4 Register indirect addressing
• MOV AX,BX
• MOV AX,[BX]
川大软件学院左航
4.2.4 Register indirect addressing
• Definition:
– physical address = (BP,BX)/(DI,SI) +
base address (segment registers)
– MOV AX,[BX]
– memory data
– offset address is stored BX、SI、DI 、 BP.
– [ ] means indirect addressing
– MOV AX,[DX]
wrong
川大软件学院左航
4.2.4 Register indirect addressing
MOV AX,[SI]
2 0
+
0
0
0
1 0
0
0
20000
1000
Code
DS
X X
opcode
SI
segment
1
21000
AX
20002
21001
AH AL
3
21000
川大软件学院左航
30
50
2
data
segment
4.2.4 Register indirect addressing
• A. DS --------[BX]、[SI]、[DI]
Base address
+
offset
• Example:
MOV AX,[BX] AX← (DS:[BX])
MOV AH,[DI] AH← (DS:[DI])
川大软件学院左航
4.2.4 Register indirect addressing
• B. SS-------- [BP]
Base address
+ offset
• Example:
MOV AX,[BP]
• MOV ES:[2000H],AL
AX← (SS:[BP])
[ES:[2000]] ←AL
川大软件学院左航
4.2.4 Register indirect addressing
• Example:
MOV [DI],10H
cause ambiguous
DI = 0200H,DS =1000H
MOV BYTE PTR [DI],10H
10200H
10H
10201H
00H
10H
MOV WORD PTR [DI],10H
10200H
川大软件学院左航
4.2.4 Register indirect addressing
• MOV [DI],[BX]
– Wrong , memory to memory is not permitted.
川大软件学院左航
4.2.4 Register indirect addressing
• Simple program P83 3-6
.MODEL SMALL (DATA & CODE)
.DATA
DATAS DW 50 DUP (?)
.CODE
.STARTUP
MOV
MOV
MOV
MOV
AX,0
ES,AX
BX,OFFSET DATAS
CX,50
AGAIN:
MOV AX,ES:[046CH]
MOV [BX],AX
INC BX
INC BX
LOOP AGAIN
.EXIT
END
川大软件学院左航
10032H
Data Segment
?
10031H
DS = 1000H
DB 50 DUP (?)
?
DATAS = 0
?
10001H
10000H
川大软件学院左航
4.2.6 Register relative addressing
川大软件学院左航
4.2.6 Register relative addressing
• Definition:
– physical address = displacement (位
移量) + (BP,BX)/(DI,SI) + base
address (segment registers)
• Examples:
– MOV AX,[SI+100H] AX ←DS:[SI+100H]
川大软件学院左航
4.2.6 Register relative addressing
• MOV AX,[SI+100H]
DS
30
00
0
Code
X X
opcode
1 00
SI
05
00
AX AH
AL
30601
30600
川大软件学院左航
30
50
segment
data
segment
4.2.6 Register relative addressing
• Examples:
– MOV AX,[DI+100H]
AX ←DS:[DI+100H]
– MOV ARRAY[SI],BL DS:[ARRAY+SI] ←BL
– MOV LIST[SI+2],CL
DS:[LIST+SI+2] ←CL
– MOV CX,[BP+10H]
CX ←SS:[BP+10H]
川大软件学院左航
4.2.6 Register relative addressing
• Simple program p88 3-8
•
•
•
•
•
•
•
.MODEL SMALL
.DATA
ARRAY DB 16 DUP (?)
DB 29H
DB 30 DUP (?)
.CODE
.STARTUP
MOV DI,10H
MOV AL,ARRAY[DI]
MOV DI,20H
MOV ARRAY[DI],AL
.EXIT
END
川大软件学院左航
P85 3-7
FFFFFH
DB 30 DUP (?)
DB 29H
?
29H
?
17
16
15
OFFSET = 0FH = ARRAY + 15
ARRAY DB
16 DUP (?)
?
DS = 30000H
00000H
OFFSET = 10H = ARRAY + 16
DS + 01H
0
ARRAY
OFFSET
川大软件学院左航
OFFSET = 0
MOV AL,
ARRAY[DI] ---ARRAY+DI+D
S*10H
Data Segment
DS =0000H
ARRAY = 0
0002EH
29H 00020H
?
?
00011H
29H 00010H
0000FH
DB 30 DUP (?)
DB 29H
DB 16 DUP (?)
?
00001H
?
00000H
川大软件学院左航
4.2.5 Base-plus-index addressing
川大软件学院左航
4.2.5 Base-plus-index addressing
• Definition:
– memory data.
– offset = BP/BX(基址)+ DI/SI (变址)
– Physical address = offset + DS/SS
• Examples:
←DS:[BX+DI] 16
CH ←SS:[BP+SI] 8
MOV CX,[BX+DI] CX
MOV CH,[BP+SI]
川大软件学院左航
4.2.5 Base-plus-index addressing
• MOV AX,[BX+SI]
30
DS
00
0
BX
12
00
SI
05
00
AX AH
AL
Code
X X
opcode
31701
31700
川大软件学院左航
30
50
segment
data
segment
4.2.5 Base-plus-index addressing
• Examples:
MOV
MOV
MOV
MOV
[BX+SI],SP
[BP+DI],AH
CL,[EDX+EDI]
[EDX+EDI],ECX
DS:[BX+SI] ← SP
SS:[BP+DI] ←AH
CL ←DS:[EDX+EDI]
DS:[EDX+EDI]←ECX
川大软件学院左航
16
8
8
32
4.2.5 Base-plus-index addressing
•
•
•
•
•
•
•
•
Simple program p85 3-7
.DOMEL SMALL
.DATA
ARRAY DB 16 DUP (?)
DB 29H
DB 30 DUP (?)
.CODE
.STARTUP
MOV BX, OFFSET ARRAY
MOV DI,10H
MOV AL,[BX+DI]
MOV DI,20H
MOV [BX+DI],AL
.EXIT
END
• DS+OFFSET ARRAY + 10H
川大软件学院左航
DS+OFFSET
ARRAY + 10H
0002EH
?
Data Segment
DS =0000H
ARRAY = 0
DB 30 DUP (?)
?
?
00011H
29H 00010H
0000FH
DB 29H
DB 16 DUP (?)
?
00001H
?
00000H
川大软件学院左航
4.2.7 Base relative-plus-index
addressing
川大软件学院左航
4.2.7 Base relative-plus-index
addressing
• Definition:
– offset = displacement (位移量) +BP/BX +DI/SI
– Physical address = offset + DS/SS
– Examples:
– MOV DH,[BX+DI+100H] DH DS:[BX+DI+100H]
川大软件学院左航
4.2.7 Base relative-plus-index addressing
• MOV AX,[BX+DI+100H]
DS
DI
BX
30
00
0
Code
X X
opcode
1 00
05
00
11
00
AX AH
AL
31701
31700
川大软件学院左航
30
50
segment
data
segment
4.2.7 Base relative-plus-index
addressing
• Examples:
–
–
–
–
MOV DH,[BX+DI+20H] DH ←DS:[BX+DI+20H]
MOV AX,FILE[BX+DI] AX ←DS:[FILE+BX+DI]
MOV LIST[BP+DI],CL SS:[LIST+BP+DI] ←CL
MOV EAX,FILE[EBX+ECX+2]
• Simple program p89 3-9
川大软件学院左航
•
•
•
•
•
•
•
•
•
•
.MODEL SMALL
.DATA
FILE EQU THIS BYTE
RECA DB 10 DUP (?)
RECB DB 10 DUP (?)
RECC DB 10 DUP (?)
RECD DB 10 DUP (?)
.CODE
.STARTUP
MOV
MOV
MOV
MOV
MOV
MOV
.EXIT
END
BX, OFFSET RECA
DI, 0
AL, FILE[BX+DI]
BX, OFFSET RECC
DI, 2
FILE[BX+DI], AL
Variable defined by EQU does not take up space in memory, but it does represent
type information.
川大软件学院左航
• .
OFFSET RECC =30
RECD
31
30
1EH
1DH
High
address
OFFSET RECC =20
OFFSET RECB = 10
OFFSET RECA = 0
RECC
RECB
RECA
21
20
15H
11
10
0BH
1
0
00H
14H
0AH
00H
川大软件学院左航
Low
address
4.2.8 Scaled-index addressing
川大软件学院左航
4.2.8 Scaled-index addressing
• Definition:
– It’s used in 80386 and above. Designed to
address word and doubleword more easily.
– Physical address = (32-bit base register)
+ scaling factor * (index registers)
• Example:
– MOV EAX,[EBX+4*ECX] ---- doubleword
– MOV AX,[EBX+2*ECX] ---- word
川大软件学院左航
4.2.8 Scaled-index addressing
• P91
•
•
•
•
•
•
•
•
•
•
3-10
.MODEL SMALL
.386
.DATA
LIST DW 0,1,2,3,4
DW 5,6,7,8
.CODE
.STARTUP
MOV EBX, OFFSET LIST
MOV ECX, 2
MOV [EBX+2*ECX], AX
MOV ECX, 4
MOV [EBX+2*ECX], AX
INC ECX
INC ECX
MOV [EBX+2*ECX], AX
.EXIT
END
川大软件学院左航
4.2.8 Scaled-index addressing
• Simple program P91
RIGHT
3-10
……
00H
08
07
06
05
04
03
02
01
00
04H
OFFSET
High
address
00H
03H
00H
02H
00H
01H
00H
00H
川大软件学院左航
Low
address
Summary of data addressing
川大软件学院左航
Summary of data addressing
1.Register addressing
– MOV AX,BX
AX
34H
BX
12H
34H
川大软件学院左航
12H
4.Register indirect addressing
MOV AX,[BX]
2 0
+
0
0
0
1 0
0
0
20000
1000
Code
DS
X X
opcode
BX
segment
1
21000
AX
20002 2
21001
30
AH AL
3
21000
川大软件学院左航
50
data
segment
6.Register Relative Addressing
• MOV AX,[BX+100H]
30
DS
00
XX
opcode
0
1 00
05
00
AX AH
AL
BX
30600
川大软件学院左航
30
50
Code
segment
data
segment
Summary of data addressing
2.Immediate addressing
– MOV CX,1234H
CX
12H
12H
34H
34H
OPCODE
川大软件学院左航
CS
Summary of data addressing
3.Direct data addressing
– MOV AX,NUM
AX
DS
30H
50H
32001H
30H
32000H 50H
30000H
NUM
DS
NUM 2000H ?
川大软件学院左航
NUM DW 3050H
MOV AX,NUM
30
0
30000
2000
+
3
AX
00
DS
2
20
00
opcode
NUM
Code
segment
32000
AH AL
4
30002 1
32001
30H
32000
川大软件学院左航
50H
data
segment
NUM =
2000H
5.Base-plus-Index Addressing
• MOV AX,[BX+SI]
30
DS
00
XX
opcode
0
BX
12
00
SI
05
00
AX AH
AL
31701
31700
川大软件学院左航
30
50
Code
segment
data
segment
7. Base relative-plus-index addressing
• MOV AX,[BX+DI+100H]
DS
DI
BX
30
00
0
Code
X X
opcode
1 00
05
00
11
00
AX AH
AL
31701
31700
川大软件学院左航
30
50
segment
data
segment
Summary of data addressing
• DS-----------BX,DI,SI
• SS------------BP
• MOV AX,ARRAY[BX]
• MOV AX,[BX+100H]
川大软件学院左航
4.2.9 Data Structures
川大软件学院左航
4.2.9 Data Structures
• If we want store an address book. Must
we copy 100 times?
• Can we do it more efficiently?
川大软件学院左航
4.2.9 Data Structures
• Solution:
– If all the data is in the same format. We
can use data structure to define a template.
It will do the duplicate work for us .
川大软件学院左航
4.2.9 Data Structures
• Structure in c++
–
–
–
–
–
–
–
–
–
–
–
struct Info
{
char names[32];
char street[32];
char city[16];
};
void main()
{
Info classmates;
classmates = {“Frank”, “Wenhua road”, “CD”};
}
川大软件学院左航
4.2.9 Data Structures
• Example3.11 p91
INFO STRUC
NAMES DB 32 DUP(?)
STREET DB 32 DUP(?)
CITY DB 16 DUP(?)
INFO ENDS
NAME1 INFO <‘BOB’,’123STREET’,’CHENGDU’>
MOV SI,OFFSET NAME1.NAMES
P93—3-12
川大软件学院左航
4.2.9 Data Structures
• Example3.12
• INFO STRUC
NAMES DB 32 DUP(?)
STREET DB 32 DUP(?)
CITY DB 16 DUP(?)
INFO ENDS
MOV CX,32
MOV AL,0
MOV DI,OFFSET NAME1.NAMES
REP STOSB
川大软件学院左航
4.3 Program Memory-Addressing
Modes
川大软件学院左航
4.3 Program Memory-Addressing
Modes
• 4.3.1 Direct Program Memory
Addressing
• 4.3.2 Relative Program Memory
Addressing
• 4.3.3 Indirect Program Memory
Addressing
川大软件学院左航
4.3.1 Direct Program Memory
Addressing
川大软件学院左航
4.3.1 Direct Program Memory
Addressing
• Definition:
– the address is stored with the opcode.
• Example:
– JMP [10000H]
– CS:1000H;IP:0000H
川大软件学院左航
4.3.2 Relative Program Memory
Addressing
• Example:
– JMP [2]
p94 figure 3-15
– Only IP is changed.
–
–
–
–
–
10000
10001
10002
10003
10004
EB
02
---
JMP [2]
IP before JMP
IP after JMP
川大软件学院左航
4.3.3 Indirect Program Memory
Addressing
• Example:
– See p94 first para & p95 3-16/3-13
– Only IP is changed.
–
–
–
–
–
JMP
JMP
JMP
JMP
JMP
AX
NEAR PTR [BX]
NEAR PRT[DI+2]
TABLE[BX]
ECX
川大软件学院左航
4.3.3 Indirect Program Memory
Addressing
– See p95 3-16/3-13
• Table DW LOC0
•
DW LOC1
•
DW LOC2
• MOV BX, 4
• JMP TABLE[BX]
川大软件学院左航
4.4 Stack Memory-Addressing Modes
川大软件学院左航
4.4 Stack Memory-Addressing Modes
• It is usually used in PUSH, POP, CALL to
protect some important data or pointers.
川大软件学院左航
4.4 Stack Memory-Addressing Modes
FFFFFH
• Example:
– PUSH BX
(LIFO)
XX
opcode
EAX
EBX
CODE
SEGMENT
12 34
ECX
Stack bottom
EDX
STACK
ESP
12 SP-1
34 SP-2
P96 2 para from bottom
SS*10H
00000H
川大软件学院左航
SEGMENT
Stack top
• We will discuss JMP, CALL, PUSH and
POP in detail in the following chapters.
川大软件学院左航