Why can't I assign the value of the PID chunk of the FS register to an unsigned int?

Short description (32 bit build)

Why does this throw an improper operand error?

unsigned int GetPID() {

    unsigned int id;
    __asm {
        mov id, fs:[0x20];
    }
    return id;

but not this?

unsigned int GetPID() {

    unsigned int id;
    __asm {
        mov ebx, fs:[0x20];
        mov id, ebx;
    }
    return id;
}

I'm new to using assembly within C and I'd like some explanations on how things work when I use it.

Note. I know using this function to get the current process id is bad. This is just an experiment, im also learning about operating systems.

Side question: is it ok to write half of a function in assembly brackets? and then finish the function with C? or should every function be either all C or all assembly?

The Intel assembly doesn't support MOV from memory to memory. id is a memory location. You can only MOV from memory to a register or vice versa.

That said, check out the __readfsdword compiler intrinsic.

Vaccines: Vac-Gen/Why Are Childhood Vaccines So Important?, Immunity is the body's way of preventing disease. Children are born with an immune system composed of cells, glands, organs, and fluids located throughout the� Music video by Liz Phair performing Why Can't I?. #LizPhair #WhyCantI #Vevo

but not this? Because the register is being used to trampoline the value; without this trampoline, the opcode may be something like: movl -4[ebp], fs:[0x20]. You aren’t out of the woods yet -- ebx is a callee-preserved register in most implementations, so by corrupting it, you would trigger an error somewhere up the call stack. Those are particularly nasty to debug, because all traces of GetPID may be gone (stack rewritten) before the corruption is detectable.

ok to write a function?: my opinion is that all C or all assembly is the best: it is clearer and more maintainable. Even with the gcc-constraints specification, inlining is a crapshoot between compiler versions and bugs within various versions. Who wants that baked into your source? An assembly module need only conform to the relevant ABI [ usually well published ], and provides a nice spot to say " this is the machine dependent stuff ".

Opinion, Death rates followed with a lag. At this point we can only look longingly at Italy's success in containing the coronavirus: Restaurants and cafes are� Being overweight, getting too little exercise, and smoking all can work against the good blood flow that is key to erections. For some men, a little alcohol may help take the edge off.

I found a long time ago that writing inline assmebly in .c files was essentially always a mistake. Rather than having a short list of obvious places you have to change when porting to a new processor, you end up with them scattered everywhere. I took the liberty of rewriting your function in a .asm file for you:

.486
.MODEL FLAT
.CODE
PUBLIC _GetPID
_GetPID PROC
    mov eax, fs:[0x20]
    ret
_GetPID ENDP
END

Why do some COVID-19 patients infect many others, whereas most , Here are just a few of the common ones: Fear: A person may be afraid of what will happen if they decide to leave the relationship. Believing Abuse is Normal: A � “Why Can’t We Be Friends?” is a song by the funk band War off of their 1975 studio album of the same name. The song reached #6 on the Billboard Hot 100 in the summer of 1975.

Why Do People Stay in Abusive Relationships?, Why Can't You Read My Mind? Overcoming the 9 Toxic Thought Patterns that Get in the Way of a Loving Relationship [Bernstein PhD, Jeffrey, Magee, Susan]� Why can we see the Moon in daylight this week? People only tend to notice the Moon during daylight hours when it’s: In their line of sight, so about 10º to 20º above the horizon.

Why Can't You Read My Mind? Overcoming the 9 Toxic Thought , Why can't I join my session? If you're having trouble connecting to your meeting, review some of the troubleshooting tips and articles listed� More Americans can vote by mail in November than before the pandemic; find out which states have changed rules. Barring a landslide, we may not have a result in the presidential election on Nov. 3.

Why can't I join my session?, Why Is My Post Type Unsupported? Why is my boost unavailable? To learn more about why your boost is unavailable or how you can boost a similar post,� Can’t go? You’re not alone. About 20% of Americans have occasional constipation-- bowel movements less than three times a week.Or if they do poop, the output is hard, small, and painful to

Comments
  • Because that's the way the processor works. In assembly you cannot move anything anywhere, there are strict rules. Every processor typo has different rules.
  • Ironic, @Jabberwocky: I think you mean "Every processor type has different rules."
  • @JohnBollinger too late for editing. indeed the o in typo is a typo ;-)
  • Just use the GetCurrentProcessId function. There's no need to use inline assembly or to play around with undocumented Windows internal structures.
  • If you write conforming (and this is harder than it appears) inline assembly with GCC what kind of bugs are you referring to? Generally speaking the bugs are almost exclusively a misuse of inline assembly that lead to undefined behavior. Undefined behavior can manifest itself differently across compilers and compiler options. If you properly understand all the rules (and there are a lot) for GCC inline assembly it does work as expected.
  • I do prefer compiler intrinsics over external assembly or inline assembly when available. with MSVC I'd consider utilizing intrinsics like __readfsbyte, __readfsdword, __readfsqword, __readfsword if needing to reference data through the FS segment
  • Thanks for the heads up on ebx and the other tips. i didnt know that. im gonna read up more on x86
  • The rules for GCC inline assembly have changed over the years, particularly in pinning down what volatile means. That isn't a slight against GCC, more against the standards group. With LTO now inspecting the opcodes to determine the critical ordering, there could be another coming. I recall some version of the ppc code generator requiring a fake 256 byte local variable to get it to believe that "memory" actually meant "memory".