%ifndef FILE_INC %define FILE_INC %define FBUF_DEFAULT_SIZE 0 %define FBUF_ALLOC_SIZE 1024*32 %ifndef SYS_INC %include "sys.inc" %endif %ifndef ALLOC_INC %include "alloc.inc" %endif %ifndef ZSTRING_INC %include "zstring.inc" %endif struc FileData fd_fname: resq 1 fd_buffer: resq 1 fd_size: resq 1 fd_fd: resq 1 endstruc %macro make_fbuffer 3 ;; %1 = File Name ;; %2 = Buffer Name ;; %3 = Buffer Size section .data %2_fname: db %1,0 %2_buffer: times %3 db 0 db 0 %2_bufferLen: equ $ - %2_buffer section .bss %2_fd: resq 1 section .data align 8 %2_filedata: istruc FileData at fd_fname, dq %2_fname at fd_buffer, dq %2_buffer at fd_size, dq %2_bufferLen at fd_fd, dq %2_fd iend %endm %macro fopen 1 mov rax, SYS_OPEN lea rdi, [rel %1_fname] xor rsi, rsi ; READ-ONLY syscall mov [rel %1_fd], rax %endm %macro flen 1 mov rax, SYS_LSEEK mov rdi, [rel %1_fd] mov rsi, 0 mov rdx, 2 syscall %endm %macro fbegin 1 mov rax, SYS_LSEEK mov rdi, [rel %1_fd] mov rsi, 0 mov rdx, 0 syscall %endm %macro fread 1 mov rax, SYS_READ mov rdi, [rel %1_fd] lea rsi, [rel %1_buffer] mov rdx, %1_bufferLen syscall %endm %macro fclose 1 mov rax, SYS_CLOSE mov rdi, [rel %1_fd] syscall %endm ffopen: push rbp mov rbp, rsp ;; RAX = filedata push rax mov rdi, [rax + fd_fname] mov rax, SYS_OPEN xor rsi, rsi syscall pop rdx mov rcx, rdx mov rdx, [rdx + fd_fd] mov [rdx], rax mov rax, rcx ;; mov rsp, rbp pop rbp ret ffread: mov rdi, [rax + fd_fd] mov rdi, [rdi] mov rsi, [rax + fd_buffer] mov rdx, [rax + fd_size] mov rax, SYS_READ syscall ;; ret print_filedata: push rbp mov rbp, rsp ;; RAX = filedata push rax mov rax, [rax + fd_buffer] call print_zstring pop rax mov rsp, rbp pop rbp ret %endif