2
int main()
{
    __asm__("movl $0x1,%%eax;
            movl $0x0,%%ebx;
            int $0x80;
            ":::"eax","ebx");
}

Linux で exit() の動作をシミュレートしようとしています。しかし、最近の Linux では、いくつかの終了ハンドラーが exit() の後に呼び出されるため、これを行うのは非常に難しいと思います。だから私は exit() の古いバージョンを書きます。おそらく10年前に、いくつかのコードでそれを見つけることができます. gccでコンパイルします。

gcc -o exit exit.c

そして、それは私にこれらのメッセージを与えます。

exit.c: In function ‘main’:
exit.c:3:13: warning: missing terminating " character [enabled by default]
exit.c:3:5: error: missing terminating " character
exit.c:4:13: error: expected string literal before ‘movl’
exit.c:6:27: warning: missing terminating " character [enabled by default]
exit.c:6:13: error: missing terminating " character

コードを注意深く調べましたが、コードが間違っているとは思いません。それで、それは何ですか?

4

2 に答える 2

5

引用文字列内に改行を埋め込むことはできません

// bad
"two
lines"

のように書き換えます。

// good
"two\n"
"lines"

プリプロセッサは文字列をシームレスに結合します。

"two\nlines"
于 2012-04-18T08:38:39.123 に答える
1

From what I remember of inline assembly, you probably need to terminate each line with \n\t after the semicolon or something like that.

Clarification:
It is not enough to terminate each line in inline assembly with ;. Inline assembly is fed directly to the assembler by gcc as a string. If you do not terminate each line with \n, the assembler will get strings like movl $0x1,%%eax;movl $0x0,%%ebx; which it will not be able to parse. You probably do not need to use \n\t any longer since gcc can handle assembly files where commands are not preceded by \t.

于 2012-04-18T08:35:52.553 に答える