Author Topic: prolog and epilog in 64 bit code functions  (Read 657 times)

markallyn

  • Member
  • **
  • Posts: 192
prolog and epilog in 64 bit code functions
« on: October 26, 2017, 03:29:22 AM »
Hello everyone,

I'm fighting my way up the 64 bit learning curve.  Here's one more question.

If I write a frame function and need to do an PROLOG and EPILOG, do I need the .dot directives (.pushreg, etc.) for anything other than SEH?  The documentation I've seen seems to indicate that their only function is for adding unwind code. 

Thanks,
Mark Allyn

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5339
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: prolog and epilog in 64 bit code functions
« Reply #1 on: October 26, 2017, 01:52:55 PM »
Mark,

The sad tale but true is that to get a prologue/epilogue working in 64 bit MASM, you write them by test piece in conjunction with a debugger/disassembler. This is what I did with MASM and used ArkDasm to keep looking at the results. I am not even vaguely interested in structured exception handling as I am a fan of writing code that does not have mistakes in it in the first place.

The existing reference material is appalling and often wrong and you need to get a very good understanding of how the 64 bit Windows stack works in conjunction with the actual details of the Win 64 FASTCALL calling convention. You know you have got it right when you can successfully call any Windows API and don't be afraid to test the content of RSP before and after calls to make sure you have not modified the stack.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8352
  • Assembler is fun ;-)
    • MasmBasic
Re: prolog and epilog in 64 bit code functions
« Reply #2 on: October 26, 2017, 02:07:41 PM »
I am not even vaguely interested in structured exception handling as I am a fan of writing code that does not have mistakes in it in the first place.

Amen :t

AW

  • Member
  • *****
  • Posts: 1215
  • Let's Make ASM Great Again!
Re: prolog and epilog in 64 bit code functions
« Reply #3 on: October 26, 2017, 02:40:00 PM »
If I write a frame function and need to do an PROLOG and EPILOG, do I need the .dot directives (.pushreg, etc.) for anything other than SEH?  The documentation I've seen seems to indicate that their only function is for adding unwind code. 

The subject is difficult and the documentation you can find is sparse, incomplete and even wrong. I have been working on the subject for the past few weeks and will come up with something that will publish on Codeproject.com today or tomorrow.

felipe

  • Member
  • ****
  • Posts: 712
  • Asssssssembly language...said the snake.
Re: prolog and epilog in 64 bit code functions
« Reply #4 on: October 26, 2017, 03:54:10 PM »
Wow, seems like 64 bit is like moving to mars.  :dazzled:

 :biggrin:
Felipe.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5339
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: prolog and epilog in 64 bit code functions
« Reply #5 on: October 26, 2017, 08:40:27 PM »
Hi felipe,

One you get the bare mechanics up and working the rest of it is a bit different but reasonably straight forward. Even with 32 bit MASM you mainly relied on the built in default PROC/ENDP prologue and epilogue and usually just turned it off and on when you wrote code with no stack frame. With 64 bit MASM it did not have the default prologue/epilogue so you had to write your own which was OK as it had the preprocessor capacity to do this. The real problem was the reference data was very poor and often wrong and the win 64 FASTCALL calling convention was different to the win32 STDCALL and C calling conventions.

On the bright side, you have many more registers to work with and laying out more complicated algorithms is a lot cleaner and more straight forward and you just don't need to modify the stack at all to produce viable fast code.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

felipe

  • Member
  • ****
  • Posts: 712
  • Asssssssembly language...said the snake.
Re: prolog and epilog in 64 bit code functions
« Reply #6 on: October 27, 2017, 03:19:22 AM »
I want to start 64 bit, but i think i should know much more of 32 bit yet. For example more complex instruction sets like mmx and sse and all that. I think a have a feeling of ignorance that i can't stand for it if a don't know those things and just jump to the newest bits.  :dazzled:

 :biggrin:
Felipe.