_nosay 发表于 2016-06-29 15:34

__asm__(...);必须作为函数体吗?

本帖最后由 _nosay 于 2016-06-29 15:42 编辑

自己写的测试程序:
/* test.c */
#include <stdio.h>

int n;

// 这样编译不过,外面加上void fun() {}就可以编译(只关心编译,不关心逻辑)。。
//void fun()
//{
__asm__(
      "add_n:\n\t"
      "addl 1, %%ecx\n\t"
      :"=&c"(n)
      :"0"(10)
      :
      );
//}

int main()
{
__asm__(
      "jmp add_n\n\t"
      );

      printf("n=%d\n", n);

      return 0;
}
Linux2.4.0代码:
/* include/asm-i386/hw_irq.h */
#define BUILD_COMMON_IRQ() \
asmlinkage void call_do_IRQ(void); \
__asm__( \
        "\n" __ALIGN_STR"\n" \
        "common_interrupt:\n\t" \
        SAVE_ALL \
        "pushl $ret_from_intr\n\t" \
        SYMBOL_NAME_STR(call_do_IRQ)":\n\t" \
        "jmp "SYMBOL_NAME_STR(do_IRQ));

/* arch/i386/kernel/i8259.c */
BUILD_COMMON_IRQ()
i8259.c不是将__asm__(...);放在函数外了吗?

nswcfd 发表于 2016-06-29 18:42

不是吧?echo 'asm("nop");' | gcc -c -x c -
可以正常编译的?

_nosay 发表于 2016-06-30 06:50

本帖最后由 _nosay 于 2016-06-30 08:59 编辑

回复 2# nswcfd

那可能是只能有指令部,test.c总是在第一个:的地方报错。

xxx@xxx:~/test/5$ gcc test.c -g -Wall
test.c:10:2: error: expected ‘)’ before ‘:’ token
:"=&c"(n)
^

nswcfd 发表于 2016-06-30 17:33

本帖最后由 nswcfd 于 2016-06-30 17:34 编辑

好像是的,换成asm( "nop" : )就会报错。(使用扩展汇编语法)

nswcfd 发表于 2016-07-14 09:57

找到“理论基础”了

https://gcc.gnu.org/onlinedocs/gcc/Basic-Asm.html#Basic-Asm

Using extended asm (see Extended Asm) typically produces smaller, safer, and more efficient code, and in most cases it is a better solution than basic asm. However, there are two situations where only basic asm can be used:

* Extended asm statements have to be inside a C function, so to write inline assembly language at file scope (“top-level”), outside of C functions, you must use basic asm. You can use this technique to emit assembler directives, define assembly language macros that can be invoked elsewhere in the file, or write entire functions in assembly language.
* Functions declared with the naked attribute also require basic asm (see Function Attributes).

nswcfd 发表于 2016-07-14 09:58

注,naked属性,好像x86没有用

Use this attribute on the ARM, AVR, IP2K, RX and SPU ports to indicate that the specified function does not need prologue/epilogue sequences generated by the compiler

http://stackoverflow.com/questions/2716884/using-the-naked-attribute-for-functions-in-gcc

_nosay 发表于 2016-07-14 11:39

回复 5# nswcfd

soga:em09: !你太强大了,怎么查到的?

captivated 发表于 2016-07-14 13:21

。。。
这还用查么。。。

拿 C 来说,类似于 y = x + 3; 这样的语句,能放全局吗?

既然 __asm__ 是内嵌关键字,它首先要遵从 C 语言的约束啊。
如果要纯粹的汇编也非常简单,另起一个汇编源文件,加个符号想怎么写怎么写。

captivated 发表于 2016-07-14 13:22

脚本才是那种到处乱扔花花草草不用管的语言。

_nosay 发表于 2016-07-14 13:50

回复 8# captivated

{:yxh31:}
页: [1] 2
查看完整版本: __asm__(...);必须作为函数体吗?