Advertisement
MichaelPetch

SO78999226 - stage1.asm

Sep 18th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. [BITS 16] ; We are working in 16-bit Real Mode
  2. [org 0x7c00] ; The origin (starting address) of the bootloader in memory, which is 0x7C00 as loaded by the BIOS.
  3. ; This is the physical address where the bootloader is loaded into memory.
  4.  
  5. start: ; Start of execution, this label marks the entry point of the code.
  6. jmp main ; Jump to the 'main' label to start execution.
  7.  
  8. main: ; Main routine of the bootloader begins here.
  9.  
  10. ; -------------------------
  11. ; Setup segment registers
  12. ; -------------------------
  13. cld ; Set direction forward (DF=0) for string instructions (STOS, LODS, CMPS etc)
  14. cli ; Clear interrupts to ensure no interrupts occur while setting up segments.
  15. xor ax, ax ; Set AX to 0x0 (which is 0x0 >> 4).
  16. ; Explanation: We are using segment:offset addressing in real mode.
  17. ; Physical address = Segment * 16 + Offset
  18. ; So, the segment 0x0 * 16 = 0x0 (physical address).
  19. mov ds, ax ; Set Data Segment (DS) to 0x0. DS points to the bootloader code/data in memory.
  20. mov es, ax ; Set Extra Segment (ES) to 0x0. ES is also set to point to our code/data.
  21.  
  22. ; -------------------------
  23. ; Setup stack
  24. ; -------------------------
  25. mov ss, ax ; Set Stack Segment (SS) to 0 (base of memory).
  26. mov sp, 0xFFFE ; Set the Stack Pointer (SP) to the highest address within the current 64KB segment (0x0000:0xFFFE).
  27. ; In real mode, the stack grows downward from 0xFFFE and 0xFFFE should be set to an even offset like 0xFFFE, not an odd one.
  28.  
  29. sti ; Re-enable interrupts after segment and stack setup is complete.
  30.  
  31. ; -------------------------
  32. ; Load Stage 2 bootloader from disk
  33. ; -------------------------
  34. mov ah, 02h ; BIOS Interrupt 13h, Function 02h: Read sectors from the disk.
  35. mov al, 02h ; Read 1 sector from the disk (this corresponds to the size of a sector, which is 512 bytes).
  36.  
  37. mov ch, 00h ; Set Cylinder number to 0 (since both Stage 1 and Stage 2 are on Cylinder 0).
  38. mov cl, 02h ; Set Sector number to 2 (Stage 1 is in Sector 1, so Stage 2 starts at Sector 2).
  39. mov dh, 00h ; Set Head number to 0 (assuming we are using Head 0 for now).
  40. mov dl, 80h ; Use the first hard drive (usually 0x80 for the primary hard disk).
  41.  
  42. mov bx, 0x8000 ; Set BX to 0x8000, the offset address where Stage 2 will be loaded.
  43. ; Stage 2 will be loaded into memory using segment:offset addressing.
  44. ; ES = 0x0, BX = 0x8000, so the physical address = ES * 16 + BX.
  45. ; Formula: 0x0 * 16 + 0x8000 = 0x0 + 0x8000 = 0x8000 (the physical address where Stage 2 is loaded).
  46.  
  47. int 13h ; Call BIOS interrupt 13h to read the specified sectors into memory.
  48.  
  49. jc disk_read_error ; If carry flag is set (indicating an error), jump to the error handler.
  50.  
  51. pass: ; If the disk read was successful (carry flag is cleared), continue from here.
  52. jmp 0x0000:0x8000 ; Jump to the loaded Stage 2 at address 0x0000:0x8000 (this is where Stage 2 resides).
  53. ; Here, 0x0000 is the segment, and 0x8000 is the offset.
  54. ; Physical address = 0x0000 * 16 + 0x8000 = 0x8000, where Stage 2 is loaded.
  55.  
  56. disk_read_error:
  57. int 18h ; If the disk read fails, call INT 18h to attempt a boot from a different device (like network boot).
  58. ; This error message will occur --> IO write(0x01f0): current command is 20h displayed on bochs emulator.
  59.  
  60. TIMES 510-($-$$) DB 0 ; Pad the bootloader to ensure it is exactly 512 bytes, with zeros filling the remaining space.
  61. DW 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement