assemblyvirtual-machinenasmldfloppy

Correctly compiling and linking multiple asm files into a .vfd - for Oracle VM?


Lately I've been wanting to get into assembly coding, just to have some experience under my belt. I decided to look into it and was getting some good results(though its simple asm), however everything is still blurry, and I would much appreciate some input from others on how to correctly build multiple .asm files into .. &.. &.. to finish with a .vfd or a .img file for Oracle VM.

So over the past few weeks in my spare time, I've been researching around the web trying to find a working method that will enable me to:

1. Compile my .asm files into individual .obj files
2. Link these multiple files into one .bin
3. Place this .bin into a .vfd or .img
4. Finally ran on the Oracle VM.

The programs i am currently using on my windows 7 64bit system are:

(all through MinGW)

1. nasm   - to compile the asm to .obj / .bin files
2. ld     - to link .obj files into a .bin (I think)
3. dd     - to create a .img file from a .bin file

After some time of messing about I've been able to compile a single .asm file into a .bin file, and then placed it into a .img using dd (the below was from tutorials online):

(a bat file which executes these commands in the order below)

1. nasm -f bin -o bootloader.bin bootloader.asm  
2. dd if=bootloader.bin of=output.img count=1440

and then I just ran the Oracle VM which reads the output.img and displays it correctly. YAY.

Now. I've coded in Visual c++ for some years now, and i typically use many files in single solution. I feel that when coding in asm, I should also be able to have multiple files. So I've read around and many use 'ld' - a linker which 'joins' .obj files into .bin files?

So I edited the above bat file to the following:

1a. nasm -f bin -o bootloader.bin bootloader.asm 
1b. nasm -f bin -o kernel.bin     kernel.asm 
2.  ld -o link.bin bootloader.obj kernel.obj 
3.  dd if=link.bin of=output.img  count=1440

The first error i got was: "cannot perform PE operations on non PE output file". After I Googled.. and Googled... and Googled.... and Googled..... (one link to the next) and I couldn't find any other methods that worked - I just got more errors! I'm honestly lost. - I tried several different programs (eg objcopy), lots of different parameters, etc.. and I haven't made too much progress. I need guidance. :)

And here is my final questions. Will ''linking'' these files together with ld give me the desired results I seek? (multiple files in one final binary file) and how can I do it correctly on my windows system?

I'm not all to in-depth with asm or these programs so I've tried to read up as much as I could before i got here. So please if you could keep the explanations nice and clear. Im sorry if its a lot to ask for, and please no grilling. :)

Thanks!


Solution

  • I don't think there's any way to "link" multiple flat binary files (Nasm's -f bin output). If we were forced to use (G)as, there's no -f bin output. ld will create a flat binary from an ELF linkable object. I think the command line is something like ld -oformat binary -T.text=0x7C00 -o boot.bin boot.o. I don't know if MinGW's ld will do it, and I don't know if it will handle multiple files. Something to try, at least...

    Linking won't do you any good with your bootsector, in any case. When your computer (or VM?) boots, only the first sector is loaded - 512 bytes exactly. You'll have to provide code to load your "kernel.bin" (or "stage2.bin" from disk (or .vfd?) to a known location in memory. In order to easily find this further code, it is convenient to have it located immediately after the boot sector. You can use cat or copy (I think you'll want the /b option) to combine "boot.bin" and "kernel.bin" to a single file - then dd it to floppy (or .vfd?).

    "kernel.bin" is more flexible. It can be larger than 512 bytes, and could be built from multiple object files. You'll want a flat binary, or you could provide a loader for some executable format - probably MZ, since you're still in 16-bit mode. Once you've switched to 32- (or 64-) bit Pmode, you'll probably want to arrange to load PE or ELF or whatever executable format...

    Best source of information I know for this kind of thing is http://www.osdev.org - check it out, if you haven't. Happy bootin'!