Advertisement
MichaelPetch

SO79013023 - first_stage.asm

Sep 23rd, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. ;
  2. ; By alpluspluss 09/22/2024 AD
  3. ;
  4. ; first stage bootloader
  5. ; for x86_64
  6.  
  7. [BITS 16]
  8. [ORG 0x7C00]
  9.  
  10. start:
  11. ; setup segments & stack at 0x9C00
  12. ; because it can overlap some BIOS data / video memory
  13. xor ax, ax
  14. mov ds, ax
  15. mov es, ax
  16. mov ss, ax
  17. mov sp, 0x7C00
  18.  
  19. mov [bootDrive], dl
  20.  
  21. mov cx, 0x0002 ; sector 2 (CH = 0, CL = 2)
  22. mov bx, 0x7E00 ; load stage 2 at memory address 0x7E00
  23. mov dl, [bootDrive] ; load boot drive number
  24.  
  25. load_loop:
  26. mov ah, 0x02 ; BIOS read sector function
  27. mov al, 3 ; Read 3 sectors (second stage)
  28. mov ch, 0 ; head number
  29. int 0x13 ; read disk op
  30. jc disk_error ; jump if disk error
  31.  
  32. ; print 'Loaded stage 2 booloader'
  33. mov si, successMessage
  34. call print_16bit
  35.  
  36. mov dl, [bootDrive]
  37. jmp 0x0000:0x7E00 ; jump to segment 0x0000 at offset 0x7E00
  38.  
  39. ; print error msg for debugging purpose in case the
  40. ; bootloader broke
  41. disk_error:
  42. mov si, diskErrorMessage
  43. call print_16bit
  44. mov al, ah ; store error code in AH
  45. call get_hex_code
  46. cli ; disable interrupt *IMPORTANT*
  47. .hlt:
  48. hlt
  49. jmp .hlt ; inf recursive loop
  50.  
  51. print_16bit:
  52. cld ; clear
  53. lodsb
  54. or al, al
  55. jz done
  56. mov ah, 0x0E ; print letters
  57. int 0x10 ; BIOS interrupt
  58. jmp print_16bit
  59. done:
  60. ret
  61.  
  62. get_hex_code:
  63. push ax
  64. push bx
  65. mov bx, ax
  66. shr al, 4
  67. call print_hex
  68. mov al, bl
  69. and al, 0x0F
  70. call print_hex
  71. pop bx
  72. pop ax
  73. ret
  74.  
  75. print_hex:
  76. and al, 0x0F
  77. add al, '0'
  78. cmp al, '9'
  79. jle .print
  80. add al, 7
  81. .print:
  82. mov ah, 0x0E
  83. int 0x10
  84. ret
  85.  
  86. diskErrorMessage db 'Disk error code ', 0
  87. successMessage db 'Loaded bootloader stage 2', 13, 10, 0
  88. bootDrive db 0
  89.  
  90. ; keeps the file limit as 512 otherwise it returns a negative value
  91. times 510 - ($ - $$) db 0
  92. ; boot signature
  93. dw 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement