syscalls: implement read syscall
This commit is contained in:
+36
-24
@@ -1,33 +1,45 @@
|
||||
section .data
|
||||
file_path db '/ebalo/huy.txt', 0 ; Specific file path, null-terminated
|
||||
message db 'Hello, World from KatauOS!', 10, 0 ; Message to write, with newline
|
||||
message_len equ $ - message - 1 ; Length of message (14 bytes, excluding null)
|
||||
file_path db '/ebalo/huy.txt', 0 ; File path
|
||||
message db 'Hello, World from KatauOS!', 10, 0 ; Message with newline
|
||||
message_len equ $ - message - 1 ; Length excluding null terminator
|
||||
|
||||
section .bss
|
||||
buffer resb 256 ; Buffer for reading file content
|
||||
read_count resd 1 ; Store bytes read count
|
||||
|
||||
section .text
|
||||
global _start
|
||||
|
||||
_start:
|
||||
; Open the file
|
||||
mov eax, 5 ; System call number for sys_open
|
||||
mov ebx, file_path ; Pointer to file path
|
||||
mov ecx, 65 ; Flags: O_WRONLY (1) | O_CREAT (64)
|
||||
mov edx, 0x1A4 ; Mode: 0644 (rw-r--r--)
|
||||
int 0x80 ; Invoke system call
|
||||
mov edi, eax ; Save file descriptor in edi
|
||||
; Reopen file for reading
|
||||
mov eax, 5 ; sys_open
|
||||
mov ebx, file_path
|
||||
mov ecx, 0 ; O_RDONLY
|
||||
mov edx, 0 ; Mode (unused)
|
||||
int 0x80
|
||||
mov edi, eax ; Save new descriptor
|
||||
|
||||
; Write to the file
|
||||
mov eax, 4 ; System call number for sys_write
|
||||
mov ebx, edi ; File descriptor
|
||||
mov ecx, message ; Pointer to message
|
||||
mov edx, message_len ; Number of bytes to write
|
||||
int 0x80 ; Invoke system call
|
||||
; Read file content
|
||||
mov eax, 3 ; sys_read
|
||||
mov ebx, edi
|
||||
mov ecx, buffer
|
||||
mov edx, 256
|
||||
int 0x80
|
||||
mov [read_count], eax ; Save bytes read count
|
||||
|
||||
; Close the file
|
||||
mov eax, 6 ; System call number for sys_close
|
||||
mov ebx, edi ; File descriptor
|
||||
int 0x80 ; Invoke system call
|
||||
; Write to stdout
|
||||
mov eax, 4 ; sys_write
|
||||
mov ebx, 1 ; stdout
|
||||
mov ecx, buffer
|
||||
mov edx, [read_count] ; Use actual bytes read
|
||||
int 0x80
|
||||
|
||||
; Exit the program
|
||||
mov eax, 1 ; System call number for sys_exit
|
||||
xor ebx, ebx ; Return status 0
|
||||
int 0x80 ; Invoke system call
|
||||
; Close file
|
||||
mov eax, 6 ; sys_close
|
||||
mov ebx, edi
|
||||
int 0x80
|
||||
|
||||
; Exit
|
||||
mov eax, 1 ; sys_exit
|
||||
xor ebx, ebx ; Return 0
|
||||
int 0x80
|
||||
|
||||
Reference in New Issue
Block a user