中断
硬中断:键盘鼠标这些输入都会触发中断。
软中断:系统调用产生的中断,常说的80中断(新版的sysenter原语),在我们app中调用某些指令的时候,就会触发中断,比如read write。
软中断的过程
java读网络 – jvm read() – c库read() - > 内核空间 -> system_call() (系统调用处理程序)-> sys_read()
硬件层面
通过ax寄存器填入调用号
参数通过bx cx dx si di传入内核
返回值通过ax返回
汇编看中断
centos 安装汇编
第一步:先判断系统是否已经安装了nasm--------------->打开终端,执行whereis nasm ;如果显示nasm: /usr/bin/nasm ,则已经安装;如果只显示nasm: ,则未安装。
第二步:yum install build-essential nasm
下面展示一些 内联代码片
。
;hello.asm
;write(int fd, const void *buffer, size_t nbytes)
;fd 文件描述符 file descriptor - linux下一切皆文件
section data;数据段申明
msg db "Hello", 0xA;要输出的字符串+ 换行
len equ $ - msg;字串长度
section .text;代码段申明
global _start;指定入口函数
_start:;在屏幕上显示一个字符串
mov edx, len;参数三:字符串长度
mov ecx, msg;参数2 要显示的字符串
mov ebx, 1 ;文件描述符1 std_out
mov eax, 4 ;write函数系统调用号 4
int 0x80;调用内核功能
mov ebx, 0;参数1退出代码
mov eax, 1 ;exit函数系统调用号
int 0x80;调用内核功能
编译:nasm -f elf hello.asm -o hello.o
链接:ld -m elf_i386 -o hello hello.o
效果演示:hello hello.asm hello.o
hello是链接结果;hellp.asm是汇编代码;hello.o是编译结果;
调用:
./hello
输出了“hello字段”
hello代码讲解
write(int fd, const void *buffer, size_t nbytes)
hello里面最终要调用的函数就是write函数;
{ mov edx, len;参数三:字符串长度
mov ecx, msg;参数2 要显示的字符串
mov ebx, 1 ;文件描述符1 std_out
mov eax, 4 ;write函数系统调用号 4
}
mov eax 4 调用write函数;
传入第一个参数fd =1 到ebx寄存器;
传入第二个参数msg到ecx寄存器;
传入第三个参数len 到edx寄存器;
{int 0x80}
调用write函数
阻塞
调用write的时候 代码还能继续往下走就是非阻塞。
用户态
一个程序要么处于内核态,要么处于用户态