Search code examples
cassemblyx86inline-assembly

What does "asm volatile("pushfl; popl %0" : "=r" (eflags))" mean? in assembly at c?


now I try to understand xv6 code. and I found below code.

I think this code is related to assembly, but I couldn't find what it means.

especially, I found the front part of asm volatile(:) means assembly opeation set and what is the eflags, but I couldn't find what the back of this("=r" (eflags)) means.

So my question is.. what does the below code mean? If you can give me some answer or advice, I'll really thank you for your sharing. Thank you :)

static inline uint readeflags(void)
{
  uint eflags;
  asm volatile("pushfl; popl %0" : "=r" (eflags));
  return eflags;
}

Solution

  • To answer your last question, the "=r" (eflags) part is the way in which one specifies in GCC inline assembly¹ what variable to use with a storage "location" and in what manner (https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html). Locations are numbered from 0, and multiple locations can be specified. %0 hence being the first one.

    pushfl pushes the contents of the EFLAGS register onto the stack, and pop %0 removes the topmost value, i.e. the EFLAGS value and places it in location %0, which would be the eflags variable. It is done this way, because the IA-32 instruction set does not contain an instruction to read EFLAGS directly.


    1: also used by LLVM, but not MSVC – inline assembly is not standardized!