Now we begin our exploration in the assembly language
description
Transcript of Now we begin our exploration in the assembly language
川大软件学院左航
Now we begin our exploration
in the assembly language
川大软件学院左航
Chapter 4
Chapter 4:Assembly Language-
Addressing 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
1,2,3 operand SS/DSCS
川大软件学院左航
4.1 Prerequisite knowledge about instructions
• We will use the most common instruction “MOV” to illustrate data addressing modes.
MOV operand1 operand2
Copy content
DST SRC
川大软件学院左航
4.1 Prerequisite knowledge about instructions
• MOV X,Y X = Y
opcode operand1 operand2
AX
BX
CX
DX
00100101
11001011
11110000
10111001
10101110
immediate data MOV AX, 23H
2.register
3.memory
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 = AX + 1234H
opcode operand1 operand2
AX
BX
CX
DX
00100101
11001011
11110000
10111001
10101110
immediate data 1234H
memcpu
川大软件学院左航
12H34HOpecode
00000H
FFFFFH
1FFFFH
3FFFFH
90000H
9FFFFH
MEMMORY 1M
CS
30000HDS
SS 10000H
CS + IP
川大软件学院左航
4.2.2 Immediate addressing
• Example:– MOV AX,1234H
AH AL
12H
34H
opcode
AX
Code segment
00105
00104
00103
川大软件学院左航
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 AX,100H AX ← 100H
– MOV EAX,0A5FH EAX ←0A5FH
– MOV AH,1101B AH ←1101B
– MOV AL,’A’---------AL ← 41H
川大软件学院左航
4.2.2 Immediate addressing• A simple program .MODEL TINY (only cs)
0000 .CODE .STARTUP0100 B8 0000 MOV AX,00103 B8 0000 MOV BX,0000H0106 B9 0000 MOV CX,00109 8B F0 MOV SI,AX .EXIT (return to DOS) END
川大软件学院左航
4.2.3 Direct data addressing
川大软件学院左航
4.1 Prerequisite knowledge about instructions
• MOV AX,NUM AX = AX + NUM
opcode operand1 operand2
AX
BX
CX
DX
00100101
11001011
11110000
10111001
10101110
immediate data 1234H
川大软件学院左航
20H00HOpecode
3050
00000H
FFFFFH
1FFFFH
3FFFFH
90000H
9FFFFH
MEMMORY 1M
CS
30000HDS
SS 10000H
Physical address
= segment base address + offset
=DS + 2000H
Address of NUM
Content of NUM
川大软件学院左航
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
30 00 020
00
opcode
30
50
AH ALAX
DS
30002
Code
segment
data
segment
30000
2000
32000
+
32001
32000
2
3
1
4
NUM DW 3050H
MOV AX,NUM
NUM
川大软件学院左航
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 AL,BL (8) AL ← BL
– MOV DS,AX (16) DS ← AX
– MOV SP,BP (16) SP ← BP
– MOV 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
2 0 0 0 0
X X
opcode
30
50AH ALAX
DS
20002
Code
segment
data
segment
20000
1000
21000
+
21001
21000
1
2
3
MOV AX,[SI]
1 0 0 0 SI
川大软件学院左航
4.2.4 Register indirect addressing
• A. DS --------[BX] 、 [SI] 、 [DI]
• Example:
MOV AX , [BX] AX← (DS:[BX])
MOV AH , [DI] AH← (DS:[DI])
Base address + offset
川大软件学院左航
4.2.4 Register indirect addressing
• B. SS-------- [BP]
• Example:
MOV AX , [BP] AX← (SS:[BP])
• MOV ES:[2000H],AL [ES:[2000]] ←AL
Base address + offset
川大软件学院左航
4.2.4 Register indirect addressing
• Example: MOV [DI],10H cause ambiguous
DI = 0200H,DS =1000HMOV BYTE PTR [DI],10H
MOV WORD PTR [DI],10H
00H
10H
10H
10200H
10200H
10201H
川大软件学院左航
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 AGAIN: DATAS DW 50 DUP (?) MOV AX,ES:
[046CH] .CODE MOV [BX],AX .STARTUP INC BX
INC BX MOV AX,0 LOOP AGAIN MOV ES,AX .EXIT MOV BX,OFFSET DATAS END MOV CX,50
川大软件学院左航
?
?
? 10000H
10001H
10031H
10032H
DB 50 DUP (?)
Data Segment
DS = 1000H
DATAS = 0
川大软件学院左航
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]
30 00 0
X X
opcode
30
50
Code
segment
data
segment
1
00
05 00
30600
AH ALAX
DS
SI
30601
川大软件学院左航
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 MOV DI,10H
• .DATA MOV AL,ARRAY[DI]
• ARRAY DB 16 DUP (?) MOV DI,20H
• DB 29H MOV ARRAY[DI],AL
• DB 30 DUP (?) .EXIT
• .CODE END
• .STARTUP
川大软件学院左航
? 17 29H 16 ? 15
? 0
FFFFFH
00000H
DS = 30000H
ARRAY DB 16 DUP (?)
OFFSET
DB 30 DUP (?)
OFFSET = 0FH = ARRAY + 15
ARRAY OFFSET = 0
OFFSET = 10H = ARRAY + 16
DS + 01H
DB 29H
P85 3-7
川大软件学院左航
29H 00020H
?
?
29H
?
?
00000H
00001H
0000FH
00010H
00011H
0002EH
DB 16 DUP (?)
DB 30 DUP (?)
DB 29HData Segment
DS =0000H
ARRAY = 0
MOV AL, ARRAY[DI] ----ARRAY+DI+DS*10H
川大软件学院左航
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: MOV CX,[BX+DI] CX ←DS:[BX+DI] 16
MOV CH,[BP+SI] CH ←SS:[BP+SI] 8
川大软件学院左航
4.2.5 Base-plus-index addressing
• MOV AX ,[BX+SI]
30 00 0
X X
opcode
30
50
Code
segment
data
segment
12 00
05 00
31700
AH ALAX
DS
BX
SI
31701
川大软件学院左航
4.2.5 Base-plus-index addressing
•Examples:MOV [BX+SI],SP DS:[BX+SI] ← SP
16MOV [BP+DI],AH SS:[BP+DI] ←AH
8MOV CL,[EDX+EDI] CL ←DS:[EDX+EDI]
8MOV [EDX+EDI],ECX DS:[EDX+EDI]←ECX
32
川大软件学院左航
4.2.5 Base-plus-index addressing• Simple program p85 3-7
• .DOMEL SMALL MOV BX, OFFSET ARRAY
• .DATA MOV DI,10H• ARRAY DB 16 DUP (?) MOV AL,[BX+DI]• DB 29H MOV DI,20H• DB 30 DUP (?) MOV [BX+DI],AL• .CODE .EXIT• .STARTUP END
• DS+OFFSET ARRAY + 10H
川大软件学院左航
?
?
?
29H
?
? 00000H
00001H
0000FH
00010H
00011H
0002EH
DB 16 DUP (?)
DB 30 DUP (?)
DB 29HData Segment
DS =0000H
ARRAY = 0
DS+OFFSET ARRAY + 10H
川大软件学院左航
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]
30 00 0
X X
opcode
30
50
Code
segment
data
segment
1
00
05 00
31700
AH ALAX
DS
DI11 00BX
31701
川大软件学院左航
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 MOV BX, OFFSET
RECA• RECA DB 10 DUP (?) MOV DI, 0• RECB DB 10 DUP (?) MOV AL, FILE[BX+DI]• RECC DB 10 DUP (?) MOV BX, OFFSET
RECC• RECD DB 10 DUP (?) MOV DI, 2• .CODE MOV FILE[BX+DI], AL• .STARTUP .EXIT • END
Variable defined by EQU does not take up space in memory, but it does represent type information.
川大软件学院左航
• .1EH
1D H
15H14H
0 B H0 A H
0 0 H0 0 HOFFSET RECA = 0 Low
address
High address
RECA
10
10
11
2021
3031
OFFSET RECB = 10
OFFSET RECC =20
OFFSET RECC =30
RECB
RECC
RECD
川大软件学院左航
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 MOV EBX, OFFSET LIST• .386 MOV ECX, 2• .DATA MOV [EBX+2*ECX], AX• LIST DW 0,1,2,3,4 MOV ECX, 4• DW 5,6,7,8 MOV [EBX+2*ECX], AX• .CODE INC ECX• .STARTUP INC ECX• MOV [EBX+2*ECX], AX• .EXIT• END
川大软件学院左航
4.2.8 Scaled-index addressing
• Simple program P91 3-100 0 H
0 4 H0 0 H0 3 H0 0 H0 2 H0 0 H0 1 H0 0 H0 0 H
03
……
RIGHT
Low address
High address
OFFSET
04
020100
05
06
07
08
川大软件学院左航
Summary of data addressing
川大软件学院左航
Summary of data addressing 1.Register addressing
– MOV AX,BX
34H 12H 34H 12H
AX BX
川大软件学院左航
4.Register indirect addressing
2 0 0 0 0
X X
opcode
30
50AH ALAX
DS
20002
Code
segment
data
segment
20000
1000
21000
+
21001
21000
1
2
3
MOV AX,[BX]
1 0 0 0 BX
川大软件学院左航
6.Register Relative Addressing
• MOV AX,[BX+100H]
30 00 0 X X
opcode
30
50
Code
segment
data
segment
1
00
05 00
30600
AH ALAX
DS
BX
川大软件学院左航
Summary of data addressing 2.Immediate addressing
– MOV CX,1234H
12H 34H12H
CX 34H
OPCODE
CS
川大软件学院左航
Summary of data addressing3.Direct data addressing
– MOV AX,NUM
30H 50H30H
AX50H
DS 30000H
2000H ?
DS
NUM
NUM32000H
32001H
川大软件学院左航
30 00 0 20
00
opcode
30H
50HAH ALAX
DS
30002
Code
segment
data
segment
30000
2000
32000
+
32001
32000
2
3
1
4
NUM DW 3050H
MOV AX,NUM
NUM
NUM = 2000H
川大软件学院左航
5.Base-plus-Index Addressing
• MOV AX ,[BX+SI]
30 00 0 XX
opcode
30
50
Code
segment
data
segment
12 00
05 00
31700
AH ALAX
DS
BX
SI
31701
川大软件学院左航
7. Base relative-plus-index addressing
• MOV AX,[BX+DI+100H]
30 00 0
X X
opcode
30
50
Code
segment
data
segment
1
00
05 00
31700
AH ALAX
DS
DI11 00BX
31701
川大软件学院左航
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.NAMESP93—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,32MOV AL,0MOV DI,OFFSET NAME1.NAMESREP 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 EB– 10001 02– 10002 --– 10003 --– 10004
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 AX– JMP NEAR PTR [BX]– JMP NEAR PRT[DI+2]– JMP TABLE[BX]– JMP 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
• Example:– PUSH BX (LIFO)
12 34
EAX
EBX
ECX
EDX
ESP
SS*10H
XX
opcode
12 SP-1
34 SP-2
STACK
SEGMENT
CODE
SEGMENT
P96 2 para from bottom
Stack top
Stack bottom
FFFFFH
00000H
川大软件学院左航
• We will discuss JMP, CALL, PUSH and POP in detail in the following chapters.