flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [solved] fasmg proc64 differences when no parameters defined

Author
Thread Post new topic Reply to topic
Beege



Joined: 24 Nov 2018
Posts: 13
Location: USA Indiana
Beege 21 Oct 2019, 05:47
I recently started learning x64 on windows and noticed that the proc macro doesn't use a push rbp - mov rbp, rsp - leave and also does not adjust the alignment when preserving an odd number of registers vi uses keyword if no parameters are defined. Does anyone know why its like that? I'm mainly just trying to learn if that's for certain types of situations or more of an option that gets implemented to be sure the macro does not touch rsp? Or something else? Any feedback is appreciated. Thanks


Last edited by Beege on 17 Nov 2019, 20:26; edited 1 time in total
Post 21 Oct 2019, 05:47
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 21 Oct 2019, 11:08
Please take a look at the section of documentation that covers customizing procedures. If the standard prologue/epilogue macros do not do what you'd expect, you can alter them (see also this thread for a bit of history: https://board.flatassembler.net/topic.php?p=98814). The standard prologue for 64-bit Windows currently looks like this:
Code:
macro prologuedef procname,flag,parmbytes,localbytes,reglist
 { local loc,fill,counter
   loc = (localbytes+15) and (not 15)
   parmbase@proc equ rbp+16
   localbase@proc equ rbp-loc
   push rbp
   mov rbp,rsp
   if loc+fill
    sub rsp,loc+fill
   end if
   counter = 0
   irps reg, reglist \{ push reg
                        counter = counter+1 \}
   fill = 8*(counter and 1) }    
The number of pushed registers is taken into consideration when computing the "fill" value. But if you find that there is a bug there, please let us know.
Post 21 Oct 2019, 11:08
View user's profile Send private message Visit poster's website Reply with quote
Beege



Joined: 24 Nov 2018
Posts: 13
Location: USA Indiana
Beege 22 Oct 2019, 01:50
Tomasz Grysztar wrote:
But if you find that there is a bug there, please let us know.

Thank you Tomasz. The code that macro produces from the original fasm vs the one in fasmg is where I ran into this and got confused. For that macro the input/output code I get is:
Code:
;in
proc _testfunc uses rbx
  ret
endp

;out - 0x554889E54883EC08535BC9C3
0:  55                 push   rbp
1:  48 89 e5           mov    rbp,rsp
4:  48 83 ec 08        sub    rsp,0x8
8:  53                 push   rbx
;                           code.....
9:  5b                 pop    rbx
a:  c9                 leave
b:  c3                 ret
    


The current fasmg has a if parmbytes | localbytes
Code:
macro prologuedef procname,flag,parmbytes,localbytes,reglist
        local loc,fill
        loc = (localbytes+15) and (not 15)
        parmbase@proc equ rbp+16
        localbase@proc equ rbp-loc
        if parmbytes | localbytes
                push rbp
                mov rbp,rsp
                if loc+fill
                        sub rsp,loc+fill
                end if
        end if
        ...
    


So for the same input I end up with:
Code:
0:  53                      push   rbx
1:  5b                      pop    rbx
2:  c3                      ret
    


I use the fasmg.dll as part of a library for autoit to get some inline asm functionality (thread here) https://www.autoitscript.com/forum/topic/199758-flat-assembler-g-udf-with-x64/ and any time I want to call a function defined within my autoit script, the alignment has to be 16 byte or else I get a crash. So for this example if I put a fastcall in the body of that to another function in the script, I have to add the additional sub rsp, 8 instruction. On the other hand when I'm calling any function from windows api dlls like from kernel32, the alignment of rsp doesn't seem to matter. With or without the sub rsp, 8 - my script completes fine. So its seems I have some functions I need the alignment for and some I don't, but also a macro that may or may not define that alignment depending on if you have arguments. I add all that up in my head and feel like Im missing something I havent learned about yet. Confused
Post 22 Oct 2019, 01:50
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 22 Oct 2019, 05:29
You're right, this macro has not been translated to fasmg properly. I'm uploading a correction.
Post 22 Oct 2019, 05:29
View user's profile Send private message Visit poster's website Reply with quote
Beege



Joined: 24 Nov 2018
Posts: 13
Location: USA Indiana
Beege 27 Oct 2019, 17:55
Tomasz Grysztar wrote:
You're right, this macro has not been translated to fasmg properly. I'm uploading a correction.


Thank you for updating this Tomasz. With that being the fix I believe the epiloguedef needs the same check removed.

Code:
macro epiloguedef procname,flag,parmbytes,localbytes,reglist
        match any, reglist
                iterate reg, reglist
                        indx %%-%+1
                        pop reg
                end iterate
        end match
        if parmbytes | localbytes   <-----
                leave
        end if
        retn
end macro    
Post 27 Oct 2019, 17:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 27 Oct 2019, 18:32
Thank you! I did not give this enough thought when uploading.
Post 27 Oct 2019, 18:32
View user's profile Send private message Visit poster's website Reply with quote
Beege



Joined: 24 Nov 2018
Posts: 13
Location: USA Indiana
Beege 27 Oct 2019, 21:33
Tomasz Grysztar wrote:
Thank you! I did not give this enough thought when uploading.

Your very welcome. Thanks for fasm in general!

Sorry for the lack of clarity in my first post. I actually found a post by you that I believe is this exact same situation only it was from about 10 years ago and the orginal fasm so I told myself "that cant be my problem.. He fixed it 10 years ago". Im thinking now you either translated from an old copy or you have incredible consistency with the first drafts of macros. Cheers! Very Happy

https://board.flatassembler.net/topic.php?p=99833#99833
Post 27 Oct 2019, 21:33
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.