我正在学习Linux上的asm (noobuntu10.04)我得到了以下代码:http://asm.sourceforge.net/intro/hello.html
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string这是一个简单的hello world。在Linux +上运行,直接(显然)调用内核。有人能解释一下到底是怎么回事吗?我认为它读取eax & ebx处理器寄存器中的整数& ecx、edx数据,并在内核被调用时定义系统调用。如果是这样,当调用int0x80时,不同的整数组合是否定义了不同的系统调用?
我不擅长手册页,但我已经阅读了我能找到的所有相关的手册页,有没有手册页告诉我什么组合定义了什么syscall?
任何帮助都是非常感谢的。一行一行的解释将是令人惊讶的…先进的-Thanks杰里米
发布于 2010-07-31 07:58:11
当您调用int 0x80时,内核会查看eax寄存器的值来确定要调用的函数(这是"syscall号“)。根据这个数字,其余的寄存器被解释为表示特定的事物。sys_write调用需要按如下方式设置寄存器:
eax包含4ebx包含文件descriptorecx包含数据的地址到writeedx包含字节数有关更多详细信息,请参阅Linux System Calls。
发布于 2010-07-31 08:04:39
section .text
global _start ;must be declared for linker (ld)这只是标题材料,汇编程序的“文本”部分只是机器指令(相对于数据、只读数据和BSS部分)。global行类似于说_start函数是“公共的”。
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel从注释中我们知道我们正在查看sys_write函数,因此我们可以使用man 2 write来获取详细信息。C原型提供了以下参数:fd、*buf和count。从%ebx开始,我们看到它们是匹配的(%ebx = fd,%ecx =要写入的字符串,%edx =字符串的长度)。然后,由于我们是一个用户进程,我们必须请求内核执行输出。这是通过SYSCALL接口完成的,write()函数(显然)被赋予了数字4。INT 0x80是一个软件中断,它调用Linux内核的SYSCALL例程。
您可以在Linux头文件中找到所有syscall的实际数量(假设您已经安装了它们)。在我的系统上,我检查了指向/usr/include/asm/unistd.h的/usr/include/sys/syscall.h,然后选择了/usr/include/asm-i386/unistd.h。我明白了,#define __NR_write 4。
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel与上一段的最后两行一样,这只是加载syscall id并执行软件中断以退出程序(删除它的内存映射和清理)。
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string这是数据部分,它只是描述了我们在程序中使用的变量。
发布于 2010-07-31 08:00:07
有太多的系统调用要求每个系统调用都有不同的汇编语言指令。
取而代之的是,调用陷阱指令。eax的值确定将调用哪个系统调用。其他寄存器是系统调用的参数。
系统调用列在内核内部。
https://stackoverflow.com/questions/3376129
复制相似问题