Is it possible to access 32-bit registers in C?

22,280

Solution 1

If you want to only read the register, you can simply:

register int ecx asm("ecx");

Obviously it's tied to instantiation.

Another way is using inline assembly. For example:

asm("movl %%ecx, %0;" : "=r" (value) : );

This stores the ecx value into the variable value. I've already posted a similar answer here.

Solution 2

Which registers do you want to access?

General purpose registers normally can not be accessed from C. You can declare register variables in a function, but that does not specify which specific registers are used. Further, most compilers ignore the register keyword and optimize the register usage automatically.

In embedded systems, it is often necessary to access peripheral registers (such as timers, DMA controllers, I/O pins). Such registers are usually memory-mapped, so they can be accessed from C...

by defining a pointer:

volatile unsigned int *control_register_ptr = (unsigned int*) 0x00000178;

or by using pre-processor:

#define control_register (*(unsigned int*) 0x00000178)

Or, you can use Assembly routine.

For using Assembly language, there are (at least) three possibilities:

  1. A separate .asm source file that is linked with the program. The assembly routines are called from C like normal functions. This is probably the most common method and it has the advantage that hw-dependent functions are separated from the application code.
  2. In-line assembly
  3. Intrinsic functions that execute individual assembly language instructions. This has the advantage that the assembly language instruction can directly access any C variables.

Solution 3

You can embed assembly in C

http://en.wikipedia.org/wiki/Inline_assembler

example from wikipedia

extern int errno;

int funcname(int arg1, int *arg2, int arg3)
{
  int res;
  __asm__ volatile(
    "int $0x80"        /* make the request to the OS */
    : "=a" (res)       /* return result in eax ("a") */
      "+b" (arg1),     /* pass arg1 in ebx ("b") */
      "+c" (arg2),     /* pass arg2 in ecx ("c") */
      "+d" (arg3)      /* pass arg3 in edx ("d") */
    : "a"  (128)       /* pass system call number in eax ("a") */
    : "memory", "cc"); /* announce to the compiler that the memory and condition codes have been modified */

  /* The operating system will return a negative value on error;
   * wrappers return -1 on error and set the errno global variable */
  if (-125 <= res && res < 0) {
    errno = -res;
    res   = -1;
  }  
  return res;
}
Share:
22,280
C4theWin
Author by

C4theWin

uuh... yeah

Updated on July 05, 2022

Comments

  • C4theWin
    C4theWin almost 2 years

    Is it possible to access 32-bit registers in C ? If it is, how ? And if not, then is there any way to embed Assembly code in C ? I`m using the MinGW compiler, by the way. Thanks in advance!

  • C4theWin
    C4theWin almost 14 years
    Nope, I actually know exactly what I`m doing. But thanks for helping me out. :)
  • C4theWin
    C4theWin almost 14 years
    Thanks a lot for the link! Very useful.
  • C4theWin
    C4theWin almost 14 years
    Thanks a lot for the code examples. Exact piece of information I needed. ;)
  • Garmekain
    Garmekain over 7 years
    How can you get that 0x00000178 number?