-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSFUNC.ASM
299 lines (251 loc) · 5.48 KB
/
OSFUNC.ASM
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
; -----------------------------------------------------------
; Small C Compiler for TRDOS 386 (v2.0.9 and later)
; Erdogan Tan - 2024
; Beginning: 05/09/2024
; Last Update: 23/09/2024
; -----------------------------------------------------------
; Derived from 'osfunc.asm' file of KolibriOS SCC source code
; 2024
; 20/08/2024 ; TRDOS 386 v2.0.9
; 29/04/2016
_ver equ 0
_exit equ 1
_fork equ 2
_read equ 3
_write equ 4
_open equ 5
_close equ 6
_wait equ 7
_creat equ 8
_link equ 9
_unlink equ 10
_exec equ 11
_chdir equ 12
_time equ 13
_mkdir equ 14
_chmod equ 15
_chown equ 16
_break equ 17
_stat equ 18
_seek equ 19
_tell equ 20
_mount equ 21
_umount equ 22
_setuid equ 23
_getuid equ 24
_stime equ 25
_quit equ 26
_intr equ 27
_fstat equ 28
_emt equ 29
_mdate equ 30
_video equ 31
_audio equ 32
_timer equ 33
_sleep equ 34
_msg equ 35
_geterr equ 36
_fpsave equ 37
_pri equ 38
_rele equ 39
_fff equ 40
_fnf equ 41
_alloc equ 42
_dalloc equ 43
_calbac equ 44
_dma equ 45
_stdio equ 46
; 12/09/2024 ('sys' macro in FASM format)
macro sys op1,op2,op3,op4
{
if op4 eq
else
mov edx, op4
end if
if op3 eq
else
mov ecx, op3
end if
if op2 eq
else
mov ebx, op2
end if
mov eax, op1
int 40h
}
; -----------------------------
;
; OS function implementation
; SmallC for KolibriOS
;
init_osfunc:
; 05/09/2024 - TRDOS 386 v2.0.9
; nothing to do for now
retn
;Main OS functions
_OS_fopen:
;Implement "fopen"
;esp+4 - mode
;esp+8 - file name
mov eax, [esp+4] ; file open mode
mov ebx, [esp+8] ; file name
xor ecx, ecx ; 0 ; open for read
cmp byte [eax], 'r'
je short open_read
inc ecx ; 1 ; open for write
cmp byte [eax], 'w'
je short open_write
;bad mode
file_notfound:
access_error:
xor eax, eax ; 0 ; invalid open mode
retn
open_read:
;Open for read
open_write:
;Open for write
sys _open
;jc short file_notfound ; return 0
jnc short open_create_ok ; 18/09/2024
; 18/09/2024
open_create:
or ecx, ecx
jz short file_notfound ; return 0
; ecx = 1 -> open for write
cmp eax, 2 ; file not found ?
jne short access_error
dec ecx ; ecx = 0 -> ordinary file
; ebx = file name
sys _creat
jc short access_error ; create_err
open_create_ok:
; eax = file handle
; 15/09/2024
inc eax ; file handle (1 based)
.close_ok:
close_ok: ; 12/09/2024
retn
_OS_fclose:
;Close file
;esp+4 - file handle
mov ebx, [esp+4] ; file handle (1 based)
dec ebx ; 0 based
sys _close
jnc short close_ok ; eax = 0
.close_err:
read_err: ; 12/09/2024
mov eax, -1 ; BAD
retn
_OS_fgetc:
;Load char from file
;esp+4 - input file
mov eax, [esp+4] ; file handle, 1 based
; -1 -> STDIN
inc eax ; -1 -> 0
jz short _OS_getc ; 16/09/2024
dec eax ; 1 based
dec eax ; 0 based file handle
mov byte [character], 0
sys _read, eax, character, 1
jc short read_err
and eax,eax
jnz short _OS_fgetc_ok
dec eax
; eax = -1 ; EOF
retn
_OS_fgetc_ok:
mov al, [character]
retn
_OS_getc:
sys _stdio, 0 ; read STDIN (wait keypress)
; eax (al) = character (ascii) code
retn
character: db 0
_OS_fputc:
;Save char to file
;esp+4 - output file
;esp+8 - char to write
mov ecx, [esp+8]
mov [character], cl
mov eax, [esp+4] ; file handle, 1 based
; -1 -> STDOUT/STDERR
inc eax ; -1 -> 0
jz short _OS_putc_@ ; 16/09/2024
dec eax ; 1 based
dec eax ; 0 based file handle
sys _write, eax, character, 1
retn
_OS_putc:
;Save char to file
;esp+4 - char to write
mov ecx, [esp+4]
_OS_putc_@:
; cl = character to be written
sys _stdio, 2 ; write to STDOUT (display char)
calloc_ok: ; 13/09/2024
retn
_OS_calloc:
;Alloc memory
; TRDOS 386 v2 feature
; default u.break (bss) address is file size for PRG files
; temporary - 23/09/2024
;mov eax,[ubreak]
;and eax,eax
;jnz short ubreak_ready
sys _break, -1 ; get u.break address
jc short calloc_err
ubreak_ready: ; temporary - 23/09/2024
; eax = [u.break]
add eax, 3
and al, not 3 ; dword aligned allocation (malloc) address
push eax ; *
mov eax, [esp+8] ; element size (integer=4)
mov ebx, [esp+12] ; structure/area length (element count)
mul ebx
;;;
; temporary - 23/09/2024
;mov ecx, eax
;mov edi,[esp]
;;;
add eax, [esp] ; *
; new break point - old break point = allocation size
; temporary - 23/09/2024
;mov [ubreak],eax
;;;
; temporary - 23/09/2024
;xor eax, eax
;rep stosb
;;;
;pop eax ; *
;retn
sys _break, eax ; set new u.break address
pop eax ; * ; 13/09/2024
;jc short .calloc_err
; return value
; eax = start/beginning address
;retn
; 13/09/2024
jnc short calloc_ok
calloc_err:
; write "malloc error" message to STDERR
mov esi, .malloc_err_msg
.calloc_err_p:
lodsb
or al, al
jz short .calloc_err_x
sys _stdio, 3, eax
jmp short .calloc_err_p
.calloc_err_x:
sys _exit, -1
retn ; normally cpu must not come here !
.malloc_err_msg:
db 0Dh, 0Ah
db 07h ; beep !
db "Memory Allocation Error!"
db 0Dh, 0Ah, 0
; temporary - 23/09/2024
;ubreak: dd 0
_OS_exit:
mov ebx, [esp+4] ; exit code ; 13/09/2024
sys _exit
retn ; normally cpu must not come here !