flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Counter variable in forward block, impossible?

Author
Thread Post new topic Reply to topic
xoru



Joined: 02 Dec 2013
Posts: 9
Location: Netherlands
xoru 18 Feb 2019, 11:59
Hi there,

This might be a silly question and it might also be possible I'm completely not understanding the way a forward block works, however I assumed the next code would work:

Code:
macro reserve_locals [names*] {
        common
                size@reserve_locals = 0
        
        forward 
                size@reserve_locals = size@reserve_locals + 4
                names equ ebp - size@reserve_locals
        
        common
                if size@reserve_locals
                        sub esp, size@reserve_locals
                end if 
}
    


For context, this macro is used in a runtime-assembly procedure that assembles and loads code on run time for extension modules. I am trying to have this macro make it easier to reserve locals on the stack. It seems though, that when used, each name in the names group has an identical address afterwards.

Am I doing this correctly, or is the size@reserve_locals value placed in the forward block at the end? Meaning, the value used in the sub esp, size@reserve_locals is identical to every single 'forward' entry

Cheers!

_________________
HLT
Post 18 Feb 2019, 11:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 18 Feb 2019, 12:18
You can do it like this:
Code:
macro reserve_locals [names*] {
    common
        local size@reserve_locals
        virtual at ebp - size@reserve_locals
    forward
                names rd 1
    common
                size@reserve_locals = $ - $$
        end virtual
        if size@reserve_locals
                sub esp, size@reserve_locals
        end if
}    
Post 18 Feb 2019, 12:18
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 18 Feb 2019, 12:21
In fasm EQU defines a text substitution, therefore each of the names you define ends up becoming "ebp - size@reserve_locals" text, and you end up using the final value of "size@reserve_locals" (because it is accessed at the time when you use one of these names - remember that what is done by EQU is just to replace one text with another).

There are two possible solutions. The one I would recommend is to use LABEL AT instead of EQU, a value assigned to label is computed at the time of definition:
Code:
        forward 
                size@reserve_locals = size@reserve_locals + 4
                label names at ebp - size@reserve_locals    


You could also keep using EQU and just use a local name to store the offset:
Code:
        forward 
                size@reserve_locals = size@reserve_locals + 4
                local offset
                offset = -size@reserve_locals
                names equ ebp + offset    
But I do not recommend using EQU for such purposes at all. Keep in mind that all it does is replace one text with another, and this can often lead to unintended side effects.

PS. Oh, revolution's variant shows how to approach the problem in a much better way in general. Mine are more about gradually correcting your original code to show what the underlying problem was.
Post 18 Feb 2019, 12:21
View user's profile Send private message Visit poster's website Reply with quote
xoru



Joined: 02 Dec 2013
Posts: 9
Location: Netherlands
xoru 18 Feb 2019, 12:44
Thanks a tonne guys, these different solutions really give me a nice perspective on this issue. I think I totally misunderstood equ up until now, that explains a lot of trouble from the past!

Cheers!
Post 18 Feb 2019, 12:44
View user's profile Send private message Visit poster's website 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.