Advertisement
MichaelPetch

SO78992609 - first_stage.asm

Sep 17th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. [BITS 16]
  2. [ORG 0x7C00]
  3.  
  4. start:
  5. ; Set up segments and stack
  6. xor ax, ax
  7. mov ds, ax
  8. mov es, ax
  9. mov ss, ax
  10. mov sp, 0x7000 ; Move stack below bootloader
  11.  
  12. ; Store boot drive number
  13. mov [bootDrive], dl
  14.  
  15. ; Load Stage 2 in chunks (3 sectors at a time, total 32 sectors)
  16. mov cx, 0x0002 ; Start at sector 2 (CH=0, CL=2)
  17. mov bx, 0x7E00 ; Start loading at memory address 0x8000
  18. mov dl, [bootDrive] ; Load the boot drive number
  19.  
  20. load_loop:
  21. mov ah, 0x02 ; BIOS read sector function
  22. mov al, 3 ; Read 3 sectors at a time
  23. mov ch, 0 ; Cylinder number
  24. mov dh, 0 ; Head number
  25. int 0x13 ; BIOS interrupt to read disk
  26. jc disk_error ; Jump if error
  27.  
  28. ; Advance to next set of sectors
  29. add cx, 3 ; Increment sector number
  30. add bx, 1536 ; Move memory address (3 * 512 = 1536 bytes)
  31.  
  32. cmp cx, 34 ; Stop when sector >= 34 (32 sectors total from 2)
  33. jl load_loop ; Continue loading until all sectors are read
  34.  
  35. ; Print success message
  36. mov si, successMsg
  37. call print_string
  38.  
  39. ; Jump to stage 2 at 0x8000
  40. jmp 0x0000:0x7E00
  41.  
  42. disk_error:
  43. ; Display error and halt
  44. mov si, diskErrorMsg
  45. call print_string
  46. mov al, ah ; Error code in AH
  47. call print_hex
  48. cli ; Disable interrupts
  49. .halt:
  50. hlt ; Halt CPU
  51. jmp .halt ; Loop forever
  52.  
  53. print_string:
  54. lodsb
  55. or al, al
  56. jz done
  57. mov ah, 0x0E
  58. int 0x10
  59. jmp print_string
  60. done:
  61. ret
  62.  
  63. print_hex:
  64. push ax
  65. push bx
  66. mov bx, ax
  67. shr al, 4
  68. call print_hex_digit
  69. mov al, bl
  70. and al, 0x0F
  71. call print_hex_digit
  72. pop bx
  73. pop ax
  74. ret
  75.  
  76. print_hex_digit:
  77. and al, 0x0F
  78. add al, '0'
  79. cmp al, '9'
  80. jle .print
  81. add al, 7
  82. .print:
  83. mov ah, 0x0E
  84. int 0x10
  85. ret
  86.  
  87. diskErrorMsg db 'Disk Error! Code: ', 0
  88. successMsg db 'Stage 2 loaded successfully!', 13, 10, 0
  89. bootDrive db 0
  90.  
  91. times 510 - ($ - $$) db 0
  92. dw 0xAA55 ; Boot signature
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement