read-print file

This commit is contained in:
janis 2025-10-16 21:33:58 +02:00
parent 7b413d540c
commit 703a8ba968
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -10,6 +10,8 @@ section .data
file_error_msg_len equ $ - file_error_msg
error_msg db "Error: "
error_msg_len equ $ - error_msg
buffer_size equ 1024
buffer times buffer_size db 0
section .text
global _start
@ -30,14 +32,37 @@ _start:
; open file for reading
mov rdx, rax ; filename pointer
call fopen_read
mov rax, 42
; exit with success
mov rdx, 0
call exit
.read_loop:
mov r9, rax ; file descriptor
mov rax, 0 ; syscall: read
mov rdi, r9 ; fd
lea rsi, [buffer] ; buffer
mov rdx, buffer_size ; size
syscall
cmp rax, 0 ; check for EOF
jle .exit ; if rax <= 0, exit loop
mov rcx, rax ; number of bytes read
; write to stdout for now
mov rax, 1 ; syscall: write
mov rdi, 1 ; fd: stdout
lea rsi, [buffer] ; buffer
mov rdx, rcx ; len: bytes read
syscall
jmp .read_loop
.no_filename:
call panic
.exit:
call exit
;; ==============================
;; Helper functions
;; ==============================
;; Abort the program with a default panic message
panic:
mov rdx, panic_msg
mov rcx, panic_msg_len
@ -54,10 +79,6 @@ exit:
mov rdi, rdx
syscall
;; ==============================
;; Helper functions
;; ==============================
;; Writes a string to stderr:
;; rdx: pointer to string
;; rcx: length of string
@ -147,4 +168,4 @@ fopen_read:
pop rsi
add rsp, rsi ; dealloc
ret
call panic