flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Need Help with my Bootloader...

Author
Thread Post new topic Reply to topic
tspier2
Guest




tspier2 24 Feb 2006, 02:37
Alright, I took some bits and pieces of a few bootloaders I found on here, added some to it, took some out, etc. The only problem is...it won't work. Here is my code... (Btw, my code doesn't look this bad, I just didn't have time to reorder it all for the post.

Code:
;=============================================
;       Bootloader
;=============================================



org 0x7c00                                      ; Bootsector Starting Read Location

use16                                              ; Use 16-Bit Assembler Code



;=================================

;       Defined Data and Messages

;=================================

        RealmodeMsg                     db 'Entering Realmode...'
        ProtectedmodeMsg             db 'Entering Protected Mode...'
        LoadFloppyDrvMsg             db 'Loading Floppy Driver...'
        FloppyDrvTestMsg              db 'Testing Floppy Driver...'
        LoadMouseDrvMsg             db 'Loading Mouse Driver...'
        MouseDrvTestMsg              db 'Testing Mouse Driver...'
        LoadKeybMapMsg             db 'Loading Keyboard Map...'
        LoadKeybDrvMsg               db 'Loading Keyboard Driver...'
        KeybDrvTestMsg                db 'Testing Keyboard Driver...'
        LoadSysLibraryMsg           db 'Loading System Library...'
        LoadAsmLibaryMsg           db 'Loading Assembly Library...' 
        LoadCeeLibraryMsg          db 'Loading C Library...' 
        SuccessMsg                      db '[Done]'



;=========================
;       Entering Realmode
;=========================



start:

        xor ax,ax                               ; Initialize all the Registers

        mov ds,ax       

        mov es,ax       

        mov ss,ax       

        mov sp,0x7c00                      ; Starting Read Location



        mov ax, 3                               ; Sets mode 0x03

        int 0x10                                  ; Runs Interrupt 10h

        
        mov bl, 2                                 ; Sets Cursor on Screen

        mov ah, 2

        mov dx, 0x0201

        int 0x10                                  ; Runs Interrupt 10h



        mov bp, RealmodeMsg           ; Prints the Starting Messages

        mov ax,0x1301

        mov bx,7

        mov cx,12

        mov dx,0x0201

        int 0x10                                   ; Runs Interrupt 10h
        
;============================
;       Setting-Up Protected Mode
;============================

        cli
        lgdt [gdtr]
                
        mov eax,cr0
        or al,0x1
        mov cr0,eax
                
        jmp 0x10: protected

;===========================
;       Entering Protected Mode
;===========================
use32

protected:
        mov ax,0x8
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov esp,0x7c00
        
        mov byte [es:0xB8000], ProtectedmodeMsg
        jmp $
        
;======================
;       Turns off Floppy
;======================

        mov dx,3F2h
        mov al,0
        out dx,al



;==============================================

;       Loads FAT, ROOT, and Searches for File

;==============================================



;       mov di,0x0050                   ; Loads Root to:
;       mov ax,19                         ; 0x0000:0x0500
;       mov cx,14
;       call Load_Root                  ; Calls the function that loads
                                                 ; Kernel Sectors
                                                                
;====================================
;       Sets up Global Descriptor Table
;====================================
gdt:    dw              0x0000, 0x0000, 0x0000, 0x0000
          dw              0xFFFF, 0x0000, 0x9200, 0x00CF
          dw              0xFFFF, 0x0000, 0x9800, 0x00CF
                
gdt_end:

gdtr:   dw              gdt_end - gdt - 1
          dd              gdt



;=============================

;       Set code to 512 Bytes

;=============================



times 510- ($-start) db 0

dw 0xaa55    
Post 24 Feb 2006, 02:37
Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 24 Feb 2006, 11:36
Are you sure you want the data before the instructions without any "jump past data instruction" in front of it?
Post 24 Feb 2006, 11:36
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 24 Feb 2006, 16:29
My advice: If you don't want to jump, you MUST put all the global variables after the code, so they WON'T get executed... I would choose this method

OR, you can put a jump RIGHT BEFORE your global data (i.e the strings)... this is because the code will start executing your 'db' directives (IT WILL NOT START at start: label, 'cause labels ARE just some names that are NOT in the .exe/.bin/whatever..).. so, try something like:

Code:
;=============================================
;       Bootloader
;=============================================



org 0x7c00                                      ; Bootsector Starting Read Location

use16                                              ; Use 16-Bit Assembler Code

; OK, processor WILL EXECUTE from here...
jmp start  ; jump to start label

;=================================

;       Defined Data and Messages

;=================================

        RealmodeMsg                     db 'Entering Realmode...'
        ProtectedmodeMsg             db 'Entering Protected Mode...'
        LoadFloppyDrvMsg             db 'Loading Floppy Driver...'
        FloppyDrvTestMsg              db 'Testing Floppy Driver...'
        LoadMouseDrvMsg             db 'Loading Mouse Driver...'
        MouseDrvTestMsg              db 'Testing Mouse Driver...'
        LoadKeybMapMsg             db 'Loading Keyboard Map...'
        LoadKeybDrvMsg               db 'Loading Keyboard Driver...'
        KeybDrvTestMsg                db 'Testing Keyboard Driver...'
        LoadSysLibraryMsg           db 'Loading System Library...'
        LoadAsmLibaryMsg           db 'Loading Assembly Library...' 
        LoadCeeLibraryMsg          db 'Loading C Library...' 
        SuccessMsg                      db '[Done]'



;=========================
;       Entering Realmode
;=========================



start:
...    
Post 24 Feb 2006, 16:29
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 24 Feb 2006, 19:54
1. you need the jmp.
2. You need a print routine like this:
Code:
        lea esi,[ProtectedmodeMsg]                         mov edi,0xB8000 + 4            mov ecx,51        cld        rep movsbProtectedmodeMsg  db 'E n t e r i n g   P r o t e c t e d   M o d e . . . '    
NOTE: the space in between the letters are needed, so you need to cut & paste as is.
3. Also note that, with the data at the start the
Code:
times 510- ($-start) db 0dw 0xaa55    
may not give the right size:
Post 24 Feb 2006, 19:54
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 28 Feb 2006, 17:03
1. Why do you really need the jump? It can be done without it as well...
2. Why don't use BIOS? For booting up, I think a smaller bootloader is preferred over a faster (without slow BIOS) bootloader.

BTW: Nice 'manual' printing routine, I was just about trying to figure out one myself (without BIOS 'cause I want speed and flexibility). thanks Very Happy
Post 28 Feb 2006, 17:03
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 01 Mar 2006, 00:50
Heres why you need the jump:
Code:
;=============================================;       Bootloader;=============================================org 0x7c00 ; Bootsector Starting Read Locationuse16 ; Use 16-Bit Assembler Code;=================================;       Defined Data and Messages;================================= ; ***** CS:IP is pointing here ***** RealmodeMsg    db 'Entering Realmode...'    
There should be code here not data Wink
by put the jmp code, it jumps over the data, to the next code (instruction).

Heres a better ver, of pmode PrintString:
Code:
       lea esi,[ProtectedmodeMsg] ; point to string                     mov edi,0xB8000 + 4  ; point to screen       mov ah,0x09 ; color of char       call PrintString ; call function       ;****some more code here ****; Print string functonPrintString:       cld  ;process from left to rightPrintStringLoop  ; Lable for loop       lodsb  ;load byte from ds:esi to al       or al,al ;Sets the zero flag if al = 0          jz PrintEnd ; If  zero flag set, jump to end       stosw  ; move a word from AX to es:edi       jmp PrintStringLoop  ; loopPrintEnd:       ret ; ret to the next instr after call; DataProtectedmodeMsg  db 'Entering Protected Mode.....  ', 0    

NOTE: It for pmode, will need moding for realmode
Post 01 Mar 2006, 00:50
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 01 Mar 2006, 18:13
Dex4u wrote:
There should be code here not data Wink
by put the jmp code, it jumps over the data, to the next code (instruction).

Why not put the 'data' AFTER the code, instead of before it... so you won't need the jmp:

Code:
;=============================================
;       Bootloader
;=============================================
org 0x7c00 ; Bootsector Starting Read Location

use16 ; Use 16-Bit Assembler Code
;=================================

; code here
start:
....




;       Defined Data and Messages

;=================================
 ; ***** CS:IP is pointing here *****
 RealmodeMsg    db 'Entering Realmode...'    


Dex4u wrote:
Heres a better ver, of pmode PrintString:
Code:
       lea esi,[ProtectedmodeMsg] ; point to string              
       mov edi,0xB8000 + 4  ; point to screen
       mov ah,0x09 ; color of char
       call PrintString ; call function
       ;****some more code here ****

; Print string functon
PrintString:
       cld  ;process from left to right
PrintStringLoop  ; Lable for loop
       lodsb  ;load byte from ds:esi to al
       or al,al ;Sets the zero flag if al = 0   
       jz PrintEnd ; If  zero flag set, jump to end
       stosw  ; move a word from AX to es:edi
       jmp PrintStringLoop  ; loop
PrintEnd:
       ret ; ret to the next instr after call

; Data
ProtectedmodeMsg  db 'Entering Protected Mode.....  ', 0
    

NOTE: It for pmode, will need moding for realmode

Cool Cool nice and fast routine, thx
Post 01 Mar 2006, 18:13
View user's profile Send private message Reply with quote
kake_zinger



Joined: 15 Jul 2004
Posts: 51
kake_zinger 02 Mar 2006, 10:10
There is no real reason for the jump like shown above since we're using Fasm (a very simple single pass assembler would not have knowledge of data coming later), but usually it is done to establish a full segment for our code partly in order to be sure about the addressing because some strange bios might use 07C0:0000 instead of 0000:7C00. You actually pointed to this in another thread yourself.

By executing

jmp 07C0:start

start: more here

we're establishing the CS as 07C0 and getting a full 64k segment for our code whereas when loading to 0000:7C00 we only have 83FF bytes left in the segment 0000.

However in practice this is needless because the first priority is to enter full 32bit mode as soon as possible unless you're doing 16bit code on purpose.
Post 02 Mar 2006, 10:10
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 02 Mar 2006, 19:17
Your right kake_zinger, coming from other assemblers, i alwayed assumed you needed the jmp there, but it seems you do not.
Also note, as the (start) is used for the
Code:
times 510- ($-start) db 0dw 0xaa55    

Data before the lable, will make the file bigger than 512bytes.
Post 02 Mar 2006, 19:17
View user's profile Send private message Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 557
smiddy 03 Mar 2006, 19:43
Greets All,

My tar pence on the JMP instruction or opcode as it were is that if you intend on being compatible with FAT storage on a floppy, then it is one of the requirements written into the specification. However, if you don't intend on using FAT for your floppy as a storage arrangement, then you can jump or not jump depending on your implementation.

@Dex4u,

Your print string function assumes that the address within the current ES starts at physical address 0. Anyone using it will need to understand the implications of using the routine if they do change their segments and may be using 24-bit protected mode, or offset segments of memory differently. That being said, given a FLAT memory model, this works like a champ! Wink (BTW, I am working my way back into coding between now and the 20th; Can you tell?)

-smiddy
Post 03 Mar 2006, 19:43
View user's profile Send private message Reply with quote
tspier2
Guest




tspier2 03 Mar 2006, 23:30
Alright, I looked at the comments, and I created a new copy of it. Can someone compile it, and try booting it for me? My other computer that I used for this isn't working right now, so I can't do it.

Code:
;=============================================
;       Bootloader
;=============================================

use16                                              ; Use 16-Bit Assembler Code

jmp start

;=================================
;       Defined Data and Messages
;=================================

        RealmodeMsg                      db 'Entering Realmode...', 0
        ProtectedmodeMsg              db 'Entering Protected Mode...', 0
        LoadFloppyDrvMsg              db 'Loading Floppy Driver...', 0
        FloppyDrvTestMsg               db 'Testing Floppy Driver...', 0
        LoadMouseDrvMsg              db 'Loading Mouse Driver...', 0
        MouseDrvTestMsg               db 'Testing Mouse Driver...', 0
        LoadKeybMapMsg              db 'Loading Keyboard Map...', 0
        LoadKeybDrvMsg               db 'Loading Keyboard Driver...', 0
        KeybDrvTestMsg                db 'Testing Keyboard Driver...', 0
        LoadSysLibraryMsg            db 'Loading System Library...', 0
        LoadAsmLibaryMsg            db 'Loading Assembly Library...', 0 
        LoadCeeLibraryMsg           db 'Loading C Library...', 0
        SuccessMsg                      db '[Done]', 0

;=========================
;       Entering Realmode
;=========================

start:

        xor ax,ax                                 ; Initialize all the Registers
        mov ds,ax       
        mov es,ax       
        mov ss,ax       
        mov sp,0x7c00                       ; Starting Read Location

        mov ax, 3                              ; Sets mode 0x03
        int 0x10                                ; Runs Interrupt 10h
         
        mov bl, 2                               ; Sets Cursor on Screen
        mov ah, 2
        mov dx, 0x0201
        int 0x10                                ; Runs Interrupt 10h

        mov bp, RealmodeMsg         ; Prints the Starting Messages
        mov ax,0x1301
        mov bx,7
        mov cx,12
        mov dx,0x0201
        int 0x10                                ; Runs Interrupt 10h
        
;=================================
;       Setting-Up Protected Mode
;=================================

        cli
        lgdt [gdtr]
                
        mov eax,cr0
        or al,0x1
        mov cr0,eax
                
        jmp 0x10: protected

;===============================
;       Entering Protected Mode
;===============================

protected:

use32
 
       lea esi,[ProtectedmodeMsg]             
       mov edi,0xB8000 + 4
       mov ah,0x09                                                          ; Color
       call PrintStringFunction                                           ; Calls Function to Print the String

PrintStringFunction:
       cld
       
PrintStringLoop:                                                          ; Label for Loop
       lodsb                                                                    ; Load Byte from DS:ESI to al
       or al,al                                                                 ; Sets the Zero Flag if AL = 0   
       jz PrintEnd                                                          ; If Zero Flag is set, jump to end
       stosw                                                                  ; Move a Word from AX to ES:EDI
       jmp PrintStringLoop
       
PrintStringEnd:
       ret
        
;========================
;       Turns off Floppy
;========================

        mov dx,3F2h
        mov al,0
        out dx,al



;==============================================
;       Loads FAT, ROOT, and Searches for File
;==============================================

;       mov di,0x0050                                   ; Loads Root to:
;       mov ax,19                                          ; 0x0000:0x0500
;       mov cx,14
;       call Load_Root                                  ; Calls the function that loads
                                                                 ; Kernel Sectors
                                                                
;=======================================
;       Sets up Global Descriptor Table
;=======================================

gdt:           dw              0x0000, 0x0000, 0x0000, 0x0000
                 dw              0xFFFF, 0x0000, 0x9200, 0x00CF
                 dw              0xFFFF, 0x0000, 0x9800, 0x00CF
                
gdt_end:

gdtr:        dw              gdt_end - gdt - 1
               dd              gdt



;=============================
;       Set code to 512 Bytes
;=============================

times 510- ($-start) db 0
dw 0xaa55    
Post 03 Mar 2006, 23:30
Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach 07 Mar 2006, 14:02
are you sure that you got enought space for all those strings? i think i counted to 360 something bytes, and all you got in total is 512.
Post 07 Mar 2006, 14:02
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 07 Mar 2006, 17:22
Also because the start label, is after the strings, you would end up with a file 360+512 or 872 bytes in size.
Post 07 Mar 2006, 17:22
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 07 Mar 2006, 20:05
kake_zinger: yes, you would need to jump to set correct segments, but I thought it might be needless because you already jump after you load your kernel. but sure, the FAR jump is needed, though a NEAR jump can be avoided in this situation Wink

sorry for late post, sometimes i'm not on the NET for days (i.e not very often)
Post 07 Mar 2006, 20:05
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.