News:

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

Question about LOCAL and when to use it

Started by Technomancer, June 07, 2006, 01:22:19 PM

Previous topic - Next topic

Technomancer

Hi guys, i have a portion of some code here

.data
...blah blah, not related to my question
suma PROTO :DWORD, :DWORD

.code
main proc
LOCAL var1:DWORD
LOCAL var2:DWORD
... blah blah,not related again
invoke suma, var1,var2
....
suma proc var1:DWORD var2:DWORD
mov eax,var1
add eax,var2
ret
suma endp

Now i got this from an asm tutorial. I am very ignorant about high level constructs. I read up about them of course and i definitely know what a local variable is and what PROTO is IN THEORY, but i am confused about some points

1. Why is there a need for the author to declare a prototype of the procedure in this case in the .data section? I never really know when i should use PROTO and when i shouldn't.
2. When should i use local variables and when not to? In this case why is the author using local variables. The following program is a calculator in asm by the way.
3. After suma is called, var1:DWORD var2:DWORD are again delcared in the suma procedure. Why?
4. Basically i am most confused about how they are all link up together? Do "var1:DWORD var2:DWORD" in the procedure retrieve values from somewhere or something? Where does the local variable play a part? I am not sure how they are all link up together into one big happy family. Greater minds please explain  :8)

Thanks.

Ossa

Quote from: Technomancer on June 07, 2006, 01:22:19 PM
1. Why is there a need for the author to declare a prototype of the procedure in this case in the .data section? I never really know when i should use PROTO and when i shouldn't.

A PROTO is needed by the assembler, so that it knows about procedures that will appear later in the code (IIRC this is because MASM is a 1 pass assembler). I recommend that you always use PROTOs. You do not NEED them when the procedure is defined before it is called (and is in the same file).

Quote from: Technomancer on June 07, 2006, 01:22:19 PM
2. When should i use local variables and when not to? In this case why is the author using local variables. The following program is a calculator in asm by the way.

Local variables allow the same variable NAME to be used multiple times - they are not the same variable. This is because the SCOPE of local variables is... well... local to the procedure. Only the procedure in which they are defined can use them.

Quote from: Technomancer on June 07, 2006, 01:22:19 PM
3. After suma is called, var1:DWORD var2:DWORD are again delcared in the suma procedure. Why?

Because they are parameters for this second procedure. This function alos uses variables with these names... they could be called anything you like - they have nothing to do with the first two.

Quote from: Technomancer on June 07, 2006, 01:22:19 PM
4. Basically i am most confused about how they are all link up together? Do "var1:DWORD var2:DWORD" in the procedure retrieve values from somewhere or something? Where does the local variable play a part? I am not sure how they are all link up together into one big happy family. Greater minds please explain  :8)

They have nothing to do with each other EXCEPT that the call:

invoke suma, var1,var2

passes the variables form one to the other; this means that they have the same VALUE, but are NOT the same variable.

If that isn't clear, do ask again and I'll explain in more detail,
Ossa
Website (very old): ossa.the-wot.co.uk

Technomancer

Thank you for replying Ossa. This is what i understand so far. (invoke suma,var1,var2) var1 and var2 and arguments for the suma procedure. The LOCAL var1:DWORD and LOCAL var2:DWORD are actually space allocated for when users key in (input) values for the calculator program and they only work in the "main" procedure since they will be destroyed at the end of the procedure.

I guess i am still confused about this part.

suma proc var1:DWORD var2:DWORD
mov eax,var1
add eax,var2
ret
suma endp


I don't really understand this part.
When doing mov eax,var1 how does the assembler recognises what var1 is since it is not linked to the LOCAL var1:DWORD as you said. How does it "read" what the value is .. at var1 ? This program is basically a calculator. At the main proc (where the local variables are) the local var allocates memory for users to input 2 values, which will be var1 and var2.. So my main problem is, i cannot connect how mov eax,var1 and add eax,var2 in the suma proc "reads" and "link-up" that var1 and var2 to the LOCAL VARIABLES since you said "They have nothing to do with each other ".

Thanks for your help mate.

evlncrn8

translates to basically

mov eax,[ebp+8]
add eax,[ebp+0ch]
ret

or something like that, the compiler just builds the ebp frame

hutch--

Techno,

The basics of a LOCAL variable are that they are created at run time on the stack and this means dynamically in memory. A GLOBAL variable in assembler is an OFFSET to a fixed location in the binary file that is created at assembler time. A LOCAL gives you scope control directly within a procedure that nothing else can get at as it only exists within that procedure.

The distinction between a GLOBAL variable and a LOCAL variable is where you need it and what scope you need. A GLOBAL can be accessed and modified anywhere in the binary file which is useful when you need it but a nuisance when you don't. A LOCAL variable does not have this problem and is constructed when the procedure is started and is destroyed when it finishes.

Its basically pick what you need within the scope that will do the job and you get the best of both types.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Ossa

Quote from: Technomancer on June 07, 2006, 03:10:16 PM
I guess i am still confused about this part.

suma proc var1:DWORD var2:DWORD
mov eax,var1
add eax,var2
ret
suma endp


I don't really understand this part.
When doing mov eax,var1 how does the assembler recognises what var1 is since it is not linked to the LOCAL var1:DWORD as you said. How does it "read" what the value is .. at var1 ? This program is basically a calculator. At the main proc (where the local variables are) the local var allocates memory for users to input 2 values, which will be var1 and var2.. So my main problem is, i cannot connect how mov eax,var1 and add eax,var2 in the suma proc "reads" and "link-up" that var1 and var2 to the LOCAL VARIABLES since you said "They have nothing to do with each other ".

Hi again,

maybe it would help if I told you that this would work equally as well:


suma proc a:DWORD b:DWORD
mov eax, a
add eax, b
ret
suma endp


a and b exist in the stack (like var1 and var2 in the other procedure (main)) but at a different location to the var1 and var2 defined in main. They are "copied" to the new values when "suma" is called. This is done by the "invoke" macro. What it does is this:

push var2
push var1
call suma


I gave an explaination of this in a tutorial I wrote a while back:

The Stack: http://ossa.the-wot.co.uk/tutorial_asm1_tex.htm#1_5
Calling a procedure: http://ossa.the-wot.co.uk/tutorial_asm1_tex.htm#1_9

I hope that helps,
Ossa
Website (very old): ossa.the-wot.co.uk

ollydbg

Local variables are in stack.
Global variables are in .data

BogdanOntanu


My_Function PROC argument_01:dword, argument_02:dword
LOCAL local_var_01:dword, local_var_02:dword

mov eax [argument_01]
mov ebx,[local_var_01]

ret
ENDP


The declarations that come right after PROC are ARGUMENTS while declarations that come after LOCAL are local variables
Both are allocated automatically on the stack and (depending on the calling conventions) they are cleared at procedure exit after / before the  ret instruction(s)

This confusion between ARGUMENTS and LOCAL variables is because MASM (unlike TASM) does not have a sepparated ARG keyword
The advantage is that when you colapse code inside an IDE like RAdASM you can still see the PROC's arguments in MASM style
The confuzion is increased by the use of the same names (or very close names) for arguments and local variables.

BTW there is also an USES keyword related to PROC's

To increase confusion: the LOCAL keyword works differently inside a MACRO :D


PS. nice picture Olly ;)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

JohnnyReb

A structure, such as WNDCLASSEX, is initialized in Iczelion's Turorial at execution time because it is declared LOCAL.  An alternative is to declare it in the .CODE section and have (most of) the structure values initialized by the assembler.  The trade-offs, as it seems to me, is that a LOCAL keeps the program simpler to maintain which is a big deal in larger programs, but a GLOBAL simplifies the executable.  So, what other trade-offs should a person consider when deciding whether to use LOCAL or GLOBAL variables.
It is tempting to build similar structures, such as a WNDCLASSSEX, in a template and forgo the complexity of the code.
JohnnyReb
PS: My first assembler project was to build a Z80 driver for a 300 baud modem on my TRS-80.

Technomancer

Thanks for all the replies and help :U . One more question though.

IF one of my arguments for my invoke is a pointer to something, like this : invoke SomeFunc,ADDR arg_1

When at SomeFunc proc later, i suppose i need to put a arg_1: PTR DWORD there right? I will give an example of my question.

Example :

INVOKE TimerStart,ADDR arg_1
... blah blah....
TimerStart PROC USES <whatever registers>
pSavedTime: PTR dword ;Is this pointer addr arg_1 ?

mov eax,pSavedTime

Will pSavedTime be == arg_1
Is it copied over also like what ossa explained ? Does the assembler automatically copies addr arg_1 to pSavedTime ? If addr arg_1 is 0x400000, pSavedtime will be 0x400000 too ?


Ossa

QuoteWill pSavedTime be == arg_1

No.

QuoteDoes the assembler automatically copies addr arg_1 to pSavedTime

Yes.

QuoteIf addr arg_1 is 0x400000, pSavedtime will be 0x400000 too ?

Yes.

pSavedTime will be a pointer to arg_1... it will not be equal to arg_1. I think you understand, but forgot to say "address" in the first quote there.

Ossa
Website (very old): ossa.the-wot.co.uk

Mark Jones

Tech, one more thing. Globals might be easier to use when learning assembler, because there can only be one definition for each global and they are accessible from any procedure scope.

As hutch says, locals are created in the stack memory at run-time, which is much more complex than global vars. The best method I've seen for fully understanding this is to single-step through the code in a debugger such as Ollydbg and watch what happens.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08