In Programming from the Ground Up, in chapter 3 I read
The general form of memory address references is this:
ADDRESS_OR_OFFSET(%BASE_OR_OFFSET, %INDEX, MULTIPLIER)All fields are optional. To calculate the address, simply perform the following calculation:
FINAL ADDRESS = ADDRESS_OR_OFFSET + %BASE_OR_OFFSET + MULTIPLIER * %INDEX
ADDRESS_OR_OFFSETandMULTIPLIERmust both be constants, while the other two must be registers. If one of the pieces is left out, it is just substituted with zero in the equation.
Now, I assume that substituted with zero is a typo, because if MULTIPLIER's default was 0, then the value of %INDEX would be irrelevant, as the product would always be zero anyway (indeed). I guess 0 is default for the other 3?
Nonetheless, what confuses me the most is that form the description above I understand that parenthesis and commas have the function of determining which parts of what we write map to the 4 "operands" of the addressing.
But then, in the following chapter I read
For example, the following code moves whatever is at the top of the stack into
%eax:movl (%esp), %eaxIf we were to just do
movl %esp, %eax
%eaxwould just hold the pointer to the top of the stack rather than the value at the top.
But I don't understand why. I mean,
given the
FINAL ADDRESSexpression above, I would say that- if we put
%espin parenthesis, it will play the role of%BASE_OR_OFFSET, withADDRESS_OR_OFFSETand%INDEXdefaulting to 0 andMULTIPLIERto 1, - if we put
%espnot in parenthesis, it will play the role ofADDRESS_OR_OFFSET, with%BASE_OR_OFFSETand%INDEXdefaulting to 0 andMULTIPLIERto 1,
and the sum would still be the same.
- if we put
Furthermore, how is
%espconstant?- Maybe I'm making the mistake of thinking that it is not constant because I think about the content of
%esp? - If that's the case, and
%espis constant becasue is the name of a physically fixed register, then what is a non constant, in this context?
- Maybe I'm making the mistake of thinking that it is not constant because I think about the content of