Megh Parikh
Megh Parikh

Reputation: 964

how to boot this code?

i am a newbie to assembly and program in c (use GCC in Linux)

can anyone here tell me how to compile c code into assembly and boot from it using pen drive

i use the command (in linux terminal) :

 gcc -S bootcode.c

the code gives me a bootcode.S file

what do i do with that ???

i just wanna compile the following code and run it directly from a USB stick

#include<stdio.h>
void main()
{
  printf ("hi");
}

any help here ???

Upvotes: 0

Views: 398

Answers (2)

KhiloMilitant Khilo
KhiloMilitant Khilo

Reputation: 113

First of all,

You Should be aware that when you are writing bootloader codes , you should know that you are CREATING YOUR OWN ENVIRONMENT of CODE, that means, there is nothing such ready made C Library available to you or anything similar , ONLY and ONLY BIOS SERVICES (or INTERRUPT ROUTINES).

Now, if you got this, you will probably figure out that the above code won't boot since, you don't have the "stdio.h" header, this means that the CPU when executing your compiled code won't find this header and thereby won't understand what is "printf" (since printf is a method of the stdio.h header). So if you want to print any string you need to write this function by YOUR OWN either in a separate file as a header and link its object file at compilation time when creating the final binary file or in the same file. it is up to you. There could be other ways, I'm not well familiar with them, just do some researches.

Another thing you should know, it is the BIOS who is responsible for loading this boot code (your above code in your case) into memory location 0x07C00 (0x0000h:0x7C00 in segment:offset representation), so you HAVE to mention in your code that you are writing this code on this memory location, either by

1-using the ORG instruction

2-Or by loading the appropriate registers for that (cs,ds,es)

Also, you should get yourself familiar with the segment:offset memory representation scheme, just google it or read intel manuals.

Finally, for the BIOS to load your code into the 0x07C00, the boot code must not exceed 512byte (ONLY ON FIRST SECTOR OF THE BOOTABLE MEDIA, since a sectore is 512byte) and he must find at the last two byte of this first sector (byte 510 & byte 511) of your code the boot signature 0x55AA, otherwise the BIOS won't consider this code AS BOOTABLE. Usually this is coded as :

ORG 0x7C00

...

your boot code and to load more codes since 512byte won't be sufficient.

...

times 510 - ($ - $$) db 0x00 ; Zerofill up to 510 bytes

               dw 0xAA55 ;Boot Sector signature,written in reverse order since it

                             will be stored as little endian notation

Just to let you know, I'm not covering everything here, because if so, I'll be writing pages about it, you need to look for more resources on the net, and here is a link to start with(coding in assembly):

http://www.brokenthorn.com/Resources/OSDevIndex.html

That's all, hopefully this was helpful to you...^_^

Khilo - ALGERIA

Upvotes: 2

Andro
Andro

Reputation: 2232

Booting a computer is not that easy. A bootloader needs to be written. The bootloader must obey certain rules and correspond with hardware such as ROM. You also need to disable interrupts, reserve some memory etc. Look up MikeOS, it's a great project that can better help you understand the process. Cheers

Upvotes: 2

Related Questions