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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:19:30+00:00 2026-05-22T17:19:30+00:00

I’m trying to have thread-safe local variables in an assembly program. I’ve searched on

  • 0

I’m trying to have thread-safe local variables in an assembly program.
I’ve searched on the net, but I haven’t found anything simple.

I’m currently using GCC assembler, as the program is a mix of C code and assembly, but the final program will contain code for multiple-platforms / calling conventions.

For now, I’ve declared my variables using the .lcomm pseudo-op.
As I understand it, those variables will be placed in the .bss section.
So I guess they will be shared by all threads.

Is there a way to have a kind of TLS variables directly in assembly, or should I use platform-specific implementations, like pthread or __declspec on Windows?

Hope it’s clear enough. Don’t hesitate to ask if more information is needed.

Thanks to everyone,

EDIT

Here’s the code in question:

.lcomm  stack0, 8
.lcomm  stack1, 8

.globl _XSRuntime_CallMethod
_XSRuntime_CallMethod:

    pushq   %rbp
    movq    %rsp,   %rbp

    xor     %rax,   %rax

    popq    stack0( %rip )
    popq    stack1( %rip )

    callq   *%rdi

    pushq   stack1( %rip )
    pushq   stack0( %rip )

    leave
    ret

Basically, it’s used to redirect a call to a C function.

The C prototype is:

extern uint64_t XSRuntime_CallMethod( void ( *m )( void * self, ... ), ... );

It takes a function pointer as first argument, hence the callq *%rdi, as I’m testing this with system V ABI.

The assembly code is very simple, and I’d like to keep it that way, so it can be easily maintainable.

The question is: how to make the stack0 and stack1 variables thread safe.

  • 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-22T17:19:31+00:00Added an answer on May 22, 2026 at 5:19 pm

    Not that familiar with assembler so:

    .lcomm  stack0, 8
    .lcomm  stack1, 8
    
    .globl _XSRuntime_CallMethod
    _XSRuntime_CallMethod:
    
        pushq   %rbp // save BP
        movq    %rsp,   %rbp // load BP with SP
    
        xor     %rax,   %rax  // clear AX
    
        popq    stack0( %rip )  // pop return address into STACK0
        popq    stack1( %rip )  // pop flags into stack1
    
        callq   *%rdi  // call the indirect procedure, so putting flags/return to         XSRuntime_CallMethod onto stack
    
        pushq   stack1( %rip ) // put caller flags onto stack
        pushq   stack0( %rip ) // put caller return onto stack
    
        leave // clean passed parameters from stack
        ret   // and back to caller
    

    Is this how it works, yes??

    If so, would it not be easier to just jump to the indirect procedure, rather than calling it? You then don’t need any extra variables to hold the caller flags/return & the indirected procedure returns directly to the caller.

    Just a suggestion – while since I did assembler.

    If you have to store the caller address somewhere, dec the SP, (enter?), and use the stack frame. Anything else is likely to be thread-unsafe at some point.

    Rgds,
    Martin

    Well, with a TLS maybee not thread-unsafe, but what about any recursive calls? You end up with another stack in the TLS to cover this, so you may as well use the ‘SP’ stack

    Martin

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

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.