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

后端 未结 7 1295
猫巷女王i
猫巷女王i 2020-12-14 11:55

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

7条回答
  •  半阙折子戏
    2020-12-14 12:36

    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;
    }
    

提交回复
热议问题