Here is a little assembly guide I wrote about copying sector data to a floppy disk.
This little demo shows you how to write the boot sector. You could modify the code (assuming you know what you're doing
) to write the data you want. You'll need a UNIX system, and the standard as86.
My tutorial assumes you want to write a piece of assembly code to the disk, called 'boot.o'. Ignore all reference to this
Writing Boot Sector to Floppy
-----------------------------
We have to write a C program that copies our code to first sector of the floppy disk. Here it is:
#include <sys/types.h> /* unistd.h needs this */
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
int main()
{
char boot_buf[512];
int floppy_desc, file_desc;
file_desc = open("./boot", O_RDONLY);
read(file_desc, boot_buf, 510);
close(file_desc);
boot_buf[510] = 0x55;
boot_buf[511] = 0xaa;
floppy_desc = open("/dev/fd0", O_RDWR);
lseek(floppy_desc, 0, SEEK_CUR);
write(floppy_desc, boot_buf, 512);
close(floppy_desc);
}
First, we open the file boot in read-only mode, and copy the file descripter of the opened file to variable file_desc. Read from the file 510 characters or until the file ends. Here the code is small, so the latter case occurs. Be decent; close the file.
The last four lines of code open the floppy disk device (which mostly would be /dev/fd0). It brings the head to the beginning of the file using lseek, then writes the 512 bytes from the buffer to floppy.
The man pages of read, write, open and lseek (refer to man 2) would give you enough information on what the other parameters of those functions are and how to use them. There are two lines in between, which may be slightly mysterious. The lines:
boot_buf[510] = 0x55;
boot_buf[511] = 0xaa;
This information is for BIOS. If BIOS is to recognize a device as a bootable device, then the device should have the values 0x55 and 0xaa at the 510th and 511th location. Now we are done. The program reads the file boot to a buffer named boot_buf. It makes the required changes to 510th and 511th bytes and then writes boot_buf to floppy disk. If we execute the code, the first 512 bytes of the floppy disk will contain our boot code. Save the file as write.c.
To make executables out of this file you need to type the following at the Linux bash prompt.
as86 boot.s -o boot.o
ld86 -d boot.o -o boot
cc write.c -o write
First, we assemble the boot.s to form an object file boot.o. Then we link this file to get the final file boot. The -d for ld86 is for removing all headers and producing pure binary. Reading man pages for as86 and ld86 will clear any doubts. We then compile the C program to form an executable named write.
Insert a blank floppy into the floppy drive and type
./write
The disk will be written, and Voila!
GOOD BYE !
[url=www.lightningstudios.co.uk]
[/url]