The MASM Forum

General => The Campus => Topic started by: Gunther on January 29, 2021, 08:33:03 PM

Title: Win64 Stack
Post by: Gunther on January 29, 2021, 08:33:03 PM
Suppose you have a procedure that in turn calls several other procedures (console application). Is the balancing of the stack at the beginning and end of the caller
sufficient or must this be done before and after each individual call of the callees?

Gunther
Title: Re: Win64 Stack
Post by: hutch-- on January 29, 2021, 10:29:13 PM
Gunther,

Do not waste you time with amateur stack twiddling, if you are using the standard 64 bit code in the 64 bit add on, the stack entry and exit do this for you, with your own code, you terminate a procedure with a bare RET with no stack balancing required.
Title: Re: Win64 Stack
Post by: morgot on January 30, 2021, 12:56:37 AM
hutch--,
but in main procedure I need code 'sup rsp,80h' or not? If default masm64 settings used.
Title: Re: Win64 Stack
Post by: Vortex on January 30, 2021, 02:54:30 AM
No need of stack manipulation if you are using the Masm64 SDK :

include     \masm32\include64\masm64rt.inc

.data

DlgBox      db 'DLGBOX',0

.code

start PROC

    invoke  GetModuleHandle,0
    xor     r8,r8
    invoke  DialogBoxParam,rax,ADDR DlgBox,\
            r8,ADDR DlgProc,r8
           
    invoke  ExitProcess,0

start ENDP

DlgProc PROC hWnd:QWORD,uMsg:QWORD,wParam:QWORD,lParam:QWORD

    mov     hWnd,rcx

    cmp     rdx,WM_CLOSE    ; uMsg = rdx
    jne     @f

    invoke  EndDialog,hWnd,0
    mov     rax,1
    ret   
@@:
    xor     rax,rax
    ret

DlgProc ENDP

END
Title: Re: Win64 Stack
Post by: hutch-- on January 30, 2021, 04:57:22 AM
Hi morgot,

The stack design in the 64 bit version handles the stack correctly and it has a number of variations for different stack types, I have that documented in the newer help file but you do not need to make stack corrections, the macros do it correctly.
Title: Re: Win64 Stack
Post by: Gunther on January 30, 2021, 05:26:23 AM
Steve,

Quote from: hutch-- on January 29, 2021, 10:29:13 PM
Do not waste you time with amateur stack twiddling, if you are using the standard 64 bit code in the 64 bit add on, the stack entry and exit do this for you, with your own code, you terminate a procedure with a bare RET with no stack balancing required.

But there are situations where you have to dig in the dirt. That was the aim of my question.

Gunther
Title: Re: Win64 Stack
Post by: Gunther on January 30, 2021, 07:00:53 AM
Erol,

Quote from: Vortex on January 30, 2021, 02:54:30 AM
No need of stack manipulation if you are using the Masm64 SDK :

Thank you for your example. Is it also true for a console application?

Gunther
Title: Re: Win64 Stack
Post by: Vortex on January 30, 2021, 07:36:21 AM
Hi Gunther,

The same is valid for console applications and DLLs.
Title: Re: Win64 Stack
Post by: hutch-- on January 30, 2021, 08:07:28 AM
 :biggrin:

Constructing the macros around making ML64 usable was something like picking fly specs out of pepper with a large blunt pair of pliers while blindfolded and with your hands tied behind your back. Its all there in the macro file but you risk going blind and mad trying to read it. 😉
Title: Re: Win64 Stack
Post by: Gunther on January 30, 2021, 11:52:06 AM
Erol,

Quote from: Vortex on January 30, 2021, 07:36:21 AM
The same is valid for console applications and DLLs.

Thank you for your fast reply. Do you have a similar example for console application, which uses the fancy macro magic for stack balancing?

Gunther
Title: Re: Win64 Stack
Post by: hutch-- on January 30, 2021, 11:58:25 AM
Gunther,

Console apps differ only in that they do not provide a Windows UI interface. As long as you use the masm64rt.inc file the full support is there to use. There are a number of console output macros but "conout" is probably the most flexible.
Title: Re: Win64 Stack
Post by: daydreamer on January 30, 2021, 09:46:47 PM
create threads also work the same in 64bit?OSreserve some space automatically for each thread?
so going 64bit,when chess engine,go engine or other game engine that uses recursive climb up and down tree algos ,its needed with some fake stack instead of 32bit push when climbing up and pop when climbing down?
Title: Re: Win64 Stack
Post by: Vortex on January 30, 2021, 10:40:06 PM
Hi Gunther,

Here is a console example. The function GetProcAddr emulating GetProcAddress depends on the function BinStrSearch, you can check the attachment.

include     \masm32\include64\masm64rt.inc

EXTERN      GetProcAddr:PROC

.data

user32      db 'user32.dll',0
msgbox      db 'MessageBoxA',0
string1     db 'Address of MessageBoxA exported by user32.dll = %X',0

.data?

hDLL        dq ?

.code

start PROC

    invoke  LoadLibrary,ADDR user32
    mov     hDLL,rax

    invoke  GetProcAddr,rax,ADDR msgbox
    cmp     rax,-1
    je      @f

    invoke  vc_printf,ADDR string1,rax
@@:
    invoke  FreeLibrary,hDLL

    invoke  ExitProcess,0

start ENDP

END
Title: Re: Win64 Stack
Post by: Gunther on January 31, 2021, 09:35:10 AM
Thank you Erol. It was a great help.

Gunther
Title: Re: Win64 Stack
Post by: felipe on February 01, 2021, 12:52:42 AM
Hello Gunther!

Quote from: Gunther on January 29, 2021, 08:33:03 PM
Suppose you have a procedure that in turn calls several other procedures (console application). Is the balancing of the stack at the beginning and end of the caller sufficient?

Yes it is. And now a simple example for illustration purposes:


Is in the attachments section =)


assembling: ml64 /c console0.asm
linking: link /entry:start /subsystem:console console0.obj

:thup:
Title: Re: Win64 Stack
Post by: felipe on February 01, 2021, 01:10:04 AM
Note that this is a "main" procedure, calling other "procedures". The prolog adjust the stack aligment and creates the space needed for the parameters of all callees. The epilog doesn't exist here because we end the program directly with ExitProcess... :icon_idea:
Title: Re: Win64 Stack
Post by: Gunther on February 02, 2021, 12:26:41 PM
Felipe,

Quote from: felipe on February 01, 2021, 01:10:04 AM
Note that this is a "main" procedure, calling other "procedures". The prolog adjust the stack aligment and creates the space needed for the parameters of all callees. The epilog doesn't exist here because we end the program directly with ExitProcess... :icon_idea:

thank you for your help.  :thumbsup:

Gunther