This is the mail archive of the mailing list for the binutils project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How can I do calling for external address in my "C" programm by inline GAS?

On 27/03/2012 17:53, Dmitrij K wrote:
Ian wrote:
Dmitrij K<> writes:
7       asm volatile (
8           "movq %0, %%rcx;" /* str into RCX is first integer argument
(amd64 notation) */
9           "movq %1, %%rdi;" /* address of PRINTF into RDI is Must be
preserved by called function (amd64 notation) */
10          "call * %%rdi;" /* call PRINTF */
11       :  /* no output */
12       :"r"(str),"r"(p_printf)         /* input */
13       : "rcx", "rdi", "memory" /* clobbered register */
14      );
15      }

On x86_64 the first parameter should go into %rdi, not %rcx. You are putting the function address into %rdi, so in effect you are treating the function address as a printf format string.


Thanks for your reply.

I did as you told me, but it is not working too :( (I had used %r15 instead
%rdi for p_printf, and %rdi for str)...

I have not ideas how I can to call func by the address.
I can't find examples of calling functions with args by address (as needs me).
I had seen examples without args only (abi, internet)...
I need help still.

Maybe you have a something tips/examples for me ?

Why not compile up a C/C++ program that does what you want, then disassemble it to figure out what it's doing - reference the ABI document as you go.

Excuse the poor coding :) but something like this might be a starting point,

#include <stdio.h>
typedef int(*ptr_type)(const char*, ...);

void do_call (ptr_type func, const char* message)
  (void) func (message);

main ()
  do_call (&printf, "Hello World\n");
  return 0;

the disassembly of do_call should contain things of interest to you so long as you don't put the compiler optimisation too high (otherwise it might inline into main).

Good luck,


PS: Sorry, but I have not an experience of programming on assembler (excluding 2-3 programms "hello world" for GAS 32 bit) . I have some experience of programming on c/c++ only (And I'm away from proff, I'm an amateur, and I'm doing it for myself).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]