/
hw86.s
executable file
·655 lines (529 loc) · 10.2 KB
/
hw86.s
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
;
; Hardware utilities for x86
;
[BITS 16]
;
; void dump_regs()
;
;
global _dump_regs
_dump_regs:
pusha
push cs
push ds
push es
push ss
push .dumpstr
call _sputstr
pop ax
pop ax
pop ax
pop ax
pop ax
popa
ret
.dumpstr db "REG DUMP:",13,10,"SS=%x ES=%x DS=%x CS=%x",13,10,"DI=%x SI=%x BP=%x SP=%x",13,10,"BX=%x DX=%x CX=%x AX=%x",13,10, 0
extern _sputstr
;
; void io_set_text_mode();
; Set the text mode to 80x25 16 colors
; and cursor shape to blinking
;
global _io_set_text_mode
_io_set_text_mode:
push ax
push bx
mov al, 0x03
mov ah, 0x00
int 0x10
mov ax, 0x1112 ; Use this to set 80x50 video mode
mov bl, 0
int 0x10
pop bx
pop ax
ret
;
; void io_clear_screen()
; Clears the text screen for text mode
;
global _io_clear_screen
_io_clear_screen:
pusha
mov dx, 0 ; Position cursor at top-left
mov bh, 0
mov ah, 2
int 10h ; BIOS interrupt to move cursor
mov ah, 6 ; Scroll full-screen
mov al, 0 ; Normal white on black
mov bh, 0x07 ; scolor
mov cx, 0 ; Top-left
mov dh, [_screen_height] ; Bottom-right
sub dh, 1
mov dl, [_screen_width]
sub dl, 1
int 10h
popa
ret
extern _screen_width, _screen_height
;
; void io_out_char(uchar c);
; Write a char to display in teletype mode
; Parameters are passed on the stack
global _io_out_char
_io_out_char:
push bx
push ax
mov bx, sp ; Save the stack pointer
mov al, [bx+6] ; Get char from string
mov ah, 0Eh ; int 10h teletype function
int 10h ; Print it
pop ax
pop bx
ret
;
; void io_out_char_attr(uint x, uint y, uchar c, uchar color);
; Write a char to display in specific position
;
global _io_out_char_attr
_io_out_char_attr:
push ax
push bx
push cx
push dx
; Set cursos position
mov bx, sp
mov dl, [bx+10]
mov dh, [bx+12]
mov ah, 2
mov bh, 0
int 10h
; Print char
mov bx, sp
mov al, [bx+14]
mov bl, [bx+16]
mov ah, 9
mov bh, 0
mov cx, 1
int 10h
pop dx
pop cx
pop bx
pop ax
ret
;
; void io_out_char_serial(uchar c);
; Write a char to serial port
;
global _io_out_char_serial
_io_out_char_serial:
push bx
push ax
push dx
test byte [_serial_status], 0x80
jne .skip
mov dx, 0 ; Port number
mov bx, sp ; Save the stack pointer
mov al, [bx+8] ; Get char from string
mov ah, 01h ; int 14h ah=01h: write
int 0x14 ; Send it
mov byte [_serial_status], ah
.skip:
pop dx
pop ax
pop bx
ret
;
; uchar io_in_char_serial();
; Read a char from serial port
;
global _io_in_char_serial
_io_in_char_serial:
push dx
push ax
test byte [_serial_status], 0x80
jne .skip
mov dx, 0 ; Port number
mov al, 0
mov ah, 02h ; int 14h ah=02h: read
int 0x14 ; Get it
;mov byte [_serial_status], ah
test byte ah, 0x80
mov ah, 0
je .skip
mov ax, 0
.skip:
pop dx
pop dx
ret
extern _serial_status
;
; void io_hide_cursor();
; Hide the cursor in text mode using BIOS
;
global _io_hide_cursor
_io_hide_cursor:
push ax
push cx
mov ch, 0x20
mov ah, 0x01
int 0x10
pop cx
pop ax
ret
;
; void io_show_cursor();
; Show the cursor in text mode using BIOS
;
;
global _io_show_cursor
_io_show_cursor:
pusha
mov ch, 6
mov cl, 7
mov ah, 1
mov al, 3
int 10h
popa
ret
;
; void io_get_cursor_position(uint* x, uint* y);
; Get the cursor position
;
;
global _io_get_cursor_pos
_io_get_cursor_pos:
push ax
push bx
push dx
mov ah, 3
mov bh, 0
int 10h
mov bx, sp
mov bx, [bx+8]
mov byte [bx], dl
mov bx, [bx+10]
mov byte [bx], dh
pop dx
pop bx
pop ax
ret
;
; void io_set_cursor_position(uint x, uint y);
; Set the cursor position
;
;
global _io_set_cursor_pos
_io_set_cursor_pos:
push ax
push bx
push dx
mov bx, sp
mov byte dl, [bx+8]
mov byte dh, [bx+10]
mov ah, 2
mov bh, 0
int 10h
pop dx
pop bx
pop ax
ret
;
; uint io_in_key()
;
;
global _io_in_key
_io_in_key:
mov ax, 0
mov ah, 10h ; BIOS call to wait for key
int 16h
ret
;
; void halt()
;
;
global _halt
_halt:
cli
hlt
;
; void get_time(uchar* BDCtime, uchar* date)
; Get system time
;
global _get_time
_get_time:
pusha
mov ah, 02h
int 1ah
mov bx, sp
mov ax, [bx+18]
mov bx, ax
mov [bx], ch
inc bx
mov [bx], cl
inc bx
mov [bx], dh
mov ah, 04h
int 1ah
mov bx, sp
mov ax, [bx+20]
mov bx, ax
mov [bx], cl
inc bx
mov [bx], dh
inc bx
mov [bx], dl
popa
ret
;
; uint read_disk_sector(uint disk, uint sector, uint n, uchar* buff)
;
;
global _read_disk_sector
_read_disk_sector:
pusha
mov bx, sp ; Save the stack pointer
mov al, [bx+18]
mov [tdev], al
call set_disk_params
mov ax, [bx+20] ; Ax = start logical sector
cmp byte [dsects], 0
je .param_failure
cmp byte [dsides], 0
je .param_failure
call disk_lba_to_hts
mov ah, 2 ; Params for int 13h: read disk sectors
mov al, [bx+22] ; Number of sectors to read
mov si, [bx+24] ; Set ES:BX to point the buffer
mov bx, ds
mov es, bx
mov bx, si
mov word [.n], 0
pusha ; Prepare to enter loop
.read_loop:
popa
pusha
stc ; A few BIOSes do not set properly on error
int 13h ; Read sectors
jnc .read_finished
inc word [.n]
cmp word [.n], 2
jg .read_failure
call disk_reset ; Reset controller and try again
jnc .read_loop ; Disk reset OK?
jmp .read_failure ; Fatal double error
.read_finished:
popa ; Restore registers from main loop
popa ; And restore from start of this system call
mov ax, 0 ; Return 0 (for success)
ret
.read_failure:
mov [.n], ax
popa
popa
mov ax, [.n] ; Return 1 (for failure)
ret
.param_failure:
popa
mov ax, 1
ret
.n dw 0
;
; uint write_disk_sector(uint disk, uint sector, uint n, uchar* buff)
;
;
global _write_disk_sector
_write_disk_sector:
pusha
mov bx, sp ; Save the stack pointer
mov al, [bx+18]
mov [tdev], al
call set_disk_params
mov ax, [bx+20] ; Ax = start logical sector
cmp byte [dsects], 0
je .param_failure
cmp byte [dsides], 0
je .param_failure
call disk_lba_to_hts
mov ah, 3 ; Params for int 13h: write disk sectors
mov al, [bx+22] ; Number of sectors to read
mov si, [bx+24] ; Set ES:BX to point the buffer
mov bx, ds
mov es, bx
mov bx, si
stc ; A few BIOSes do not set properly on error
int 13h ; Read sectors
jc .write_failure
popa ; And restore from start of this system call
mov ax, 0 ; Return 0 (for success)
ret
.write_failure:
mov [.n], ax
popa
mov ax, [.n] ; Return error code
ret
.param_failure:
popa
mov ax, 1
ret
.n dw 0
tdev db 0
;
; Reset disk
;
;
disk_reset:
push ax
push dx
mov ax, 0
mov dl, [tdev]
stc
int 13h
pop dx
pop ax
ret
;
; disk_lba_to_hts -- Calculate head, track and sector for int 13h
; IN: logical sector in AX; OUT: correct registers for int 13h
;
disk_lba_to_hts:
push bx
push ax
mov bx, ax ; Save logical sector
mov dx, 0 ; First the sector
div word [dsects] ; Sectors per track
add dl, 01 ; Physical sectors start at 1
mov cl, dl ; Sectors belong in CL for int 13h
mov ax, bx
mov dx, 0 ; Now calculate the head
div word [dsects] ; Sectors per track
mov dx, 0
div word [dsides] ; Disk sides
mov dh, dl ; Head/side
mov ch, al ; Track
pop ax
pop bx
mov dl, [tdev] ; Set disk
ret
struc DISKINFO
.id resw 1
.name resb 4
.fstype resw 1
.fssize resw 2
.sectors resw 1
.sides resw 1
.cylinders resw 1
.disk_size resw 2
.size:
endstruc
;
; set_disk_params
; To allow disk read and write
;
set_disk_params:
pusha
mov byte bl, [tdev]
mov bh, 0
push bx
call _disk_to_index
pop bx
mov bl, DISKINFO.size
mul bl
mov bx, [_disk_info + eax + DISKINFO.sectors]
mov [dsects], bx
mov bx, [_disk_info + eax + DISKINFO.sides]
mov [dsides], bx
popa
ret
extern _disk_to_index
extern _disk_info
;
; uint get_disk_info(uint disk, uint* st, uint* hd, , uint* cl);
;
;
global _get_disk_info
_get_disk_info:
pusha
clc
mov bx, sp ; Save the stack pointer
mov al, [bx+18]
mov [tdev], al
mov di, 0
mov byte dl, [tdev]
mov ah, 8 ; Get disk parameters
int 13h
jc .error ; Detect possible errors
cmp dl, 0
je .error
cmp ah, 0
jne .error
push cx
mov bx, sp ; Save the stack pointer
mov ax, [bx+22]
and cx, 3Fh ; Maximum sector number
mov bx, ax
mov [bx], cx ; Sector numbers start at 1
mov bx, sp ; Save the stack pointer
mov ax, [bx+24]
mov bx, ax
movzx dx, dh ; Maximum head number
add dx, 1 ; Head numbers start at 0 - add 1 for total
mov [bx], dx
pop cx
mov ax, cx ; Cylinders
and ax, 0C0h
shl ax, 2
and cx, 0FF00h
shr cx, 8
or cx, ax
mov ax, cx
inc ax
mov cx, ax
mov bx, sp
mov ax, [bx+24]
mov bx, ax
mov [bx], cx
popa
mov ax, 0
ret
.error:
popa
mov ax, 1
ret
dsects dw 0 ; Current disk sectors per track
dsides dw 0 ; Current disk sides
;
; Install IRS
;
;
INT_CODE equ 0x70
global _install_ISR
_install_ISR:
cli ; hardware interrupts are now stopped
xor ax, ax
mov es, ax
mov bx, [es:INT_CODE*4]
; add routine to interrupt vector table
mov dx, _ISR
mov [es:INT_CODE*4], dx
mov ax, cs
mov [es:INT_CODE*4+2], ax
sti
ret
;
; IRS
; Interrput Service Routine
;
_ISR:
pushad
cld ; clear on function entry
push cx
push ax
call _kernel_service
pop cx
pop cx
mov [result], ax
popad
mov ax, [result]
iret
result dw 0 ; Result of ISR
extern _kernel_service