aboutsummaryrefslogtreecommitdiff
path: root/file.inc
blob: 3415c4f9795ba9ee49447eb3c6943717b1d67d05 (plain)
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
%ifndef FILE_INC
%define FILE_INC
%define FBUF_DEFAULT_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
fname:	resq	1
buffer:	resq	1
size:	resq	1
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
%2_bufferLen:	equ $ - %2_buffer
	db 0
	section .bss
%2_fd:	resq 1
	section .data
%2_filedata:
	istruc FileData
	at	fname,	dq	%2_fname
	at	buffer,	dq	%2_buffer
	at	size,	dq	%2_bufferLen
	at	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 + fname]
	mov	rax,	SYS_OPEN
	xor	rsi,	rsi
	syscall
	pop	rdx
	mov	rcx,	rdx
	mov	rdx,	[rdx + fd]
	mov	[rdx],	rax
	mov	rax,	rcx
	;;
	mov	rsp,	rbp
	pop	rbp
	ret

print_filedata:
	push	rbp
	mov	rbp,	rsp
	;; RAX = filedata
	push	rax
	mov	rax,	[rax + buffer]
	call	print_zstring
	pop	rax
	mov	rsp,	rbp
	pop	rbp
	ret

%endif