News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

esp vs [esp]

Started by AssemblyBeginner, March 07, 2015, 09:05:35 PM

Previous topic - Next topic

AssemblyBeginner

Hi all,

I am trying to get my head around pointers in asm
esp and [esp] ..  Which one is the contents of the register and which one is the memory address of the register ?!

Regards.


Vortex

esp : This register is  pointing a memory address.
[esp] : the content of the esp register

esp is a special register pointing the address of an important memory portion, the stack.

Similarly, eax,ebx,ecx,edx,esi,edi and ebp can indicate addresses or other values. In your case, the memory address of [esp] is the register esp itself.

rrr314159

There is no memory address of a register! Unless you're a low-level chip designer. Inside instructions its "address" is 7, but I don't think you're interested in that.

"esp" will refer to the contents of the register, such as 0, 1, 0c765ff0h, etc. IF it has a number like the last one, then [esp] will refer to the contents of the memory at 0c765ff0h (for instance). IF it has a number like the first two, then [esp] will cause an error; unless you're using register arithmetic ...

Hope that clears it up a bit

While typing this vortex also replied - I guess this is still relevant, another slant on same facts
I am NaN ;)

AssemblyBeginner

Thank you both

Can I deduce from your statements that a register such as esp MUST ALWAYS contain a valid memory address ?

AssemblyBeginner

ok.. after re-reading the answers, I think I understand now

Again, thank you for your kind assistance

hutch--

This is your rough distinction with a register.

ESP contains a value, specifically the address of the current stack location.
The notation [esp] is what is contained AT that address.

The first is the address, the second is the content at that address.

rrr314159

hmmm ... didn't really read Vortex's answer b4 but on the face of it he seems to be saying the opposite!

Vortex:
Quote[esp] : the content of the esp register

me:
Quote"esp" will refer to the contents of the register...

you: "so which is the contents, [esp] or esp ???

Well ... I'm sure what I'm thinking is right, whether I'm expressing it well is another story; makes sense to me, but Vortex may have a different understanding of the word "content", ...? Here's my solution: wait for dedndave to sort it out!

Anther reply happened again while I'm typing, but I'm 2 tired to see what it says ...
I am NaN ;)

rrr314159

ps I agree with hutch, of course, so as far as I can see that makes it 2 to 1 ;)
probably just an  issue of semantics
I am NaN ;)

hutch--

It is a simple notation issue, in MASM which uses the historical Intel notation, a 32 bit register holds a 32 bit value. When you enclose it in square brackets it refers to the content at an address. The address must be a valid address or you will try to read or write to an address that is not allocated to the current running process and you will get a protected mode page fault.

rrr314159

that's not what I meant, rather the reason Vortex seems to be saying the opposite of you and me is probably semantics; like, he's using the word "content" and thinking of "address", or something.

BTW not to pick nits .. well, actually, to pick nits: esp doesn't have to have a legal stack address! There's a technique where you use it as an 8th GP register and carefully don't touch the stack while doing so. I think Mark Larson mentions it. The idea is, for a really demanding algorithmic situation, free up that 8th register temporarily.
I am NaN ;)

dedndave

i can assure you that Erol (Vortex) knows the correct answer
but, his text is misleading
either he goofed up, or it's a simple language barrier (English is not his primary language)
use Hutch's explanation, instead (Reply #5)   :P

hutch--


.data?
esp_ dd ?
.code
....
mov esp_, esp
....
mov esp, esp_

make sure you have written everything off the stack first in a proc.

iZ!

In some cases you have to specify:
- dword ptr[ESP] or
- byte ptr [esp]
- ...
Depends on the amount of bytes from memory you are referring to..

But usually EBP is used to read from the stack.

i.e. :

;-----------
push ebp
mov ebp,esp
mov eax,[ebp+4] ; same as mov eax,dword ptr[ebp+4]
mov bx,[ebp+8]
mov ecx [ebp+10]
....

pop ebp

;---------

Or.. Maybe I'm wrong.. You'd probably be better off using general purpose registers instead of EBP


jj2007

Everything written above is correct. But you'd learn a lot if you assembled the snippet below and ran it with Olly.

include \masm32\include\masm32rt.inc

.code
start:
push 12345678h ; this decrements esp by 4 bytes and fills the memory pointed to by esp with 12345678h
mov eax, [esp] ; eax now holds 12345678h
mov edx, esp ; edx points to the current stack, like esp
mov ecx, [edx] ; ecx now holds 12345678h
pop edx ; get the content of the stack in edx (123..), and increment esp by 4 bytes
exit

end start

AssemblyBeginner

Thanks everyone for their guidance

Coming from a Visual Basic background, the subject of pointers and variable addresses is a kind of an intimidating tabou :)

VB is easier to learn and work with mainly due to the fact that it isolates the programmer from dealing with pointers .. VB does most of the addressing dirty work for you behind the scenes .. But this comes at a high price particularly when one wants to achieve advanced functionality (in VB) such as programming the Windows API and/or using some advanced COM technics 

I am sure, I now have a better understanding .. so ,again, thanks everyone for their input :)