"In the indexed addressing mode, the instruction contains an address to load the data from, 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."
Lets say we have the following numbers in memory: 10, 15, 20, 23. The size of a single unit of storage is a byte which can represent up to the number 255 (1 1 1 1 1 1 1 1 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255!). In this case, lets assume each number occupies a single byte of storage.
If 2006 is the location of the number 10, then 2007 is the address of 15 and 2008 is the address of 20. Using indexed addressing, if our instruction specifies the address 2002 and an index register with the value 4, we just need to increase the value in the register by 1 three more times to access all of our numbers!
"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."
If our numbers didn't take up one byte, but instead took up four bytes of storage, then we can't just increase the index register by 1 every time. If the number 10 takes up four bytes and begins at address 2006, then it occupies addresses 2006, 2007, 2008, and 2009. Updating the index register by 1 if it starts at 2006 means it's still pointing at the part of memory being used by the number 10.
This is where the multiplier for the index is useful. Instead of saying, "move N bytes", we can now say, "move N*M bytes". In our case, M = 4 because there are 4 bytes in a word. Therefore, each time we up the value of N by 1, we jump one word instead of one byte. In this case, since our first word is at location 2002, our second word is 4 bytes away. Our index, however, is going to be 1 and not 2 because we start counting at 0 (the address 2002 already points directly at our first word with an index register of 0).
No comments:
Post a Comment