flat assembler
Message board for the users of flat assembler.

Index > OS Construction > My first ever bootloader for x86

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
The Redox OS (written in Rust) caught my interest, and I was curious enough to find out about their bootloader written in ASM. Then I recall Tomasz has a game TetraOS also bootable. This code is a result upon reference to both sources:

Code:
format binary as 'img'
org 7C00h

        xor     ax,ax
        mov     ds,ax        
        mov     es,ax
        mov     ss,ax
        mov     sp,7C00h
        push    ax
        push    start
        retf

start:
        mov     al,72                ; "H"
        call        print
        mov     al,105                ; "i"
        call        print
        jmp     $

print:
            pusha
            mov     bx, 7                ; Foreground color
            mov     ah, 0eh                ; Write Character in Teletype (TTY) Mode
            int     10h
            popa
            ret
    


Code edited after listening to advice from @sinsi and @macomics. Thanks to both of you.

This bootloader , boot,img, just boot up to say "Hi" on screen!

I am not sure how others are doing it (bootloader), but this is the result of my learning process.

Feedback welcomed!

And yes, since I am on Linux, I use this command to "burn" the image to USB flash drive:
Code:
sudo dd if=boot.img of=/dev/sdb    
[/b]


Description: My "OS" prints "Hi"
Filesize: 377.72 KB
Viewed: 1883 Time(s)

Screenshot from 2021-08-06 18-58-01.png




Last edited by FlierMate on 07 Aug 2021, 13:12; edited 1 time in total
Post 06 Aug 2021, 17:08
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 738
Location: Adelaide
sinsi
Code:
start:
       mov al,72      ; "H"
       call    print
       mov al,105    ; "i"
       call    print
       int     10h
       ret
    

You might want to change that "ret" since you don't know where it returns to.
For something like this test, I usually use "jmp $"
Post 07 Aug 2021, 05:27
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
sinsi wrote:
Code:
start:
       mov al,72      ; "H"
       call    print
       mov al,105    ; "i"
       call    print
       int     10h
       ret
    

You might want to change that "ret" since you don't know where it returns to.
For something like this test, I usually use "jmp $"


Good advice. Although the result is the same but it make more sense to use "jmp $".
Thank you.
Post 07 Aug 2021, 07:30
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 161
Location: Russia
macomics
Quote:

xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp,7C00h
push ax
push start
retf

start:
mov al,72 ; "H"
call print
mov al,105 ; "i"
call print
int 10h ; e.g. AX = 0x0069
ret

print:
pusha
mov bx, 7 ; Foreground color
mov ah, 0eh ; Write Character in Teletype (TTY) Mode
int 10h
popa
ret

After returning from the second call of the print function, the value of AH will be restored, so the subsequent int 10h will try to set the 105 (not correct) screen mode and the call will return an error.


Last edited by macomics on 07 Aug 2021, 12:48; edited 2 times in total
Post 07 Aug 2021, 12:02
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
macomics wrote:
Quote:

xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp,7C00h
push ax
push start
retf

start:
mov al,72 ; "H"
call print
mov al,105 ; "i"
call print
int 10h ; e.g. AX = 0x0069
ret

print:
pusha
mov bx, 7 ; Foreground color
mov ah, 0eh ; Write Character in Teletype (TTY) Mode
int 10h
popa
ret

After returning from the second call of the print function, the value of AH will be restored, so the subsequent int 10h will try to set the 105 (not correct) screen mode and the call will return an error.


Yeah, you are right. The redundant "int 10h" was there by mistake as I did not remove it. Thank you for the detailed analysis.
Post 07 Aug 2021, 12:43
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18220
Location: In your JS exploiting you and your system
revolution
fasm can use the ASCII codes directly. No need to convert to decimal.
Code:
mov al,"H"
call print
mov al,"i"
call print    
Post 07 Aug 2021, 19:45
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
revolution wrote:
fasm can use the ASCII codes directly. No need to convert to decimal.
Code:
mov al,"H"
call print
mov al,"i"
call print    


I didn't know that. This will make my life easier, if I need to customize the message. Thank you , revolution!
Post 08 Aug 2021, 20:52
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
May I ask something, out of curiosity, when I disassemble the boot.img file, I notice there are no mention of "org 7c00h"??

Please refer to screenshot, the disasembly of hex dump has no where mentioning the offset 7c00h?
Code:
00000000    31  C0  8E  D8  8E  C0  8E  D0  BC  00  7C  50  68  10  7C  CB      1.........|Ph.|.
00000010    B0  48  E8  07  00  B0  69  E8  02  00  EB  FE  60  BB  07  00      .H....i.....`...
00000020    B4  0E  CD  10  61  C3                                              ....a.    


Sorry for my ignorance.

And one more question, why do I need to copy the boot.img to a 8GB FAT partition in USB flash drive, but not a 32GB FAT32 partition?

When I copied boot.img to a 32GB USB flash drive, it didn't show my "Hi" message, just prints "Missing operating system". But after I create a 8GB small partition in 32GB USB flash drive, then it works like a charm again.
Can someone explain ?


Description: i8086 disassembly
Filesize: 90.86 KB
Viewed: 1775 Time(s)

Screenshot from 2021-08-09 03-31-41.png


Post 08 Aug 2021, 20:58
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18220
Location: In your JS exploiting you and your system
revolution
org affects the offset used. In your code you don't have any offsets so it makes no difference.
Code:
org 0x7c00
my_string db 'Hello world!$'
mov bx, my_string ; <--- bx = 0x7c00    
Post 08 Aug 2021, 21:16
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
revolution wrote:
org affects the offset used. In your code you don't have any offsets so it makes no difference.
Code:
org 0x7c00
my_string db 'Hello world!$'
mov bx, my_string ; <--- bx = 0x7c00    


From your explanation, I found this:

Code:
push     start    


is actually...

Code:
push 0x7c10    


So I see now why the "org 7c00h" is needed.

Thank you once again!
Post 08 Aug 2021, 22:40
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18220
Location: In your JS exploiting you and your system
revolution
Thanks for the correction.
Post 08 Aug 2021, 22:53
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
revolution wrote:
Thanks for the correction.


My bad, I should have set base address as 0x7c00 in the online disassembler.


Description: Disassembly with Corrected base address
Filesize: 95.4 KB
Viewed: 1755 Time(s)

7c00h.png


Post 08 Aug 2021, 23:44
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18220
Location: In your JS exploiting you and your system
revolution
call is relative. The generated code is not affected by org.

Same for jmp, and jcc.
Post 08 Aug 2021, 23:51
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 161
Location: Russia
macomics
Quote:

When I copied boot.img to a 32GB USB flash drive, it didn't show my "Hi" message, just prints "Missing operating system". But after I create a 8GB small partition in 32GB USB flash drive, then it works like a charm again.
Can someone explain ?

Check the USB/HDD settings in the BIOS/EFI on your computer. The details depend on the version and manufacturer of your equipment, but it is worth looking in this direction.
Post 09 Aug 2021, 08:50
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 129
FlierMate
I should have added these two lines at the bottom of the source file:

Code:
rb 7C00h+510-$
dw 0AA55h
    


So now I can attach it to VM. Laughing


Description: This bootloader runs in QEMU
Filesize: 55.06 KB
Viewed: 719 Time(s)

boot.jpg


Post 09 Oct 2021, 16:20
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 161
Location: Russia
macomics
Slightly more versatile design
Code:
 define BytesPerSector 512
; Not all media sectors have the same size. Two signatures are required for such media: by offset 510 in the sector and at the end of the sector
  if $$ + 510 + $ > 0
    rb $$ + 510 + $
  else if $$ + 510 + $ < 0
    err  ; code out of sector bounds!
  end if
  dw 0AA55h
  if BytesPerSector > 512
    rb $$ + BytesPerSector - 2 - $
    dw 0AA55h
  end if    
Post 09 Oct 2021, 20:54
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.