Addressing Modes


In the Section called Data Accessing Methods in Chapter 2 we learned the different types of addressing modes available for use in assembly language. This section will deal with how those addressing modes are represented in assembly language instructions.

The general form of memory address references is this:

 ADDRESS_OR_OFFSET(%BASE_OR_OFFSET,%INDEX,MULTIPLIER) 

All of the fields are optional. To calculate the address, simply perform the following calculation:

 FINAL ADDRESS = ADDRESS_OR_OFFSET + %BASE_OR_OFFSET + MULTIPLIER * %INI 

ADDRESS_OR_OFFSET and MULTIPLIER must both be constants, while the other two must be registers. If any of the pieces is left out, it is just substituted with zero in the equation.

All of the addressing modes mentioned in the Section called Data Accessing Methods in Chapter 2 except immediate-mode can be represented in this fashion.

direct addressing mode

  • This is done by only using the ADDRESS_OR_OFFSET portion. Example:

     movl ADDRESS, %eax 

  • This loads %eax with the value at memory address ADDRESS.

indexed addressing mode

  • This is done by using the ADDRESS_OR_OFFSET and the %INDEX portion. You can use any general-purpose register as the index register. You can also have a constant multiplier of 1, 2, or 4 for the index register, to make it easier to index by bytes, double-bytes, and words. For example, let's say that we had a string of bytes as string_start and wanted to access the third one (an index of 2 since we start counting the index at zero), and %ecx held the value 2. If you wanted to load it into %eax you could do the following:

     movl string_start(,%ecx,1), %eax 

  • This starts at string_start, and adds 1 * %ecx to that address, and loads the value into %eax.

indirect addressing mode

  • Indirect addressing mode loads a value from the address indicated by a register. For example, if %eax held an address, we could move the value at that address to %ebx by doing the following:

     movl (%eax), %ebx 

base pointer addressing mode

  • Base-pointer addressing is similar to indirect addressing, except that it adds a constant value to the address in the register. For example, if you have a record where the age value is 4 bytes into the record, and you have the address of the record in %eax, you can retrieve the age into %ebx by issuing the following instruction:

     movl 4 (%eax), %ebx 

immediate mode

  • Immediate mode is very simple. It does not follow the general form we have been using. Immediate mode is used to load direct values into registers or memory locations. For example, if you wanted to load the number 12 into %eax, you would simply do the following:

     movl $12, %eax 

  • Notice that to indicate immediate mode, we used a dollar sign in front of the number. If we did not, it would be direct addressing mode, in which case the value located at memory location 12 would be loaded into %eax rather than the number 12 itself.

register addressing mode

  • Register mode simply moves data in or out of a register. In all of our examples, register addressing mode was used for the other operand.

These addressing modes are very important, as every memory access will use one of these. Every mode except immediate mode can be used as either the source or destination operand. Immediate mode can only be a source operand.

In addition to these modes, there are also different instructions for different sizes of values to move. For example, we have been using movl to move data a word at a time. in many cases, you will only want to move data a byte at a time. This is accomplished by the instruction movb. However, since the registers we have discussed are word-sized and not byte-sized, you cannot use the full register. Instead, you have to use a portion of the register.

Take for instance %eax. If you only wanted to work with two bytes at a time, you could just use %ax. %ax is the least-significant half (i.e. - the last part of the number) of the %eax register, and is useful when dealing with two-byte quantities. %ax is further divided up into %al and %ah. %al is the least-significant byte of %ax, and %ah is the most significant byte.[14] Loading a value into %eax will wipe out whatever was in %al and %ah (and also %ax, since %ax is made up of them). Similarly, loading a value into either %al or %ah will corrupt any value that was formerly in %eax. Basically, it's wise to only use a register for either a byte or a word, but never both at the same time.

click to expand
Layout of the %eax register

For a more comprehensive list of instructions, see Appendix B.

[14]When we talk about the most or least significant byte, it may be a little confusing. Let's take the number 5432. In that number, 54 is the most significant half of that number and 32 is the least significant half. You can't quite divide it like that for registers, since they operate on base 2 rather than base 10 numbers, but that's the basic idea. For more information on this topic, see Chapter 10.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net