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
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.
hutch--,
but in main procedure I need code 'sup rsp,80h' or not? If default masm64 settings used.
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
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.
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
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
Hi Gunther,
The same is valid for console applications and DLLs.
: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. 😉
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
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.
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?
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
Thank you Erol. It was a great help.
Gunther
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:
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:
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