内核如何知道"int 0x80"和"int 0x80"之间的区别?和"int x" [英] How kernel know the difference between "int 0x80" and "int x"
问题描述
int 0x80是一个系统调用,它也是128的六进制形式. 为什么内核使用int 0x80作为中断,而当我声明int x时,他知道这只是一个名为x的整数,反之亦然?
int 0x80 is a system call, it's also 128 in hexa. why kernel use int 0x80 as interrupt and when i declare int x he knows it's just an integer named x and vice versa ?
推荐答案
您似乎对C语言和汇编语言之间的差异感到困惑.两种都是编程语言,(如今)都接受0xNNNN
表示法以十六进制形式写数字,并且通常可以通过某种方式在C程序中嵌入汇编语言的小片段,但它们是不同的语言 .关键字int
在C语言中的含义与在(x86)汇编语言中的含义完全不同.
You appear to be confused about the difference between C and assembly language. Both are programming languages, (nowadays) both accept the 0xNNNN
notation for writing numbers in hexadecimal, and there's usually some way to embed tiny snippets of assembly language in a C program, but they are different languages. The keyword int
means something completely different in C than it does in (x86) assembly language.
对于C编译器,int
始终和仅意味着声明一些涉及整数的内容,在任何情况下都不能立即跟随int
数字文字. int 0x80
(或int 128
或int 23
或其他类似的东西)在C语言中始终是语法错误.
To a C compiler, int
always and only means to declare something involving an integer, and there is no situation where you can immediately follow int
with a numeric literal. int 0x80
(or int 128
, or int 23
, or anything else of the sort) is always a syntax error in C.
对于x86汇编程序,int
始终和仅意味着为INTerrupt指令生成机器代码,并且必须为单个数字文字 立即关注. int x;
始终是x86汇编语言中的语法错误.
To an x86 assembler, int
always and only means to generate machine code for the INTerrupt instruction, and a single numeric literal must immediately follow. int x;
is always a syntax error in x86 assembly language.
一个明显的后续问题:如果C编译器无法将int
识别为INTerrupt指令,那么C程序(针对x86编译)如何进行系统调用?这个问题有四个补充答案:
Obvious follow-up question: If a C compiler doesn't recognize int
as the INTerrupt instruction, how does a C program (compiled for x86) make system calls? There are four complementary answers to this question:
-
在大多数情况下,在C程序中,您不会直接进行系统调用.相反,您可以在C库中调用为您执行此操作的函数.据C编译器了解,在处理程序时,
open
(例如)与任何其他外部函数没有什么不同.因此,不需要 生成int
指令.它只是call open
.
Most of the time, in a C program, you do not make system calls directly. Instead, you call functions in the C library that do it for you. When processing your program, as far as the C compiler knows,
open
(for instance) is no different than any other external function. So it doesn't need to generate anint
instruction. It just doescall open
.
但是C库只是别人为您编写的C语言,不是吗?但是,如果您反汇编open
的实现,则确实会看到int
指令(或者也许是syscall
或sysenter
).编写C库的人是如何做到的? 他们用汇编语言而不是C编写该函数.或他们使用该技术将汇编语言的片段嵌入到C程序中,这使我们进入...
But the C library is just more C that someone else wrote for you, isn't it? Yet, if you disassemble the implementation of open
, you will indeed see an int
instruction (or maybe syscall
or sysenter
instead). How did the people who wrote the C library do that? They wrote that function in assembly language, not in C. Or they used that technique for embedding snippets of assembly language in a C program, which brings us to ...
该如何工作?这不是意味着C编译器有时需要将int
理解为汇编助记符吗?不必要.让我们看一下用于插入程序集的GCC语法-这可能是针对x86/32/Linux的open
的实现:
How does that work? Doesn't that mean the C compiler does need to understand int
as an assembly mnemonic sometimes? Not necessarily. Let's look at the GCC syntax for inserting assembly—this could be an implementation of open
for x86/32/Linux:
int open(const char *path, int flags, mode_t mode)
{
int ret;
asm ("int 0x80"
: "=a" (ret)
: "0" (SYS_open), "d" (path), "c" (flags), "D" (mode));
if (ret >= 0) return ret;
return __set_errno(ret);
}
您不需要理解其中的大部分内容:这个问题的目的很重要,是的,它说了int 0x80
,但是它在字符串文字中表示了 .编译器将逐字复制该字符串文字的内容到生成的汇编语言文件中,然后将其馈送到汇编器.它不需要知道它的意思.那是汇编程序的工作.
You don't need to understand the bulk of that: the important thing for purpose of this question is, yes, it says int 0x80
, but it says it inside a string literal. The compiler will copy the contents of that string literal, verbatim, into the generated assembly-language file that it will then feed to the assembler. It doesn't need to know what it means. That's the assembler's job.
更一般地说,有很多词在C语言中表示一件事,而在汇编语言中则表示完全不同的事. C编译器产生汇编语言,因此它必须知道"这些单词的两个含义,对吗?可以,但是不会混淆它们,因为它们总是在单独的上下文中使用. "add"是C编译器知道如何使用的程序集助记符,并不意味着在C程序中命名变量"add"有任何问题,即使在该程序中使用了"add"指令也是如此. /p>
More generally, there are lots of words that mean one thing in C and a completely different thing in assembly language. A C compiler produces assembly language, so it has to "know" both of the meanings of those words, right? It does, but it does not confuse them, because they are always used in separate contexts. "add" being an assembly mnemonic that the C compiler knows how to use, does not mean that there is any problem with naming a variable "add" in a C program, even if the "add" instruction gets used in that program.
这篇关于内核如何知道"int 0x80"和"int 0x80"之间的区别?和"int x"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!