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 8875173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:54:33+00:00 2026-06-14T18:54:33+00:00

I am trying to write a small piece of my code in GCC style

  • 0

I am trying to write a small piece of my code in GCC style extended asm (x86-64 target) and am having trouble encoding struct offsets.

I have a struct s with a member size_t a[], a pointer to such a struct and an index both of which are generated within the asm block.

Now I need to address that element in asm

asm (
    "mov %[displ](%[s], %[index], 8), %%rbx"
    : [s] "+r" (s)
    , [index] "+r" (i)
    : "memory", "cc", "rax", "rbx"
);

How can I encode displ into the asm block? Passing offsetof(struct s, a) as an immediate prefixes it with $ and generates invalid assembly.

asm (
    "mov %[displ](%[s], %[index], 8), %%rbx"
    : [s] "+r" (s)
    , [index] "+r" (i)
    : [displ] "i" (offsetof(struct s, a))
    : "memory", "cc", "rax", "rbx"
);
  • 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-14T18:54:34+00:00Added an answer on June 14, 2026 at 6:54 pm

    It actually is possible, using the %c... operand modifier:

    #include <stddef.h>
    #include <stdint.h>
    
    struct s
    {
      int a, b;
    };
    
    int foo (struct s *s, int i)
    {
      int r;
      asm (
           "movl %c[displ](%[s],%[index],8), %[r]\n\t"
           : [r] "=r" (r)
           : [s] "r" (s) , [index] "r" ((uintptr_t)i),
             [displ] "e" (offsetof(struct s, b))
           :
           );
    
      return r;
    }

    Thanks where thanks is due – found that here. There’s a gcc mailing list posting referring to this as well; the keywords there are “output substitution”.
    The stackoverflow posting What does %c mean in GCC inline assembly code? also has an explanation about %c in particular.

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

Sidebar

Related Questions

Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)
I'm trying to write a small piece of code that merges lines alternatively from
so I'm trying to write a small piece of android code where I want
I'm trying to write a small piece of code that passes a small formula
I am trying to write a small piece of Actionscript that gives focus to
I am trying to write a small jquery code, but I just stuck at
I wrote a small piece of code that rapidly read and write to a
I'm trying to write a small piece of Javascript that will sort through a
I'm trying to write a small block of code that prints out the files
I'm trying to write a small script which parses and executes Brainfuck code, to

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.