-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTLL_Libs_ring0.txt
More file actions
724 lines (570 loc) · 12.4 KB
/
HTLL_Libs_ring0.txt
File metadata and controls
724 lines (570 loc) · 12.4 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
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
$$$$_kmalloc$$$$
_kmalloc:
push rbx
push rdx
mov rax, [heap_ptr]
mov rdx, rax
add rdx, rbx
mov [heap_ptr], rdx
mov rax, rdx
; Standard bump alloc: Return OLD pointer, increment variable.
; My previous logic was: load ptr, save to RDX, add size, save back.
; This means RAX returned the END of the block? No.
; Correct Bump Alloc Logic:
sub rdx, rbx
mov rax, rdx
pop rdx
pop rbx
ret
%%%%_kmalloc%%%%
$$$$_kfree$$$$
; _kfree: No-op in bump allocator
_kfree:
ret
%%%%_kfree%%%%
$$$$print_str$$$$
; print_str: Prints string at RSI with length RDX
; [In] RSI: Pointer to string
; [In] RDX: Length of string (This is what your compiler provides!)
print_str:
push rsi
push rax
push rcx
mov rcx, rdx
cmp rcx, 0
je .done
.loop:
mov al, [rsi]
call print_char
inc rsi
dec rcx
jnz .loop
.done:
pop rcx
pop rax
pop rsi
ret
%%%%print_str%%%%
$$$$print_char$$$$
print_char:
; 1. Save all registers to the stack
push rdi
push rbx
push rcx
push rdx
push rax
push rsi
; 2. CRITICAL FIX: Save the character (AL) into BL immediately.
; Since we pushed RBX, we can use BL as a safe scratchpad for this function.
mov bl, al
; 3. Handle Newline Character (ASCII 10)
cmp bl, 10
je .handle_newline
; 4. Handle Horizontal Line Wrapping
cmp qword [cursor_x], 80
jl .x_ok
mov qword [cursor_x], 0
inc qword [cursor_y]
.x_ok:
; 5. CRITICAL FIX: Ensure we scroll BEFORE calculating the memory address
cmp qword [cursor_y], 25
jl .y_ok
call .scroll_logic
.y_ok:
; 6. Calculate Memory Offset: (y * 80 + x) * 2
mov rax, [cursor_y]
imul rax, 80 ; imul is safe, it won't clobber RDX like 'mul' does
add rax, [cursor_x]
shl rax, 1 ; Multiply by 2 (char + attribute)
; 7. Write to the Buffer
lea rdi, [video_buffer + rax]
mov [rdi], bl ; Write the character we saved in BL
mov byte [rdi+1], 0x0F ; White text on Black background
inc qword [cursor_x]
jmp .done
.handle_newline:
mov qword [cursor_x], 0
inc qword [cursor_y]
cmp qword [cursor_y], 25
jl .done
call .scroll_logic
jmp .done
.scroll_logic:
; Move the entire screen up by one line (160 bytes)
lea rsi, [video_buffer + 160]
lea rdi, [video_buffer]
mov rcx, 480 ; 3840 bytes / 8 bytes per qword
cld
rep movsq
; Clear the newly created bottom line with spaces
lea rdi, [video_buffer + 3840]
mov rax, 0x0F200F200F200F20 ; "Space" char with white attribute
mov rcx, 20 ; 160 bytes / 8
rep stosq
mov qword [cursor_y], 24
ret
.done:
; Restore all registers and return
pop rsi
pop rax
pop rdx
pop rcx
pop rbx
pop rdi
ret
%%%%print_char%%%%
$$$$array_clear$$$$
; array_clear: Resets the array size to 0.
; [In] RDI: Pointer to the array struct.
array_clear:
mov qword [rdi + 8], 0
ret
%%%%array_clear%%%%
$$$$get_user_input$$$$
get_user_input:
push rbx
push rcx
push rdx
push rsi
push rdi
push r12
push r13
mov r12, rdi
mov r13, rsi
; --- 1. Print Prompt ---
mov rax, [r13 + 8]
cmp rax, 0
je .read_start
mov rcx, rax
mov rbx, [r13]
xor rdx, rdx
.prompt_loop:
mov al, [rbx + rdx*8]
call print_char
inc rdx
loop .prompt_loop
call draw_all ; Show the prompt immediately
.read_start:
.wait_key:
in al, 0x64
test al, 1
jz .wait_key
in al, 0x60
test al, 0x80
jnz .wait_key
lea rdx, [scan_map]
and rax, 0xFF
mov al, [rdx + rax]
cmp al, 0
je .wait_key
cmp al, 10
je .input_done
cmp al, 8
je .handle_backspace
; Valid Char: Echo to buffer
call print_char
; --- THE FIX: Blit to hardware so user sees the character ---
call draw_all
mov rdi, r12
movzx rsi, al
call array_append
jmp .wait_key
.handle_backspace:
; Safety Guard: Don't backspace if we are at the start of the line!
cmp qword [cursor_x], 0
je .wait_key
; Safety Guard: Don't backspace if the input buffer is empty!
cmp qword [r12 + 8], 0
je .wait_key
mov rdi, r12
call array_pop
dec qword [cursor_x]
mov rax, [cursor_y]
imul rax, 80
add rax, [cursor_x]
shl rax, 1
lea rdi, [video_buffer + rax]
mov word [rdi], 0x0F20 ; Space in buffer
call draw_all
jmp .wait_key
.input_done:
mov al, 10
call print_char
call draw_all ; Final newline sync
pop r13
pop r12
pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
ret
%%%%get_user_input%%%%
$$$$print_number$$$$
; print_number: Prints 64-bit number in RAX
print_number:
push rax
push rbx
push rcx
push rdx
cmp rax, 0
jne .p_setup
mov al, '0'
call print_char
jmp .p_done
.p_setup:
mov rbx, 10
; digit counter
xor rcx, rcx
.p_loop:
xor rdx, rdx
; rax / 10
div rbx
; remainder
push rdx
inc rcx
test rax, rax
jnz .p_loop
.p_print:
pop rax
add al, '0'
call print_char
loop .p_print
.p_done:
; Print newline
mov al, 10
call print_char
call draw_all
pop rdx
pop rcx
pop rbx
pop rax
ret
%%%%print_number%%%%
$$$$input$$$$
input:
push rdi
push rbx
mov rbx, 0
.wait_key:
in al, 0x64
test al, 1
jz .wait_key
in al, 0x60
test al, 0x80
jnz .wait_key
lea rdx, [scan_map]
and rax, 0xFF
mov al, [rdx + rax]
cmp al, 0
je .wait_key
cmp al, 10
je .finish
cmp al, 8
je .handle_backspace
mov [rdi], al
inc rdi
inc rbx
call print_char
call draw_all ; <--- Sync every key
jmp .wait_key
.handle_backspace:
cmp rbx, 0
je .wait_key
dec rdi
dec rbx
; Logic to erase in buffer
dec qword [cursor_x]
mov rax, [cursor_y]
push rbx
mov rbx, 80
mul rbx
add rax, [cursor_x]
shl rax, 1
lea rdx, [video_buffer + rax]
mov word [rdx], 0x0F20
pop rbx
call draw_all ; <--- Sync backspace
jmp .wait_key
.finish:
mov byte [rdi], 0
mov al, 10
call print_char
call draw_all
mov rax, rbx
pop rbx
pop rdi
ret
%%%%input%%%%
$$$$array_copy$$$$
; array_copy: 64-bit
; [In] rdi: dest struct ptr, rsi: source struct ptr
array_copy:
push rdi
push rsi
; Get size
; Offset 8 is size (assuming qword structure)
mov rcx, [rsi + 8]
mov [rdi + 8], rcx
; capacity
mov [rdi + 16], rcx
cmp rcx, 0
je .done
; Allocate
mov rbx, rcx
; * 8 (qwords)
shl rbx, 3
call _kmalloc
; Store pointer
mov [rdi], rax
; Copy
mov rdi, rax
; Load source data pointer
mov rsi, [rsi]
rep movsq
.done:
pop rsi
pop rdi
ret
%%%%array_copy%%%%
$$$$array_pop$$$$
array_pop:
; [In] RDI: Pointer to the array struct.
; Check if size is already 0
cmp qword [rdi + 8], 0
je .done
; Decrement size
dec qword [rdi + 8]
.done:
ret
%%%%array_pop%%%%
$$$$array_append$$$$
array_append:
push rbx
push rcx
push rax
mov rcx, [rdi + 8]
mov rbx, [rdi]
cmp rbx, 0
jne .store
mov rbx, 1024
call _kmalloc
mov rbx, rax
mov [rdi], rbx
mov qword [rdi+16], 128
.store:
mov [rbx + rcx*8], rsi
inc qword [rdi + 8]
pop rax
pop rcx
pop rbx
ret
%%%%array_append%%%%
$$$$_htll_draw$$$$
; --- _htll_draw(x, y, color) ---
; Stack: [RSP]=Ret, [RSP+8]=Color, [RSP+16]=Y, [RSP+24]=X
_htll_draw:
mov rdx, [rsp+8] ; Color
mov rsi, [rsp+16] ; Y
mov rdi, [rsp+24] ; X
cmp rdi, 80
jge .done
cmp rsi, 50
jge .done
mov rax, rsi
shr rax, 1 ; Y / 2
imul rax, 160 ; Row * 160
imul rbx, rdi, 2 ; X * 2
add rax, rbx
add rax, video_buffer ; <--- TARGETS BUFFER
mov bl, byte [rax+1] ; Read existing color from buffer
test rsi, 1
jnz .draw_bottom
.draw_top:
and bl, 240
and dl, 15
or bl, dl
jmp .write_pixel
.draw_bottom:
and bl, 15
and dl, 15
shl dl, 4
or bl, dl
.write_pixel:
mov byte [rax+0], 223
mov byte [rax+1], bl
.done:
ret
%%%%_htll_draw%%%%
$$$$_htll_clear$$$$
; --- _htll_clear(color) ---
; Stack: [RSP]=Ret, [RSP+8]=Color
_htll_clear:
mov rdx, [rsp+8] ; Read Color
mov rax, rdx
shl rax, 4
or rax, rdx
mov rdx, rax
mov rdi, video_buffer ; <--- TARGETS BUFFER
mov rcx, 2000
.loop:
mov byte [rdi], 32 ; Space char
mov byte [rdi+1], dl
add rdi, 2
dec rcx
jnz .loop
ret
%%%%_htll_clear%%%%
$$$$draw_all$$$$
; --- The Buffer Swap (Writes to VRAM) ---
draw_all:
cld
mov rsi, video_buffer
mov rdi, 0xB8000
mov rcx, 500 ; 4000 bytes / 8 bytes per qword = 500 ops
rep movsq ; Blit buffer to VRAM
ret
%%%%draw_all%%%%
$$$$_htll_get_key$$$$
; --- _htll_get_key() ---
_htll_get_key:
xor rax, rax
in al, 0x64
test al, 1
jz .no_key
in al, 0x60
ret
.no_key:
ret
%%%%_htll_get_key%%%%
$$$$_htll_draw_char$$$$
; --- _htll_draw_char(x, y, char_id, color_id) ---
; ABI: Caller-Cleanup Stack (Peeking)
; Stack: [RSP]=Ret, [RSP+8]=Color, [RSP+16]=Char, [RSP+24]=Y, [RSP+32]=X
_htll_draw_char:
mov r8, [rsp+8] ; Color ID
mov r9, [rsp+16] ; Character ID
mov rsi, [rsp+24] ; Y
mov rdi, [rsp+32] ; X
cmp rdi, 80
jge .done
cmp rsi, 25
jge .done
imul rsi, 160
imul rdi, 2
add rsi, rdi
add rsi, video_buffer ; <--- TARGETS BUFFER
mov byte [rsi], r9b
mov byte [rsi+1], r8b
.done:
ret
%%%%_htll_draw_char%%%%
$$$$bcd2bin$$$$
bcd2bin:
movzx eax, al ; AL -> EAX (32-bit)
mov ecx, eax
and ecx, 0x0F ; low nibble
shr eax, 4 ; high nibble
imul eax, 10 ; multiply high nibble by 10
add eax, ecx ; add low nibble
ret
%%%%bcd2bin%%%%
$$$$print_two_digits$$$$
; ----------------------------
; Print number in RAX as two digits
; ----------------------------
print_two_digits:
xor rdx, rdx ; clear RDX for div
mov rcx, 10
div rcx ; RAX = quotient (tens), RDX = remainder (units)
mov al, al ; get quotient in AL
add al, '0'
call print_char
mov al, dl ; remainder
add al, '0'
call print_char
ret
%%%%print_two_digits%%%%
$$$$_htll_display_clock$$$$
; --- _htll_display_clock(x, y, color, utc_dir, utc_val, is_12h) ---
; ABI: Caller-Cleanup Stack (Peeking)
; Stack: [RSP]=Ret, [RSP+8]=12h, [RSP+16]=utc_val, [RSP+24]=utc_dir, [RSP+32]=color, [RSP+40]=y, [RSP+48]=x
_htll_display_clock:
; --- Read Arguments from Stack ---
mov rdi, [rsp+48] ; X
mov rsi, [rsp+40] ; Y
mov rdx, [rsp+32] ; Color
mov r8, [rsp+24] ; UTC Direction (1 for +, 0 for -)
mov r9, [rsp+16] ; UTC Value
mov r10, [rsp+8] ; 12h Mode Flag (1 for 12h, 0 for 24h)
; --- Set Cursor Position ---
mov [cursor_x], rdi
mov [cursor_y], rsi
; --- Read RTC and store in registers (local variables) ---
; r12 = hour, r13 = minute, r14 = second
mov al, 4
out 0x70, al
in al, 0x71
call bcd2bin
mov r12, rax
mov al, 2
out 0x70, al
in al, 0x71
call bcd2bin
mov r13, rax
mov al, 0
out 0x70, al
in al, 0x71
call bcd2bin
mov r14, rax
; --- Apply UTC Offset ---
cmp r8, 1
je .utc_add
.utc_sub:
sub r12, r9
jmp .utc_done
.utc_add:
add r12, r9
.utc_done:
; Handle hour wrapping
.utc_wrap_check:
cmp r12, 23
jg .utc_wrap_sub
cmp r12, 0
jl .utc_wrap_add
jmp .apply_12h_mode
.utc_wrap_sub:
sub r12, 24
jmp .utc_wrap_check
.utc_wrap_add:
add r12, 24
jmp .utc_wrap_check
; --- Apply 12-hour formatting if requested ---
.apply_12h_mode:
cmp r10, 1
jne .print_time
; It is 12h mode
cmp r12, 12
jg .subtract_12
cmp r12, 0
je .is_midnight
jmp .print_time ; Hours 1-12 are fine
.subtract_12:
sub r12, 12
jmp .print_time
.is_midnight:
mov r12, 12 ; 0 hour becomes 12 AM
; --- Print the final time ---
.print_time:
mov rax, r12
call print_two_digits
mov al, ':'
call print_char
mov rax, r13
call print_two_digits
mov al, ':'
call print_char
mov rax, r14
call print_two_digits
ret
%%%%_htll_display_clock%%%%