flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Addressing parameters and stack variables with ESP

Thread Post new topic Reply to topic

Joined: 17 Jun 2003
Posts: 100
Frank 27 Jan 2004, 20:49

aaro has provided a set of macros that free EBP for use by the programmer -- see this thread. Here I present an alternative set of macros that do the same.

Using ESP instead of EBP has its risk (see below). Don't use either aaro's or my macros as a plugin-and-forget replacement for the macros that come with FASM and FRESH. If you do so, your code WILL break!

Both macro sets should be safe to use with linear code -- that is, where the processor sees one (assembled) source-code line after the next. In contrast, if there are JMPs or CALLs inside a procedure, it appears safer to rely on EBP as the stack pointer. That is because both sets of macros use a compile-time variable to keep track of the number of PUSHes and POPs. The assembler will update that variable source-code line by source-code line, whereas the CPU may jump over the corresponding instructions. Therefore, JMPs and CALLs inside a procedure can introduce a mismatch between compile-time variable and run-time stack pointer, in which case code will break.

That said, each of the macro sets can be quite useful for certain kinds of procedures (e.g., "leaf procedures"):
a) because they free the EBP register for the programmer, and
b) because they handle increments / decrements to the stack pointer automatically, which is both more convenient and less error-prone than doing this "by hand".

Compared to aaro's macro set, the one I present here has similar advantages:
- frees EBP for use by the programmer;
- named procedure parameters;
- named local (or: dynamic) variables;
- unused procedures are not assembled to begin with, saving size;

and a better INVOKE in addition:
- it takes into account that STDCALL procedures clear the stack;
- it integrates with FASMW's procedure parameter counting system.

In my macro set, any number of local (or: dynamic) variables may be declared, but each of them can only be a DWORD. In aaro's system, a couple of DWORDs can be declared at a time. I have tried to allow the declaration of arbitrary stack variables including STRUCs, but was unsuccessful so far -- see this thread.

The files
Attached are:

proc.inc, my set of macros;
test.asm, an example program that shows how the macro system works, here: in the context of a Windows program that also uses the standard WIN32AXP.INC file that is delivered with FASMW;
disasm.asm, a disassembly of the compiled test program, so that one can easily see the offsets from ESP that the macro system computes.

Feedback, bug reports, ideas for improvement and -- even more important -- for further simplification of these macros is highly welcome. Have fun!



Description: disassembly of the test program
Filename: disasm.asm
Filesize: 2.4 KB
Downloaded: 663 Time(s)

Description: test program
Filename: test.asm
Filesize: 948 Bytes
Downloaded: 669 Time(s)

Description: set of macros
Filename: proc.inc
Filesize: 3.13 KB
Downloaded: 681 Time(s)

Post 27 Jan 2004, 20:49
View user's profile Send private message Reply with quote

Joined: 09 Jan 2004
Posts: 173
Location: Ru#27
S.T.A.S. 22 Feb 2004, 11:55
I found a small bug, that could give problems with "POP localvar"
Here's the correct variant
macro pop arg
  ..pushsize = ..pushsize - 4
  pop     arg
Post 22 Feb 2004, 11:55
View user's profile Send private message Reply with quote

Joined: 17 Jun 2003
Posts: 100
Frank 23 Feb 2004, 20:04
Weird -- but you are right. Thank you!
Post 23 Feb 2004, 20:04
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.