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

Vortex

My apologies to AssemblyBeginner if I am wrong. Probably, my mistake as a non-native English speaker.

esp : the content is a memory address, the stack
[esp] : the value stored by the memory address pointed by esp


hutch--

Its only a semantic issue, what Erol said here is correct.

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

ESP The stack POINTER. We all know ESP can be used as a general purpose register if its set up very carefully but its designation as a POINTER is its common usage.

Fortunately Erol's English is far better than my Turkish so all I can say is "yavaş yavaş", (slow slow), we deal with people from around the world here.  :biggrin:

MichaelW

Quote from: iZ! on March 08, 2015, 02:09:41 AM
But usually EBP is used to read from the stack.

One detail that seems to have been forgotten here is that for indirect memory operands that reference (E)BP the default segment register is SS instead of DS. This detail does not matter under Windows, were SS == DS, but did matter under 16-bit DOS for memory models where SS != DS, where to access the stack you had to use BP or an explicit SS segment override.

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

In my test, timing access to memory allocated from the stack through EBP and ESI, comparing this code:

  mov   ecx, 100
@@:
  mov   eax, [ebp+ecx*4]
  dec   ecx
  jnz   @B


To this:

  mov   ecx, 100
@@:
  mov   eax, [esi+ecx*4]
  dec   ecx
  jnz   @B


And running on a Core-i3 typical results were

118 cycles, EBP
118 cycles, ESI
118 cycles, EBP
118 cycles, ESI
118 cycles, EBP
117 cycles, ESI


Well Microsoft, here's another nice mess you've gotten us into.

rrr314159

QuoteOne detail that seems to have been forgotten here is that for indirect memory operands that reference (E)BP the default segment register is SS instead of DS.

- No doubt others forgot it, but not me, since I didn't know it
I am NaN ;)

dedndave

hadn't forgotten - but they point to the same address space in win nt   :t

AssemblyBeginner

include \masm32\include\masm32rt.inc

.data?
        var dd ?
.code

start:
      mov var , 1000           ; stores the value '1000' in the 32-bit integer stored at location var         
      print  str$(var),13,10   
     
      mov [var] , 2            ; stores the value '2' in the 32-bit integer stored at location var
                               ; same as (mov var , 2)
      print str$(var),13,10    ; returns 2

      inkey "Press a key to continue ..."
      invoke ExitProcess, 0
end start


As you all can see in the code above, moving a value into the variable var can be achieved via :
mov var , somevalue
as well as
mov [var] , somevalue

Both methods seem to be the same .. How can that be ?!

Based on the answers given to the question on this post, I thought that putting a variable between brackets (ie:[var]) referred to the contents at the address var .. whereas
var referred to the actual variable address

Or is this just true for registers and NOT for variables ?

I hope I have explained the problem well ... Thank you

FORTRANS

Hi,

Quote from: AssemblyBeginner on March 11, 2015, 07:33:35 AM
Both methods seem to be the same .. How can that be ?!

   For variables the two are the same.

Quote
Or is this just true for registers and NOT for variables ?

   Exactly.  Registers are different from variables in that respect.

HTH,

Steve N.

AssemblyBeginner

Quote from: FORTRANS on March 11, 2015, 08:36:34 AM
Hi,

Quote from: AssemblyBeginner on March 11, 2015, 07:33:35 AM
Both methods seem to be the same .. How can that be ?!

   For variables the two are the same.

Quote
Or is this just true for registers and NOT for variables ?

   Exactly.  Registers are different from variables in that respect.

HTH,

Steve N.

Thanks steve N,

Does this apply to instructions other than mov such as Push, Pop, add , sub , and , or  ..etc  ?

dedndave

it does

for MASM, we generally only use brackets with registers

however, GoAsm (and perhaps some other assemblers) use brackets on all memory operands
the reason i mention this is, you may have seen code in the forum with brackets on names
that code is usually for the GoAsm assembler - check which sub-forum it's in   :t

hutch--

Its pretty simple, MASM ignores brackets around variables where other assemblers use that notation. Brackets around registers is what MASM will recognise and it means specifically the CONTENT at the ADDRESS in that register.

AssemblyBeginner