1. Oct 13, 2012

### twoski

1. The problem statement, all variables and given/known data

I have a homework question: Calculate the effective address and determine the addressing mode that is specified for each memory operand in the following statements.

(a) MOV EAX, [label1] ; label1 is at location 0x00D5A360
(b) MOV EAX, [EBX] ; EBX points to location 0x00D5A360
(c) MOV EAX, label1[EDI] ;label1 is at location 0x00D5A360, DI is equal to 8
(d) MOV EAX, [EBP + ESI*4] ;EBP points to location 0x00D5A360, ESI is equal to 4
(e) MOV EAX, [EBX+100h] ;EBX points to location 0x00D5A360

3. The attempt at a solution

First of all, i am confused as to why assembly uses these addresses with arbitrary names. Why do EAX, EBX, etc. exist? My professor is terrible at explaining these things and his slides for lectures are cryptic.

Secondly, i cannot find a website that sufficiently explains the differences between direct/immediate/direct register/indirect register/base/base indexed/simple addressing. I don't know how to calculate effective addresses either and i can't find anything in the lecture slides about it.

My guesses for these:

• Direct addressing is when you use [] to denote the data stored in a location.
• Indirect is the opposite, you do not use [] and you access a location rather than data. (I don't get how this is any different though. if i did MOV X,Y and MOV X,[Y] where Y = 3 wouldn't it do the same thing?)
• Register addressing looks to me like it's the exact same as indirect addressing, at least from the examples our teacher gave. Maybe it only uses the arbitrary predefined addresses?
• Register indirect addressing is when you use [] on the predefined addresses like EAX.
• Base addressing is like indirect addressing except you add a displacement to your base address (why would anyone do this?! wouldn't you be accessing data from some place that you don't want to?!)
• Base indexed is like a combination of indexed and base addressing. Again, i don't see the use in it.

Anyhoo, back to solving it.

MOV EAX, [label1] makes no sense to me since [label1] results in the data stored at label1. But the question doesn't specify what is stored there, it just gives the address. I am guessing that using [] on a label results in its location. So if i have this hexadecimal number as my result, how do i figure out the effective address? Is that all i need?

I can't make much headway without knowing a lot of this stuff. It's just frustrating because i can't find any concrete definitions of anything.

2. Oct 13, 2012

### Staff: Mentor

Basic addressing allows you to load and store a single piece of data

Indexed addressing is used for working with a one dimensional array of data with the labeled address being element 0 of the array.

For 2D and above arrays then some calculation of an index is required as multi dimensioned arrays are flattened into a one dimensional structure.

3. Oct 13, 2012

### rcgldr

EAX, EBX are the names of the registers as 32 bit registers on a X86 processor. AX, BX, ... are the names for 16 bit registers, while RAX, RBX, are the names for 64 bit registers. In 64 bit mode, there are 8 additional registers with names like R8, R9, ..., R15, where a suffix is used, R8B is the lower 8 bits of R8, R8W is the lower 16 bits, R8D is the lower 32 bits, and just R8 is the 64 bit version.

Do a web search for "x86 addressing modes" or something similar:

Wiki article:

For some assemblers (Microsoft) [ ... ] is ignored by the assembler. If the value inside [ ... ] is a register or label reference to some location in memory, it's an address. If the value is an immediate value, without a segment prefix, it's an immediate value: MOV EAX, [01234h] is the same as MOV EAX, 01234h (just moves the value 01234h into EAX). If you want to move the contents of memory at location 01234h into EAX, you could use MOV EAX, DS:[01234h]. There isn't any implied order either, so MOV EAX,4[EDI] is the same as MOV EAX,[EDI+4].

LEA is a special instruction that loads the address into a register instead of the contents of memory at that address. So LEA EAX,[EBX*4] is the same as the two instruction sequence, MOV EAX,EBX ... SHL EAX,2.

Last edited: Oct 13, 2012
4. Oct 14, 2012

### twoski

I guess what's confusing me is why exactly we need to use these registers. Can't we just specify our own registers in .data?

I don't find the wikipedia article to be all that helpful. What does a matrix full of arbitrary letters tell me about addressing modes? What do the letters in the first {}'s even represent?

Ah, so the answer to the first one would simply be the address since [label1] gives us its address. What i'm worried about is that there's some sort of trickery to the question - do i need to do any additional math with that address to get the effective address?

5. Oct 14, 2012

### marty1

The registers are what are connected directly to the Arithmetic/Logic Unit. Think of them as the parameters provided to the internal hardware. They are faster to access than external memory. You set them with values of the outside world and then perform the operations on them. They also receive the results to be stored externally later.

In a way, they are the I/O ports for the ALU.

6. Oct 14, 2012

### twoski

Oh, this is starting to make much more sense now.

7. Oct 14, 2012

### marty1

If you move from one cpu architecture to another the differences you will see regarding registers are the number of registers, width of the registers, bit level access, byte level access, word access, some of them will be dedicated to certain operations (hardwired to certain ALU functions), etc. SOme have completely general purpose registers whereby any can be specified as parameters for operations and any can recieve the result, other only certain registers receive results .. others only use certain ones for multiplications and on and on. Some microcontroller architectures even map them to memory addresses. Some don't have a proper random access register file at all but only a stack that operand are pushed onto and results popped off of. It can get confusing, but in the end they are just latches inside the cpu that provide bits to the ALU and place that the ALU to store the results.

8. Oct 14, 2012

### twoski

Our professor never bothered explaining to us how you find the effective address with each addressing method, thankfully Wikipedia explains it well. These questions are a breeze!

Thank you all for the help.

Last edited: Oct 14, 2012
9. Oct 14, 2012

### rcgldr

The naming conventions used by Intel are different than what you mentioned (direct, indirect, ...). The three operand types are immediate, register, or memory. In the case of memory, the standard addressing modes are: displacement, base + displacement, index * scale + displacement, base + index + displacement, base + index * scale + displacement.