Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

Creating a temporary variable

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Creating a temporary variable

Lets say you need a temporary variable, but you want to create it inside an inline or macro you could use several options such as using inherited variables or globals however probably the best option would be like so:

// First we will push a 0 integer onto the stack
// If you require a pointer to an int, then push your int value instead.
push 0

// esp is the pointer to the stack which is the top value (latest push)
// the stack is is 4 byte aligned (int or dword size)
myFunction(esp);

// finished with the int, remove it from the stack without trashing a register
add esp, 4

a sample of it in use would be the filewrite macro:

inline function filewrite(handle, text, len) {
      push eax
      WriteFile($handle,$text,$len,esp,0);
      add esp, 4
      
      $return eax;
}

in this case the WriteFile api requires a pointer to a dword to write the written byte count, filecreate does not use it so we can just use the temporary solution as so.

Replies

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

nice article ,

instead of push you can use this

 sub Esp,4
or
 Esp -= 4;

and that is useful if you need more than 4 bytes local variable inside macro so you can allocate it as that

sub Esp, 16 // 16 bytes variable

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

absolutely, but it should be noted that if it is for a 4 byte var then push is smaller :)