Page 1 of 1

How to build this file to elf format.

Posted: Wed May 28, 2008 8:13 am
by huxuelei
Hi, I have written a small program with nasm which display a string on the screen.
Here is my code:

Code: Select all

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
CHECKSUM               equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header (in NASM syntax)
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd CHECKSUM

section .data
BootMessage db "Booting ....", 0
BootMessageLength equ $ - BootMessage
section .text
global _start
_start:
	xor ax, ax
	mov es, ax
	mov ds, ax
	mov ss, ax

	mov bp, BootMessage
	mov cx, BootMessageLength
	mov ax, 01301h
	mov bx, 000Eh
	mov dl, 0
	int 10h
	
	jmp $
First, I compile and link this program like this:

Code: Select all

nasm -f elf -o kernel.o kernel.s
ld -s -Ttext 0x30400 -o kernel.elf kernel.o
But I got a error information like this:

kernel.o(.text+0x15): In function '_start':
kernel.s:relocation truncated to fit:R_386_16 against '.data'


Then, I change the link command to this:

Code: Select all

nasm -f elf -o kernel.o kernel.s
ld -s -Ttext 0 -o kernel.elf kernel.o
At this time, complie and link passed.

I copy the elf file to floppy, ready to use grub to load it to memory.
Here is the grub command I use:

Code: Select all

root (fd0)
kernel /boot.elf
But then I got a error:
Error 7: Loading below 1MB is not supported.

I am so confusing about these.
Why can not use the flag '-Ttext 0x30400' when I link the program?
Why grub say 'Loading below 1MB is not supported'?

Please help me. Thanks.

[Edit by AJ - Code Tags Added]

Posted: Wed May 28, 2008 8:25 am
by inflater
GRUB enters protected mode and you are trying to execute real mode instrunctions. If you want your OS in real mode, I suggest you strip off the multiboot stuff and go here: http://www.osdever.net/downloads.php and download some of real mode bootloaders. And I highly doubt you could make a 16bit elf executable.

I suggest reading the osdev wikipedia.

BTW don't use colors. Especially the dark blue font color in the mtdark2 theme is... disgusting.

Posted: Wed May 28, 2008 8:40 am
by huxuelei
inflater wrote:GRUB enters protected mode and you are trying to execute real mode instrunctions. If you want your OS in real mode, I suggest you strip off the multiboot stuff and go here: http://www.osdever.net/downloads.php and download some of real mode bootloaders. And I highly doubt you could make a 16bit elf executable.

I suggest reading the osdev wikipedia.

BTW don't use colors. Especially the dark blue font color in the mtdark2 theme is... disgusting.
Oh, sorry. I just want to make things clearly.But I will not use color in the future.

Posted: Wed May 28, 2008 8:56 am
by JamesM
Hi,

Firstly, as mentioned this forum has [ code ] tags, please use them as it ensures that code is shown in a monospace font and on a nice background for all theme users.

On to your problem - You've got two problems here.

Firstly, your compilation problem. I assume it's because you're not specifying the location for your .data section. I assume that NASM is choosing a default location for it which is outside the range of the 16-bit relocation entries you're using.

So specify a .data start location on your command line.

The second is that you're trying to execute real mode code. You're trying to use BIOS interrupt int 10h, which is real mode only. GRUB loads you in protected mode, so you're going to get into difficulties there. (I.e. nothing will work).

If you want to use GRUB, you'll be in 32-bit protected mode. There are several tutorials out there to help you on that, but if you prefer to stick with 16-bit real mode, you'll have to ditch GRUB - again there are several tutorials to help you there too.

Cheers,

James

Posted: Wed May 28, 2008 5:21 pm
by huxuelei
Hi, I am sorry. I am a new here, I did not know the rule about list the code.

I won't do that any more.

Posted: Thu May 29, 2008 4:14 am
by Combuster
huxuelei wrote:Hi, I am sorry. I am a new here, I did not know the rule about list the code.
Then would you please read the rules so you know about all the other things as well, thanks.

Posted: Thu May 29, 2008 4:17 am
by JamesM
Combuster wrote:
huxuelei wrote:Hi, I am sorry. I am a new here, I did not know the rule about list the code.
Then would you please read the rules so you know about all the other things as well, thanks.
Combuster, thats OTT - his post was perfectly fine with the exception of colour.

A simple "There are code tags" would suffice IMHO.

Posted: Thu May 29, 2008 4:50 am
by jal
JamesM wrote:but if you prefer to stick with 16-bit real mode, you'll have to ditch GRUB
I don't see why. Just switch back to real mode after some relocation of code, and you should be fine (not that I think that's within the capabilities of the OP, but one could).


JAL