News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Passing a structure

Started by Biterider, March 22, 2019, 05:36:44 AM

Previous topic - Next topic

Biterider

Hi
Today I found a difference in the behavior of UASM in 32 bit and 64 bit. Using the invoke directive and a structure as argument, in 32 bit (stdcall) all members of the structure are passed in sequence. Using exactly the same syntax, but in 64 bit (fastcall), only a pointer to the structure is passed as argument. I checked all settings I know and I can't find the reason why UASM acts that way. Maybe has it something to do with the calling convention, but I have not found any clues on this particular topic.

Maybe the UASM team can shed some light on this matter.  :biggrin:

.code
TestProc proc Arg1:RECT
  ret
TestProc endp


start proc
  local Rct:RECT

  invoke TestProc, Rct
  invoke ExitProcess, 0
start endp



In 32 bit mode (stdcall)
  invoke TestProc, Rct
01361059 FF 75 FC             push        dword ptr [ebp-4]
0136105C FF 75 F8             push        dword ptr [ebp-8]
0136105F FF 75 F4             push        dword ptr [ebp-0Ch]
01361062 FF 75 F0             push        dword ptr [Rct]
01361065 E8 96 FF FF FF       call        TestProc (01361000h)


In 64 bit mode (fastcall)
  invoke TestProc, Rct
000000013F3A1075 48 8D 4D F0          lea         rcx,[Rct]
000000013F3A1079 E8 82 FF FF FF       call        TestProc (013F3A1000h)


Biterider

habran

Hi Biterider :biggrin:
IMHO it actually behaves the same, it sends  a reference.
The difference is in allocating the structure. Here is one example how 64 bit allocates the local data:

WinMain proc FRAME hInst : HINSTANCE, hPrevInst : HINSTANCE, CmdLine : LPSTR, CmdShow : UINT

local wc : WNDCLASSEXA
local msg : MSG
local hwnd : HWND
local Rct : RECT
push  rbp
.pushreg rbp
mov   rbp, rsp
.setframe rbp, 0
sub   rsp, sizeof WNDCLASSEXA + sizeof MSG + sizeof HWND + 13 * 8; make sure rsp is 16 - byte aligned
.allocstack sizeof WNDCLASSEXA + sizeof MSG + sizeof RECT + sizeof HWND + 13 * 8
.endprolog
.....
.....
Cod-Father

aw27

STDCALL always pushes everything unless you expressly tell to pass by reference - the default is to pass by value. Arrays are an exception but in ASM there is no direct concept of array. In 64-bit Windows ABI (why call it fastcall?)  everything over 8 bytes is passed by reference.

Biterider

Hi
Thanks for the answers. I have found the corresponding passage in the Microsoft x64 ABI.
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2017
"Any argument that does not fit in 8 bytes, or is not 1, 2, 4, or 8 bytes, must be passed by reference."

This is interesting. I tested what happens with a 3 byte structure

MyStruct struct
  a1  BYTE  ?
  a2  BYTE  ?
  a3  BYTE  ?
MyStruct ends

.code
TestProc proc Arg1:MyStruct
  ret
TestProc endp

start proc
  local Argument:MyStruct

  invoke TestProc, Argument
  invoke ExitProcess, 0
start endp

  invoke TestProc, Argument
000000013F9E1015 48 8B 4D FD          mov         rcx,qword ptr [Argument]



This does not seem to fulfill the ABI. OTOH, following the link "Parameter passing" from the previous paper
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2017#parameter-passing
it says that:
"All arguments are right-justified in registers, so the callee can ignore the upper bits of the register and access only the portion of the register necessary."

That can also be applied to the 3 byte structure, but this is not what the calling convention specifies.
BTW, the same happens to a 5, 6, 7 byte structure.

Thanks, Biterider

aw27

rcx will be filled with 5 random bytes that don't belong to the structure. It is probably a bug.

johnsa

That definitely seems like a bug to me. Will add it to the log for 2.49

Biterider


johnsa

I think the best option, rather than anything automated would be to perform a check

when you use invoke myProc, myStruct

if myStruct is not 1/2/4/8 an error will be emitted ("Structure parameter not 1/2/4/8 bytes, please pass by reference") so that it's clear what the situation is and force the programmer to then use ADDR.

Sound ok ?

Biterider

Hi
It's OK for me.  :t
Please add to the error conditions sizes bigger than 8 bytes.

Thanks!!

Biterider

nidud

#9
deleted

aw27

Quote from: nidud on March 26, 2019, 10:23:52 PM
For the RECT struct:

msvc x86 will pass rc as a reference in eax
msvc x64 will pass rc as a reference in rcx
masm x86 will push rc
masm x64 will return error (no invoke)
uasm x86 will push rc
uasm x64 will pass rc as a reference in rcx

For the 3 byte struct:

msvc x86 will push ms (8 bytes)
msvc x64 will pass ms as a reference in rcx
masm x86 will push ms - push ax + push word ptr ms
masm x64 will return error (no invoke)
usmc x86 will return error A2070: invalid instruction operands - push ms
uasm x64 will pass ms in rcx - mov rcx, qword ptr [rbp-5H]
asmc x64 will pass ms in cx - mov cx, word ptr [rbp-5H]
I may be wrong but I don't think so on this:
msvc x86 will push ms (8 bytes):
   mov   BYTE PTR _somethree$[ebp], 11      ; 0000000bH
   mov   BYTE PTR _somethree$[ebp+1], 22      ; 00000016H
   mov   BYTE PTR _somethree$[ebp+2], 33      ; 00000021H

   mov   eax, esp
   mov   cx, WORD PTR _somethree$[ebp]
   mov   WORD PTR [eax], cx
   mov   dl, BYTE PTR _somethree$[ebp+2]
   mov   BYTE PTR [eax+2], dl
   call   _threeByteFunction@4



nidud

#11
deleted

aw27

Quote from: nidud on March 27, 2019, 01:15:35 AM
Quotemsvc x86 will pass rc as a reference in eax
It does actually push the arguments there...
Cool, we are talking about stdcall.

Quote
Note that the default calling convention for VC x86 is C, not STDCALL.
We are talking about stdcall since the beginning.

Quote
The result will be the same thought.
:biggrin:

nidud

#13
deleted

aw27

I have checked MASM and MSVC for the 32-bit case and 3 byte strutures.
There is indeed a bug in MASM when using invoke with the 3 byte structure.
On the other hand MSVC handles well the situation, it does not makes 2 pushes totaling 8 bytes, it reserves 4 bytes in the stack through "sub esp" and puts the structure there by moving 1 byte more 1 word. The receiving end knows how to pick it correctly.