Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 686731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:03:15+00:00 2026-05-14T02:03:15+00:00

I have a piece of C++ code (compiled with g++ under a GNU/Linux environment)

  • 0

I have a piece of C++ code (compiled with g++ under a GNU/Linux environment) that load a function pointer (how it does that doesn’t matter), pushes some arguments onto the stack with some inline assembly and then calls that function, the code is like :

unsigned long stack[] = { 1, 23, 33, 43 };

/* save all the registers and the stack pointer */
unsigned long esp;
asm __volatile__ ( "pusha" );
asm __volatile__ ( "mov %%esp, %0" :"=m" (esp));

for( i = 0; i < sizeof(stack); i++ ){
    unsigned long val = stack[i];
    asm __volatile__ ( "push %0" :: "m"(val) );
}

unsigned long ret = function_pointer();

/* restore registers and stack pointer */
asm __volatile__ ( "mov %0, %%esp" :: "m" (esp) );
asm __volatile__ ( "popa" );

I’d like to add some sort of

#ifdef _LP64
   // 64bit inline assembly
#else
   // 32bit version as above example
#endif

But i don’t know inline assembly for 64bit machines, anyone could help me?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:03:15+00:00Added an answer on May 14, 2026 at 2:03 am

    While it shouldn’t be much of a problem to call a function pointer with the appropriate arguments in inline assembly, I don’t think recoding this naively in x64 will help you, because the calling conventions to be used are very probably different (defaults for 32bit and 64bit linux are definitely different). Have a look here for details. So I guess, if you can get away without inline assembly in this case (see the other answer), it’ll be easier to port.

    Edit: OK, I see you may have to use assembly. Here are some pointers.

    According to Agner Fog’s document, linux x64 uses RDI, RSI, RDX, RCX, R8, R9 and XMM0-XMM7 for parameter transfer. This implies that in order to achieve what you want (disregarding floating-point use) your function will have to:

    (1) save all registers that need to be saved (RBX, RBP, R12-R15): Set aside space on the stack and move these registers there. This will be somthing along the lines of (Intel syntax):

    sub rsp, 0xSomeNumber1
    mov [rsp+i*8], r# ; insert appropriate i for each register r# to be moved
    

    (2) Evaluate the number of arguments you will have to pass by stack to the target function. Use this to set aside the required space on the stack (sub rsp, 0xSomeNumber2), taking into account 0xSomeNumber1 so that the stack will be 16-byte aligned at the end, i.e. rsp must be a multiple of 16. Don’t modify rsp after this until your called function has returned.

    (3) Load your function arguments on the stack (if necessary) and in the registers used for parameter transfer. In my view, it’s easiest if you start with the stack parameters and load register parameters last.

    ;loop over stack parameters - something like this
    mov rax, qword ptr [AddrOfFirstStackParam + 8*NumberOfStackParam]
    mov [rsp + OffsetToFirstStackParam + 8*NumberOfStackParam], rax
    

    Depending on how you set up your routine, the offset to the first stack parameter etc. may be unnceccessary. Then set up the number of register-passed arguments (skipping those you don’t need):

    mov r9, Param6
    mov r8, Param5
    mov rcx, Param4
    mov rdx, Param3
    mov rsi, Param2
    mov rdi, Param1
    

    (4) Call the target function using a different register from the above:

    call qword ptr [r#] ; assuming register r# contains the address of the target function
    

    (5) Restore the saved registers and restore rsp to the value it had on entry to your function. If necessary, copy the called function’s return value wherever you want to have them. That’s all.

    Note: the above sketch does not take account of floating point values to be passed in XMM registers, but the same principles apply.
    Disclaimer: I have done something similar on Win64, but never on Linux, so there may be some details I am overlooking. Read well, write your code carefully and test well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some piece of code that behaves differently under Mac OSX and Linux
I have a piece of templated code that is never run, but is compiled.
I have a piece of code that defines a function that takes a function
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have this piece of code in a function. I want to print the
I have a piece of code that was written by someone else before the
I have a piece of code (below) that can get the text of an
I have ported a piece of C++ code, that works out of core, from
Let's say I have a piece of code that runs fine on an OS.
I have this piece of code that finds words that begin with @ or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.