Page 1 of 1

NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 11:51 am
by osdevkid
Hi All,

I am new to this forum. I have a doubt in bootloader written in NASM, the below ASM code working fine after comment the line "SECTION .data align=16", let me know the reason please...

Code: Select all

[ORG 0]
jmp 07C0h:start     ; initialize CS:IP values (go to code segment 0x07C0)

SECTION .text
start:
	; Update the segment registers
	mov ax, 0x7C0		; 0x7C0 is code segment address, same initiated to other registers
	mov ds, ax
	mov es, ax
	mov ss, ax

	mov si, hello		; print hello string on the screen

	cld			; clear direction flag (set DF = 0) to increment SI register

	.l1:
		lodsb		; load [SI] contents in to AL register and increment SI = SI + 1
		cmp al, 0		; compare 0 (null) is received
		je .l2

		mov ah, 0Eh
		int 10h

		jmp .l1		; continue to lable 'lbl_1'

	.l2:

SECTION .data  align=16
	hello db 'Welcome hello world', 0

	times 510-($-$$) db 0
	dw 0AA55h


Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 12:20 pm
by f2
Hello osdevkid,

The size of a bootsector is 512 bytes and is loaded by the BIOS at address 0x7C00. The two last bytes of this bootsector (at offset 0x7DFE) should contain the value 0xAA55. If the BIOS doesn't find this value, it shows an error message or it tries to boot from another disk. In your bootloader, you define a ".data" section. So, the size of your bootsector is 544 bytes (32 bytes for the ".text" section and 512 bytes for ".data") and the value 0xAA55 is located at 0x7E1E. That's why your code doesn't work.

Cheers,
Tommy

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 12:26 pm
by osdevkid
f2 wrote:Hello osdevkid,

The size of a bootsector is 512 bytes and is loaded by the BIOS at address 0x7C00. The two last bytes of this bootsector (at offset 0x7DFE) should contain the value 0xAA55. If the BIOS doesn't find this value, it shows an error message or it tries to boot from another disk. In your bootloader, you define a ".data" section. So, the size of your bootsector is 544 bytes (32 bytes for the ".text" section and 512 bytes for ".data") and the value 0xAA55 is located at 0x7E1E. That's why your code doesn't work.

Cheers,
Tommy
Yes you are right.
Hi, I have used the below command to generate a image file

Code: Select all

nasm main.asm -o main.img
Any possibility to solve this problem, why it is coming like this ?

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 1:06 pm
by shahadat
remove the following two line

SECTION .text
SECTION .data align=16

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 1:19 pm
by osdevkid
shahadat wrote:remove the following two line

SECTION .text
SECTION .data align=16
In the code, what I have calculated to fill zeros is not worked proper after added the line "SECTION .data align=16"

Code: Select all

times 510-($-$$) db 0
dw 0AA55h
Any body can give your justification, why this happens?

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 1:40 pm
by shahadat
when you give the following line
SECTION .data align=16

the '$$' indicate the start of data section.
the '$' define current offset relative to [org]
thus,
510 - ($-$$) = 510 - ( 0x32 - 0x20 ) = 510 - 0x12 = 492

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 1:47 pm
by osdevkid
shahadat wrote:when you give the following line
SECTION .data align=16

the '$$' indicate the start of data section.
the '$' define current offset relative to [org]
thus,
510 - ($-$$) = 510 - ( 0x20 - 0) = 530
I have questions:
1) Each segments minimum size is 512 bytes ? since code segment is very small, why data segment goes beyound 512 bytes?

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 1:52 pm
by shahadat
sorry osdevkid, the previous calculation was wrong.

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 2:14 pm
by shahadat
each time you define a section, '$' and '$$' gets redifined.

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 2:37 pm
by f2
osdevkid wrote:[
since code segment is very small, why data segment goes beyound 512 bytes?
I have also one question: why you really want to add a .data segment? It is useless and your bootloader
works if your remove that line, as indicated in your first post.

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 2:40 pm
by osdevkid
shahadat wrote:each time you define a section, '$' and '$$' gets redifined.
Thank you very much "shahadat".

So we can save the '$' & '$$' values in some register, before move to next segment, at last we can calculate the exact bytes required to fill zeros, is it possible ?

@Others, please post your comments...

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 2:47 pm
by osdevkid
f2 wrote:
osdevkid wrote:[
since code segment is very small, why data segment goes beyound 512 bytes?
I have also one question: why you really want to add a .data segment? It is useless and your bootloader
works if your remove that line, as indicated in your first post.
Hi "f2" thank you very much for your post.

Leave about the bootloader, when we move from one segment to another segment,
1) '$', '$$' are redefined, so my calculation is not perfect. How to make it perfect?
2) At particular offset I want to write some data, in a normal application (with segments), How to do it.

Which are interesting, guys pls go ahead...

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 5:02 pm
by Combuster
You posted 11 questions in three threads in less than 4 hours. Have you read the manual, especially when keeping our comments in mind?

Re: NASM bootloader with data segment => not working....

Posted: Sun Nov 21, 2010 10:40 pm
by osdevkid
Combuster wrote:You posted 11 questions in three threads in less than 4 hours. Have you read the manual, especially when keeping our comments in mind?
Yes, I read the manual and exerciesed programs. Still I have lot of questions in my mind, first I am doing google, if I cant able to find the answer, then I come here.

Manuals are good and common for all, but when we encounter specific problems, FORUMS are best place to clarify them.

especially comments from experts like you.

Re: NASM bootloader with data segment => not working....

Posted: Mon Nov 22, 2010 1:34 am
by Solar
osdevkid wrote:Yes, I read the manual and exerciesed programs. Still I have lot of questions in my mind, first I am doing google, if I cant able to find the answer, then I come here.

Manuals are good and common for all, but when we encounter specific problems, FORUMS are best place to clarify them.

especially comments from experts like you.
Yes, well...

This is a forum about OS development.

There is some consensus that you shouldn't try to do OS development in a language you are not fluent in. You should first get familiar with your language of choice in writing "normal" (user space) programs.

Which means beginner ASM questions are somewhat misplaced here, and should rather go to some general ASM programming forum.

Sure you can go ask an Indy Car driver or a Rallye driver to teach you how to drive. He might even do it, out of general generosity. But you'd be better off talking to a driving instructor.