Team LiB
Previous Section Next Section

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

indexed addressing mode

indirect addressing mode

base pointer addressing mode

immediate mode

register addressing mode

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.


Team LiB
Previous Section Next Section