flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
LocoDelAssembly
Do you use the exact same asm code when you pass the structures by value?
The emms instruction shouldn't be there, you need it for MMX stuff but you are using SSE. PS: Consider adding a fourth dummy field in the structure, since if the structure is not aligned to 16 bytes, it could cross a page boundary and cause a page fault if there is no memory allocated there. |
|||
![]() |
|
jmcclane
Yes I use that code...
This is not optimized for boundary that i must do... Thanks for emms! |
|||
![]() |
|
r22
My guess is when you use ByVal for the structure instead of ByRef VB.NET is sending some runtime optimized/mangled version of the structure to your DLL. Using the LayoutKind.Sequential attribute property might help.
Code: [StructLayout(LayoutKind.Sequential)] Public Structure vec3_t Public x As Single Public y As Single Public z As Single Private pad As Single End Structure |
|||
![]() |
|
jmcclane
I tried but nothing .... thanks anyway
I think that is the same problem in c# |
|||
![]() |
|
ProphetOfDoom
Hi,
I only use 64-bit Linux and am not familiar with VB or the "proc" macro so I might be completely wrong but... As I understand it, passing by value means the entire structure is copied onto the stack. Thus your assembly function should expect ( 4 + 12 + 12 = 28 ) bytes on the stack. I doubt the "proc" macro takes this into account? Assuming the old EBP and the return address are also placed on the stack, I suspect you'd find v1 at [ESP + 12] and v2 at [ESP + 24]. So try: Code: lea ecx, [esp + 24] lea eax, [esp + 12] Not sure tho. |
|||
![]() |
|
LocoDelAssembly
I just checked that it is as ProphetOfDoom says, the struct is copied to the stack instead of giving you a pointer to a copy (just by the copy alone I'd recommend you stick to ByRef unless you have a very good reason not to).
In case you want to get this working like this anyway here is how I did it: Code: Option Strict On Imports System.Runtime.InteropServices Module Main Public Structure vec3_t Public x As Single Public y As Single Public z As Single Private pad As Single Public Overrides Function ToString() As String Return String.Format("<{0},{1},{2}>", Me.x, Me.y, Me.z) End Function End Structure <DllImport("math.dll", EntryPoint:="VectorAdd", CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Sub VectorAdd(ByRef vD As vec3_t, ByVal vA As vec3_t, ByVal vB As vec3_t) End Sub Sub Main() Dim a, b, d As vec3_t a.x = 1 a.y = 2 a.z = 3 b.x = 0 b.y = -2 b.z = 7 VectorAdd(d, a, b) Console.WriteLine("a = " & a.ToString()) Console.WriteLine("b = " & b.ToString()) Console.WriteLine("d = " & d.ToString() & " (a + b)") Console.ReadLine() End Sub End Module Code: format PE GUI 4.0 DLL include 'win32wxp.inc' section '.text' code readable executable proc DllEntryPoint hinstDLL,fdwReason,lpvReserved mov eax,TRUE ret endp struct vec3_t x dd ? y dd ? z dd ? rd 1 ends VectorAdd: virtual at esp .oldIP dd ? .pDest dd ? .vA vec3_t .vB vec3_t .size = $-$$ end virtual mov edx, [.pDest] lea ecx, [.vB] lea eax, [.vA] movups xmm0, [ecx] movups xmm1, [eax] addps xmm0, xmm1 movups [edx], xmm0 ret .size - 4 dd VectorAdd ; To force relocations since I don't remember now the magic code to force the relocs section to be non empty. section '.edata' export data readable export 'Math.dll',\ VectorAdd, 'VectorAdd' section '.reloc' fixups data readable discardable .end DllEntryPoint Console output: Code: a = <1,2,3> b = <0,-2,7> d = <1,0,10> (a + b) |
|||
![]() |
|
jmcclane
Thanks a lot for help...,
here is another sample but now I use vector4 struct Code: <StructLayout(LayoutKind.Sequential)> Public Structure vec4_t Public x As Single Public y As Single Public z As Single Public w As Single End Structure <DllImport("C:\math.dll", EntryPoint:="VectorAdd4", CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Sub VectorAdd4(ByRef vD As vec4_t, ByRef vA As vec4_t, ByRef vB As vec4_t) End Sub Code: proc VectorAdd4 vD, vA, vB mov edx, [vD] ;dest mov ecx, [vB] ;v2 mov eax, [vA] ;v1 movaps XMM0, [ecx] movaps XMM1, [eax] addps XMM0, XMM1 movaps [edx], XMM0 ret endp and when I replace muvaps with movups it's work otherwise not....?? but vec4 struct is aligned... [EDIT by Loco]Added code tags[/edit]
Last edited by jmcclane on 28 Feb 2013, 18:13; edited 1 time in total |
|||||||||||
![]() |
|
LocoDelAssembly
How are you guaranteeing alignment? Having a structure of 16 bytes doesn't imply its base address is multiple of 16.
|
|||
![]() |
|
jmcclane
Try to put align 16...? don't know
|
|||
![]() |
|
LocoDelAssembly
jmcclane wrote: Try to put align 16...? don't know In case it is not obvious, you'll have to make procedures that accept arrays as parameters, otherwise you are very likely to be making your code slower than just doing addition from VB.Net side because of the pinvoke overhead. |
|||
![]() |
|
jmcclane
Thanks a lot LocoDelAssembly!!!
I'm looking for instructions on net already... but I think it's a problem for vb and c # Maybe I will found something... thanks again |
|||
![]() |
|
comrade
Yippee-ki-yay, motherfucker!
![]() |
|||
![]() |
|
jmcclane
Bruce Willis will align:)
|
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.