汇编 - 系统调用
系统调用是用户空间和内核空间之间接口的API。我们已经使用了系统调用。sys_write和sys_exit,分别用于写入屏幕和退出程序。
Linux 系统调用
您可以在汇编程序中使用 Linux 系统调用。要在程序中使用 Linux 系统调用,您需要执行以下步骤 -
- 将系统调用号放入EAX寄存器中。
- 将系统调用的参数存储在寄存器 EBX、ECX 等中。
- 调用相关中断(80h)。
- 结果通常返回到 EAX 寄存器中。
有六个寄存器存储所使用的系统调用的参数。它们是 EBX、ECX、EDX、ESI、EDI 和 EBP。这些寄存器采用连续的参数,从 EBX 寄存器开始。如果有超过六个参数,则第一个参数的内存位置存储在 EBX 寄存器中。
以下代码片段显示了系统调用 sys_exit 的使用 -
mov eax,1 ; system call number (sys_exit) int 0x80 ; call kernel
以下代码片段显示了系统调用 sys_write 的使用 -
mov edx,4 ; 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
所有系统调用及其编号(在调用 int 80h 之前放入 EAX 中的值)都列在/usr/include/asm/unistd.h中。
下表显示了本教程中使用的一些系统调用 -
| %eax | 姓名 | %ebx | %ecx | %edx | %esx | %edi |
|---|---|---|---|---|---|---|
| 1 | 系统退出 | 整数 | - | - | - | - |
| 2 | 系统分叉 | 结构体pt_regs | - | - | - | - |
| 3 | 系统读取 | 无符号整数 | 字符* | 尺寸_t | - | - |
| 4 | 系统写入 | 无符号整数 | 常量字符* | 尺寸_t | - | - |
| 5 | 系统打开 | 常量字符* | 整数 | 整数 | - | - |
| 6 | 系统关闭 | 无符号整数 | - | - | - | - |
例子
以下示例从键盘读取数字并将其显示在屏幕上 -
section .data ;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a number
lenUserMsg equ $-userMsg ;The length of the message
dispMsg db 'You have entered: '
lenDispMsg equ $-dispMsg
section .bss ;Uninitialized data
num resb 5
section .text ;Code Segment
global _start
_start: ;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
;Output the message 'The entered number is: '
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
; Exit code
mov eax, 1
mov ebx, 0
int 80h
当上面的代码被编译并执行时,它会产生以下结果 -
Please enter a number: 1234 You have entered:1234