flat assembler
Message board for the users of flat assembler.

Index > OS Construction > CD/DVD/BD Loader: INT 13h, Function 42 help...

Goto page Previous  1, 2, 3
Author
Thread Post new topic Reply to topic
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 09 Mar 2012, 00:39
I wonder if there is a difference between

TIMES 33 db 0

and

db 33 dup(0)

Shocked
Post 09 Mar 2012, 00:39
View user's profile Send private message Send e-mail Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Mar 2012, 03:03
revolution wrote:
smiddy: Are you sure that 0x1fe stack is enough? Have you determined why you code failed on loading to 0xf000? I can understand why 0xf800 failed because of stack overwriting but why fail at 0xf000 unless the stack usage is more than 0x800.
It started at SS=0 and SP=0, it decremented towards 0:FFFF, 0:FFFE, ..., 0:F000 or somewhere within that 2048 bytes, I assume. I reverted back from using single sector loads to 32 sectors per load, and that also worked now. I researched the INT 13h Function 42h buffer, and I couldn't find anything to indicate it writes outside of the BIOS area. I will continue to monitor it to see how it goes, but for now it loads my OS and the OS runs as expected (minus the bugs it already had). Smile
Post 09 Mar 2012, 03:03
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Mar 2012, 03:28
edfed wrote:
Smile it looks like a tutorial with all the comments. cool! Very Happy
That is kind of the plan. My in line with my own learning versus coming back to it many years later. I wrote my OS originally in 2005 and it wasn't much, but now I'm having fits trying to remember what I did and in the places where I didn't comment I am having to relearn it. That's not too bad, because that is the point of what I'm doing, to learn to code and to learn the x86 hardware better. Smile Thanks for the complement!
Post 09 Mar 2012, 03:28
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 09 Mar 2012, 03:29
shutdownall wrote:
I wonder if there is a difference between

TIMES 33 db 0

and

db 33 dup(0)

Shocked
Convention? I like the way you did the second one better perhaps, uses less space. Wink
Post 09 Mar 2012, 03:29
View user's profile Send private message Reply with quote
den010



Joined: 28 Jul 2014
Posts: 18
Location: Metro Manila - Muntinlupa City
den010 03 Aug 2014, 13:06
Hi Smiddy,

Can you share to me your whole working code? or post it here instead?


Thank you

_________________
I'm a Completely Newbie : Need help much Sad
Post 03 Aug 2014, 13:06
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 03 Aug 2014, 13:14
Nope. I'm not ready to.
Post 03 Aug 2014, 13:14
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 07 Dec 2014, 04:41
den010 here ya go man, hope it helps ya.

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                           ;;
;; Project          :   smiddyOS (CD-ROM Booter)                             ;;
;; Author           :   smiddy                                               ;;
;; Website          :                                                        ;;
;; Date             :   December 6, 2014                                     ;;
;; Quick Information:   Compact Disc bootloader beta to load bootable        ;;
;;                      software.                                            ;;
;; Filename         :   cdboot3.asm                                          ;;
;; Assembler Command:   Using Fresh IDE v2.2.2                               ;;
;;                                                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                           ;;
;; Usage:  Using CDBOOT3.BIN as the boot loader from the created ISO from    ;;
;;         the CD-ROM creating CDROM.ASM from within Fresh IDE. Please       ;;
;;         visit:                                                            ;;
;;                                                                           ;;
;;  http://fresh.flatassembler.net/index.cgi?page=content/2_download.txt#H1. ;;
;;                                                                           ;;
;;         You can also build this with FASM, it is after all in the FASM    ;;
;;         sematics. Visit http://flatassembler.net/ to get the latest       ;;
;;         version. As of this writing, it is 1.71.29, please donate if you  ;;
;;         are able, it is a fantastic work.                                 ;;
;;                                                                           ;;
;;         See CDROM.ASM for further details.                                ;;
;;                                                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                           ;;
;; v1.00  - Beta release; CD-ROM compatible booter, capable of booting       ;;
;;          bootable software, in this case SMIDDYOS.EXE. Change the file    ;;
;;          name to one you wish to load instead. The loader expects to be   ;;
;;          booted at 0060:0000 (600h linear) and the bootable software gets ;;
;;          loaded at 0200:0000 (2000h linear) which is launched from        ;;
;;          0202:0000 (2020h linear) since SMIDDYOS.EXE is a MZ EXE (DOS     ;;
;;          executeable). You can change the JMP to what ever you wish to    ;;
;;          make your code work.                                             ;;
;;                                                                           ;;
;;          Limitations include that the boot file needs to be in the root   ;;
;;          directory and the root directory can not be longer than one CD   ;;
;;          sector (2048 bytes) in size.                                     ;;
;;                                                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   format binary as "bin"               ; Tell FASM to make an BIN image

   ORG 0

BootSector:                             ; Label in order to determine size of
                                        ; code for padding at the end.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Start Boot Procedure                                                      ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Begin:                                  ; Begining of instructions

    CLI                                 ; Turn off interrupts
    mov ax,0060h                        ; Boot Segment (no need to copy there)
    mov ds,ax
    mov es,ax
    MOV [CDDriveNumber],DL              ; Save boot drive, right away
    MOV AX,7000h
    MOV DS,AX                           ; Segment saved variables for SmiddyOS
    MOV BX,535h                         ; Offset of boot drive saved variable
    MOV [DS:BX],DL                      ; Save the boot drive for SmiddyOS use
    MOV AX,60h
    MOV DS,AX                           ; Reset segment
    XOR AX,AX                           ; Zero out the stack segment
    MOV SS,AX
    MOV SP,5FEh                         ; 0000:05FE is the stack for loader
    STI                                 ; Turn interrupts back on

    MOV SI,WelcomeMessage               ; Tell everyone hello
    CALL PrintString                    ; Print the welcome message
    MOV SI,TheEndOfTheLine              ; Go to the next line
    CALL PrintString

    MOV AL,[CDDriveNumber]              ; Load drive number in for comparison
    CMP AL,0                            ; Compare to known BAD drive number for
                                        ; CD/DVD/BD
    JE Failure                          ; If it is, it failed.

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; Get Status (Bootable CD-ROM)                                          ;;
    ;;                                                                       ;;
    ;; Call with: AX = 4B01H                                                 ;;
    ;; DL = Drive number                                                     ;;
    ;; DS:SI = Empty specification packet (needs to be 13h in size)          ;;
    ;;                                                                       ;;
    ;; Return: If Function Successful,                                       ;;
    ;; Carry Flag = Clear                                                    ;;
    ;;                                                                       ;;
    ;; If Function Unsuccessful,                                             ;;
    ;; Carry Flag = Set                                                      ;;
    ;; AX = Status code                                                      ;;
    ;;                                                                       ;;
    ;; DS:SI = Specification packet filled                                   ;;
    ;;       Offset Size Description                                         ;;
    ;;       00h    db   size of packet in bytes (13h)                       ;;
    ;;       01h    db   boot media type                                     ;;
    ;;                   Bit(s) Description                                  ;;
    ;;                   3-0    media type.                                  ;;
    ;;                   0000   no emulation.                                ;;
    ;;                   0001   1.2M diskette.                               ;;
    ;;                   0010   1.44M diskette.                              ;;
    ;;                   0011   2.88M diskette.                              ;;
    ;;                   0100   hard disk                                    ;;
    ;;                   Other reserved                                      ;;
    ;;                   5-4    reserved (0)                                 ;;
    ;;                   6      image contains ATAPI driver                  ;;
    ;;                   7      image contains SCSI driver(s)                ;;
    ;;       02h    db   drive number                                        ;;
    ;;                   00h floppy image                                    ;;
    ;;                   80h bootable hard disk                              ;;
    ;;                   81h-FFh nonbootable or no emulation drive           ;;
    ;;       03h    db   CD-ROM controller number                            ;;
    ;;       04h    dd   Logical Block Address (LBA) of disk image to emulate;;
    ;;       08h    dw   device specification (same as boot media type plus) ;;
    ;;                   (IDE) bit 0: Drive is slave instead of master       ;;
    ;;                   (SCSI)  bits 7-0: LUN and PUN                       ;;
    ;;                   bits 15-8: Bus number                               ;;
    ;;       0Ah    dw   segment of 3K buffer for caching CD-ROM reads       ;;
    ;;       0Ch    dw   load segment for initial boot image (if 0000h, load ;;
    ;;                   at segment 07C0h; only valid for INT 13h/AH=4Ch)    ;;
    ;;       0Eh    dw   number of 512-byte virtual sectors to load (only    ;;
    ;;                   valid for INT 13h/AH=4Ch)                           ;;
    ;;       10h    db   low byte of cylinder count (for INT 13/AH=08h)      ;;
    ;;       11h    db   sector count, high bits of cylinder count           ;;
    ;;                   (for INT 13h/AH=08h)                                ;;
    ;;       12h    db   head count (for INT 13h/AH=08h)                     ;;
    ;;                                                                       ;;
    ;; Comments:                                                             ;;
    ;;                                                                       ;;
    ;; The Function is used to get the status of the CD/DVD/BD. This function;;
    ;; is called first for "no emulation" booting of optical media. If the   ;;
    ;; Function is successful, the carry flag is clear, else carry flag is   ;;
    ;; set and AX register returns status code.                              ;;
    ;;                                                                       ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GetBootedCDStatus:

    MOV DL,[CDDriveNumber]              ; Prepare to read CD-ROM, load boot
                                        ;    drive
    MOV AH,4Bh                          ; Use function 4Bh
    MOV AL,1
    MOV SI,DiskResultsBuffer            ; Results buffer to load
    INT 13h                             ; Call Check Extensions Present?

    JC Failure                          ; Need to engage the Failure Procedure

    MOV AH,255                          ; Load proposed error code (255 is our
                                        ;   failure code for drive number miss
                                        ;   detected)
    MOV DL,[DiskResultsBuffer+2]        ; Load resultant drive information
    CMP [CDDriveNumber],DL              ; Compare to see if they are the same
                                        ;    (they should be)
    JNE Failure                         ; If they are not, Failure

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; More information can be gathered here if needed later, not needed at  ;;
    ;; this time.                                                            ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; Check INT 13h Extensions Present                                      ;;
    ;;                                                                       ;;
    ;; Call with: AH = 41H                                                   ;;
    ;; DL = Drive number (80h - FFh; but we're interested in CDs for this    ;;
    ;;         exercise)                                                     ;;
    ;; BX = 55AAh (Signature?)                                               ;;
    ;;                                                                       ;;
    ;; Return: If Function Successful,                                       ;;
    ;; Carry Flag = Clear                                                    ;;
    ;; BX = AA55h if installed                                               ;;
    ;; AH = major version of extensions                                      ;;
    ;;    01h = 1.x                                                          ;;
    ;;    20h = 2.0 / EDD-1.0                                                ;;
    ;;    21h = 2.1 / EDD-1.1                                                ;;
    ;;    30h = EDD-3.0                                                      ;;
    ;; AL = internal use                                                     ;;
    ;; CX = API subset support bitmap                                        ;;
    ;;      1 - Device Access using packet structure (INT 13h AH=42h-44h,    ;;
    ;;             47h,48h)                                                  ;;
    ;;      2 - Drive Locking and Ejecting (INT 13h AH=45h,46h,48h,49h,      ;;
    ;;             INT 15h AH=52h)                                           ;;
    ;;      4 - Enhanced Disk Drive Support (INT 13h AH=48h,AH=4Eh)          ;;
    ;; DH = extension version (v2.0+ ??? -- not present in 1.x)              ;;
    ;;                                                                       ;;
    ;; If Function Unsuccessful,                                             ;;
    ;; Carry Flag = Set                                                      ;;
    ;; AH = 01h (invalid function)                                           ;;
    ;;                                                                       ;;
    ;; Comments:                                                             ;;
    ;;                                                                       ;;
    ;; The Function is used to see if INT 13h extensions are present (not    ;;
    ;; really needed if you can do the previous function, right?)            ;;
    ;;                                                                       ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GetINT13hExtenstions:

    MOV DL,[CDDriveNumber]              ; Prepare to read CD-ROM, load boot
                                        ;    drive
    MOV AH,41h                          ; Use function 41h: Check Extensions
                                        ;    Present
    MOV BX,55AAh                        ; Signature?
    INT 13h                             ; Call Check Extensions Present?
                                        ;    (This makes no sense if you try
                                        ;    INT 13h Function 4B01h, but
                                        ;    details are provided upon return
                                        ;    if CF isn't set)

    JC Failure                          ; Nope, get out of here...

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; Extended Read Function                                                ;;
    ;;                                                                       ;;
    ;; Call with AH=42h                                                      ;;
    ;; DL = drive number                                                     ;;
    ;; DS:SI = Disk Address Packet (DAP)                                     ;;
    ;;       Offset Size Description                                         ;;
    ;;       00h    db   Size of packet (10h or 18h; we're using 10h here,   ;;
    ;;                      see below)                                       ;;
    ;;       01h    db   Reserved (0)                                        ;;
    ;;       02h    dw   Number of sectors to read (max 007Fh for Phoenix    ;;
    ;;                      EDD)                                             ;;
    ;;       04h    dd   Initial segment:offset where to load the read in    ;;
    ;;                      sector(S)                                        ;;
    ;;       08h    dq   starting absolute sector number (for non-LBA        ;;
    ;;                      devices, compute as                              ;;
    ;;                      (Cylinder*NumHeads + SelectedHead) *             ;;
    ;;                      SectorPerTrack + SelectedSector - 1              ;;
    ;;       10h    dq   (EDD-3.0, optional) 64-bit flat address of transfer ;;
    ;;                      buffer; used if dd at 04h is FFFFh:FFFFh         ;;
    ;;                                                                       ;;
    ;; Return: If Function Successful,                                       ;;
    ;; Carry Flag = Clear                                                    ;;
    ;; AH = 00h                                                              ;;
    ;;                                                                       ;;
    ;; If Function Unsuccessful,                                             ;;
    ;; Carry Flag = Set                                                      ;;
    ;; AH = Error code                                                       ;;
    ;;                                                                       ;;
    ;; Error code:                                                           ;;
    ;;   00h    successful completion                                        ;;
    ;;   01h    invalid function in AH or invalid parameter                  ;;
    ;;   02h    address mark not found                                       ;;
    ;;   03h    disk write-protected                                         ;;
    ;;   04h    sector not found/read error                                  ;;
    ;;   05h    reset failed (hard disk)                                     ;;
    ;;   05h    data did not verify correctly (TI Professional PC)           ;;
    ;;   06h    disk changed (floppy)                                        ;;
    ;;   07h    drive parameter activity failed (hard disk)                  ;;
    ;;   08h    DMA overrun                                                  ;;
    ;;   09h    data boundary error (attempted DMA across 64K boundary or    ;;
    ;;             >80h sectors)                                             ;;
    ;;   0Ah    bad sector detected (hard disk)                              ;;
    ;;   0Bh    bad track detected (hard disk)                               ;;
    ;;   0Ch    unsupported track or invalid media                           ;;
    ;;   0Dh    invalid number of sectors on format (PS/2 hard disk)         ;;
    ;;   0Eh    control data address mark detected (hard disk)               ;;
    ;;   0Fh    DMA arbitration level out of range (hard disk)               ;;
    ;;   10h    uncorrectable CRC or ECC error on read                       ;;
    ;;   11h    data ECC corrected (hard disk)                               ;;
    ;;   20h    controller failure                                           ;;
    ;;   31h    no media in drive (IBM/MS INT 13 extensions)                 ;;
    ;;   32h    incorrect drive type stored in CMOS (Compaq)                 ;;
    ;;   40h    seek failed                                                  ;;
    ;;   80h    timeout (not ready)                                          ;;
    ;;   AAh    drive not ready (hard disk)                                  ;;
    ;;   B0h    volume not locked in drive (INT 13 extensions)               ;;
    ;;   B1h    volume locked in drive (INT 13 extensions)                   ;;
    ;;   B2h    volume not removable (INT 13 extensions)                     ;;
    ;;   B3h    volume in use (INT 13 extensions)                            ;;
    ;;   B4h    lock count exceeded (INT 13 extensions)                      ;;
    ;;   B5h    valid eject request failed (INT 13 extensions)               ;;
    ;;   B6h    volume present but read protected (INT 13 extensions)        ;;
    ;;   BBh    undefined error (hard disk)                                  ;;
    ;;   CCh    write fault (hard disk)                                      ;;
    ;;   E0h    status register error (hard disk)                            ;;
    ;;   FFh    sense operation failed (hard disk)                           ;;
    ;;                                                                       ;;
    ;; Initial DAP:                                                          ;;
    ;;                                                                       ;;
    ;;   db 10h       - Initial Volume Descriptor, which has the important   ;;
    ;;                     data we need to get to the root directory where   ;;
    ;;                     our OS is stored.                                 ;;
    ;;   db 0         - Unused, should be zero                               ;;
    ;;   dw 1         - Number of sectors to read (initial one should be one ;;
    ;;                     for Volume Descriptor)                            ;;
    ;;   dd 0200:0000 - Initial segment:offset where to load the read in     ;;
    ;;                     sector(S)                                         ;;
    ;;   dq 16        - Starting sector to read in, then we need to get to   ;;
    ;;                     the root directory                                ;;
    ;;                                                                       ;;
    ;; Comments:                                                             ;;
    ;;                                                                       ;;
    ;; This functions is called three times: Initially to get the Initial    ;;
    ;; Volume Descriptor so we can find the root directory; then it gets     ;;
    ;; used again to read in the root directory, so we can scan for          ;;
    ;; SmiddyOS; finally once we recognize SmiddyOS we load it and jump to   ;;
    ;; it, if all goes as it should "on a good day" as expected (this is     ;;
    ;; done in FindSmiddyOS).                                                ;;
    ;;                                                                       ;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    MOV DL,[CDDriveNumber]              ; Set it up again
    MOV AH,42h                          ; Read from drive function
    MOV SI,DiskAddressPacket            ; Load SI with address of the DAP
    INT 13h                             ; Call read sector from drive
    JC Failure                          ; Nope, hosed, get out

    MOV ES,[DiskAddressPacket.Segment]  ; Sets up so we can analyze the data
    MOV DI,[DiskAddressPacket.Offset]   ; Sets up so we can analyze the data

    MOV BX,40                           ; Load BX with Volume Label offset
                                        ;    (Check CDROM.ASM or ISO 9660)

VolumeLabelLoop:

    MOV CL,[ES:DI+BX]                   ; Grab a letter
    CMP CL,' '                          ; Is it a space? (Assumes end of
                                        ;    string is space, may run out)
    JE .VolumeLabelDone                 ; Yes, we are done

    MOV [VolumeLabel+BX-40],CL
    INC BX
    JMP VolumeLabelLoop                 ; Need to compare BX to length of
                                        ;    Volume Label on CD (32?)

.VolumeLabelDone:

    MOV byte [VolumeLabel+BX-40],0      ; End the string

    MOV EAX,[ES:DI+158]                 ; LBA of root directory, where all
                                        ;    things start.
    MOV [DiskAddressPacket.End],EAX     ; Load packet with new address on CD of
                                        ;    the root directory
    MOV DL,[CDDriveNumber]              ; Set it up again
    MOV AH,42h                          ; Read from drive function
    MOV SI,DiskAddressPacket            ; Load SI with address of the DAP
    INT 13h                             ; Call read sector from drive

    JC Failure                          ; Nope, hosed, get out

    CALL FindSmiddyOS
    MOV AH,0FEh                         ; If the FindSmiddyOS makes it back
                                        ;   here, then the file was not found

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Failure: Displays failure with an error code number.                      ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Failure:

    mov AL,AH                           ; Put the error into lowest bits
    mov cl,8                            ; only 8 bits
    call ToHex                          ; Get what is in the XX to display

    MOV SI,FailureMessage               ; reboot message
    CALL PrintString
    mov si,HexBuffer
    CALL PrintString
    MOV SI,AddTheHAtTheEnd
    CALL PrintString
    MOV SI,TheEndOfTheLine
    CALL PrintString
    mov SI,PressAnyKeyMessage
    CALL PrintString
    MOV AH,0                            ; Reboot on error
    INT 16h                             ; BIOS GetKey
    INT 19h                             ; BIOS Reboot

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FindSmiddyOS: Locates SmiddyOS in the root directory of the CD/DVD/BD and ;;
;;               launchs it if it finds it.                                  ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

NumberOfSectorsToRead:   dd 0

FindSmiddyOS:

    MOV ES,[DiskAddressPacket.Segment]
    MOV DI,[DiskAddressPacket.Offset]

    CMP byte [VolumeLabel],0
    JE .NoLabel

    JMP .LabelDone

.NoLabel:

;    MOV SI,HasNoLabel
;    CALL PrintString

.LabelDone:

;    MOV SI,TheEndOfTheLine
;    CALL PrintString
;    MOV SI,TheEndOfTheLine
;    CALL PrintString

.GrabDirectoryEntry:

    MOV AL,[ES:DI]                      ; Length of the current directory entry
    MOV [DirectoryEntrySize],AL

    MOV EAX,[ES:DI+2]                   ; Starting sector of directory entry
    MOV [FileSector],EAX

    MOV EAX,[ES:DI+10]                  ; Size of directory entry on CD/DVD/BD
    MOV [FileSize],EAX

    MOV AL,[ES:DI+32]                   ; File's name length (see El Torito of
                                        ;    ISO:9660 or CDROM.ASM)
    MOV [FileNameLength],AL

    XOR BX,BX                           ; Initialize BX
    XOR CX,CX                           ; Initialize CX
    MOV SI,DI
    ADD SI,33
    MOV BX,FileNameEntry
    MOV AL,[FileNameLength]
    CMP AL,1                            ; Check to see if a dot or dotdot entry
    JE .DotAndDotDotEntries

.LoopFileNameEntry:                     ; Capture the filename entry for later
                                        ;    manipulation

    MOV AL,[ES:SI]
    MOV [DS:BX],AL
    INC BX
    INC CX
    INC SI
    XOR AX,AX
    MOV AL,[FileNameLength]
    CMP CX,AX
    JB .LoopFileNameEntry

    MOV byte [DS:BX],0
    JMP .FileNameEntryDisplay

.DotAndDotDotEntries:

    MOV AL,[FileEntryNumber]
    CMP AL,0
    JA .DoDotDotEntry
    MOV word [FileNameEntry],'.'        ; First directory entry
    MOV byte [FileNameEntry+1],0        ; End the string with a termination
    JMP .FileNameEntryDisplay

.DoDotDotEntry:

    MOV word [FileNameEntry],'..'       ; Second directory entry
    MOV byte [FileNameEntry+2],0        ; End the string with a termination

.FileNameEntryDisplay:

    MOV SI,FileNameEntry
    PUSH DI
    MOV DI,FileName
    CALL StringCompare
    POP DI
    JC .EndOfTheLine


    MOV EAX,[FileSector]
    MOV [DiskAddressPacket.End],EAX     ; Save the starting sector into DAP
    MOV EAX,[FileSize]
    XOR EDX,EDX
    MOV EBX,2048
    DIV EBX
    INC EAX
    MOV [NumberOfSectorsToRead],EAX     ; This is the absolute number of
                                        ;   sectors to be read.
    CMP AX,32
    JA .UpdateSectorsToRead
    MOV [DiskAddressPacket.SectorsToRead],AX  ; Save number of sectors to read
    JMP .PassedTheUpdate

.UpdateSectorsToRead:

    MOV [DiskAddressPacket.SectorsToRead],word 32
    SUB [NumberOfSectorsToRead],dword 32

.PassedTheUpdate:

.ReadTheSectorsLoop:

    MOV DL,[CDDriveNumber]              ; Set it up again
    MOV AH,42h                          ; Read from drive function
    MOV SI,DiskAddressPacket            ; Load SI with address of the DAP
    INT 13h                             ; Call read sector from drive
    JC Failure                          ; Nope, hosed, get out

    CMP [NumberOfSectorsToRead], dword 0
    JE .StartTheOperatingSystem

    MOV EAX,[DiskAddressPacket.SectorsToRead]   ; Add sectors read (32) to it,
                                                ;   for the new starting sector
    ADD [DiskAddressPacket.End],EAX     ; Update sector to start reading from
                                        ;    on CD/DVD/BD
    MOV EBX,2048
    MUL EBX                             ; Update EAX with amount of space used
    SHR EAX,4                           ; Shift it right to update the segment
                                        ;    to load next
    ADD [DiskAddressPacket.Segment], AX ; Segment to load next in memory
    CMP [NumberOfSectorsToRead],dword 32 ; Is this still too big (greater than
                                        ;     64k barrier?
    JAE .UpdateSectorsToRead            ; If so, need to load another 32
                                        ;    sectors
    MOV EAX,[NumberOfSectorsToRead]     ; Else
    MOV [DiskAddressPacket.SectorsToRead],AX ; We load what is left
    MOV [NumberOfSectorsToRead],dword 0 ; Zero out this bad boi!
    JMP .ReadTheSectorsLoop

.StartTheOperatingSystem:


    MOV DL,[CDDriveNumber]              ; Drive into DL for booting

    JMP     0202h:0h                    ; Start SmiddyOS (For standard DOS
                                        ;    executeable)

.EndOfTheLine:

    XOR CX,CX                           ; Prepare CX to do math for DI
    MOV CL,[DirectoryEntrySize]         ; Get the size of the directory entry
    ADD DI,CX                           ; Add that size to the DI to get to the
                                        ;    next record
    CMP byte [ES:DI],0                  ; Is the next entry = 0?
    JE .End                             ; If so, we're at the end of the
                                        ;    directory, move on
    XOR CX,CX
    MOV BX,FileNameEntry

.ClearFileNameEntry:

    MOV byte [DS:BX],0                  ; Erase the begining of the
                                        ;    FileNameEntry
    INC BX
    INC CX
    CMP CX,254
    JB .ClearFileNameEntry
    MOV byte [DS:BX],0
    INC byte [FileEntryNumber]
    JMP .GrabDirectoryEntry             ; If not, we have more processing to do

.End:

    RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PrintString                                                               ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

PrintString:

    PUSHA                               ; Save Registers

.Loop:

    LODSB                               ; Load SI into AL, increment SI one
                                        ;    byte
    OR AL,AL                            ; AL = 0?
    JZ .Done                            ; If yes, get out
    MOV AH,0Eh
    MOV BH,0
    MOV BL,7                            ; character attribute
    INT 10h                             ; Display character in AL
    JMP .Loop                           ; Do it again

.Done:

    POPA                                ; Replace registers

    RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ToHex                                                                     ;;
;; Loads HexBuffer with ASCII corresponding to 8, 16, or 32 bit interger in  ;;
;;    hex.                                                                   ;;
;; Requires interger in AL, AX, or EAX depending on bit size                 ;;
;; Requires the number of bits in the CL                                     ;;
;; Returns a full buffer or an empty buffer on error                         ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ToHex:

    pusha

    MOV BX,0                    ; Load BX with pointer offset
    MOV [TheEAX],EAX            ; Save the EAX
    MOV [TheCL],CL              ; Save the CL
    CMP CL,8                    ; Check for 8 bits
    JNE .Check16
    JMP .Loop1                  ; Start loading the buffer

.Check16:

    CMP CL,10h                  ; Check for 16 bits
    JNE .Check32
    JMP .Loop1                  ; Start loading the buffer

.Check32:

    CMP CL,20h                  ; Check for 32 bits
    JNE .ErrorBits

.Loop1:

    MOV EAX,[TheEAX]            ; Reload EAX with the converter
    SUB CL,4                    ; Lower bit count by 4 bits
    SHR EAX,CL

    AND AL,0Fh
    ADD AL,'0'
    CMP AL,'9'
    JBE .LoadBuff1
    ADD AL,'A'-'0'-10           ; Convert to "A" to "F"

    JMP .LoadBuff1

.Loop2:
    MOV EAX,[TheEAX]            ; Reload EAX again
    SUB CL,4                    ; Lower bit count by 8 bits
    SHR EAX,CL
    AND AL,0Fh
    ADD AL,'0'
    CMP AL,'9'
    JBE .LoadBuff2
    ADD AL,'A'-'0'-10           ; Convert A,B,C,D,E,F

    JMP .LoadBuff2

.LoadBuff1:

    MOV [HexBuffer+BX],AL       ; Load buffer with AL
    INC BX                      ; Increment buffer pointer
    JMP .Loop2                  ; Do next byte

.LoadBuff2:

    MOV [HexBuffer+BX],AL       ; Load buffer with AL
    INC BX                      ; Increment buffer pointer
    CMP CL,0                    ; Check if we're done
    JNE .Loop1                  ; Do next Byte

.ErrorBits:

    MOV AL,0
    MOV [HexBuffer+BX],AL       ; End the string with a zero

    popa

    RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; StringCompare                                                             ;;
;;                                                                           ;;
;;    Compares the equality of two strings, loaded in SI and DI.             ;;
;;                                                                           ;;
;;    Sets the CF if not the same.                                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

StringCompare:

    PUSH SI                             ; Save SI to be restored at the end
    PUSH DI                             ; Save DI to be restored at the end

.Loop:

    MOV AL,[SI]                         ; Grab byte from SI
    MOV BL,[DI]                         ; Grab byte from DI
    CMP AL,BL                           ; Compare if they are equal
    JNE .NotEqual                       ; They aren't equal

    CMP AL,0                            ; Both bytes are null
    JE .Done

    INC DI                              ; Increment DI
    INC SI                              ; Increment SI
    JMP .Loop                           ; Start looping

.NotEqual:

    STC                                 ; Set the carry flag to indicate
                                        ;    failure
    JMP .End

.Done:

    CLC                                 ; Clear the carry flag to indicate
                                        ;    success

.End:

    POP DI                              ; Restore DI before returning to caller
    POP SI                              ; Restore SI before returning to caller

    RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; StringLength                                                              ;;
;;                                                                           ;;
;;    Checks the length of the string in the SI and returns it in the AX.    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

StringLength:

    PUSH BX

    XOR BX,BX

.Loop:

    CMP byte [SI+BX],0
    JE .End

    INC BX
    JMP .Loop

.End:

    MOV AX,BX

    POP BX

    RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Program Data                                                              ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DirectoryEntrySize:         db 0
FileSector:                 dd 0
FileSize:                   dd 0
FileNameLength:             db 0
FileNameEntry:              TIMES 255 db ' '
FileEntryNumber:            db 0
DriveDisplayMessage:        db 'Volume in drive: ',0  ; Get label from Primary
                                                      ;    Volume Descriptor
                                                      ;    (loaded first)
HasNoLabel:                 db 'no label',0
FileNamePointer:            dw 0

DataSector:                 DW 0
CDDriveNumber:              db 0
RootDirectorySector         dd 0

FileName:                   DB 'SMIDDYOS.EXE',0       ; FAT Filename, 8.3, or
                                                      ;    11 characters

WelcomeMessage:             db 13,10,'smiddy OS CD boot v1.00',225,13,10,0
                                                      ; Version of CD-Boot
FailureMessage:             db 'Failure: ',0
DriveNumberReported:        db 'Drive number reported: ',0
PressAnyKeyMessage:         db 'Press any key to reboot...',13,10,0
AddTheHAtTheEnd:            db 'h',0
TheEndOfTheLine:            db 13,10,0  ; Used to print the end of the line
                                        ;    string
SizeOfPacketInBytes:        db 'The size of packet in bytes: ',0
BootMediaType:              db 'The boot media type: ',0
DriveNumberFromPacket:      db 'Drive number from packet: ',0
VolumeLabel:                TIMES 33 db 0             ; Where the volume label
                                                      ;    is stored

DiskAddressPacket:          db 16,0     ; a.k.a. DAP
.SectorsToRead:             dw 1        ; Number of sectors to read (read size
                                        ;    of OS)
.Offset:                    dw 0        ; Offset :0000
.Segment:                   dw 0200h    ; Segment 0200: currerntly, will change
                                        ;    to 0200:0000 for loading SmiddyOS
.End:                       dq 16       ; Sector 16 or 10h on CD-ROM

DiskResultsBuffer:          times 30 db 0 ; Return buffer from int 13h ah=48h
HexBuffer:                  DB 0,0,0,0,0,0,0,0,0 ; Buffer for hex text string
TheEAX:                     dd 0        ; Saves EAX
TheCL:                      db 0


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 512 Byte Code Boot Signature                                              ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

TIMES 2046-($-BootSector)   DB 0
                            DW 0AA55h   ; Bootloading sector signature (for
                                        ;    CD/DVD/BD) I am not sure this is
                                        ;    needed for optical media booting.    


Please, there are a lot of folks who helped to put this together, and I likely used code off the web, so be kind and give credit where you should. I put this out here for your use as you will, hopefully others will learn from it as I have.
Post 07 Dec 2014, 04:41
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.