Author Topic: Enter / standard prolog  (Read 138 times)

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Enter / standard prolog
« on: December 08, 2017, 01:42:53 AM »
I wonder why assemblers use the prologue/epilogue pair and not Enter/exit pair?
Could you explain it, I am curious and I don't understand too.
thanks.
Kenavo
---------------------------
Grincheux / Jokaste

markallyn

  • Member
  • **
  • Posts: 119
Re: Enter / standard prolog
« Reply #1 on: December 08, 2017, 02:58:26 AM »
Jokaste,

My novice understanding is that:
Quote
push rbp
mov rbp, rsp

is faster than:
Quote
enter

...and many programmers prefer higher speed.  However, difference is quite small, as I understand it.  The "leave" instruction (which I assume you intended when you wrote "exit") is apparently about tthe same speed as:

Quote
mov rsp, rbp
pop rbp

Regards,
Mark Allyn

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Re: Enter / standard prolog
« Reply #2 on: December 08, 2017, 03:04:32 AM »
Thanks for your (quick) answer.
I did not remember the mnemonic for LEAVE.
I will see if the number of bytes is the same and/or if the stack is aligned automatically.
There must be a reason for this pair of mnemonic exists.

Kenavo
---------------------------
Grincheux / Jokaste

felipe

  • Member
  • ***
  • Posts: 363
  • assemble the unassembled.
Re: Enter / standard prolog
« Reply #3 on: December 08, 2017, 04:32:38 AM »
There must be a reason for this pair of mnemonic exists.

High level constructs to support high level languages. Probably compiler writers from ancient times would found much easier to use this instructions while programming. For example for nested procedures.

Have a look here for an aw27 authored topic related to this:
http://masm32.com/board/index.php?topic=6564.0.

Felipe.

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Re: Enter / standard prolog
« Reply #4 on: December 08, 2017, 05:38:03 AM »
Quote
Probably compiler writers from ancient times would found much easier to use this instructions while programming.

With actuall assemblers it seems (to me) that with all the macros (high Level, it seems) programmers don't write assembly code but macros code.


Thank you for the link, but it is not the purpose (nested procedures).
I am thinking about using variables from an other proc, like GoAsm does.
Kenavo
---------------------------
Grincheux / Jokaste

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4926
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Enter / standard prolog
« Reply #5 on: December 08, 2017, 09:16:13 AM »
Its generally the case that where you have to write high level code in assembler (API, CRT etc ...) the code takes so much longer than the entry and exit of the proc that it simply does not matter what you use to create the stack frame. The next factor is how would you test the entry and exit speed of a procedure when the techniques for constructing a stack frame do not lend themselves to loop code at all.

MASM has used the mnemonic LEAVE for many years but used the well known stack construction,

    push ebp        ; preserve base pointer
    mov ebp, esp    ; stack pointer into ebp

as the entry method for 32 bit procedures.

Here is a test piece for the 2 main methods of creating a stack frame in win64.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .data
      mtxt db "How D",0
      ttxt db "Title",0
      pmsg dq mtxt
      pttl dq ttxt

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    conout str$(rsp)," stack pointer on entry",lf


  ; --------------------------
  ; full manual procedure call
  ; --------------------------
 ;     mov rcx, 0
 ;     mov rdx, pmsg
 ;     mov r8, pttl
 ;     mov r9, MB_OK
 ;     call testproc

  ; ---------------------------
  ; proc call with shadow space
  ; ---------------------------
    ; invoke testproc,0,pmsg,pttl,MB_OK

  ; ------------------------
  ; same as full manual call
  ; ------------------------
    rcall testproc,0,pmsg,pttl,MB_OK

    conout str$(rax)," procedure return value",lf

    conout str$(rsp)," stack pointer on exit",lf

    waitkey

    invoke ExitProcess,0

    ret

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 NOSTACKFRAME

 testproc proc

 ;     push rbp                ; manually construct stack frame
 ;     mov rbp, rsp

    enter 80h, 0h

    sub rsp, 256            ; allocate local space on the stack

    call MessageBox         ; call API with arguments loaded in 1st 4 registers

    leave                   ; exit the stack frame

 ;     mov rsp, rbp            ; exit the stack frame
 ;     pop rbp

    ret

 testproc endp

 STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end

 comment ^

.text:0000000140001000 C8800000                   enter 0x80, 0x0
.text:0000000140001004 4883EC60                   sub rsp, 0x60

.text:0000000140001104 C9                         leave
.text:0000000140001105 C3                         ret

        versus

.text:0000000140001106 55                         push rbp
.text:0000000140001107 488BEC                     mov rbp, rsp
.text:000000014000110a 4881EC00010000             sub rsp, 0x80

.text:0000000140001117 488BE5                     mov rsp, rbp
.text:000000014000111a 5D                         pop rbp
.text:000000014000111b C3                         ret

 ^
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


This is the batch file to build it.

@echo off

set appname=manual

if exist %appname%.obj del %appname%.obj
if exist %appname%.exe del %appname%.exe

\masm32\bin64\ml64.exe /c %appname%.asm

\masm32\bin64\polink.exe /SUBSYSTEM:CONSOLE /MACHINE:X64 /ENTRY:entry_point /nologo /LARGEADDRESSAWARE %appname%.obj

dir %appname%.*

pause

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

felipe

  • Member
  • ***
  • Posts: 363
  • assemble the unassembled.
Re: Enter / standard prolog
« Reply #6 on: December 08, 2017, 02:25:58 PM »
Thank you for the link, but it is not the purpose (nested procedures).

I'm just saying that the reason is basically a high level facility (from old times).
Felipe.

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Re: Enter / standard prolog
« Reply #7 on: December 08, 2017, 10:45:33 PM »
Thaks Hutch.
At the final time it is the same (number of bytes and speed).


Felipe, I began programming in 1980, in Cobol, APL and Assembler on BULL DPS6 and 7, on PDP and VAX, on NCR and on Digital mini-computers.
There was not DOS. It appears next year.
So the old times were when I began, I was 21 years old!
I am vexed, but I know it was not your intention, but suddenly you realized that you are an old man (58 years)!
Gamin


Philippe
Kenavo
---------------------------
Grincheux / Jokaste

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4926
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Enter / standard prolog
« Reply #8 on: December 08, 2017, 11:42:35 PM »
 :biggrin:

> but suddenly you realized that you are an old man (58 years)!

Ha ha, you are only a young fella.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

markallyn

  • Member
  • **
  • Posts: 119
Re: Enter / standard prolog
« Reply #9 on: December 09, 2017, 12:54:37 AM »
Yup,  Hutch is right.  You're a wee one.  I'm 75.  I fought with Job Control Language on IBM 360 machines.  I lost.

Mark Allyn

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Re: Enter / standard prolog
« Reply #10 on: December 09, 2017, 01:30:48 AM »

Its mine on 2017/12/24 - 58 years.


I also new JCL on Punch Cards.
Kenavo
---------------------------
Grincheux / Jokaste

felipe

  • Member
  • ***
  • Posts: 363
  • assemble the unassembled.
Re: Enter / standard prolog
« Reply #11 on: December 09, 2017, 02:39:42 AM »
Felipe, I began programming in 1980, in Cobol, APL and Assembler on BULL DPS6 and 7, on PDP and VAX, on NCR and on Digital mini-computers.
There was not DOS. It appears next year.
So the old times were when I began, I was 21 years old!
I am vexed, but I know it was not your intention, but suddenly you realized that you are an old man (58 years)!
Gamin


Philippe

Come on! I was just paraphrasing hutch, when he talks about the old DOS and old, antique instructions... :bgrin:

I wasn't saying to none of here old... :icon14:
Felipe.

felipe

  • Member
  • ***
  • Posts: 363
  • assemble the unassembled.
Re: Enter / standard prolog
« Reply #12 on: December 09, 2017, 02:55:45 AM »
the reason is basically a high level facility

Btw, i said that, based on my reads from old (may i say old now?  :redface:) Intel manuals...And my memory from those reads.
 :bgrin:
Felipe.

markallyn

  • Member
  • **
  • Posts: 119
Re: Enter / standard prolog
« Reply #13 on: December 09, 2017, 04:01:40 AM »
Loved the cake! 

The best part of PCs compared to the old days is that one doesn't have to run up and down 4 flights of stairs with the deck of cards to fix a crumby line of Fortran.

Mark

Jokaste

  • Regular Member
  • *
  • Posts: 40
  • Never be pleased, always improve
    • Grincheux's Tools
Re: Enter / standard prolog
« Reply #14 on: December 09, 2017, 05:31:40 AM »
And put the sticker on a band when it is left...
We went to the same school... :eusa_boohoo:
Kenavo
---------------------------
Grincheux / Jokaste