-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.asm
More file actions
284 lines (237 loc) · 4.27 KB
/
Copy pathwrite.asm
File metadata and controls
284 lines (237 loc) · 4.27 KB
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
; write.asm - Append one line to a fixed text-file region (todo)
[BITS 16]
[ORG 0xA000]
SYSCALL_INT equ 0x80
SYS_PUTC equ 0x01
SYS_PUTS equ 0x02
SYS_NEWLINE equ 0x03
SYS_GETC equ 0x04
SYS_READ_RAW equ 0x07
SYS_WRITE_RAW equ 0x08
CTRL_C equ 0x03
%ifndef LOG_SECTOR
LOG_SECTOR equ 15
%endif
%ifndef LOG_SECTORS
LOG_SECTORS equ 1
%endif
LOG_BUF_SIZE equ 512
start:
mov ax, 0
mov ds, ax
mov es, ax
mov si, msg_title
call sys_puts
; Load existing log text from disk
mov bx, log_buf
mov al, LOG_SECTORS
mov ch, 0
mov cl, LOG_SECTOR
mov dh, 0
mov ah, SYS_READ_RAW
int SYSCALL_INT
cmp ah, 0
jne .read_fail
; Find end (first 0 byte) in log buffer
mov bx, log_buf
xor cx, cx
.find_end:
cmp cx, LOG_BUF_SIZE
jae .full
cmp byte [bx], 0
je .have_end
inc bx
inc cx
jmp .find_end
.have_end:
mov [append_ptr], bx
mov ax, bx
sub ax, log_buf
mov [append_ofs], ax
mov si, msg_prompt
call sys_puts
call read_line
; Ctrl+C from prompt aborts write and returns to csh.
cmp byte [line_buf], CTRL_C
je .cancelled
cmp byte [line_buf], 0
je .empty
; Append line to log buffer
mov di, [append_ptr]
mov si, line_buf
.append_loop:
mov al, [si]
cmp al, 0
je .append_crlf
cmp di, log_buf + LOG_BUF_SIZE - 3
jae .full
mov [di], al
inc di
inc si
jmp .append_loop
.append_crlf:
cmp di, log_buf + LOG_BUF_SIZE - 3
jae .full
mov byte [di], 13
inc di
mov byte [di], 10
inc di
mov byte [di], 0
mov ax, di
sub ax, log_buf
mov [append_end_ofs], ax
; Write whole sector back
mov bx, log_buf
mov al, LOG_SECTORS
mov ch, 0
mov cl, LOG_SECTOR
mov dh, 0
mov ah, SYS_WRITE_RAW
int SYSCALL_INT
cmp ah, 0
jne .write_fail
; Read back and verify appended bytes.
mov bx, log_buf
mov al, LOG_SECTORS
mov ch, 0
mov cl, LOG_SECTOR
mov dh, 0
mov ah, SYS_READ_RAW
int SYSCALL_INT
cmp ah, 0
jne .verify_fail
mov bx, log_buf
add bx, [append_ofs]
mov si, line_buf
.verify_line:
mov al, [si]
cmp al, 0
je .verify_crlf
cmp al, [bx]
jne .verify_fail
inc si
inc bx
jmp .verify_line
.verify_crlf:
cmp byte [bx], 13
jne .verify_fail
inc bx
cmp byte [bx], 10
jne .verify_fail
inc bx
cmp byte [bx], 0
jne .verify_fail
mov si, msg_ok
call sys_puts
call sys_newline
ret
.cancelled:
ret
.empty:
mov si, msg_empty
call sys_puts
call sys_newline
ret
.full:
mov si, msg_full
call sys_puts
call sys_newline
ret
.read_fail:
mov si, msg_read_fail
call sys_puts
call sys_newline
ret
.write_fail:
mov si, msg_write_fail
call sys_puts
call sys_newline
ret
.verify_fail:
mov si, msg_verify_fail
call sys_puts
call sys_newline
ret
read_line:
xor cx, cx
mov bx, line_buf
.read_loop:
call sys_getc
; Ctrl+C cancels line entry.
cmp al, CTRL_C
je .cancel
cmp al, 13
je .done
cmp al, 8
je .backspace
call sys_putc
cmp cx, 79
jae .read_loop
mov si, cx
mov [bx + si], al
inc cx
jmp .read_loop
.backspace:
cmp cx, 0
je .read_loop
mov al, 8
call sys_putc
mov al, ' '
call sys_putc
mov al, 8
call sys_putc
dec cx
mov si, cx
mov byte [bx + si], 0
jmp .read_loop
.cancel:
mov byte [line_buf], CTRL_C
call sys_newline
ret
.done:
mov si, cx
mov byte [bx + si], 0
call sys_newline
ret
sys_putc:
mov ah, SYS_PUTC
int SYSCALL_INT
ret
sys_puts:
mov ah, SYS_PUTS
int SYSCALL_INT
ret
sys_newline:
mov ah, SYS_NEWLINE
int SYSCALL_INT
ret
sys_getc:
mov ah, SYS_GETC
int SYSCALL_INT
ret
msg_title:
db "write - append one line to todo", 13, 10, 0
msg_prompt:
db "text> ", 0
msg_ok:
db "append ok", 0
msg_empty:
db "nothing written", 0
msg_full:
db "todo file full", 0
msg_read_fail:
db "read failed", 0
msg_write_fail:
db "write failed", 0
msg_verify_fail:
db "write verify failed", 0
append_ptr:
dw 0
append_ofs:
dw 0
append_end_ofs:
dw 0
line_buf:
times 80 db 0
log_buf:
times LOG_BUF_SIZE db 0