News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Question about OPENDLG & SAVEDLG (masm32 library)

Started by jdoe, March 19, 2006, 08:23:41 AM

Previous topic - Next topic

jdoe

Hi,

Is there a reason why openfilebuffer is in a data? section and not on a local variable like ofn ?
I'm confused about it because I'm only using .data section in procedures to assign text to a variable.
Knowing the purpose of it will help me using local and .data sections more correctly.
And more, does adding a .const section in a procedure is fair ?


OpenFileDialog proc hParent:DWORD,Instance:DWORD,lpTitle:DWORD,lpFilter:DWORD

    LOCAL ofn:OPENFILENAME

    .data?
      openfilebuffer db 260 dup (?)
    .code



Thanks

hutch--

The tail end of the algo tells you why its .DATA? instead of a LOCAL.


    mov eax, OFFSET openfilebuffer
    ret


This allows an offset to be returned to the calling procedure where a LOCAL would cease to exist once the proc had closed. It means in the simple sense that you can just call the procedure and get a return address back from it that contains the path if one is selected. Note this much that another call to the same algo overwrites the data in the .DATA? section buffer so if you need to save it, you must copy it to another location. this is rarely ever needed but its easy enough to do.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jdoe


So what I have to understand here is that memory declared in .data/.data? sections in procedure are globally accessible from a pointer to it and subsequent call to the procedure don't duplicate but overwrite it.

It is basic knowledge for you but it is a useful information for a me... Thanks for the fast reply.


hutch--

Yes,

You have it dead right, when you declare data in either the .DATA or .DATA? sections, it has GLOBAL scope within the module it is created in so the address returned by the procedure is still valid after the procedure has closed.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark Jones

In addition to Hutch's comments, anything LOCAL is created on the stack at the current ESP, so it is therefore usually overwritten by the next procedure or deleted at program close. In that sense, it is "local". I make this distintion because handling local values and pointers reference data on the stack and not data in linear memory, so are therefore handled slightly differently than using global data defined in .DATA and .DATA?. Hope that made sense. :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

jdoe

@Mark Jones... Learning about ESP and EBP register usage is in my next to do list.  :wink


Quote from: hutch-- on March 19, 2006, 08:48:36 AM
The tail end of the algo tells you why its .DATA? instead of a LOCAL.


    mov eax, OFFSET openfilebuffer
    ret



This made me think about one more question...

Why "mov eax, OFFSET openfilebuffer" and not "lea eax, openfilebuffer"
Is there a distinction between these ?


hutch--

Basically yes,

OFFSET is an assembly time operator that puts the actual offset of the variable in the DATA section into the instruction that is using it.

mov eax, OFFSET var

gets transalated to something like

mov eax, 40ED7BCF


LEA is an assembler runtime instruction that loads the effective address of a stack variable into a register.


LOCAL var :DWORD

mov var, 100    ; copy a value into stack variable

lea eax, var      ; load the ADDRESS of var into EAX




Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php