flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > Addressing parameters and stack variables with ESP |
Author |
|
Frank 27 Jan 2004, 20:49
Hi,
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. IMPORTANT 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! Risks 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. Strengths 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. Limitations 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! Regards, Frank
|
|||||||||||||||||||||||||||||||
27 Jan 2004, 20:49 |
|
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 Code: macro pop arg { ..pushsize = ..pushsize - 4 pop arg } |
|||
22 Feb 2004, 11:55 |
|
Frank 23 Feb 2004, 20:04
Weird -- but you are right. Thank you!
|
|||
23 Feb 2004, 20:04 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.