hello world bootdisk

hello world bootdisk

Post by Marcel St? » Wed, 21 Mar 2001 19:03:08



hi
i've created a hello world bootdisk (boot pc and display "hello world") with
assembler by storing the binary assembler executable on floppy with 'dd
id=./binary of=/dev/fd0 bs=512 count=1'.
now i've tried the same with a c file:
gcc boot.c -o boot -static
dd id=./boot of=/dev/fd0 bs=512 count=1
but i looks like the system can't execute 'boot'. no error message but
nothing is being displayed. if i run boot from shell it works, so, the
binaryfile seems to be ok.

which of my steps were incorrect? thanks for your help.

cheers, marcel

 
 
 

hello world bootdisk

Post by Nate Eldredg » Thu, 22 Mar 2001 01:41:21



> hi
> i've created a hello world bootdisk (boot pc and display "hello world") with
> assembler by storing the binary assembler executable on floppy with 'dd
> id=./binary of=/dev/fd0 bs=512 count=1'.
> now i've tried the same with a c file:
> gcc boot.c -o boot -static
> dd id=./boot of=/dev/fd0 bs=512 count=1
> but i looks like the system can't execute 'boot'. no error message but
> nothing is being displayed. if i run boot from shell it works, so, the
> binaryfile seems to be ok.

> which of my steps were incorrect? thanks for your help.

In order to run a C program, you must have a loader that understands
your binary format.  The kernel does, but the BIOS does not.  So you
can't boot a C program without a lot of special effort (you could read
the kernel source to see what they do).

Furthermore, I doubt your C program fits in 512 bytes.

--

Nate Eldredge


 
 
 

hello world bootdisk

Post by Joseph A. Knapk » Thu, 22 Mar 2001 03:07:52



> hi
> i've created a hello world bootdisk (boot pc and display "hello world") with
> assembler by storing the binary assembler executable on floppy with 'dd
> id=./binary of=/dev/fd0 bs=512 count=1'.
> now i've tried the same with a c file:
> gcc boot.c -o boot -static
> dd id=./boot of=/dev/fd0 bs=512 count=1
> but i looks like the system can't execute 'boot'. no error message but
> nothing is being displayed. if i run boot from shell it works, so, the
> binaryfile seems to be ok.

What your assembler produces is a binary containing exactly the
instructions you specify: the first asm statement in your code
is the first instruction executed by the bootloader.

What gcc produces, on the other hand, is an executable program
in the format required by the underlying OS. Thus, it contains
all the machinery required to properly set up the C
runtime environment, standard input and output handling,
and so forth. There are probably thousands of instructions in
your C executable that get executed before main() is entered,
and which depend on the expected OS being present - which
at boot time, it clearly is not.

What you need to do is just compile (not link) your C code using
"gcc -c", and then use a tool like "objdump" to produce a raw binary.

HTH,

-- Joe

-- Joe Knapka
"It was just a maddened crocodile hidden in a flower bed. It could
 have happened to anyone." -- Pratchett
// Linux MM Documentation in progress:
// http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html
* Evolution is an "unproven theory" in the same sense that gravity is. *

 
 
 

hello world bootdisk

Post by Joseph A. Knapk » Thu, 22 Mar 2001 08:26:17



> hi joe

> thanks a lot for your clear and very helpful explanation. i followed your
> recommendations but there's still a bug (or a misunderstanding). ok let's
> see:

> i write a tiny c 'program' that includes stdio.h and contains a simple
> printf statement

printf() is not going to work. Two reasons:

(1) By using "gcc -c" you are telling the compiler to just compile
your code, and not to try to link it with the C library to
create an executable. Since printf() is a function in the C runtime
library, your object file contains an undefined reference to it.

(2) Even if you could link your object with the C library (which
would make it far too large to fit in the boot sector of a
disk), it won't work, because printf() works by making use
of OS facilities for doing console I/O, and you have no
OS available at boot time.

When the boot loader is running, the only functionality you
have is that built into the hardware itself, and the BIOS. That
means that *no* standard C library calls will work; if you want
I/O you have to either use the low-level BIOS routines or
read and write the hardware I/O ports and handle the interrupts
yourself. IMO, writing a bootloader in C is not a very good
idea, because you have very little control over what the
compiler is going to do with your code, and since you need
everything to fit in 512 bytes you do need fine control
over the generated code.

For an example of a working x86 bootloader and a tiny 16-bit
OS to go with it (written in NASM and Forth), have a look at

http://home.earthlink.net/~jknapka/jkf.html

HTH,

-- Joe

Quote:> i compile it using gcc -c boot.c --> results in boot.o
> i create a binary using objcopy -O binary boot.o boot     (objdump only
> seems to DISPLAY info)
> i dump it on disk using dd if=./boot of=/dev/fd0 bs=512 count=1
> so? what did i miss?
> it would be nice if you could give me another hint

> bye, marcel

-- Joe Knapka
"It was just a maddened crocodile hidden in a flower bed. It could
 have happened to anyone." -- Pratchett
// Linux MM Documentation in progress:
// http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html
* Evolution is an "unproven theory" in the same sense that gravity is. *
 
 
 

hello world bootdisk

Post by Martin Coope » Thu, 22 Mar 2001 05:23:05


Hi,
    when you write a program with the C compiler, the resulting file will be
in ELF format (assuming you are using a recent version of gcc). Running the
file requires a loder program that understands the ELF header and knows
which offset to look at for the start of the executable code.

    Martin


Quote:> hi
> i've created a hello world bootdisk (boot pc and display "hello world")
with
> assembler by storing the binary assembler executable on floppy with 'dd
> id=./binary of=/dev/fd0 bs=512 count=1'.
> now i've tried the same with a c file:
> gcc boot.c -o boot -static
> dd id=./boot of=/dev/fd0 bs=512 count=1
> but i looks like the system can't execute 'boot'. no error message but
> nothing is being displayed. if i run boot from shell it works, so, the
> binaryfile seems to be ok.

> which of my steps were incorrect? thanks for your help.

> cheers, marcel

 
 
 

1. erroneous "hello" from forked "hello world" process!


fork() and wait() are not part of the C language, so this belongs in
comp.unix.programmer.  I have redirected followups.  (Old-timers may
note that I try to be impartial about all misdirected stuff, not just
MS-DOS stuff).

His TA expects

to produce

but instead it produces one of `Hello\nHello\nWorld' or `Hello\nWorld\nHello'.

Your TA should; he or she should certainly know about the boundaries
between `things in the operating system' and `things not in the
operating system' in order to work with an OS class in the first place.
printf() is part of the C runtime system, *not* the Unix OS; fork()
and wait() are part of the Unix OS, not the C runtime system.  Thus
there is no guarantee about the way the two interact.

In particular, when printing to a `buffered' file the runtime system
buffers (hence the name) the text until it has a `suitable amount' to
write at once.  It then sends it all out with one or more write() system
calls.  Since fork() simply duplicates all the data in the program, it
duplicates any buffered text.  Thus the program above becomes one
process with `Hello\n' buffered, and one with `Hello\nWorld\n' buffered.

The real oddity is not that the word `Hello' is ever duplicated, but
rather that it is sometimes *not* duplicated.  This happens whenever
the string `Hello\n' is written out before the program forks, which
includes whenever the standard output is line buffered or unbuffered.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)

2. VMWARE

3. "Hello,Hello,Hello-Whats going on here then?"

4. SuSE kernel problem (quiesce)

5. Help. My first .c program (hello world)

6. limits.conf

7. hello world

8. sistemas raid y clustering de servidores

9. g++ errors on hello world program!

10. Help me with Perl "Hello world"

11. C++ Hello World (X).

12. How to run a "Hello world" on ppc board?

13. Hello World program...