Call external function?

300 views
Skip to first unread message

rym...@gmail.com

unread,
Oct 25, 2013, 10:37:20 AM10/25/13
to asmji...@googlegroups.com
Hello,

I'm new to AsmJit and have been wondering something.

Is it possible to call an external C(not C++) function in AsmJit? Because I need to print text to the screen, and I'm on Windows, so kernel interrupts don't work.

Thanks in advance!

Bartosz Wójcik

unread,
Oct 25, 2013, 2:17:05 PM10/25/13
to asmji...@googlegroups.com
Hello Rymg19,

Friday, October 25, 2013, 4:37:20 PM, you wrote:

> Is it possible to call an external C(not C++) function in AsmJit?
> Because I need to print text to the screen, and I'm on Windows, so kernel interrupts don't work.

Sure thing, build up an entire PE header, add imports section and off
you go :)

Bartosz Wójcik | reda...@secnews.pl | www.secnews.pl

Ryan Gonzalez

unread,
Oct 25, 2013, 3:19:18 PM10/25/13
to asmji...@googlegroups.com
...how would I go about doing that???

FYI, I'm using the Assembler interface, not the Compiler interface.



--

---
You received this message because you are subscribed to the Google Groups "asmjit-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asmjit-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ryan

Bartosz Wójcik

unread,
Oct 25, 2013, 3:33:49 PM10/25/13
to Ryan Gonzalez
Hello Ryan,

Friday, October 25, 2013, 9:19:18 PM, you wrote:

> ...how would I go about doing that???

I would start with PE COFF library specification from MSDN.

Build up MZ header, then PE header, add sections, add import section,
then when your import section is ready and you know it's pointers
(WinAPI pointers) you can add direct calls to them in your non
relocable AsmJit code.

It's hard, but doable.

Bartosz Wójcik | reda...@secnews.pl | www.secnews.pl

Ryan Gonzalez

unread,
Oct 25, 2013, 4:51:24 PM10/25/13
to asmji...@googlegroups.com
Woah...all I want to do is call putchar and getchar!!! Isn't there a...err..."simpler" way?



Bartosz Wójcik | reda...@secnews.pl | www.secnews.pl

--

---
You received this message because you are subscribed to the Google Groups "asmjit-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asmjit-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ryan

Sean Dodd

unread,
Oct 25, 2013, 5:16:55 PM10/25/13
to asmji...@googlegroups.com
If you use the compiler interface, it's simple to do with "call".  Here's an example to call external function with two doubles as inputs and a double return type:

        GPVar address(c.newGP(VARIABLE_TYPE_INT64));
        c.mov( address, (sysint_t)(void*)&someExternalFn );

        // Assume these were populated somehow, probably c.movq(GPVar, XMMVar)
        GPVar param0( c.newGP(VARIABLE_TYPE_INT64) );
        GPVar param1( c.newGP(VARIABLE_TYPE_INT64) );
        GPVar output( c.newGP(VARIABLE_TYPE_INT64) );

        ECall* ctx = c.call( address );
        ctx->setPrototype(CALL_CONV_DEFAULT, FunctionBuilder2<double, double, double>());
        ctx->setArgument(0, param0);
        ctx->setArgument(1, param1);
        ctx->setReturn(output);

Hope that helps a bit.

-Sean

Ryan Gonzalez

unread,
Oct 25, 2013, 5:27:25 PM10/25/13
to asmji...@googlegroups.com
Okay...I'll try using the compiler interface. Darn it...

Petr Kobalíček

unread,
Oct 27, 2013, 1:24:39 PM10/27/13
to asmjit-dev
Hi,

it's also possible with Assembler, but you have to know the calling convention of the function you want to call. I mean saving registers, preparing the stack and function arguments.

For example if you want to call a function having one parameter in 32-bit mode you would do something like:

Assembler a;

...
(save regs)
a.push(something)
a.call(ptr); // Assuming STDCALL that will advance the stack pointer back.
(restore regs)
...

Hope that helps.

Best,
Petr

Ryan

unread,
Oct 27, 2013, 5:16:08 PM10/27/13
to asmji...@googlegroups.com, Petr Kobalíček
Uhh...what I really needed to know is how to put the function into the variable ptr. I've worked with NASM several times, so I know about calling conventions.
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Tomislav Zubcic

unread,
Oct 28, 2013, 8:55:31 AM10/28/13
to asmji...@googlegroups.com
No need to complicate things, it's as simple as A.call((void *) getchar);

Tomislav Zubcic

unread,
Oct 28, 2013, 8:58:58 AM10/28/13
to asmji...@googlegroups.com
Seems like my reply was not sent, sorry if I'm sending again now. Anyway, it's as simple as A.call((void *) getchar); - no need to complicate things. :)


On Friday, October 25, 2013 4:37:20 PM UTC+2, rym...@gmail.com wrote:

Ryan Gonzalez

unread,
Nov 6, 2013, 12:37:48 PM11/6/13
to asmji...@googlegroups.com
Perfect! Thank you!


--
 
---
You received this message because you are subscribed to the Google Groups "asmjit-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asmjit-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ryan
Reply all
Reply to author
Forward
0 new messages