News:

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

Main Menu

Partial Tutorial X64 with full sdk translate

Started by TouEnMasm, August 23, 2015, 04:45:38 PM

Previous topic - Next topic

TouEnMasm

You will find tut02,tut03,tut04,tut05,tut31 in 64 bits in the zip
The tut03 show the minimal rules to follow with 64 bits.
The tut31 is a listview control and the more complete on the subject
Take care that use of rdi register,even preserved in stack is not allowed.
See wndproc and use rdi instead r9 after preserved it and see what happen
Le tut21 is a pipe (run \masm32\bin\ml.exe)

VERBOTEN with JWASM,extract from Jwasm doc on "OPTION WIN64":
-------------------------------------------------------------------------------------------------------------------------
Warning: You should have understood exactly what this option does BEFORE you're using it. Using PUSH/POP instruction pairs to "save" values across an INVOKE is VERBOTEN if this option is on.
-------------------------------------------------------------------------------------------------------------------------
a complete comparison on the subject between ml64 and jwasm is here
http://masm32.com/board/index.php?topic=4536.msg48597#msg48597

Here a real trap,use of push:
Quote
   start:
   ;fastcall need stack space before call
   ;one qword for return adress of call
   ;
       add rsp,-(8 + 10 * 8) ; - (align + shadow space)
   mov Saverdi,rdi      ;save rdi en data
   push rdi
   mov rdi,200
       
   invoke InitInstance,1
   pop rdi
   .if rdi != Saverdi   ;wrong if an API function had been call by the proc
         mov rdi,Saverdi
         invoke MessageBox,NULL,addr errorrdi,addr Titre,MB_OK
   .endif
   
   invoke ExitProcess,0
InitInstance PROC FRAME init:DWORD
   Local  retour:QWORD
   mov retour,1
   .if init == 1
      invoke GetModuleHandle,NULL
      mov hInstance,rax   
      ;--------------------------------------------------------------   
      invoke GetModuleFileName,hInstance,addr RelativePath,sizeof RelativePath
   .endif

FindeInitInstance:
   mov rax,retour
   ret
InitInstance endp


Fa is a musical note to play with CL

Mikl__

#1
hello, Yves!
Why 2-nd, 3-rd and 4-th lesson have a size of 14 Kbytes, and size of 5-th lesson is 5 Kbytes and size of 31-st lesson is 8.5 Kbytes? What is the reason reducing the size of the program, because they become more difficult? Clicking on the title in the 31 lesson is not sorted, and the second-clicking on the header of the first leads to a crash in program

TouEnMasm

Difference of size = debug or not debug
On which system have you a crash ? After compile ? or using the executable ?
Fa is a musical note to play with CL

TouEnMasm

it is ;
Quote
UpdatelParam proc FRAME
   LOCAL lvi:LVITEM,CPT:QWORD
   invoke RtlZeroMemory,addr lvi,sizeof lvi
   invoke DebugBreak
   invoke SendMessage,hList, LVM_GETITEMCOUNT,0,0
   dec rax
   mov CPT,rax
   .if rax > 0
      mov lvi.imask,LVIF_PARAM         
      LoopSetItem:
         xor rax,rax
         mov eax,lvi.iItem
         mov lvi.lParam,rax ;defaut param
         invoke SendMessage,hList, LVM_SETITEM,0,addr lvi
         inc lvi.iItem
         dec CPT
      jnz LoopSetItem
   .endif
   ret
UpdatelParam endp
Who don't want to work.I have put it in comment on WM_NOTIFY message and all is OK



Fa is a musical note to play with CL

Mikl__

Hi, Yves!
see my version of the program
QuoteOn which system have you a crash ? After compile ? or using the executable ?
Win 7 pro x64, I used your executable file

TouEnMasm


Found the problem and corrected it in the first post (tut31).
Use of rdi ,even preserved,is not allowed in the wndproc (it is allowed in 32 bits).
I have replaced it by r9 (more simple) and all is ok.

Fa is a musical note to play with CL

Mikl__

QuoteUse of rdi ,even preserved,is not allowed in the wndproc
Yves,
use rdi, rsi, rbp, rbx and so on, check the functionality of applications, if this is not a blue screen of death, then proceed according to the rules,
Quoteif you want it, you can it

TouEnMasm


There is Nothing on the rules who say it is not allowed to use rdi rsi rbx as in 32 bits.
If you have a link who say that,put it here
Fa is a musical note to play with CL

Mikl__


TouEnMasm


Too bad.I prefer rules writed in plain text .

Fa is a musical note to play with CL

Mikl__

#10
RegisterStatusUse
RAXVolatileReturn value register
RCXVolatileFirst integer argument
RDXVolatileSecond integer argument
R8VolatileThird integer argument
R9VolatileFourth integer argument
R10:R11VolatileMust be preserved as needed by caller;
used in syscall/sysret instructions
R12:R15NonvolatileMust be preserved by callee
RDINonvolatileMust be preserved by callee
RSINonvolatileMust be preserved by callee
RBXNonvolatileMust be preserved by callee
RBPNonvolatileMay be used as a frame pointer;
must be preserved by callee
RSPNonvolatileStack pointer
XMM0, YMM0VolatileFirst FP argument;
first vector-type argument when __vectorcall is used
XMM1, YMM1VolatileSecond FP argument;
second vector-type argument when __vectorcall is used
XMM2, YMM2VolatileThird FP argument;
third vector-type argument when __vectorcall is used
XMM3, YMM3VolatileFourth FP argument;
fourth vector-type argument when __vectorcall is used
XMM4, YMM4VolatileMust be preserved as needed by caller;
fifth vector-type argument when __vectorcall is used
XMM5, YMM5VolatileMust be preserved as needed by caller;
sixth vector-type argument when __vectorcall is used
XMM6:XMM15,
YMM6:YMM15
Nonvolatile (XMM),
Volatile (upper half of YMM)
Must be preserved as needed by callee.
YMM registers must be preserved as needed by caller.

TouEnMasm

I know all of this page
Quote
RDI Nonvolatile Must be preserved by callee
incorrect assertion because miss:
you couldn't call another function even if rdi is preserved.
It is what happen in the tut31.(couldn't use rdi instead of r9 even if rdi is preserved).
You can also search agner site with it's pdf,nothing on the subject.
search more.
it can be also that the sendmessage 64 bit don't preserve and initialise correctly rdi (????).



Fa is a musical note to play with CL

TouEnMasm


added a a pipe,tut21.It run \masm32\bin\ml.exe
Fa is a musical note to play with CL

rrr314159

@TuteEnMasm,

You can use rdi like any other nonvolatile register, e.g. rsi. Unfortunately I can't (easily) make your examples since I don't have VC, sdk64, etc. So I can't check what's going on. However my guess is: in tute31 (before the fix) you're pushing / popping rdi to save it across invocation of SendMessage. However UpdatelParam, with FRAME, screws up the stack - you can't use push/pop to preserve across such invokes in JWasm (unfortunately as I said I can't check this). So if you push/pop rdi at each individual invocation of SendMessage (4 separate times) it would work, if this guess is correct.

Regardless, there's nothing special about rdi as you say, it can be used in WndProc same as rsi.

Of course your fix works so you can leave it at that, but should correct misunderstanding about rdi.

Wish I could check your code but 2 much trouble to get it to compile since my environment is so different
I am NaN ;)

TouEnMasm


You are right,here is a sample:
After debugging,the push is destroy when a function is called Inside the proc
Did you find this NORMAL??????????

Quote
   start:
   ;fastcall need stack space before call
   ;one qword for return adress of call
   ;
       add rsp,-(8 + 10 * 8) ; - (align + shadow space)
   mov Saverdi,rdi      ;save rdi en data
   push rdi
   mov rdi,200
       
   invoke InitInstance,1
   pop rdi
   .if rdi != Saverdi   ;wrong if an API function had been call by the proc
         mov rdi,Saverdi
         invoke MessageBox,NULL,addr errorrdi,addr Titre,MB_OK
   .endif
   
   invoke ExitProcess,0
InitInstance PROC FRAME init:DWORD
   Local  retour:QWORD
   mov retour,1
   .if init == 1
      invoke GetModuleHandle,NULL
      mov hInstance,rax   
      ;--------------------------------------------------------------   
      invoke GetModuleFileName,hInstance,addr RelativePath,sizeof RelativePath
   .endif

FindeInitInstance:
   mov rax,retour
   ret
InitInstance endp
Fa is a musical note to play with CL