flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [SOLVED] Failing to restore parameters names in custom ENDP

Author
Thread Post new topic Reply to topic
anbyte



Joined: 21 Jul 2024
Posts: 9
anbyte 02 Mar 2026, 17:32
I've been trying to write custom proc/endp macros (yes, I'm aware of proc32.inc), but I've ran into a wall with my ENDP implementation.
Code:
format ELF executable
segment executable
sys_exit equ 0x01

macro proc name, [args] {
    common
        local param@base
        param@base equ 8
    forward
        param@all equ `args

        rept 1 base:param@base
        \{
            args equ [ebp+base]
        \}
        param@base equ param@base + 4
    common
        label name
        push ebp
        mov ebp,esp
}

macro endp {
        irps param,param@all
        \{
            restore param
        \}

        leave
        ret
}


retval equ "original"

proc exit, retval
        mov eax, sys_exit
        mov ebx, retval
        int 0x80
endp

entry $
        pushd -1
        call exit

        display retval
    

The procedure's parameter names are supposed to be local to the procedure, so I have to restore them when the procedure ends. I do this by assigning the procedure names to param@all, so I can restore them in ENDP using irps ..except, it's not working right. The `display retval` line should display the original value of retval (that being the string "original"), but instead it attempts to display the value of the procedure's parameter.

If you replace the call to endp with a manual `restore retval`, it works as intended. So my guess is that the endp macro is expanding only partially because it requires another pass, after which `retval` is still assigned to the parameter. But whenever I do get the code to assemble, it assembles in one pass, and when replacing `restore param` with `display param`, things seem to assemble in the right order. I suppose this is my naivety of how flatassembler processes macros.. I did look through the docs, but I haven't been able to come up with a solution. Anyone have an idea?


Last edited by anbyte on 03 Mar 2026, 09:22; edited 1 time in total
Post 02 Mar 2026, 17:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20905
Location: In your JS exploiting you and your system
revolution 03 Mar 2026, 01:38
Two changes needed.
Code:
    define param@all args ; use "define" to make sure "args" is not substituted with its value 
&
    irpv param,param@all ; irpv will iterate through each layer of the equated values    
Code:
format ELF executable
segment executable
sys_exit equ 0x01

macro proc name, [args] {
    common
        local param@base
        param@base equ 8
    forward
        define param@all args

        rept 1 base:param@base
        \{
            args equ [ebp+base]
        \}
        param@base equ param@base + 4
    common
        label name
        push ebp
        mov ebp,esp
}

macro endp {
        irpv param,param@all
        \{
            restore param
        \}

        leave
        ret
}


retval equ "original"

proc exit, retval
        mov eax, sys_exit
        mov ebx, retval
        int 0x80
endp

entry $
        pushd -1
        call exit

        display retval    
Code:
flat assembler  version 1.73.31  (16384 kilobytes memory)
original
1 passes, 106 bytes.    
Post 03 Mar 2026, 01:38
View user's profile Send private message Visit poster's website Reply with quote
anbyte



Joined: 21 Jul 2024
Posts: 9
anbyte 03 Mar 2026, 09:20
Thanks, thats perfect. I also added `restore param@all` in the irpv statement, since creating multiple procs with the same parameter names would error, presumably because of the repeated usage of param@all. Now I'm able to use the same trick for a `uses` syntax :]
Post 03 Mar 2026, 09:20
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-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.