Running an OS on VirtualBox
Asked Answered
O

2

7

I am using VirtualBox 4.1.14 on Windows 7 and I am trying to make it run my test OS. I am using the assembly code below and I am compiling it with

nasm -f bin -o boot.bin boot.asm

I am trying to convert the resulting bin file into an ISO that VB4.1.14 can use (I don't want to have to pay money or have any limits from a trial program). I have tried downloading different converters like bin2iso but VB comes up with different errors whenever I try to open the resulting ISO inside it like VERR_NOT_SUPPORTED and others.

I would prefer the solution to be a command line tool so I can use it in a batch script to make testing faster.

BITS 16

start:
    mov ax, 07C0h       ; Set up 4K stack space after this bootloader
    add ax, 288     ; (4096 + 512) / 16 bytes per paragraph
    mov ss, ax
    mov sp, 4096

    mov ax, 07C0h       ; Set data segment to where we're loaded
    mov ds, ax


    mov si, text_string ; Put string position into SI
    call print_string   ; Call our string-printing routine

    jmp $           ; Jump here - infinite loop!


    text_string db 'This is my cool new OS!', 0


print_string:           ; Routine: output string in SI to screen
    mov ah, 0Eh     ; int 10h 'print char' function

.repeat:
    lodsb           ; Get character from string
    cmp al, 0
    je .done        ; If char is zero, end of string
    int 10h         ; Otherwise, print it
    jmp .repeat

.done:
    ret


    times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
    dw 0xAA55       ; The standard PC boot signature
Osorio answered 13/5, 2012 at 12:37 Comment(2)
Just a suggestion. But most examples written similar to this put the instruction "cli" right after the start label to turn off maskable external interrupts. You might want to consider doing the same.Phylys
Although I will point out that since you perform an infinite loop here it is not really needed to use the "cli" instruction. That instruction is most commonly used as a processor synchronization tool to avoid race conditions between drivers and the kernel. It is also commonly used to halt the system when used in conjunction with the "hlt" instruction. Using "cli" then "hlt" is generally the preferred method for intentionally halting the system compared to the infinite loop like you used.Phylys
E
11

You can use dd (search dd for windows) to create a floppy for starters. The binary is just written to the first 256 bytes of a 1440 kib file.

dd if=/dev/zero of=floppy.img ibs=1k count=1440
dd if=boot.img of=floppy.img conv=notrunc

And here we go:

Enochenol answered 13/5, 2012 at 13:10 Comment(5)
copy: Is there a way to do this with C? I can do it with NASM, but I think I would like to start with C then work my way down.Ardeb
@Ardeb You'll need to setup your compiler to output a flat binary of 16 bit code. That's possible, but not advisable. A better idea is to write your bootloader in Assembly and switch to C later, or use an existing bootloader.Enochenol
@Ardeb Your CPU starts in 16 bit mode (legacy reasons, you could still boot an old DOS on it). You can switch to 32 bit mode later.Enochenol
@Enochenol Ah, okay. This is all kind of new to me :)Ardeb
I'm super late to the party, but in my testing there's no need to write it to a 1440 kib file. I was able to get virtualbox to boot from my output binary directly just by changing the extension to .img; For anyone who may stumble across this thread later, you need to add a floppy disk controller to be able to boot from it.Gonion
G
1

You can use the mkisofs tool like in this answer to a different question. The tool is available for Linux as well.

Gabrila answered 13/5, 2012 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.