Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 5.73 KB | None | 0 0
  1. [BITS 16]
  2. [ORG 0x7C00]
  3.  
  4. %define GlobalStackSegment  0x9000
  5. %define GlobalStackTop      0xFFFF
  6. %define GlobalStage2Start   0x7E00
  7. %define BootDevice      0x500   ; 0x00 = floppy 1, 0x01 = floppy 2, 0x80 = hdd 1, 0x81 = hdd 2
  8. %define BootDeviceType      0x501   ; 0 = floppy, 1 = hard drive
  9. %define SectorCount     0x502   ; the number of sectors on the boot device, starting at 1
  10. %define HeadCount       0x503   ; the number of heads on the boot device
  11. %define CylinderCount       0x504   ; the number of cylinders on the boot device
  12. %define DriveCount      0x506   ; the number of drives on the system
  13.  
  14. ; jump to our entry point (and set CS to 0)
  15. jmp 0x0:main
  16.  
  17. ; global variables
  18. NewLine: db 13, 10, 0
  19. CaptionMsg: db "iNES Boot Loader v0.1", 13, 10, "Loading... ", 13, 10, 0
  20. SectorLoadErr: db "Error loading sector.", 13, 10, 0
  21. WrongDiskErr: db "iNES may only be run from a floppy or hard disk.", 13, 10, 0
  22. InfoLoadErr: db "Error loading disk information.", 13, 10, 0
  23. HelloWorld: db "Hello from Sector 2!", 13, 10, 0
  24.  
  25.  
  26. ; entry point
  27. main:
  28.     ; setup our segment registers
  29.     mov AX, 0
  30.     mov DS, AX
  31.     mov ES, AX
  32.     mov GS, AX
  33.    
  34.     ; setup our stack
  35.     mov AX, GlobalStackSegment
  36.     mov SS, AX
  37.     mov SP, GlobalStackTop
  38.    
  39.     ; Display our loading message
  40.  
  41.     ; copy the ID of the device that loaded this boot sector,
  42.     ; and then determine whether it is a floppy or hard drive.
  43.     mov [BootDevice], DL
  44.     mov DX, 0
  45.     cmp byte [BootDevice], 0x80
  46.     je .setHardDriveType
  47.     cmp byte [BootDevice], 0x81
  48.     je .setHardDriveType
  49.     cmp byte [BootDevice], 0x00
  50.     je .setFloppyDriveType
  51.     cmp byte [BootDevice], 0x01
  52.     je .setFloppyDriveType
  53.    
  54.     ; an unsupported boot device (non-floppy and non-hard drive)
  55.     ; was used.
  56.     .unsupportedBootDevice:
  57.         push WrongDiskErr
  58.         call PrintString
  59.         call HandleFatalError
  60.        
  61.     .setHardDriveType:
  62.         mov byte [BootDeviceType], 1
  63.         jmp .continueBoot1
  64.     .setFloppyDriveType:
  65.         mov byte [BootDeviceType], 0
  66.         jmp .continueBoot1
  67.        
  68. .continueBoot1:
  69.     ; display the caption
  70.     push CaptionMsg
  71.     call PrintString
  72.    
  73.     ; now we need to grab boot disk information
  74.     call GetBootDiskInfo
  75.    
  76.     ; load sector #2
  77.     mov AX, 1
  78.     mov BX, 0x7E00
  79.     call LoadSector
  80.    
  81.     jmp 0x7E00
  82.    
  83.     cli
  84.     hlt
  85.    
  86.  
  87. ;; PrintString(const char *str)
  88. ;; The procedure takes a single WORD argument.
  89. ;; This procedure is called to display a NULL-terminated string
  90. ;; to the terminal.
  91. ;; Controls is returned back to the caller after all charactered are printed.
  92. ;;
  93. ;; i.e.
  94. ;;
  95. ;; push someString
  96. ;; call PrintString
  97. PrintString:
  98.     enter 0, 0
  99.     mov SI, [SS:BP + 4] ; get the first (and only) argument from the stack
  100.     pusha           ; save the rest of the registers
  101.  
  102.     mov AH, 0x0E
  103.     mov BH, 0x00
  104.     mov BL, 0x07
  105.    
  106.     .printChar:
  107.         lodsb       ; load string from SI into AL
  108.         or AL, AL
  109.         jz .endPrintChar
  110.        
  111.         int 0x10
  112.         jmp .printChar
  113.     .endPrintChar:
  114.     popa            ; restore the rest of the registers
  115.     leave
  116. ret 2
  117.  
  118. ;; GetBootDiskInfo()
  119. ;; Initializes boot disk information such as number of sectors, cylinders, heads, etc.
  120. ;;
  121. ;; i.e.
  122. ;;
  123. ;; call GetBootDiskInfo
  124. ;;
  125. GetBootDiskInfo:
  126.     enter 0, 0
  127.     pusha
  128.    
  129.     mov AH, 0x08 ; read drive parameters function
  130.     mov DL, [BootDevice]
  131.     int 0x13 ; BIOS interrupt
  132.     jnc .setBootDiskInfo
  133.     ; if clear flag is set, an error occurred
  134.     push InfoLoadErr
  135.     call PrintString
  136.     call HandleFatalError
  137.    
  138.     ; no error occurred, we can set our boot data
  139.     .setBootDiskInfo:
  140.         mov [DriveCount], DL
  141.         mov [HeadCount], DH
  142.         mov [SectorCount], CL
  143.         and byte [SectorCount], 00111111b
  144.         and CX, 1111111111000000b
  145.         mov [CylinderCount], CX
  146.     popa
  147.     leave
  148. ret
  149.  
  150. ;; LoadSector()
  151. ;; Loads the given 0-indexed LBA sector into buffer ES:BX.
  152. ;; AX = sector to load
  153. ;; ES:BX = buffer to load to
  154. LoadSector:
  155.     enter 0, 0
  156.     pusha
  157.    
  158.    
  159.     ; first we need to convert the given LBA sector into a CHS tuple
  160.     ; C = LBA / (sectors per track * heads per cylinder)
  161.     ; H = (LBA / sectors per track) % heads per cylinder
  162.     ; S = (LBA % sectors per track) + 1
  163.     div byte [SectorCount] ; AX / SectorCount, AL = quotient, AH = remainder
  164.     mov CL, AH  ; S = (LBA % sectors per track) + 1
  165.     add CL, 1   ; ^^^
  166.     and CL, 00111111b ; S is only 6 bits.
  167.    
  168.     mov AH, 0   ; AX = AL
  169.     div byte [HeadCount]    ; AX / HeadCount, AL = quotient, AH = remainder
  170.     mov DH, AH  ; H = (LBA / sectors per track) % heads per cylinder
  171.    
  172.     mov AX, 0
  173.     mov AL, byte [SectorCount]
  174.     mul byte [HeadCount]    ; AX = AL * HeadCount
  175.     mov DI, AX      ; DI = AX
  176.     mov DX, 0
  177.     pop AX          ; AX = LBA
  178.     div DI          ; LBA / (sectors per track * heads per cylinder),  AX = quotient, DX = remainder
  179.                 ; C = LBA / (sectors per track * heads per cylinder)
  180.     and AX, 0000001111111111b
  181.     shl AX, 6
  182.     mov CH, 0
  183.     or CX, AX
  184.    
  185.     ; CL = sector
  186.     ; CH = cylinder
  187.     ; DH = head
  188.     mov DL, [BootDevice]
  189.     mov AL, 1   ; number of sectors to read
  190.     mov AH, 0x02    ; read sector function
  191.     ; ES:BX = buffer
  192.     ; now we can actually read the sector
  193.     mov SI, 0
  194.     .tryLoadSector:
  195.         mov AH, 0x00 ;reset drive
  196.         int 0x13
  197.        
  198.         mov AH, 0x02
  199.         mov BX, 0x7E00
  200.         int 0x13
  201.         jnc .finishLoadSector
  202.         inc SI
  203.         cmp SI, 5
  204.         je .badFinishLoadSector
  205.         jmp .tryLoadSector
  206.    
  207.     ; something went wrong
  208.     .badFinishLoadSector:
  209.         push SectorLoadErr
  210.         call PrintString
  211.         mov AL, AH
  212.         add AL, 48
  213.         mov AH, 0x0E
  214.         mov BH, 0x00
  215.         mov BL, 0x07
  216.         int 0x10
  217.         call HandleFatalError
  218.    
  219.     ; everything is OK!
  220.     .finishLoadSector:
  221.    
  222.     popa
  223.     leave
  224. ret
  225.  
  226. ;; HandleFatalError()
  227. ;; This procedure is called when an unrecoverable error occurs.
  228. ;; Control is NOT handed back to the caller.
  229. ;;
  230. ;; i.e.
  231. ;;
  232. ;; push errorString
  233. ;; call PrintString
  234. ;; call HandleFatalError
  235. HandleFatalError:
  236.     enter 0, 0
  237.     cli
  238.     hlt
  239.     leave ; <-- this is never called, but put it for consistency
  240. ret
  241. ; ----------------------- SIGNATURE --------------------
  242. times 510-($-$$) db 0
  243. dw 0xAA55
  244.  
  245. push HelloWorld
  246. call PrintString
  247. cli
  248. hlt
  249.  
  250. times 1024-($-$$) db 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement