Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Failing Bootloader code

7 views
Skip to first unread message

Darren Gansberg

unread,
Dec 11, 2009, 1:42:10 PM12/11/09
to

Hi,

I've been messing around writing my "bootloader" code when I ran into
something which i just don't understand. For some reason whenever i
attempt to boot my computer from my floppy drive which has had the
following code written to it after being assembled using Nasm, my system
simply ignores the code, after attempting to read the floppy, and
proceeds to boot windows 7 from my hard drive (2nd boot device set in bios).

The code was assembled with the following commands and options for Nasm:
nasm -f bin file1.asm file1.bin
No errors were reported by nasm

I then copy it to the floppy using partcopy using:
partcopy file1.bin 0 200 -f0
No errors were reported by partcopy

All steps above are the same as those for other code I've written and
run sucessfully.

Please forgive the crap names I've used throughtout my code.

file1.asm
--------------------------
[BITS 16]
[ORG 0x7c00]

section .data
varA: db 2

main:
MOV DI, varA
CMP BYTE [DI], 1 ; expect ZF Flag in Flags reg to be clear b/c 2 - 1
! = 0
JNZ printChar ;Because ZF flag clear expect to jump to printChar which
prints a 1 and then goes to end
JMP printChar2

end: JMP $


;prints 'c' char and then to end
printChar2: MoV AH, 0x0E
MOV AL, 'C'
MOV BH, 0x00
INT 0x10
JMP end

;prints 1 as a char and go to end
printChar:
MoV AH, 0x0E
MOV AL, '1'
MOV BH, 0x00
INT 0x10
JMP end

TIMES 510-($-$$) DB 0
bootsig: DW 0xAA55

Darren

Frank Kotler

unread,
Dec 11, 2009, 4:24:08 PM12/11/09
to

Darren Gansberg wrote:
> Hi,
>
> I've been messing around writing my "bootloader" code when I ran into
> something which i just don't understand. For some reason whenever i
> attempt to boot my computer from my floppy drive which has had the
> following code written to it after being assembled using Nasm, my system
> simply ignores the code, after attempting to read the floppy, and
> proceeds to boot windows 7 from my hard drive (2nd boot device set in bios).
>
> The code was assembled with the following commands and options for Nasm:
> nasm -f bin file1.asm file1.bin
> No errors were reported by nasm

Posto! You must have used "-o file1.bin", or Nasm would have complained
about more than one input file...

> I then copy it to the floppy using partcopy using:
> partcopy file1.bin 0 200 -f0
> No errors were reported by partcopy
>
> All steps above are the same as those for other code I've written and
> run sucessfully.
>
> Please forgive the crap names I've used throughtout my code.
>
> file1.asm
> --------------------------
> [BITS 16]
> [ORG 0x7c00]
>
> section .data

".data"??? Where's ".text"??? You probably don't want a ".data" section
in a bootsector. If you do, put your boot sig at the end of it - Nasm
will move it after your ".text" section (if you had one...).

A "canonical" bootsector starts with either a jmp (near) or jmp
short/nop. Most bioses don't check for this, but it should be there to
"boot anywhere". You don't want to execute your data, in any case.

jmp short main
nop

> varA: db 2
>
> main:

Set up your segment registers - ds at least!

xor ax, ax
mov ds, ax

Maybe es, too, if you're using it (you don't, at this point).

mov es, ax

Maybe set up a sane stack, too.

cli
mov ss, ax
mov sp, 0
sti

> MOV DI, varA
> CMP BYTE [DI], 1 ; expect ZF Flag in Flags reg to be clear b/c 2 - 1
> ! = 0
> JNZ printChar ;Because ZF flag clear expect to jump to printChar which
> prints a 1 and then goes to end
> JMP printChar2
>
> end: JMP $
>
>
> ;prints 'c' char and then to end
> printChar2: MoV AH, 0x0E
> MOV AL, 'C'
> MOV BH, 0x00

Depending on bios, this interrupt *might* use bl for attribute (color).
I'd use "mov bx, 7" to set both bh (video page) and bl (white on black).

> INT 0x10
> JMP end
>
> ;prints 1 as a char and go to end
> printChar:
> MoV AH, 0x0E
> MOV AL, '1'
> MOV BH, 0x00

Here, too?

> INT 0x10
> JMP end
>
>
> TIMES 510-($-$$) DB 0
> bootsig: DW 0xAA55

See if any of that helps...

Best,
Frank

Nathan Baker

unread,
Dec 11, 2009, 6:08:19 PM12/11/09
to

"Darren Gansberg" <dar...@MUNGED.microcosmotalk.com> wrote in message
news:4b229282$0$5098$9a6e...@unlimited.newshosting.com...

>
> Hi,
>
> I've been messing around writing my "bootloader" code when I ran into
> something which i just don't understand. For some reason whenever i
> attempt to boot my computer from my floppy drive which has had the
> following code written to it after being assembled using Nasm, my system
> simply ignores the code, after attempting to read the floppy, and
> proceeds to boot windows 7 from my hard drive (2nd boot device set in
> bios).
>

You really should read the existing tutorials and study working examples
before coding your own. A decent starting point is here:
http://aodfaq.wikispaces.com/#toc30

Nathan.

http://delicious.com/evenbit
http://www.frontiernet.net/~fys/faq/


Rod Pemberton

unread,
Dec 11, 2009, 7:31:05 PM12/11/09
to

"Darren Gansberg" <dar...@MUNGED.microcosmotalk.com> wrote in message
news:4b229282$0$5098$9a6e...@unlimited.newshosting.com...
>
> I've been messing around writing my "bootloader" code when I ran into
> something which i just don't understand. For some reason whenever i
> attempt to boot my computer from my floppy drive which has had the
> following code written to it after being assembled using Nasm, my system
> simply ignores the code, after attempting to read the floppy, and
> proceeds to boot windows 7 from my hard drive (2nd boot device set in
bios).

That happens when the BIOS doesn't recognize the floppy as bootable.

> [...]
> varA: db 2
>

This is the reason why your code doesn't work. "varA: db 2" is inserting a
byte of value 2 prior to the first byte in your .bin. That causes your BIOS
to recognize the disk as a blank. It's "blank," as in unformatted, and
non-bootable. The first byte in a floppy bootsector, according to the PS/2
Tech. Ref. manuals, must be greater than 6 to be recognized as bootable:

"
The power-on self-test (POST) checks for the following characteristics
to validate a boot record:

* For a device with removable media, such as a diskette, the boot
record must contain valid code. For example, the value of the
first byte of the boot record must be greater than 6.

* For a device with nonremovable media, such as a fixed disk,
there must be a signature of hex 55AA at the end of the boot
record."
"

This brings up a second problem. The second problem is that "varA: db 2" is
becoming the first byte of the first instruction which is executed. That's
not what you coded.

Instead of what you intended to code:

00000000 BF007C mov di,0x7c00
00000003 803D01 cmp byte [di],0x1

ndisasm -b16 file1.bin, shows that you coded this:

00000000 02BF007C add bh,[bx+0x7c00]
00000004 803D01 cmp byte [di],0x1

You may want to put "varA: db 2" at the end of your code just prior to the
TIMES statement.

Unfortunately, your code *as posted* actually works on my machine. Yes, I
just learned from your post that my machine will boot disk with an initial
bootsector byte below 6! It boots a "blank" disk! However, my new machine
won't boot floppies without the 0xAA55 signature. Historically, as quoted
above, the 0xAA55 signature has never been required to boot a floppy.


Rod Pemberton


Darren Gansberg

unread,
Dec 12, 2009, 12:08:51 AM12/12/09
to

Frank Kotler wrote:
> Darren Gansberg wrote:
>> Hi,
>>
>> I've been messing around writing my "bootloader" code when I ran into
>> something which i just don't understand. For some reason whenever i
>> attempt to boot my computer from my floppy drive which has had the
>> following code written to it after being assembled using Nasm, my system
>> simply ignores the code, after attempting to read the floppy, and
>> proceeds to boot windows 7 from my hard drive (2nd boot device set in bios).
>>
>> The code was assembled with the following commands and options for Nasm:
>> nasm -f bin file1.asm file1.bin
>> No errors were reported by nasm
>
> Posto! You must have used "-o file1.bin", or Nasm would have complained
> about more than one input file...

Yes you're absolutely right sorry about that.

Thank you for the suggestions for improving the code which i'll be
implementing very shortly...I have actually worked out now what was
wrong with the code thanks to Rod in his later post.

Kind regards

Darren

Darren Gansberg

unread,
Dec 12, 2009, 12:10:31 AM12/12/09
to

Yes thanks Nathan i have been looking at existing tutorials and other
coding examples. However, i often find that somethings the leave out is
why some things are done certain ways. Hence why i am trial and erroring
my way through some real-mode code as i feel this helps me learn a lot
better...I appreciate this will sound like a long path but i feel i
"learn" a lot more about why something happens as a result of me
stuffing it up, then somewhat following and to a lesser extent copying
other people's code.

Darren

Darren Gansberg

unread,
Dec 12, 2009, 12:12:06 AM12/12/09
to
Thank you very much Rod I learnt several important things from your
reply as a result of my placement of that "varA: db 2" in my code.

The first related to what was being regarded as valid code by the bios.
As soon as i actually changed the value from 2 to 8 the disk was
detected but nothing really happened despite the disk being detected.

This led me to the second and I think more important lesson. Although I
may be wrong about this. I decided to use ndisasm for the first time on
my code as you had done. As you indicated by defining a byte in the
position I had, it had changed the nature of all the intended
instructions. As a result, I believe all my instructions were "screwed
up". Once I moved it, as you suggested, all my instructions appeared as
intended in the disassembly. I feel i now understand the structure of
the assembled code much better now as a result of having used the
disassembler.

Regards

Darren

s_dub...@munged.microcosmotalk.com

unread,
Dec 12, 2009, 3:33:26 PM12/12/09
to

On Dec 11, 12:42=A0pm, Darren Gansberg

<darm...@MUNGED.microcosmotalk.com> wrote:
> Hi,
>
> I've been messing around writing my "bootloader" code when I ran into
> something which i just don't understand. For some reason whenever i
> attempt to boot my computer from my floppy drive which has had the
> following code written to it after being assembled using Nasm, my system
> simply ignores the code, after attempting to read the floppy, and
> proceeds to boot windows 7 from my hard drive (2nd boot device set in bio=

s).
>
> The code was assembled with the following commands and options for Nasm:
> nasm -f bin file1.asm file1.bin
> No errors were reported by nasm
>

Hint: you can generate a listing file by adding: -l file1.lst it'll
give the bytes generated.

nasm -f bin -l file1.lst -o file1.bin file1.asm

> I then copy it to the floppy using partcopy using:
> partcopy file1.bin 0 200 -f0
> No errors were reported by partcopy
>
> All steps above are the same as those for other code I've written and
> run sucessfully.
>
> Please forgive the crap names I've used throughtout my code.
>
> file1.asm
> --------------------------
> [BITS 16]

Hint: you can generate a .MAP file by adding:
[MAP ALL file1.map]

> [ORG 0x7c00]
>
> section .data

Hint: NASM will use a default named section, .text, if no named
sections are specified and it will always place .text section (if any)
first in the generated file.

Hint: An alternative to ORG is to vstart the section..

[SECTION .cseg vstart=3D0x7C00]

> varA: db 2
>
> main: =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MOV DI, varA
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 CMP BYTE [DI], 1 ; expect ZF Flag in Flag=


s reg to be clear b/c 2 - 1

> ! =3D 0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 JNZ printChar ;Because ZF flag clear expe=


ct to jump to printChar which
> prints a 1 and then goes to end

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 JMP printChar2
>
> end: =A0 =A0 =A0 =A0 =A0 =A0JMP $


>
> ;prints 'c' char and then to end

> printChar2: =A0 =A0 MoV AH, 0x0E
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MOV AL, 'C'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MOV BH, 0x00
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 INT 0x10
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 JMP end


>
> ;prints 1 as a char and go to end
> printChar:

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MoV AH, 0x0E
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MOV AL, '1'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 MOV BH, 0x00
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 INT 0x10 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 JMP end

0 new messages