News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Win64 Stack

Started by Gunther, January 29, 2021, 08:33:03 PM

Previous topic - Next topic

Gunther

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
You have to know the facts before you can distort them.

hutch--

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.

morgot

hutch--,
but in main procedure I need code 'sup rsp,80h' or not? If default masm64 settings used.
Sorry for the bad English

Vortex

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

hutch--

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.

Gunther

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
You have to know the facts before you can distort them.

Gunther

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
You have to know the facts before you can distort them.

Vortex

Hi Gunther,

The same is valid for console applications and DLLs.

hutch--

 :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. 😉

Gunther

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
You have to know the facts before you can distort them.

hutch--

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.

daydreamer

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?
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Vortex

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

Gunther

Thank you Erol. It was a great help.

Gunther
You have to know the facts before you can distort them.

felipe

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: