Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦

Similar presentations


Presentation on theme: "Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦"— Presentation transcript:

1 Dr. José M. Reyes Álamo 1

2  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦ Records ◦ Pointers ◦ Other complex data types 2

3  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

4  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

5 5

6 6

7  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

8  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

9  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

10 10

11  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

12 12

13  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

14  Similar to the indexed addressing modes with two differences: 1.Allow you to combine two registers plus a displacement 2.Let you multiply the index register by a (scaling) factor of 1, 2, 4, or 8. 14

15  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

16 16

17  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

18  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

19 19

20  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

21  Is where you will typically declare your variables.  You can also embed lists of data into the static memory segment. 21

22  Holds constants, tables, and other data that your program  All readonly objects declarations must be initialized  Data here can be thought as constants 22

23  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

24  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

25  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

26  Syntax: ◦ push( reg 16 ); ◦ push( reg 32 ); ◦ push( memory 16 ); ◦ push( memory 32 ); ◦ 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

27 27

28  Syntax ◦ pop( reg 16 ); ◦ pop( reg 32 ); ◦ pop( memory 16 ); ◦ pop( memory 32 );  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

29 29

30  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

31 31

32  To declare a string the syntax is static stringName:string;  Strings are pointers  First need to go to the address indicated by the pointer and then access the individual characters 32

33 33

34 Get the n th 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


Download ppt "Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦"

Similar presentations


Ads by Google