inline assembly input output list question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

inline assembly input output list question

Post by ITchimp »

I was studying linux 2.2 kernel context switching code... I found out that the register
eax, edx and ecx, appear both in the output and input section of the inline assembly...

Why is it a good idea? Are output assignment executed or input assignment done first?
the order of the execution determined the output, isn't this dangerous?

Code: Select all

/** include/asm-i386/system.h */
#define switch_to(prev,next) do {
    unsigned long eax, edx, ecx;
    asm volatile("pushl %%ebx\n\t"
                 "pushl %%esi\n\t"
                 "pushl %%edi\n\t"
                 "pushl %%ebp\n\t"
                 "movl %%esp,%0\n\t" /* save ESP */
                 "movl %5,%%esp\n\t" /* restore ESP */
                 "movl $1f,%1\n\t"   /* save EIP */
                 "pushl %6\n\t"      /* restore EIP */
                 "jmp __switch_to\n"
                 "1:\t"
                 "popl %%ebp\n\t"
                 "popl %%edi\n\t"
                 "popl %%esi\n\t"
                 "popl %%ebx"
                 :"=m" (prev->tss.esp),"=m" (prev->tss.eip),
                  "=a" (eax), "=d" (edx), "=c" (ecx)
                 :"m" (next->tss.esp),"m" (next->tss.eip),
                  "a" (prev), "d" (next));
} while (0)
foliagecanine
Member
Member
Posts: 148
Joined: Sun Aug 23, 2020 4:35 pm

Re: inline assembly input output list question

Post by foliagecanine »

Since the assembly is labelled "volatile" that means it (likely) won't be optimized. Any input operations are done prior to the assembly block (copied from memory into the registers). Any output operations are done after the code block (copied from the registers into memory).

I don't know why they would output the registers into variables.
Maybe the output register-variables (eax, ecx, edx) could just be left over from debugging.
Since it's on the stack, I'm doubtful that those variables are used anywhere else.
My OS: TritiumOS
https://github.com/foliagecanine/tritium-os
void warranty(laptop_t laptop) { if (laptop.broken) return laptop; }
I don't get it: Why's the warranty void?
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: inline assembly input output list question

Post by Octocontrabass »

ITchimp wrote:I found out that the register eax, edx and ecx, appear both in the output and input section of the inline assembly... Why is it a good idea?
The GCC manual offers a hint.
In particular, there is no way to specify that input operands get modified without also specifying them as output operands.
EAX and EDX are clobbered by the inline assembly, but they're also used as inputs, so the only way to tell GCC that they'll be modified is to also list them as outputs. Since their output values aren't going to be used, they're assigned to variables that are never used. The Linux developers chose to do this for ECX as well, even though they could have put it in the clobber list since it's not used as an input.
ITchimp wrote:Are output assignment executed or input assignment done first? the order of the execution determined the output, isn't this dangerous?
Input assignments affect code that runs before the inline assembly, and output assignment affects code that runs after the inline assembly. It doesn't matter what order GCC evaluates them in because there is no overlap between the two.
Post Reply