Amazon_Prime

Untitled

Jan 21st, 2022 (edited)
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Declare constants for the multiboot header.
  2. MAGIC equ 0x1BADB002
  3. FLAG_VIDEO_MODE equ (1 << 2)
  4. FLAGS equ FLAG_VIDEO_MODE
  5. CHECKSUM equ -(MAGIC + FLAGS) & 0xFFFFFFFF
  6. HEADER_ADDR equ 0 ; if flags[16] is set
  7. LOAD_ADDR equ   0 ; if flags[16] is set
  8. LOAD_END_ADDR equ   0 ; if flags[16] is set
  9. BSS_END_ADDR equ 0 ; if flags[16] is set
  10. ENTRY_ADDR equ 0 ; if flags[16] is set
  11. MODE_TYPE equ 0 ; if flags[2] is set
  12. WIDTH equ 0 ; if flags[2] is set
  13. HEIGHT equ 0 ; if flags[2] is set
  14. DEPTH equ 0x20 ; if flags[2] is set
  15.  
  16. ; Declare a multiboot header that marks the program as a kernel. These are magic
  17. ; values that are documented in the multiboot standard. The bootloader will
  18. ; search for this signature in the first 8 KiB of the kernel file, aligned at a
  19. ; 32-bit boundary. The signature is in its own section so the header can be
  20. ; forced to be within the first 8 KiB of the kernel file.
  21. section .multiboot
  22. align 4
  23.     dd MAGIC
  24.     dd FLAGS
  25.     dd CHECKSUM
  26.     dd HEADER_ADDR
  27.     dd LOAD_ADDR
  28.     dd LOAD_END_ADDR
  29.     dd BSS_END_ADDR
  30.     dd ENTRY_ADDR
  31.     dd MODE_TYPE
  32.     dd WIDTH
  33.     dd HEIGHT
  34.     dd DEPTH
  35.    
  36.  
  37. ; The multiboot standard does not define the value of the stack pointer register
  38. ; (esp) and it is up to the kernel to provide a stack. This allocates room for a
  39. ; small stack by creating a symbol at the bottom of it, then allocating 16384
  40. ; bytes for it, and finally creating a symbol at the top. The stack grows
  41. ; downwards on x86. The stack is in its own section so it can be marked nobits,
  42. ; which means the kernel file is smaller because it does not contain an
  43. ; uninitialized stack. The stack on x86 must be 16-byte aligned according to the
  44. ; System V ABI standard and de-facto extensions. The compiler will assume the
  45. ; stack is properly aligned and failure to align the stack will result in
  46. ; undefined behavior.
  47. section .bss
  48. align 16
  49. stack_bottom:
  50. resb 16384 ; 16 KiB
  51. stack_top:
  52.  
  53. ; The linker script specifies _start as the entry point to the kernel and the
  54. ; bootloader will jump to this position once the kernel has been loaded. It
  55. ; doesn't make sense to return from this function as the bootloader is gone.
  56. ; Declare _start as a function symbol with the given symbol size.
  57. section .text
  58. global _start:function (_start.end - _start)
  59. _start:
  60.     ; The bootloader has loaded us into 32-bit protected mode on a x86
  61.     ; machine. Interrupts are disabled. Paging is disabled. The processor
  62.     ; state is as defined in the multiboot standard. The kernel has full
  63.     ; control of the CPU. The kernel can only make use of hardware features
  64.     ; and any code it provides as part of itself. There's no printf
  65.     ; function, unless the kernel provides its own <stdio.h> header and a
  66.     ; printf implementation. There are no security restrictions, no
  67.     ; safeguards, no debugging mechanisms, only what the kernel provides
  68.     ; itself. It has absolute and complete power over the
  69.     ; machine.
  70.  
  71.     ; To set up a stack, we set the esp register to point to the top of our
  72.     ; stack (as it grows downwards on x86 systems). This is necessarily done
  73.     ; in assembly as languages such as C cannot function without a stack.
  74.     mov esp, stack_top
  75.  
  76.     ; This is a good place to initialize crucial processor state before the
  77.     ; high-level kernel is entered. It's best to minimize the early
  78.     ; environment where crucial features are offline. Note that the
  79.     ; processor is not fully initialized yet: Features such as floating
  80.     ; point instructions and instruction set extensions are not initialized
  81.     ; yet. The GDT should be loaded here. Paging should be enabled here.
  82.     ; C++ features such as global constructors and exceptions will require
  83.     ; runtime support to work as well.
  84.  
  85.     ; Enter the high-level kernel. The ABI requires the stack is 16-byte
  86.     ; aligned at the time of the call instruction (which afterwards pushes
  87.     ; the return pointer of size 4 bytes). The stack was originally 16-byte
  88.     ; aligned above and we've since pushed a multiple of 16 bytes to the
  89.     ; stack since (pushed 0 bytes so far) and the alignment is thus
  90.     ; preserved and the call is well defined.
  91.         ; note, that if you are building on Windows, C functions may have "_" prefix in assembly: _kernel_main
  92.     extern main
  93.  
  94.     push eax
  95.     call main
  96.  
  97.     ; If the system has nothing more to do, put the computer into an
  98.     ; infinite loop. To do that:
  99.     ; 1) Disable interrupts with cli (clear interrupt enable in eflags).
  100.     ;    They are already disabled by the bootloader, so this is not needed.
  101.     ;    Mind that you might later enable interrupts and return from
  102.     ;    kernel_main (which is sort of nonsensical to do).
  103.     ; 2) Wait for the next interrupt to arrive with hlt (halt instruction).
  104.     ;    Since they are disabled, this will lock up the computer.
  105.     ; 3) Jump to the hlt instruction if it ever wakes up due to a
  106.     ;    non-maskable interrupt occurring or due to system management mode.
  107.     cli
  108. .hang:  hlt
  109.     jmp .hang
  110. .end:
  111.  
  112.     %include "CPU/interrupt.asm"
Add Comment
Please, Sign In to add comment