views:

256

answers:

7

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!

+1  A: 

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;
}
Charles Ma
A: 

I don't think you can do them directly. You can do inline assembly with code like:

asm (
    "movl $0, %%ebx;"
    "movl $1, %%eax;"
); 
paxdiablo
A: 

If you are on a 32-bit processor and using an adequate compiler, then yes. The exact means depends on the particular system and compiler you are programming for, and of course this will make your code about as unportable as can be.

In your case using MinGW, you should look at GCC's inline assembly syntax.

Crashworks
Thanks a lot for the link! Very useful.
C4theWin
A: 

there is generally no need to access the CPU registers from a program written in a high-level language: high-level languages, like C, Pascal, etc. where precisely invented in order to abstract the underlying machine and render a program more machine-independent.

i suspect you are trying to perform something but have no clue how to use a conventional way to do it.

many access to the registers are hidden in higher-level constructs or in system or library calls which lets you avoid coding the "dirty-part". tell us more about what you want to do and we may suggest you an alternative.

Adrien Plisson
Nope, I actually know exactly what I`m doing. But thanks for helping me out. :)
C4theWin
-1 : Response is irrelevant to the question.
Sparky
+2  A: 

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.

jweyrich
Thanks a lot for the code examples. Exact piece of information I needed. ;)
C4theWin
A: 

You can of course. "MinGW" (gcc) allows (as other compilers) inline assembly, as other answers already show. Which assembly, it depends on the cpu of your system (prob. 99.99% that it is x86). This makes however your program not portable on other processors (not that bad if you know what you are doing and why).

The relevant page talking about assembly for gcc is here and here, and if you want, also here. Don't forget that it can't be specific since it is architecture-dependent (gcc can compile for several cpus)

ShinTakezou
+1  A: 

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.
PauliL