-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinux_syscalls.asm
More file actions
138 lines (112 loc) · 4.09 KB
/
linux_syscalls.asm
File metadata and controls
138 lines (112 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;===============================================
; Linux x86 System Call Reference (32-bit)
; To use:
; 1) Set input registers (see each function)
; 2) call function_name
; 3) Check return value (if any) in EAX/AL
;===============================================
;-------------------------------
; 1) Print a Single Character
;-------------------------------
section .data
; (No initialized data needed for this example)
section .bss
char resb 1 ; Reserve 1 byte of uninitialized memory for character storage
section .text
global _start ; Declare _start as the entry point for the linker
_start:
mov al, 'X' ; Load ASCII 'X' into AL register (lower 8 bits of EAX)
call _print_char ; Call the character printing function
; Exit the program
mov eax, 1 ; sys_exit system call number
mov ebx, 0 ; Exit status code (0 = success)
int 0x80 ; Invoke kernel to execute system call
_print_char:
mov [char], al ; Store the character from AL into memory at [char]
mov eax, 4 ; sys_write system call number
mov ebx, 1 ; File descriptor 1 (stdout = screen output)
mov ecx, char ; Pointer to the character to print
mov edx, 1 ; Number of bytes to print (just 1 character)
int 0x80 ; Invoke kernel to execute system call
ret ; Return from function to caller
;-------------------------------
; 2) Print a String
;-------------------------------
section .data
msg db "Hello World!", 0xA ; Define string with newline (0xA)
len equ $ - msg ; Calculate string length (current pos - start)
section .text
global _start
_start:
mov esi, msg ; Load address of string into ESI register
mov edx, len ; Load pre-calculated string length into EDX
call _print_string ; Call the string printing function
; Exit
mov eax, 1
mov ebx, 0
int 0x80
_print_string:
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout
mov ecx, esi ; Move string address from ESI to ECX (where sys_write expects it)
int 0x80 ; Execute system call
ret ; Return to caller
;-------------------------------
; 3) Read character from keyboard
;-------------------------------
section .bss
char resb 1 ; Reserve 1 byte for input character storage
section .text
global _start
_start:
call _read_char ; Call function to read character (result will be in AL)
; Exit
mov eax, 1
mov ebx, 0
int 0x80
_read_char:
mov eax, 3 ; sys_read system call
mov ebx, 0 ; stdin (file descriptor 0 = keyboard input)
mov ecx, char ; Buffer to store the read character
mov edx, 1 ; Read exactly 1 byte
int 0x80 ; Execute system call
mov al, [char] ; Move the read character from memory to AL register
ret ; Return with character in AL
;-------------------------------
; 4) Open a file
;-------------------------------
section .data
filename db "test.txt", 0 ; Null-terminated filename string
section .text
global _start
_start:
mov ebx, filename ; Pointer to filename string
mov ecx, 0 ; Flags (0 = O_RDONLY = read-only)
call _open_file ; Call function (returns file descriptor in EAX)
; Exit
mov eax, 1
mov ebx, 0
int 0x80
_open_file:
mov eax, 5 ; sys_open system call
int 0x80 ; Execute (returns file descriptor in EAX or error code)
ret ; Return to caller with result in EAX
;-------------------------------
; 5) Create a File
;-------------------------------
section .data
filename db "newfile.txt", 0 ; Null-terminated filename
section .text
global _start
_start:
mov ebx, filename ; Pointer to filename string
mov ecx, 0644o ; File permissions (octal 644 = rw-r--r--)
call _create_file ; Call function (returns file descriptor in EAX)
; Exit
mov eax, 1
mov ebx, 0
int 0x80
_create_file:
mov eax, 8 ; sys_creat system call
int 0x80 ; Execute (returns file descriptor in EAX)
ret ; Return to caller