Day 48: Assembly Addressing Modes

I am slowly starting to understand the different addressing modes that can be used in Assembly. It’s a bit challenging since I am unfamiliar with how to retrieve and store data. The concept of indexing and the use of pointers is still not something I am completely familiar with. But today was another step (hopefully in the right direction).

TLDR;

Okay, so here are the highlights of what I did:

  • Went through Chapter 3 on “Programming from the Ground Up”. I tried to produce examples for each of the different addressing modes. Since assembly languages vary the syntax is a bit daunting.
  • Watched a video on some software used for tiling windows. It was pretty cool and I added to one of my playlists. When I finally try to use a Linux OS full time I would like to try it out.

Notes from “Programming from the Ground Up”


Addressing Modes

Processors have a number of different ways of accessing data, known as addressing modes. These modes are:

  • Immediate Addressing Mode (The simplest mode in which the data to access is embedded in the instruction itself)
  • Register Addressing Mode (The instruction contains a register to access, rather than a memory location)
  • Direct Addressing Mode (The instruction contains the memory address to access)
  • Indexed Addressing Mode (The instruction contains a memory address to access, and also specifies an index register to offset that address.)
  • Indirect Addressing Mode (The instruction contains a register that contains a pointer to where the data should be accessed)
  • Base Pointer Addressing Mode (This is similar to indirect addressing, but you also include a number called the offset to add to the register’s value before using it for lookup.)

There are other forms of addressing, but these are the most important ones.

Immediate Addressing Mode

This is the simplest mode in which the data to access is embedded in the instruction itself. For example, if we want to initialize a register to 0, instead of giving the computer an address to read the 0 from, we would specify immediate mode, and give it the number 0.

# Example will go here

Register Addressing Mode

In Register Addressing Mode the instruction contains a register to access, rather than a memory location. For example:

# Example will go here

Direct Addressing Mode

In Direct Addressing Mode, the instruction contains the memory address to access. For example, I could say, please load this register with the data at address 2002. The computer would go directly to byte number 2002 and copy the contents into our register.

# Example will go here

Indexed Addressing Mode

With Indexed Addressing Mode, the instruction contains a memory address to access, and also specifies an index register to offset that address. For example, we could specify address 2002 and an index register. If the index register contains the number 4, the actual address the data is loaded from would be 2006. This way, if you have a set of numbers starting at location 2002, you can cycle between each of them using an index register. On x86 processors, you can also specify a multiplier for the index. This allows you to access memory a byte at a time or a word at a time (4 bytes). If you are accessing an entire word, your index will need to be multiplied by 4 to get the exact location of the fourth element from your address. For example, if you wanted to access the fourth byte from location 2002, you would load your index register with 3 (remember, we start counting at 0) and set the multiplier to 1 since you are going a byte at a time. This would get you location 2005. However, if you wanted to access the fourth word from location 2002, you would load your index register with 3 and set the multiplier to 4. This would load from location 2014 – the fourth word. Take the time to calculate these yourself to make sure you understand how it works.

# Example will go here

Indirect Addressing Mode

With Indirect Addressing Mode, the instruction contains a register that contains a pointer to where the data should be accessed. For example, if we used indirect addressing mode and specified the %eax register, and the %eax register contained the value 4, whatever value was at memory location 4 would be used. In direct addressing, we would just load the value 4, but in indirect addressing, we use 4 as the address to use to find the data we want.

# Example will go here

Base Pointer Addressing Mode

Base Pointer Addressing Mode is similar to indirect addressing, but you also include a number called the offset to add to the register’s value before using it for lookup.

In the Section called Interpreting Memory we discussed having a structure in memory holding customer information. Let’s say we wanted to access the customer’s age, which was the eighth byte of the data, and we had the address of the start of the structure in a register. We could use base pointer addressing and specify the register as the base pointer, and 8 as our offset. This is a lot like indexed addressing, with the difference that the offset is constant and the pointer is held in a register, and in indexed addressing the offset is in a register and the pointer is constant.


Conclusion

That’s all for today. If you are interested in the MIT course you can check out the video lecture I’m currently going through. The lecture is helpful but isn’t sufficient by itself. Anyways, until next time PEACE!