MemoryAccessandOrg.pdf
(
579 KB
)
Pobierz
AoA.book
Memory Access and Organization
Memor
y Access and Organization
Chapter Two
2.1
Chapter Overview
In earlier chapters you sa
w ho
w to declare and access simple v
ariables in an assembly language pro
-
gram. In this chapter you will learn ho
w the 80x86 CPUs actually access memory (e.g., v
ariables).
Y
ou will
also learn ho
w to ef
fi
ciently or
g
anize your v
ariable declarations so the CPU can access them f
aster
. In this
chapter you will also learn about the 80x86 stack and ho
w to manipulate data on the stack with some 80x86
instructions this chapter introduces. Finally
, you will learn about dynamic memory allocation.
2.2
The 80x86 Addressing Modes
The 80x86 processors let you access memory in man
y dif
ferent w
ays. Until no
w
, you’
v
e only seen a
single w
ay to access a v
ariable, the so-called
displacement-only
addressing mode that you can use to access
scalar v
ariables. No
w it’
s time to look at the man
y dif
ferent w
ays that you can access memory on the 80x86.
The 80x86
memory addr
essing modes
pro
vide fl
e
xible access to memory
, allo
wing you to easily access
ariables, arrays, records, pointers, and other comple
x data types. Mastery of the 80x86 addressing modes is
the fi
rst step to
w
ards mastering 80x86 assembly language.
When Intel designed the original 8086 processor
, the
y pro
vided it with a fl
e
xible, though limited, set of
memory addressing modes. Intel added se
v
eral ne
w addressing modes when it introduced the 80386 micro
-
processor
. Note that the 80386 retained all the modes of the pre
vious processors. Ho
we
v
er
, in 32-bit en
vi
-
ronments lik
e
W
in32, BeOS, and Linux, these earlier addressing modes are not v
ery useful; indeed, HLA
doesn’
t e
v
en support the use of these older
, 16-bit only
, addressing modes. F
ortunately
, an
ything you can do
with the older addressing modes can be done with the ne
w addressing modes as well (e
v
en better
, as a matter
of f
act).
Therefore, you w
on’
t need to bother learning the old 16-bit addressing modes on today’
s high-per
-
formance processors. Do k
eep in mind, ho
we
v
er
, that if you intend to w
ork under MS-DOS or some other
16-bit operating system, you will need to study up on those old addressing modes.
2.2.1
80x86 Register Addressing Modes
Most 80x86 instructions can operate on the 80x86’
s general purpose re
gister set. By specifying the
name of the re
gister as an operand to the instruction, you may access the contents of that re
gister
. Consider
the 80x86 MO
V (mo
v
e) instruction
:
mov( source, destination );
This instruction copies the data from the
sour
ce
operand to the
destination
operand.
The eight-bit,
16-bit, and 32-bit re
gisters are certainly v
alid operands for this instruction.
The only restriction is that both
operands must be the same size. No
w let’
s look at some actual 80x86 MO
V instructions:
// Copies the value from BX into AX
mov( al, dl ); // Copies the value from AL into DL
mov( edx, esi ); // Copies the value from EDX into ESI
mov( bp, sp ); // Copies the value from BP into SP
mov( cl, dh ); // Copies the value from CL into DH
mov( ax, ax ); // Yes, this is legal!
, the registers are the best place to keep often used variables. As you’ll see a little later, instruc-
tions using the registers are shorter and faster than those that access memory. Throughout this chapter you’ll
see the abbreviated operands
reg
and
r/m
(register/memory) used wherever you may use one of the 80x86’s
general purpose registers.
Beta Draft - Do not distribute
© 2001, By Randall Hyde
Page
157
v
mov( bx, ax );
Remember
Chapter Two
Volume Two
2.2.2
80x86 32-bit Memory Addressing Modes
The 80x86 pro
vides hundreds of dif
ferent w
ays to access memory
.
This may seem lik
e quite a bit at fi
rst,
b
ut fortunately most of the addressing modes are simple v
ariants of one another so the
y’
re v
ery easy to learn.
And learn them you should!
The k
e
y to good assembly language programming is the proper use of memory
addressing modes.
The addressing modes pro
vided by the 80x86 f
amily include displacement-only
, base, displacement
plus base, base plus inde
x
ed, and displacement plus base plus inde
x
ed.
V
ariations on these fi
v
e forms pro
-
vide the man
y dif
ferent addressing modes on the 80x86. See, from 256 do
wn to fi
v
e. It’
s not so bad after all!
2.2.2.1
The Displacement Only Addressing Mode
The most common addressing mode, and the one that’
s easiest to understand, is the
displacement-only
(or
direct
) addressing mode. The displacement-only addressing mode consists of a 32 bit constant that spec-
ifies the address of the target location. Assuming that variable
J
is an
int8
variable allocated at address
$8088, the instruction
“
mov( J, al );”
loads the AL register with a copy of the byte at memory location
$8088. Likewise, if
int8
variable
K
is at address $1234 in memory, then the instruction “mov( dl, K );” stores
the value in the DL register to memory location $1234 (see Figure 2.1).
AL
$8088 (Address of J)
mov( J, al );
DL
$1234 (Address of K)
mov( dl, K );
Figure 2.1
Displacement Only (Direct) Addressing Mode
The displacement-only addressing mode is perfect for accessing simple scalar variables.
Intel named this the displacement-only addressing mode because a 32-bit constant (displacement) fol-
lows the
MOV opcode in memory. On the 80x86 processors, this displacement is an offset from the begin-
ning of memory (that is, address zero). The examples in this chapter will typically access bytes in memory.
Don’t forget, however, that you can also access words and double words on the 80x86 processors (see Figure
2.2).
Page
158
© 2001, By Randall Hyde
Beta Draft - Do not distribute
Memory Access and Organization
AX
$1235
$1234 (address of K)
mov( K, ax );
EDX
$1003
$1002
$1002
$1000 (address of M)
mov( edx, M );
Figure 2.2
Accessing a Word or DWord Using the Displacement Only Addressing Mode
2.2.2.2 The Register Indirect Addressing Modes
The 80x86 CPUs let you access memory indirectly through a register using the register indirect address-
ing modes. The term indirect means that the operand is not the actual address, but rather, the operand’s value
specifies the memory address to use. In the case of the register indirect addressing modes, the register’s
value is the memory location to access. For example, the instruction “mov( eax, [ebx] );” tells the CPU to
store EAX’s value at the location whose address is in EBX (the square brackets around EBX tell HLA to use
the register indirect addressing mode).
There are eight forms of this addressing mode on the 80x86, best demonstrated by the following instruc-
tions:
mov( [eax], al );
mov( [ebx], al );
mov( [ecx], al );
mov( [edx], al );
mov( [edi], al );
mov( [esi], al );
mov( [ebp], al );
mov( [esp], al );
These eight addressing modes reference the memory location at the offset found in the register enclosed by
brackets (EAX, EBX, ECX, EDX, EDI, ESI, EBP, or ESP, respectively).
Note that the register indirect addressing modes require a 32-bit register. You cannot specify a 16-bit or
eight-bit register when using an indirect addressing mode
1
. Technically, you could load a 32-bit register
with an arbitrary numeric value and access that location indirectly using the register indirect addressing
mode:
mov( $1234_5678, ebx );
mov( [ebx], al );
// Attempts to access location $1234_5678.
Unfortunately (or fortunately, depending on how you look at it), this will probably cause the operating sys-
tem to generate a protection fault since it’s not always legal to access arbitrary memory locations.
1. Actually, the 80x86 does support addressing modes involving certain 16-bit registers, as mentioned earlier. However, HLA
does not support these modes and they are not particularly useful under 32-bit operating systems.
Beta Draft - Do not distribute
© 2001, By Randall Hyde
Page 159
Chapter Two
Volume Two
The register indirect addressing mode has lots of uses. You can use it to access data referenced by a
pointer, you can use it to step through array data, and, in general, you can use it whenever you need to mod-
ify the address of a variable while your program is running.
The register indirect addressing mode provides an example of a
anonymous
variable. When using the
register indirect addressing mode you refer to the value of a variable by its numeric memory address (e.g.,
the value you load into a register) rather than by the name of the variable. Hence the phrase anonymous vari-
able.
HLA provides a simple operator that you can use to take the address of a STATIC variable and put this
address into a 32-bit register. This is the “&” (address of) operator (note that this is the same symbol that
C/C++ uses for the address-of operator). The following example loads the address of variable
J
into EBX
and then stores the value in EAX into
J
using the register indirect addressing mode:
mov( &J, ebx );
// Load address of J into EBX.
mov( eax, [ebx] );
// Store EAX into J.
Of course, it would have been simpler to store the value in EAX directly into
J
rather than using two instruc-
tions to do this indirectly. However, you can easily imagine a code sequence where the program loads one of
several different addresses into EBX prior to the execution of the “mov( eax, [ebx]);” statement, thus storing
EAX into one of several different locations depending on the execution path of the program.
Warning
: the “&” (address-of) operator is not a general address-of operator like the “&” operator in C/C++.
You may only apply this operator to static variables
2
. It cannot be applied to generic address expressions or
other types of variables. For more information on taking the address of such objects, see “Obtaining the
Address of a Memory Object” on page 191.
2.2.2.3 Indexed Addressing Modes
The indexed addressing modes use the following syntax:
mov( VarName[ eax ], al );
mov( VarName[ ebx ], al );
mov( VarName[ ecx ], al );
mov( VarName[ edx ], al );
mov( VarName[ edi ], al );
mov( VarName[ esi ], al );
mov( VarName[ ebp ], al );
mov( VarName[ esp ], al );
VarName
is the name of some variable in your program.
The indexed addressing mode computes an
effective address
3
by adding the address of the specified
variable to the value of the 32-bit register appearing inside the square brackets. This sum is the actual
address in memory that the instruction will access. So if
VarName
is at address $1100 in memory and EBX
contains eight, then “mov( VarName[ ebx ], al );” loads the byte at address $1108 into the AL register (see
Figure 2.3).
2. Note: the term “static” here indicates a STATIC, READONLY, or STORAGE object.
3. The effective address is the ultimate address in memory that an instruction will access, once all the address calculations are
complete.
Page 160
© 2001, By Randall Hyde
Beta Draft - Do not distribute
Memory Access and Organization
mov( VarName[ ebx ], al );
$1108
AL
EBX
$08
+
VarName
$1100
This is the
address of
VarName
Figure 2.3
Indexed Addressing Mode
The indexed addressing mode is really handy for accessing elements of arrays. You will see how to use
this addressing mode for that purpose a little later in this text. A little later in this chapter you will see how
to use the indexed addressing mode to step through data values in a table.
2.2.2.4 Variations on the Indexed Addressing Mode
There are two important syntactical variations of the indexed addressing mode. Both forms generate the
same basic machine instructions, but their syntax suggests other uses for these variants.
The first variant uses the following syntax:
mov( [ ebx + constant ], al );
mov( [ ebx - constant ], al );
These examples use only the EBX register. However, you can use any of the other 32-bit general purpose
registers in place of EBX. This addressing mode computes its effective address by adding the value in EBX
to the specified constant, or subtracting the specified constant from EBX (See Figure 2.4 and Figure 2.5).
Beta Draft - Do not distribute
© 2001, By Randall Hyde
Page 161
Plik z chomika:
brosaczony
Inne pliki z tego folderu:
Volume5.pdf
(23 KB)
Volume4.pdf
(29 KB)
Volume3.pdf
(27 KB)
Volume2.pdf
(24 KB)
Volume1.pdf
(29 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 19.01.2025
Angielski dla dzieci
Antyki
Audiobook
Zgłoś jeśli
naruszono regulamin