CET 3510 - Microcomputer Systems Technology
Lecture 10Dr. José M. Reyes Álamo
1
The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access◦ Variables◦ Arrays◦ Records◦ Pointers◦ Other complex data types
2
The 80x86 Addressing Modes
Most 80x86 instructions can operate on the 80x86’s general purpose register set
Registers are the best place to keep often used variables ◦ Instructions using the registers are shorter and faster
than those that access memory The general addressing modes provided by the
80x86 family are:◦ displacement-only◦ base◦ displacement plus base◦ base plus indexed◦ displacement plus base plus indexed
3
80x86 Register Addressing Modes
Also known as direct addressing mode Consists of a 32 bit constant that specifies
the address of the target location On the 80x86 processors, this displacement
is an offset from the beginning of memory (address zero)
Examples are in bytes
4
The Displacement Only Addressing Mode
5
The Displacement Only Addressing Mode
6
The Displacement Only Addressing Mode
The operand is not the actual address, but rather, the operand’s value specifies the memory address to use
The register’s value is the memory location to access Eight forms of indirect addressing mode:
◦ mov( [eax], al );◦ mov( [ebx], al );◦ mov( [ecx], al );◦ mov( [edx], al );◦ mov( [edi], al );◦ mov( [esi], al );◦ mov( [ebp], al );◦ mov( [esp], al );
7
Register Indirect Addressing Modes
Requires a 32-bit register Have to use the [ ]
◦ Alternatively can use the load effective address (lea) command In HLA can also use the & operator
◦ mov( &J, ebx );// Load address of J into EBX.◦ mov( eax, [ebx] );// Store EAX into J.
Uses:◦ Access data referenced by a pointer, ◦ Step through array data◦ To modify the address of a variable while your program is
running. ◦ To provide anonymous variables (you can read/write to/from it
but do not know its name, only the address)
8
Register Indirect Addressing Modes
Computes an effective address 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.
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 );
9
Indexed Addressing Modes
10
Indexed Addressing Modes
Two syntactical variations of the indexed addressing mode
The first variant uses the following syntax: ◦ mov( [ 32BitReg+ constant ], al );◦ mov( [32BitReg - constant ], al );
The second variant syntax: ◦ mov( VarName[32BitReg + constant ], al );◦ mov( VarName[32BitReg - constant ], al );
11
Variations on the Indexed Addressing Mode
12
Variations on the Indexed Addressing Mode
Uses:◦ If a 32-bit register contains the base address of a
multi-byte object and you wish to access a memory location some number of bytes before or after that location
◦ Accessing fields of a record (or structure) when you have a pointer to the record data
13
Variations on the Indexed Addressing Mode
Similar to the indexed addressing modes with two differences: 1. Allow you to combine two registers plus a
displacement2. Let you multiply the index register by a (scaling)
factor of 1, 2, 4, or 8.
14
Scaled Indexed Addressing Modes
Syntax◦ VarName[ IndexReg32*scale ]◦ VarName[ IndexReg32*scale + displacement ]◦ VarName[ IndexReg32*scale - displacement ]◦ [ BaseReg32 + IndexReg32*scale ]◦ [ BaseReg32 + IndexReg32*scale + displacement ]◦ [ BaseReg32 + IndexReg32*scale - displacement ]◦ VarName[ BaseReg32 + IndexReg32*scale ]◦ VarName[ BaseReg32 + IndexReg32*scale +
displacement ]◦ VarName[ BaseReg32 + IndexReg32*scale -
displacement ]
15
Scaled Indexed Addressing Modes
16
Scaled Indexed Addressing Modes
Uses:◦ To access elements of arrays whose elements are
two, four, or eight bytes each.◦ Useful for accessing elements of an array when
you have a pointer to the beginning of the array.
17
Scaled Indexed Addressing Modes
The lowest memory addresses are reserved by the operating system.
Generally, applications are not allowed to access data or execute instructions at the lowest addresses in memory.◦ “general protection fault” error
18
Run-Time Memory Organization
19
Run-Time Memory Organization
The code section contains the machine instructions that appear in an HLA program.
HLA translates each machine instruction you write into a sequence of one or more byte values.
The CPU interprets these byte values as machine instructions during program execution.
In theory, you could write a program that stores data values into memory and then transfers control to the data it just wrote, producing a program that writes itself as it executes.
This produces romantic visions of Artificially Intelligent programs that modify themselves
In real life, the effect is usually a disaster and that’s why today’s operating systems operate in protected mode.
20
The Code Section
Is where you will typically declare your variables.
You can also embed lists of data into the static memory segment.
21
The Static Sections
Holds constants, tables, and other data that your program
All readonly objects declarations must be initialized
Data here can be thought as constants
22
The readonly Section
The storage is used to declare variables that are always uninitialized when the program begins running, as opposed to the readonly section where you initialize all objects you declare, and the static section that lets you optionally initialize objects.
Usually the operating system will initialize all storage objects to zero
23
The Storage Section
Used to create automatic variables Your program will allocate memory when a
unit starts executing (usually a procedure), and will deallocate memory when the unit finishes execution.
These variables are usually located in the stack segment
24
The var Section
The stack is a dynamic data structure that grows and shrinks according to certain memory needs of the program.
The stack also stores important information about program including local variables, subroutine information, and temporary data.
The 80x86 controls its stack via the ESP (stack pointer) register.
Data is written to the stack segment by “pushing” data onto the stack and removed by “popping” or “pulling” data off of the stack.
25
The Stack Segment and the Push and Pop Instructions
Syntax:◦ push( reg16 );◦ push( reg32 );◦ push( memory16 );◦ push( memory32 );
◦ pushw( constant );◦ pushd( constant );
Whenever you push data onto the stack, the 80x86 decrements the stack pointer by the size of the data you are pushing and then it copies the data to memory where ESP is then pointing
The stack pointer should always be an even multiple of four
26
The Basic PUSH Instruction
27
The Basic PUSH Instruction
Syntax◦ pop( reg16 );
◦ pop( reg32 );
◦ pop( memory16 );
◦ pop( memory32 ); The POP instruction only supports 16-bit and 32-bit
operands; you cannot pop an eight-bit value from the stack
You cannot POP a constant value Popping a value does not erase the value in
memory, it just adjusts the stack pointer so that it points at the next value above the popped value
28
The basic POP instruction
29
The basic POP instruction
Although static and automatic variables are all simple programs may need, more sophisticated programs need the ability to allocate and deallocate storage dynamically (at run-time) under program control
These memory allocation routines let the programmer request how many bytes of storage to allocate, they return a pointer to the newly allocated storage, and they provide a facility for returning the storage to the system so the system can reuse it in a future allocation call
The heap segment of memory is used for those allocations
30
The Heap Segment
31
The Heap Segment
To declare a string the syntax isstatic stringName:string; Strings are pointers First need to go to the address indicated by
the pointer and then access the individual characters
32
Accessing the Characters Within a String
33
Accessing the Characters Within a String
Get the nth character of string s in HLA, where n is an uns32
mov( s, ebx );// Get address of string data into EBX. mov( n, ecx );// Get desired offset into string. mov( [ebx+ecx], al );// Get the desired character into AL.
Caution: Need to check the index exist, otherwise might get an error
Other code samples in the book
34
Accessing the Characters Within a String