-
Notifications
You must be signed in to change notification settings - Fork 1
/
kfs.asm
3138 lines (2953 loc) · 90.5 KB
/
kfs.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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright achiu-au (aka MaoKo)
; You can compile with fasm (https://flatassembler.net/)
format ELF executable 3H at 0500000H
entry _kernel_elf_entry
; DEBUG
macro _itoa number* {
local _number, _modulo, _length
_number = (number)
if (_number < $00)
db "-"
_number = (-_number)
end if
_length = $00
if (~(definite __itoa_virtual))
virtual at $00
__itoa_virtual::
end virtual
end if
assert (_number eqtype $00)
while _number
_modulo = _number mod $10
_number = _number / $10
_length = _length + $01
virtual __itoa_virtual
if (_modulo >= 00AH)
db ((_modulo-00AH)+"A")
else
db (_modulo+"0")
end if
end virtual
end while
repeat _length
virtual __itoa_virtual
load _number byte from __itoa_virtual:($-(%))
end virtual
db _number
end repeat
}
; DEBUG
macro __transform_struc [_item*]
{
forward
macro _item _param&
\{
\local _private
_private _item _param
\}
}
_PRESENT = 080H
rept 4H i:0H
{
_DPL#i = ((i) shl 5H)
_RPL#i = (i)
}
_DESCRIPTOR = 010H
_EXECUTABLE = 8H
_CONFORMING = 4H
_EXPAND_DOWN = 4H
_READABLE = 2H
_WRITABLE = 2H
_ACCESS = 1H
_BUSY = 2H
_TI = 4H
_286_TSS = 1H
_286_LDT = 2H
_286_CALL_GATE = 4H
_286_TASK_GATE = 5H
_286_INTERRUPT_GATE = 6H
_286_TRAP_GATE = 7H
_386_TSS = 9H
_386_CALL_GATE = 00CH
_386_INTERRUPT_GATE = 00EH
_386_TRAP_GATE = 00FH
_L = (2H shl 4H)
_D = (4H shl 4H)
_B = (4H shl 4H)
_G = (8H shl 4H)
struc TSS_16 _link*, _sp0*, _ss0*, _sp1*, _ss1*, _sp2*, _ss2*, _ip*, _flag*, _ax*, _cx*, _dx*, _bx*, _sp*, _bp*, _si*, _di*, _es*, _cs*, _ss*, _ds*, _ldt*
{
.link: dw _link
.sp0: dw _ss0
.ss0: dw _sp0
.sp1: dw _sp1
.ss1: dw _ss1
.sp2: dw _sp2
.ss2: dw _ss2
.ip: dw _ip
.flag: dw _flag
.ax: dw _ax
.cx: dw _cx
.dx: dw _dx
.bx: dw _bx
.sp: dw _sp
.bp: dw _bp
.si: dw _si
.di: dw _di
.es: dw _es
.cs: dw _cs
.ds: dw _ds
.ss: dw _ss
.ldt: dw _ldt
}
struc TSS_32 _link*, _esp0*, _ss0*, _esp1*, _ss1*, _esp2*, _ss2*, _cr3*, _eip*, _eflag*, _eax*, _ecx*, _edx*, _ebx*, _esp*, _ebp*, _esi*, _edi*, _es*, _cs*, _ss*, _ds*, _fs*, _gs*, _ldt*, _trap*, _iomap*
{
assert (((_trap) >= 0H) & ((_trap) <= 1H))
.link: dw _link
dw 0H
.esp0: dd _esp0
.ss0: dw _ss0
dw 0H
.esp1: dd _esp1
.ss1: dw _ss1
dw 0H
.esp2: dd _esp2
.ss2: dw _ss2
dw 0H
.cr3: dd _cr3
.eip: dd _eip
.eflag: dd _eflag
.eax: dd _eax
.ecx: dd _ecx
.edx: dd _edx
.ebx: dd _ebx
.esp: dd _esp
.ebp: dd _ebp
.esi: dd _esi
.edi: dd _edi
.es: dw _es
dw 0H
.cs: dw _cs
dw 0H
.ss: dw _ss
dw 0H
.ds: dw _ds
dw 0H
.fs: dw _fs
dw 0H
.gs: dw _gs
dw 0H
.ldt: dw _ldt
dw 0H
.trap: dw (_trap)
.iomap: dw _iomap
}
__transform_struc TSS_16, TSS_32
macro descriptor_table _name*, _local:0H
{
local _selector, _retreive
_selector = 0H
macro _retreive _target*
\{
local _mask
_mask = 0H
_target:
if (_local)
_mask = _TI
end if
_target\#.selector = (((_selector) * 8H) or _mask)
_selector = ((_selector) + 1H)
\}
struc DT_null
\{
_retreive .
dq 0H
\}
struc DT_dte _flag*, _access*, _offset*, _limit*
\{
_retreive .
assert (((_limit) >= 0H) & ((_limit) <= 00FFFFFH))
assert (((_offset) >= 0H) & ((_offset) <= 0FFFFFFFFH))
.limit_1: dw ((_limit) and 0FFFFH)
.offset_1: dw ((_offset) and 0FFFFH)
.offset_2: db (((_offset) shr 010H) and 0FFH)
.access: db ((_access) or _DESCRIPTOR)
.limit_2:
.flag: db ((_flag) or ((_limit) shr 010H))
.offset_3: db ((_offset) shr 018H)
\}
struc DT_gte _access*, _wdcnt*, _select*, _offset*
\{
_retreive .
assert (((_offset) >= 0H) & ((_offset) <= 0FFFFFFFFH))
assert (((_select) >= 0H) & ((_select) <= 0FFFFH))
assert (((_wdcnt) >= 0H) & ((_wdcnt) <= 01FH))
.offset_1: dw ((_offset) and 0FFFFH)
.select: dw (_select)
.wdcnt: db (_wdcnt)
.access: db ((_access) and (not _DESCRIPTOR))
.offset_2: dw ((_offset) shr 010H)
\}
struc DT_ste _access*, _offset*, _limit*
\{
_retreive .
assert (((_limit) >= 0H) & ((_limit) <= 00FFFFFH))
assert (((_offset) >= 0H) & ((_offset) <= 0FFFFFFFFH))
.limit_1: dw ((_limit) and 0FFFFH)
.offset_1: dw ((_offset) and 0FFFFH)
.offset_2: db (((_offset) shr 010H) and 0FFH)
.access: db ((_access) and (not _DESCRIPTOR))
.limit_2: db ((_limit) shr 010H)
.offset_3: db ((_offset) shr 018H)
\}
__transform_struc DT_null, DT_dte, DT_gte, DT_ste
psegment _name
}
_context = ($)
define _restore
macro psegment _name*, _mode:use32
{
local _base
_base = ($)
match,_restore \{ _context = (_base) \}
match _, _restore \{ _context = (_context + (_base)) \}
define _restore _base,_name
label _name#.start at 0H
label _name at _context
org 0H
_mode
}
macro _segsize _target*, _size*
{
_target = ((_size) - 1H)
assert ((_target) >= 0H)
}
macro reserve _name*, _origin*, _size*
{
label _name at (_origin)
label _name#.start at 0H
_segsize _name#.size, (_size)
}
macro end _continue*
{
local _missed
define _missed
match =psegment, _continue \{ define _missed 1 \}
match =descriptor_table, _continue \{ define _missed 2 \}
match _, _missed
\{
match =2, _
\\{
restruc DT_null, DT_dte, DT_gte, DT_ste
purge DT_null, DT_dte, DT_gte, DT_ste
\\}
match _base=,_name, _restore
\\{
_segsize _name\\#.size, ($)
_context = (_context - (_base))
org (_base + ($))
\\}
restore _restore
\}
match,_missed \{ end _continue \}
}
struc string data*&
{
.: db data
.sizeof = ($ - .)
}
macro enum [item*]
{
common
local _start, _first, _item, _number
_start = 0H
_first = 1H
forward
define _item item
match _target =: _attribute, item
\{
define _item _target
define _number
match =&, _attribute
\\{
assert (~(_first))
_start = ((_start) - 1H)
define _number _
\\}
match,_number
\\{
assert ((_start) < (_attribute))
_start = _attribute
\\}
\}
match _, _item
\{
assert (~(definite (_)))
_ = (_start)
\}
_start = ((_start) + 1H)
_first = 0H
}
_VGA_BLACK = 0H
_VGA_BLUE = 1H
_VGA_GREEN = 2H
_VGA_CYAN = 3H
_VGA_RED = 4H
_VGA_MAGENTA = 5H
_VGA_BROWN = 6H
_VGA_LIGHT = 7H
_VGA_TEXT_ROW = 019H
_VGA_TEXT_COLUMN = 050H
_VGA_DIMENSION = (_VGA_TEXT_ROW * _VGA_TEXT_COLUMN)
_VGA_MEMORY_SIZE = (_VGA_DIMENSION * 2H)
_VGA_MEMORY_SEG = 0B800H
define _SCREEN_COUNT 4H
assert ((_SCREEN_COUNT > 0H) & (_SCREEN_COUNT < 00AH))
macro __screen_workspace _name*, _tss_kind*, _tss_param*&
{
psegment _name
.cached: db _VGA_MEMORY_SIZE dup 0H
.cursor_flat: dd 0H
.cursor_x: dd 0H
.cursor_y: dd 0H
.foreground: db _VGA_LIGHT
.background: db _VGA_BLACK
.attrsave: db 0H
.cursor_state: db 1H
psegment _name#_tss
_tss_kind _tss_param
end psegment
end psegment
}
virtual at 0H
__screen_workspace _generic_screen_workspace_16, TSS_16, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H
end virtual
virtual at 0H
__screen_workspace _generic_screen_workspace_32, TSS_32, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H, 0H
end virtual
_GRUB_MAGIC = 01BADB002H
_GRUB_FLAGS = 0H
macro __grub_multiboot
{
segment executable writable readable
dd _GRUB_MAGIC
dd _GRUB_FLAGS
dd (-(_GRUB_MAGIC + _GRUB_FLAGS))
}
_STACK_BOTTOM = (1H shl 010H)
macro __declare_stack _name*
{
psegment _name
db _STACK_BOTTOM dup 0H
.bottom:
end psegment
}
__grub_multiboot
psegment _kernel_elf_entry
cli
cld
mov esi, _GDT
mov edi, _GDT_BASE_ADDRESS
mov ecx, ((_GDT.size + 1H) shr 2H)
rep movsd
mov ebp, esp
push _GDT_BASE_ADDRESS
pushw _GDT.size
lgdt fword [esp]
mov esp, ebp
push _IDT
pushw _IDT.size
lidt fword [esp]
mov ax, _tss_screen_segment_0.selector
ltr ax
mov ax, _ldt_screen_segment_0.selector
lldt ax
mov ax, _kernel_stack_segment_0.selector
mov ss, ax
mov esp, _kernel_stack_0.bottom
mov ax, _kernel_data_segment.selector
mov ds, ax
jmp far _kernel_code_segment.selector : _kernel_code.init
end psegment
psegment _kernel_code
.init:
mov ax, _null_segment.selector
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
define _INTEL_RESERVED_INT 020H
mov al, _INTEL_RESERVED_INT
call .pic_remap
call .init_keyboard
call .disable_vga_cursor
jmp .task_entry
_IBM_PIC_MASTER_COMMAND = 020H
_IBM_PIC_MASTER_DATA = (_IBM_PIC_MASTER_COMMAND + 1H)
_IBM_PIC_SLAVE_COMMAND = 0A0H
_IBM_PIC_SLAVE_DATA = (_IBM_PIC_SLAVE_COMMAND + 1H)
_IBM_PIC_PIT = 0H
_IBM_PIC_KEYBOARD = 1H
_IBM_PIC_EOI = 020H
define _IBM_PIC_HANDLE_INT 8H
_8259_ICW1_ICW4 = 1B
_8259_ICW1_SINGLE = 010B
_8259_ICW1_INTERVAL_4 = 100B
_8259_ICW1_LEVEL = 01000B
_8259_ICW1_ALWAYS = 10000B
_8259_ICW4_86_88 = 1B
_8259_ICW4_AUTO_EOI = 010B
_8259_ICW4_BUFFER_MODE_SLAVE = 01000B
_8259_ICW4_BUFFER_MODE_MATSER = 01100B
_8259_ICW4_SPECIAL_NESTED = 10000B
.pic_remap:
mov bl, al
in al, _IBM_PIC_MASTER_DATA
mov cl, al
in al, _IBM_PIC_SLAVE_DATA
mov dl, al
mov al, _8259_ICW1_ALWAYS or _8259_ICW1_ICW4
out _IBM_PIC_MASTER_COMMAND, al
out _IBM_PIC_SLAVE_COMMAND, al
mov al, bl
and al, not 011B
out _IBM_PIC_MASTER_DATA, al
add al, _IBM_PIC_HANDLE_INT
out _IBM_PIC_SLAVE_DATA, al
mov al, 000000100B
out _IBM_PIC_MASTER_DATA, al
mov al, 010B
out _IBM_PIC_SLAVE_DATA, al
mov al, _8259_ICW4_86_88
out _IBM_PIC_MASTER_DATA, al
out _IBM_PIC_SLAVE_DATA, al
mov al, cl
out _IBM_PIC_MASTER_DATA, al
mov al, dl
out _IBM_PIC_SLAVE_DATA, al
ret
.init_keyboard:
call ._keyboard_control_wait_input_empty
mov al, _KEYBOARD_CONTROL_ENABLE_KEYBOARD
out _KEYBOARD_CONTROL, al
call ._keyboard_control_wait_input_empty
mov al, _KEYBOARD_ENCODER_ENABLE_KEYBOARD
out _KEYBOARD_ENCODER, al
call ._keyboard_control_wait_input_empty
mov al, _KEYBOARD_ENCODER_SET_SCAN_CODE
out _KEYBOARD_ENCODER, al
call ._keyboard_control_wait_input_empty
mov al, _XT_SCAN_CODE
out _KEYBOARD_ENCODER, al
ret
.disable_vga_cursor:
mov al, 00AH
mov dx, 3D4H
out dx, al
mov al, 020H
mov dx, 3D5H
out dx, al
ret
.task_entry:
sti
call far _refresh_screen_gate.selector : 0H
push _user_stack_segment_0.selector + _RPL3
push _user_stack_0.bottom
push _user_code_segment.selector + _RPL3
push _user_code.start
retfd
.transform_flat:
; eax - the future x position
; ebx - the future y position
push ebx
cmp eax, _VGA_TEXT_COLUMN
jae .transform_flat_error
cmp ebx, _VGA_TEXT_ROW
jae .transform_flat_error
imul ebx, _VGA_TEXT_COLUMN
jc .transform_flat_error
add eax, ebx
shl eax, 1H
clc
jmp .transform_flat_exit
.transform_flat_error:
stc
.transform_flat_exit:
pop ebx
ret
irp kind*, foreground,background
{
.set_#kind:
; set foreground/background - system call
; [esp+008H] - the target foreground/background color
mov al, byte [esp+008H]
cmp al, _VGA_LIGHT
ja .set_#kind#_carry
pushw ds _screen_segment_kernel_0.selector
popw ds
mov byte [_generic_screen_workspace_32.#kind], al
popw ds
clc
jmp $+3H
.set_#kind#_carry:
stc
retfd 4H
}
.set_cursor_position:
; set cursor - system call
; [esp+008H] - the future x position
; [esp+00CH] - the future y position
mov eax, dword [esp+008H]
mov ebx, dword [esp+00CH]
mov ecx, eax
call .transform_flat
jc .set_cursor_position_exit
pushw ds _screen_segment_kernel_0.selector
popw ds
call .hide_cursor
lea eax, [_generic_screen_workspace_32.cached+eax]
mov dword [_generic_screen_workspace_32.cursor_flat], eax
mov dword [_generic_screen_workspace_32.cursor_x], ecx
mov dword [_generic_screen_workspace_32.cursor_y], ebx
call .show_cursor
call far _kernel_code_segment.selector : .refresh_screen
popw ds
.set_cursor_position_exit:
retfd 8H
.get_cursor_position:
; get cursor - system call
pushw ds _screen_segment_kernel_0.selector
popw ds
mov eax, dword [_generic_screen_workspace_32.cursor_x]
mov ebx, dword [_generic_screen_workspace_32.cursor_y]
popw ds
retfd 0H
_INVISIBLE_CURSOR = 0H
.invisible_cursor:
; invisible cursor - system call
pushw ds _screen_segment_kernel_0.selector
popw ds
call .hide_cursor
mov byte [_generic_screen_workspace_32.cursor_state], _INVISIBLE_CURSOR
call far _kernel_code_segment.selector : .refresh_screen
popw ds
retfd 0H
_SOLID_CURSOR = 1H
.solid_cursor:
; solid cursor - system call
pushw ds _screen_segment_kernel_0.selector
popw ds
mov byte [_generic_screen_workspace_32.cursor_state], _SOLID_CURSOR
call .show_cursor
call far _kernel_code_segment.selector : .refresh_screen
popw ds
retfd 0H
.worth_consider_cursor:
cmp byte [_generic_screen_workspace_32.cursor_state], _INVISIBLE_CURSOR
jz @f
cmp dword [_generic_screen_workspace_32.cursor_x], _DISPLAY_LINE_EOF
@@:
ret
.show_cursor:
call .worth_consider_cursor
jz .hide_cursor_exit
push eax ebx edi
mov edi, dword [_generic_screen_workspace_32.cursor_flat]
mov al, byte [edi+1H]
and al, 077H
mov bl, al
and bl, 00FH
mov byte [_generic_screen_workspace_32.attrsave], al
shr al, 4H
cmp al, _VGA_LIGHT
jnz @f
mov al, (_VGA_BLACK shl 4H)
cmp bl, _VGA_BLACK
jnz .show_cursor_set
mov bl, _VGA_LIGHT
jmp .show_cursor_set
@@:
mov al, (_VGA_LIGHT shl 4H)
cmp bl, _VGA_LIGHT
jnz .show_cursor_set
mov bl, _VGA_BLACK
.show_cursor_set:
or al, bl
and byte [edi+1H], not 077H
or byte [edi+1H], al
pop edi ebx eax
.show_cursor_exit:
ret
.hide_cursor:
call .worth_consider_cursor
jz .hide_cursor_exit
push eax edi
mov edi, dword [_generic_screen_workspace_32.cursor_flat]
mov al, byte [_generic_screen_workspace_32.attrsave]
mov byte [edi+1H], al
pop edi eax
.hide_cursor_exit:
ret
_SCROLL_UP = 0H
_SCROLL_DOWN = 1H
_SCROLL_LEFT = 2H
_SCROLL_RIGHT = 3H
.scroll:
; scroll - system call
; [esp+008H] - kind of scroll
; [esp+00CH] - line to scroll
; [esp+010H] - auto complete dimension
; [esp+014H] - upper row number
; [esp+018H] - left column number
; [esp+01CH] - lower row number
; [esp+020H] - right column number
cmp dword [esp+00CH], 0H
jz .scroll_exit
push ebp
mov ebp, esp
xor eax, eax
xor ebx, ebx
mov ecx, (_VGA_TEXT_ROW - 1H)
mov edx, (_VGA_TEXT_COLUMN - 1H)
cmp dword [ebp+00CH], _SCROLL_RIGHT
ja .scroll_exit
cmp dword [esp+014H], 0H
jnz .scroll_compute
mov eax, dword [esp+018H]
mov ebx, dword [esp+01CH]
mov ecx, dword [esp+020H]
mov edx, dword [esp+024H]
.scroll_compute:
mov edi, ecx
cmp edi, _VGA_TEXT_ROW
jae .scroll_exit
sub edi, eax
jb .scroll_exit
mov esi, edx
cmp esi, _VGA_TEXT_COLUMN
jae .scroll_exit
sub esi, ebx
jb .scroll_exit
cmp dword [ebp+00CH], _SCROLL_DOWN
jnz $+4H
add ebx, edi
pushw ds es
inc esi
inc edi
push esi edi
.scroll_perform:
pushw _screen_segment_kernel_0.selector _screen_segment_kernel_0.selector
popw ds es
call .hide_cursor
@@:
cmp dword [esp], 0H
jz .scroll_write_screen
push eax ebx ecx edx
mov esi, dword [ebp+00CH]
cmp esi, _SCROLL_RIGHT
jnz $+3H
xchg eax, edx
call .transform_flat
jc .scroll_write_screen
lea edi, [_generic_screen_workspace_32.cached+eax]
mov eax, dword [esp+00CH]
mov edx, dword [esp]
jmp dword [cs:_kernel_code.scroll_table+esi*4H]
.scroll_continue:
pop edx ecx ebx eax
cmp dword [ebp+00CH], _SCROLL_DOWN
jnz $+5H
dec ebx
jmp $+3H
inc ebx
dec dword [esp]
jmp @b
.scroll_write_screen:
call .show_cursor
call far _kernel_code_segment.selector : .refresh_screen
add esp, 8H
popw es ds
.scroll_exit:
leave
retfd 01CH
.scroll_up:
add ebx, dword [ebp+010H]
cmp ebx, ecx
mov ecx, dword [esp+014H]
ja .scroll_horizontal_clear
call .transform_flat
jc .scroll_horizontal_clear
lea esi, [_generic_screen_workspace_32.cached+eax]
rep movsw
jmp .scroll_continue
.scroll_down:
mov ecx, dword [esp+014H]
sub ebx, dword [ebp+010H]
cmp ebx, eax
jb .scroll_horizontal_clear
call .transform_flat
jc .scroll_horizontal_clear
lea esi, [_generic_screen_workspace_32.cached+eax]
rep movsw
jmp .scroll_continue
.scroll_left:
mov ecx, dword [esp+014H]
add eax, dword [ebp+010H]
cmp eax, edx
ja .scroll_horizontal_clear
push eax
call .transform_flat
pop ebx
jc .scroll_horizontal_clear
lea esi, [_generic_screen_workspace_32.cached+eax]
sub edx, ebx
inc edx
sub ecx, edx
xchg ecx, edx
rep movsw
mov ecx, edx
jmp .scroll_horizontal_clear
.scroll_right:
std
mov ecx, dword [esp+014H]
sub edx, dword [ebp+010H]
cmp edx, eax
jb .scroll_horizontal_clear
push eax
mov eax, edx
call .transform_flat
pop ebx
jc .scroll_horizontal_clear
lea esi, [_generic_screen_workspace_32.cached+eax]
sub edx, ebx
inc edx
sub ecx, edx
xchg ecx, edx
rep movsw
mov ecx, edx
jmp .scroll_horizontal_clear
.scroll_horizontal_clear:
xor ax, ax
call .adjust_color
rep stosw
cld
jmp .scroll_continue
.scroll_table:
dd _kernel_code.scroll_up
dd _kernel_code.scroll_down
dd _kernel_code.scroll_left
dd _kernel_code.scroll_right
_DISPLAY_LINE_EOF = not 0H
.display_string:
; display string - system call
; [esp+008H] - selector of the target string
; [esp+00CH] - offset of the target string
; [esp+010H] - size of the target string
pushw ds es
xor eax, eax
mov ecx, dword [esp+014H]
test ecx, ecx
jz .display_string_exit
mov esi, dword [esp+010H]
pushw _screen_segment_kernel_0.selector
popw ds
mov edi, dword [_generic_screen_workspace_32.cursor_flat]
mov ax, word [esp+008H]
mov dx, word [esp+00CH]
arpl dx, ax
verr dx
jnz .display_string_exit
lsl eax, dx
inc eax
sub eax, esi
jbe .display_string_exit
cmp eax, ecx
cmovb ecx, eax
mov ebx, ecx
call .adjust_color
call .hide_cursor
mov ds, dx
pushw _screen_segment_kernel_0.selector
popw es
@@:
cmp dword [es:_generic_screen_workspace_32.cursor_x], _VGA_TEXT_COLUMN
jae .display_string_restore
lodsb
stosw
inc dword [es:_generic_screen_workspace_32.cursor_x]
loop @b
.display_string_restore:
pushw es
popw ds
mov dword [_generic_screen_workspace_32.cursor_flat], edi
cmp dword [es:_generic_screen_workspace_32.cursor_x], _VGA_TEXT_COLUMN
jz .display_string_overflow
call .show_cursor
jmp .display_string_sanitize
.display_string_overflow:
mov dword [_generic_screen_workspace_32.cursor_x], _DISPLAY_LINE_EOF
.display_string_sanitize:
mov eax, ebx
sub eax, ecx
call far _kernel_code_segment.selector : .refresh_screen
.display_string_exit:
popw es ds
retfd 00CH
.adjust_color:
mov ah, byte [_generic_screen_workspace_32.background]
shl ah, 4H
or ah, [_generic_screen_workspace_32.foreground]
ret
.refresh_screen:
; refresh screen - system call
pushw ds es _screen_segment_kernel_0.selector _video_segment.selector
popw es ds
mov esi, _generic_screen_workspace_32.cached
xor edi, edi
mov ecx, (_VGA_MEMORY_SIZE shr 2H)
rep movsd
mov ecx, ((_VGA_MEMORY_SIZE shr 1H) and 1B)
rep movsw
popw es ds
retfd 0H
.atoi:
; alpha to integer - system call
; [esp+008H] - selector of target buffer
; [esp+00CH] - offset of target buffer
; [esp+010H] - size of the target buffer
pushw ds
mov ax, word [esp+6H]
mov dx, word [esp+00AH]
arpl dx, ax
verr dx
jnz .atoi_carry
lsl ecx, dx
inc ecx
mov esi, dword [esp+00EH]
sub ecx, esi
jbe .atoi_carry
mov eax, dword [esp+012H]
cmp ecx, eax
cmova ecx, eax
jecxz .atoi_exit
mov ds, dx
xor ebx, ebx
xor edx, edx
xor eax, eax
.atoi_loop:
lodsb
sub al, 030H
jc .atoi_next
cmp al, 9H
jbe .atoi_compute
or al, 020H
sub al, 031H
jc .atoi_next
cmp al, 05H
ja .atoi_next
add al, 00AH
.atoi_compute:
shl ebx, 4H
add ebx, eax
jc .atoi_carry
mov dl, 1H
loop .atoi_loop
.atoi_next:
test dl, dl
jz .atoi_carry
mov eax, ebx
lea edi, [esi-1H]
clc
jmp .atoi_exit
.atoi_carry:
stc
.atoi_exit:
popw ds
retfd 00CH
_PADDING_MAX = 8H
.itoa:
; integer to alpha - system call
; [esp+008H] - target number
; [esp+00CH] - selector of target buffer
; [esp+010H] - offset of target buffer
; [esp+014H] - padding of zero for the resulting string
; [esp+018H] - size of target buffer
xor eax, eax
cmp dword [esp+014H], _PADDING_MAX
jbe @f
mov dword [esp+014H], _PADDING_MAX
@@:
mov ecx, dword [esp+018H]
jecxz .itoa_exit
mov edi, dword [esp+010H]
mov bx, word [esp+4H]
mov dx, word [esp+00CH]
arpl dx, bx
verw dx
jnz .itoa_exit
lsl ebx, dx
inc ebx
sub ebx, edi
jbe .itoa_exit
pushw ds es
cmp ebx, ecx
cmovb ecx, ebx
mov es, dx
mov ebx, .itoa_table
mov eax, dword [esp+00CH]
push ebp
mov ebp, esp
mov esi, 010H
.itoa_loop:
xor edx, edx
div esi
xchg eax, edx
cs xlatb
dec esp
mov byte [esp], al
test edx, edx
xchg edx, eax
jnz .itoa_loop
mov eax, ebp
sub eax, esp
mov edx, dword [ebp+01CH]
cmp eax, edx
jae .itoa_write
sub edx, eax
add eax, edx
xchg ecx, edx
@@:
dec esp
mov byte [esp], 030H
loop @b
mov ecx, edx
.itoa_write:
cmp ecx, eax
cmova ecx, eax
mov eax, ecx
mov esi, esp