Search code examples
cgccassemblycpu-registers

C - Read and set assembler registers


I need to be able to access assembly registers from my C code. I there a easy way to set the value of a register to the value of a variable and get the value of a register as a variable?? Hope this makes sense...


Solution

  • If you are using VC (Microsoft's C compiler)

    int regVal;
    __asm {
      mov [regVal], eax
    }
    

    That would load whatever eax is holding into regVal

    If you're using GCC:

    int regVal;
    asm("movl %%eax, %0" : "=r"(regVal) :);
    

    Hopefully I got this right, I don't actually do inline assembly in GCC.