Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialising local variables

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
0
0
US
Hi Assemblers,
I was wondering, in high-level languages we initialize local
variable sometimes like this (java):
Code:
public static func(int param1, int param2)
{
    String errMsg = "An error occurred";

    // other code
}
now I know in Assembly we have the local directive, but how do we
initialize the variable??

Code:
/* doesnt work */
func proc, param1:word, param2:word
   LOCAL errMsg:byte "An error occured",0

/* doesnt work */
func proc, param1:word, param2:word
   LOCAL errMsg byte "An error occured",0

/* using both combos above, doesnt work */
func proc, param1:word, param2:word
   LOCAL errMsg:byte <&quot;An error occured&quot;,0>
doing this seems a bit long winded and pointless really because then we have to count the amount of chars for initialising the next var:
Code:
func proc, param1:word, param2:word
   mov byte ptr [ebp-4], &quot;An error occured&quot;
is there a quicker way? or is the a nice clean way to initialize local vars?
Appreciate any feedback || suggestions || corrections.
Kunal.
 
No, you've spotted the problem. High level languages are wonderfully good at concealing really nasty amounts of work behind layers of abstraction. Local variables are on the stack (generally), and the stack is continuously growing and shrinking. Each time a function is called, sp may contain a different value, so the string you want to initialise will need to be in a different place, and that means someone is going to have to write it there.

Note that this is particularly relevant if someone calls a function from within a simple loop. In this case the stack will actually remain unchanged, but since (a) the local variables must be initialised once, and (b) compilers are clever at optimising but I doubt they're that clever, the chances are the text will get written every time.

Incidentally it's all doubly pointless because the string has to be somewhere in memory if you want to copy it from there to your local stack space. It would be far better just to use the string where it already is.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top