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
|
%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
|