reading register values from c++

Any ways of reading the register values while debugging . for example if i would like to put an condition like 'if(eax == 100)' in the below code before Nop function.
Thanks

I am using Microsoft Visual Studio 2019

and can it be architecture independent

1
2
3
4
5
6
7
8
9
10
11
12
13
  if (GetAsyncKeyState(VK_NUMPAD2) & 1)
        {
            bAmmo = !bAmmo;

            if (bAmmo)
            {
                mem::Nop((BYTE*)(moduleBase + 0x1452135), 3);
            }
            else
            {
                mem::Patch((BYTE*)(moduleBase + 0x1452135), (BYTE*)"\x89\x58\x08", 3);
            }
        }
Last edited on
For the first question, kind of. If you mean to actually use a debugger, then check this post out; https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-use-the-registers-window?view=vs-2019
If, however you're talking about accessing these values from inside the program, you might look into inline assembly, or calling assembly functions from C++, plenty of good articles online for both. I prefer calling from an assembly file, but that's because I find inline assembly syntax strange and cumbersome. Push all the registers into ram and look up your system's calling conventions to then return a pointer to that data. It's hard to say what C++ is going to do at any one time with the available registers, even in pure assembly some of the registers are not guaranteed to maintain their values between function calls. Just by calling a function you are likely changing several registers' values. A debugger has the benefit of having an exterior perspective and probably runs the code virtually.

Pretty much no as far as the architecture question. The assembly calls you'd have to make will be different on arm from on x86_64. How these registers values get changed during a function call is different if you are on Windows VS Linux, and if it's compiled for 64 bit system or 32 bit systems. (Linux changed their calling conventions between the two). One of the key reasons that the C language was created is because assembly was so machine-dependent by its very nature.

Last edited on
Topic archived. No new replies allowed.