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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:10:04+00:00 2026-05-27T03:10:04+00:00

I have two macros, one is written in assembly and the other in C.

  • 0

I have two macros, one is written in assembly and the other in C. The second macro uses the first macro. However, I also want to write the second macro in assembly with volatile, so I can control its placement in the code. And please note that tid is a runtime value, not a constant like n.

What is a good way to write that in assembly? Also, is it possible to control placement of a C code like assembly with volatile?

#define SAVE_SP(n) __asm__ __volatile__ ("movq %rsp, msp"#n";" \
     "movq ts"#n", %rsp;" \
     )

#define SAVE_STACK_POINTER( tid ) \
    switch( tid ) \
    { \
        case 0: \
            SAVE_SP( 0 ); \
            break; \
        case 1: \
            SAVE_SP( 1 ); \
            break; \
        case 2: \
            SAVE_SP( 2 ); \
            break; \
        case 3: \
            SAVE_SP( 3 ); \
            break; \
    }
  • 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-27T03:10:04+00:00Added an answer on May 27, 2026 at 3:10 am

    You can ask gcc its idea of how to write your code in assembly: gcc -S foo.c or gcc -Wa,-alh=foo.s -c foo.c. You may want to improve on the results, of course. You will need to do a little extra: use %0 for the parameter that you pass for the assembly chunk, and declare the registers that you’ve clobbered. Look up Assembler Instructions with C Expression Operands in the GCC manual if you aren’t familiar. Here’s how this might look like (warning, typed directly into the browser, and don’t really know x86 assembly syntax).

    #define SAVE_STACK_POINTER(tid) __asm__ __volatile__ (" \
            cmpl $0, %0                                   \n\
            je .SAVE_STACK_POINTER_0                      \n\
            cmpl $1, %0                                   \n\
            je .SAVE_STACK_POINTER_1                      \n\
            cmpl $2, %0                                   \n\
            je .SAVE_STACK_POINTER_2                      \n\
            cmpl $3, %0                                   \n\
            je .SAVE_STACK_POINTER_3                      \n\
            jmp .SAVE_STACK_POINTER_done                  \n\
          .SAVE_STACK_POINTER_0:                          \n\
            movq %%rsp, msp0                              \n\
            movq ts0, %%rsp                               \n\
            jmp SAVE_STACK_POINTER_done                   \n\
          .SAVE_STACK_POINTER_1:                          \n\
            movq %%rsp, msp1                              \n\
            movq ts1, %%rsp                               \n\
            jmp SAVE_STACK_POINTER_done                   \n\
          .SAVE_STACK_POINTER_2:                          \n\
            movq %%rsp, msp2                              \n\
            movq ts2, %%rsp                               \n\
            jmp SAVE_STACK_POINTER_done                   \n\
          .SAVE_STACK_POINTER_3:                          \n\
            movq %%rsp, msp3                              \n\
            movq ts3, %%rsp                               \n\
          .SAVE_STACK_POINTER_done:                       \n\
        " : : "r" (tid))
    

    A fancier method would involve figuring out how many bytes each movq–movq–jmp block takes (note: I haven’t checked, I use 8) and making a computed jump into it; something like

    __asm__("                        \n\
        movl %0, %eax                \n\
        mul  8, %eax                 \n\
        add  4, %eax                 \n\
        jmp . + %eax                 \n\
        movq %%rsp, msp0             \n\
        movq ts0, %%rsp              \n\
        jmp .SAVE_STACK_POINTER_done \n\
        …
      .SAVE_STACK_POINTER_done:      \n\
    " : : "r" (tid) : "%eax")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two macros. The first one takes a symbol as the only parameter
I have two spreadsheets... when one gets modified in a certain way I want
I have two targets for my project: one is the App, and the other
I have written a macro that to return specific values in one cell based
Say, if I have one or two debug functions or macros, where is a
I have a problem to create a preprocessor macro function, that concatenates two Strings
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two classes (MVC view model) which inherits from one abstract base class.
I have two update panels on a page. And I want to update both
working on an ANT build file, where I have two macros defined and need

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.