2025-06-11 04:27:15 +00:00
; Bootloader places us in 32 bit mode :)
2025-06-11 03:41:51 +00:00
bits 3 2
2025-06-11 04:27:15 +00:00
;Some symbols we'll need from other files...
2025-06-11 03:41:51 +00:00
extern k e r n _ m a i n
2025-06-11 04:27:15 +00:00
extern g d t r
2025-06-11 03:41:51 +00:00
;This will be our entrypoint function name - gotta initialize it now as global so the linker knows later.
global s t a r t
2025-05-23 05:18:12 +00:00
2025-06-11 03:41:51 +00:00
; Set up for C code. Practically the only requirement for C-generated assembly to work properly is alignment and the presence of a stack.
section . b s s a l i g n =16
2025-05-23 05:18:12 +00:00
stack_bottom :
2025-06-11 03:41:51 +00:00
resb 4 0 9 6 ; 4096 bytes (4kb) large stack. by skipping some amount of data (and eventually filling it with zeroes?), we've essentially just reserved space for our stack.
;Remember, stack grows DOWNWARD! So the last thing in the section -> the highest memory address -> the very first thing on the stack!
;Therefore, we put a label here to represent the top of our stack for later.
2025-05-23 05:18:12 +00:00
stack_top :
2025-06-11 04:27:15 +00:00
;We're gonna throw the TSS here too.
2025-06-10 13:17:21 +00:00
tss :
2025-06-11 03:41:51 +00:00
resb 1 0 4
2025-05-23 05:18:12 +00:00
2025-06-10 13:17:21 +00:00
2025-06-11 03:41:51 +00:00
;Actual code. Entry point goes here!
section . t e x t
;Here it is!
2025-05-23 05:18:12 +00:00
start :
2025-06-11 03:41:51 +00:00
;We made a GDT! Let's use it!
2025-06-11 03:58:28 +00:00
lgdt [ g d t r ]
;Now we start by setting the code segment (CS) with a far jump...
jmp 0 x08 : s e g m e n t _ b e _ g o n e
2025-06-11 03:41:51 +00:00
2025-06-11 03:58:28 +00:00
segment_be_gone :
;Now we go ahead and dump our kernel mode data segment (0x10) into the rest of our segments.
;Also preserving eax because it has the bootloader's multiboot magic in it and I want to check it in main.
mov c x , 0 x10
mov d s , c x
mov e s , c x
mov s s , c x
mov f s , c x
mov g s , c x
;Lets set up the stack. Stack grows downward on x86. We did the work earlier of defining where the top of the stack is, so just tell esp.
2025-06-11 03:41:51 +00:00
mov e s p , s t a c k _ t o p ;set the stack pointer
push e b x
push e a x
;To C-land!
2025-05-23 05:18:12 +00:00
call k e r n _ m a i n
2025-06-11 03:41:51 +00:00
;You should never get here, but in case you do, we will just hang.
2025-05-23 05:18:12 +00:00
hang :
2025-06-11 03:41:51 +00:00
cli ;Interrupts: off
hlt ;Halt!
jmp h a n g ;just in case...
2025-05-23 05:18:12 +00:00
2025-06-11 03:58:28 +00:00