Wednesday, May 30, 2007

NASM and Boot loader

NASM

Netwide assembler we can generate plain text binary using NASM. This NASM is very useful to write own os.

Download site for NASM

http://sourceforge.net/project/showfiles.php?group_id=6208

1). DOS 16-bit binaries (OBSOLETE) 0.98.39 February 27, 2005 - Download
2). DOS 32-bit binaries 0.98.39 January 15, 2005 - Download

Boot Loader program

boot.asm
========code starts here=============
[bits 16]
org 0x7c00

mov si,msg
call DisplayMessage
hlt

DisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x02
int 0x10
jmp DisplayMessage
.DONE:
ret
msg db 0x0d, 0x0a, "Boot loader test example", 0x0d, 0x0a, 0x00
TIMES 510-($-$$) DB 0
DW 0xAA55
=========code ends here==============


assemble this program as: .>nasm -o boot boot.asm

After assembled this program plain machine instruction stored in boot file.

boot file size will be 512 bytes, now we need to write these instruction into boot sector of floppy
for that we need to write one C program, otherwise we can use some other utility(debug ect...) to write these instruction into boot sector of floppy.

==========code starts here===========

#include
#include
#include
main()
{
static unsigned char inc[512],inc1[512];
int i=0;
FILE *fp;
fp=fopen("boot","rb");
if(fp==NULL)
{
printf("file openning error");
exit(0);
}
while(i<512) here="="="="="="="="="="="="">
steps to do

1. Insert floppy in floppy drive
2. write boot.asm file
3 assemble with nasm as nasm -o boot boot.asm
4. write c program to copy boot to boot sector of floppy drive.
5. reboot the system and set first bootable device to floppy in SETUP.

Now u can see the text "Boot loader test example" on screen.

2 comments:

Viral :) said...
This comment has been removed by the author.
Viral :) said...
This comment has been removed by a blog administrator.