ESP Problem

  • Thread starter Thread starter JR
  • Start date Start date
J

JR

Hi Folks,

I'm trying to call a C# method from assembler language. Specifically, I'm
trying to persuade the Turbo68k emulator to interface with C#.

This is where the function is called in assembler:

push edx // Contains 'Data' parameter.
push ebx // Contains 'Address' parameter.
call dword [edi+12] // Call function.
add esp, byte 8 // Clean up.

Here is some extracts from the C# code:

public delegate void WriteWordHandler(uint Address, ushort Data);

public void WriteWord(uint Address, ushort Data)
{
}

I've also made a C .dll to help massage the data structures together.
(Essentially a series of functions, callable from C#, that set-up the
internal data structures for the emulator. These all appear to be working
correctly.)

Eg.
Turbo68k.SetWrWordHandler(WriteWord);

[DllImport("M68KEmulator.dll")]
public static extern void SetWrWordHandler(WriteWordHandler Handler);

The problem is when the WriteWord method is called the Address and Data
parameters contain the correct values, but when the WriteWord function
returns an exception is thrown saying ESP has not been saved over a function
call. I know the emulator code is configured and working correctly because
everything is working correctly until I need to call into managed code.

I've also tried the WriteWord method with the the following prototype:

public void WriteWord(uint Address, uint Data)

I tried it because I guess the assembler code is pushing eight bytes onto
the stack, but it results in the same error.

Thanks for your help.

John.
 
John,

Let me first say, your a brave man for trying to use managed code from asam.

I haven't tried that but I have had to implement many interop layers to be
called by C.
In my experience, I always need to write a Managed C++ dll to handle the
call to a C# dll.

Maybe if you create a Interop dll and try to call it from asam it would
work.

Hope this helps.
 
Hi Glen,

Thanks for your response, but I have just two minutes ago figured out the
problem. The clean up code for the stack in the assembler is not necessary.
When I remove the "add esp, byte 8" line everything works like a dream.

I must say a little note in the .NET documentation about how to call
callbacks from assembler would've saved me a lot of stress trying to figure
this one. :-|

Thanks.

John.
Let me first say, your a brave man for trying to use managed code from asam.
This is where the function is called in assembler:

push edx // Contains 'Data' parameter.
push ebx // Contains 'Address' parameter.
call dword [edi+12] // Call function.
add esp, byte 8 // Clean up.
 
Back
Top