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

  • SEARCH
  • Home
  • 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 8211893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:34:48+00:00 2026-06-07T10:34:48+00:00

I want to test inline asm capabilty on gcc. So, I type and compile

  • 0

I want to test inline asm capabilty on gcc.
So, I type and compile following code on ubuntu 12.04 64-bit
but system shows ”segmentation fault” on screen when it runs.
I don’t have any idea what causes the problem.

#include <stdio.h>
char Format[]="Hello world %d\n";
int main()
{
    asm
    (
        "movl $3,4(%esp);"
        "movl $Format,(%esp);"
        "call printf;"
    );
    return 0;
}

Thank you guys for helping me a program newbie.
I use Code::blocks as IDE to write this code. I had tried to use 64-bit registers such like %rdx, but logs of Build messages shows ” Error: bad register name `%rdx’ ” when compiling the code. I think this means the gcc invoked by Code::blocks is 32-bit version, hence it can’t recognize those registers.
I modify the code to reserve the stack space

#include <stdio.h>
char Format[]="Hello world %d\n";
int main()
{
    asm
    (
        "subl $8,%esp;"         //I  don't know  $4, $8, $12, $16, $20 which is correct
                                        //but I had tried them all but results are still  ''segmentation fault."
        "movl $3,4(%esp);"
        "movl $Format,(%esp);"
        "call printf;"
        "movl %ebp,%esp;"
    );
    return 0;
}

and even use -m32 as compiler option, but it still shows ”segmentation fault “.

thanks again for who helps.

  • 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-06-07T10:34:51+00:00Added an answer on June 7, 2026 at 10:34 am

    System V ABI for x64 mandates that the first six integer/pointer arguments to a function should go in registers %rdi, %rsi, %rdx, %rcx, %r8 and %r9. The stack is used to pass further arguments. It also requres that when calling functions with variable number of arguments (like printf), %rax should be set to the total number of floating-point arguments passed in the XMM registers. The right sequence to call printf() in your case is:

    xorl %eax, %eax
    movl $Format, %edi
    movl $3, %esi
    call printf
    

    %rax should be set to 0 since no floating-point arguments are being passed. This code also uses the fact that VA of initialised data usually lies somewhere in the first 4 GiB and thus shorter 32-bit instructions are used. Of course printf will still examine the full content of %rdi to determine where the format string is located in memory.

    Your code uses the 32-bit calling convention and should theoretically work if cross-compiled as 32-bit with -m32 but you should first reserve stack space for the arguments using something like subl $20, %esp and restore it after the call with addl %20, %esp, otherwise you are either overwriting the stack of main() or ret will pick the wrong return address. Here is a fully working (tested) C/asm code that compiles and run in 32-bit mode:

    #include <stdio.h>
    
    char Format[] = "Hello world, %d\n";
    
    int main (void)
    {
       asm
       (
          // Make stack space for arguments to printf
          "subl $8, %esp\n"
          "movl $3, 4(%esp)\n"
          "movl $Format, (%esp)\n"
          "call printf\n"
          // Clean-up the stack
          "addl $8, %esp\n"
       );
       return 0;
    }
    
    $ gcc -m32 -o test.x test.c
    $ ./test.x
    Hello world, 3
    

    Remark: I use \n instead of ; at the end of each assembly line only to improve the readability of the compiler assembly output – it is irrelevant to the correctness of the code.

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

Sidebar

Related Questions

I want to test the following code: private bool TestException(Exception ex) { if ((Marshal.GetHRForException(ex)
I want to develop a test project for inline editing using jQuery. In this
All the code is inline here: http://shanxinc.com/test/fancybox2/ All I'm trying to do is get
I want test a FIX gateway for our company and was wondering if anything
I want to test the login() action in my UsersController.php <?php class UsersController extends
I want to test it on a tablet because it is more natural to
I want to test Add new item activity in my application. When user fills
I want to test some QAbstractListModels I already implemented, is it better to use
I want to test if a sentence contains anything else than white-space characters. This
I want to test the block function and the value for the context. The

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.