The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Cobra on November 10, 2006, 12:14:47 PM

Title: Finding out the total size of local variables
Post by: Cobra on November 10, 2006, 12:14:47 PM
I need to find out the total size of the local variables without manually counting them. Why does the sample code below not reference the correct size? Am I missing something here?


SomeProcedure PROC

locals_label LABEL BYTE

    LOCAL var1:DWORD
    LOCAL var2:DWORD
    LOCAL var3:BYTE
    LOCAL var4[3]:BYTE
    LOCAL var5:QWORD
    LOCAL var6[1000]:DWORD

locals_size = $ - locals_label

    mov    ecx, locals_size  ; does not contain the correct size. 
    ret

SomeProcedure ENDP
Title: Re: Finding out the total size of local variables
Post by: sinsi on November 10, 2006, 12:30:35 PM
Locals are allocated on the stack, so using the program pointer ($) won't work. Try this
  SomeProcedure PROC
    LOCAL a:DWORD,b[1000]:BYTE
      mov eax,ebp  ;EBP has the start of the locals
      sub eax,esp  ;ESP has the end
      ;now EAX has the length of all the locals (in this case, 1004)
  SomeProcedure ENDP

Title: Re: Finding out the total size of local variables
Post by: Ratch on November 10, 2006, 02:50:54 PM
Cobra,

     You can define the local variables as a STRUC, then it is easy to count them no matter how you mix the data sizes.  See #7 response of the link below.  Ratch

http://www.masm32.com/board/index.php?topic=3783.0
Title: Re: Finding out the total size of local variables
Post by: ToutEnMasm on November 10, 2006, 03:49:07 PM
The exact size in bytes
         mov ecx,ebp
         lea edx,lastlocal
                                                sub ecx,edx
Where last local is the name of the last .