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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:03:09+00:00 2026-05-31T18:03:09+00:00

static inline void *__memset(void *s, char c, size_t n) { int d0, d1; asm

  • 0
static inline void *__memset(void *s, char c, size_t n) {
int d0, d1;
asm volatile (
    "rep; stosb;"
    : "=&c" (d0), "=&D" (d1)
    : "0" (n), "a" (c), "1" (s)
    : "memory");
return s;
}

What are “d0” and “d1” used for? Could you please explain all the code completely?Thank you!

  • 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-31T18:03:11+00:00Added an answer on May 31, 2026 at 6:03 pm

    What are “d0” and “d1” used for?

    In effect, it says that the final values of %ecx, %edi (assuming 32-bit) are stored in d0, d1 respectively. This serves a couple of purposes:

    It lets the compiler know that, as outputs, these registers are effectively clobbered. By assigning them to temporary variables, an optimizing compiler also knows that there is no need to actually perform the ‘store’ operation.

    The “=&” specifies these as early-clobber operands. They may be written to before all the inputs are consumed. So if the compiler is free to choose an input register, it shouldn’t alias these two.

    This isn’t technically necessary for %ecx, since it’s explicitly named as an input: "0" (n) – the ‘rep’ count in this case. I’m not sure it’s necessary for %edi either, since it can’t be updated before the input "1" (s) is consumed, and the instruction executed. And again, as it’s explicitly named as an input, the compiler isn’t free to choose another register. In short, “=&” doesn’t hurt here, but it doesn’t do anything.

    As "a" (c) specifies an input-only register %eax set to (c), the compiler may assume that %eax still holds this value after the ‘asm’ – which is indeed the case with "rep; stosb;".

    "memory" specifies that memory can be modified in a way unknown to the compiler – which is true in this case, it’s setting (n) bytes starting at (r) to the value (c) – assuming the direction flag is cleared, which it should be. This does have the effect of forcing a reload of values, as the compiler can’t assume that registers reflect the memory values they’re supposed to anymore. It doesn’t hurt, and it may be necessary to make it safe for a general case memset, but it’s often overkill.

    Edit: Input operands may not overlap clobber operands. It doesn’t make sense to specify something as input-only and clobbered. I don’t think the compiler allows this, and it wouldn’t be wise to use an ambiguous specification even if it did. From the manual:

    You may not write a clobber description in a way that overlaps with an input or output operand. For example, you may not have an operand describing a register class with one member if you mention that register in the clobber list.


    Reviewing some old answers, I thought I would add a link to the excellent Lockless GCC inline ASM tutorial. The article builds on prior sections, unlike the gcc manual which is best described as a ‘reference’, and not really suited to any sort of structured learning.

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

Sidebar

Related Questions

static char st[][8192]; void foo ( int tab_size){ st = (char**) malloc ((tab_size+1)*sizeof(char)*8192); }
that's the code: static inline void shrinkData(const vector<Data> &data, unsigned short shrinkType){ #define CASE_N(N)
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
Assume this code: static inline void inc(int64_t* atomic) { __asm__ __volatile__ ( lock incq
In file A.hpp, I have extern boost::signal<void (model::Bullet&, Point&, Point&, int)> signal_createBullet; and so
static void Main() { string str; str = Console.ReadLine(); while (str != null)//HERE! {
static void Main(string[] args) { //read in the file StreamReader convert = new StreamReader(../../convert.txt);
static void main(args){ System.in.withReader { def input = it.readLine() for(def i = 0; i
Code static void MyClass::ThreadEntryStatic() { //... } void MyClass::Begin() { CreateThread(..,ThreadEntryStatic,..); } In which
I have a static inline function defined in an H file, and at one

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.