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

2020-02-08 17:53发布

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!

7条回答
冷血范
2楼-- · 2020-02-08 18:27

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.
查看更多
你好瞎i
3楼-- · 2020-02-08 18:27

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)

查看更多
Summer. ? 凉城
4楼-- · 2020-02-08 18:34

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.

查看更多
【Aperson】
5楼-- · 2020-02-08 18:37

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;
}
查看更多
乱世女痞
6楼-- · 2020-02-08 18:42

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.

查看更多
ら.Afraid
7楼-- · 2020-02-08 18:43

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

asm (
    "movl $0, %%ebx;"
    "movl $1, %%eax;"
); 
查看更多
登录 后发表回答