❔ P/Invoke x86 ASM
I have the following x86 assembly function:
I am compiling it into a win64 dll using NASM:
nasm -f win64 maths.asm -o maths.dll
The consuming project is set to 64 bit:
Bindings in C#:
Does anyone have any idea what format this should be in for the runtime to accept, I assumed using the same platform architecture and CDECL calling conventions would be enough for this to work.
Thanks22 Replies
The regs used here don't seem right?
But even then, this should just return corrupt data, not throw an exception, @jkortech any ideas why this'd throw that?
Oh wait, the bot quoted incorrectly in #allow-unsafe-blocks
yeah that was my bad, I had the asm from when I was trying to force everything 32 bit in the question so I switched it to the 64 bit regs
basically ive tried every combination of 32/64 bit configs to get this to just run
Does it also error out with DllImport instead of LibraryImport?
yeah exactly the same, I checked the output from the library import source generator and it uses the same DllImport anyways
@Bambosa nasm only creates obj files
Also you declared the asm function as
_add
but you import add
they need to have a matching nameyeah but that should still be a different exception then
No? p/invoke anything other than a valid dll throws that
@Kouhai /人◕ ‿‿ ◕人\ I've used with and without
_
and based on the cdecl calling convention you should indeed declare with _ and call without but happy to be wrong on thatStill nasm creates obj files
You need to link to a dll
But if you export it as
_add
you have to p/invoke it as _add
as well ok I will give it a go, I need a def file for the linker right?
You might do something like
lld-link maths.obj /dll /noentry /out:maths.dll /export:_add
math.obj is the file generated by nasm
so
nasm -f win64 maths.asm -o maths.obj
Here I'm using llvm's linker
lldok cool, didn't have it so will comment once i've gotten everything linked
thanks
You could use vs c++ developer tools linker as well @Bambosa
Which would be
link
instead of lld-link
I didn't have the C++ workload installed either, just the .NET stuff (new installation)
Oh
just as easy to grab llvm from choco
Yup
it worked!
thank you 👍
Np! <:MadoThumbsUp_MM:406514447973351444>
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.