การประกอบ - การโทรระบบ
การเรียกระบบคือ API สำหรับอินเทอร์เฟซระหว่างพื้นที่ผู้ใช้และพื้นที่เคอร์เนล เราได้ใช้การโทรของระบบแล้ว sys_write และ sys_exit สำหรับเขียนลงในหน้าจอและออกจากโปรแกรมตามลำดับ
การเรียกระบบ Linux
คุณสามารถใช้การเรียกระบบ Linux ในโปรแกรมประกอบของคุณ คุณต้องทำตามขั้นตอนต่อไปนี้เพื่อใช้การเรียกระบบ Linux ในโปรแกรมของคุณ -
- ใส่หมายเลขการโทรของระบบในทะเบียน EAX
- จัดเก็บอาร์กิวเมนต์สำหรับการเรียกระบบในการลงทะเบียน EBX, ECX และอื่น ๆ
- โทรไปที่การขัดจังหวะที่เกี่ยวข้อง (80 ชม.)
- โดยปกติผลลัพธ์จะถูกส่งกลับในทะเบียน 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
syscalls ทั้งหมดแสดงอยู่ใน/usr/include/asm/unistd.hพร้อมกับตัวเลข (ค่าที่จะใส่ใน EAX ก่อนที่คุณจะเรียก int 80h)
ตารางต่อไปนี้แสดงการเรียกระบบบางส่วนที่ใช้ในบทช่วยสอนนี้ -
% eax | ชื่อ | % ebx | % ecx | % edx | % esx | % edi |
---|---|---|---|---|---|---|
1 | sys_exit | int | - | - | - | - |
2 | sys_fork | โครงสร้าง pt_regs | - | - | - | - |
3 | sys_read | int ที่ไม่ได้ลงนาม | ถ่าน * | size_t | - | - |
4 | sys_write | int ที่ไม่ได้ลงนาม | const ถ่าน * | size_t | - | - |
5 | sys_open | const ถ่าน * | int | int | - | - |
6 | sys_close | int ที่ไม่ได้ลงนาม | - | - | - | - |
ตัวอย่าง
ตัวอย่างต่อไปนี้อ่านตัวเลขจากแป้นพิมพ์และแสดงบนหน้าจอ -
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