News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bootloader not working

Started by zak100, July 23, 2009, 03:45:48 AM

Previous topic - Next topic

zak100

Hi,
I am trying to create a bootloader using the following code:


.386
.model flat, stdcall
.code
start:

cld
        mov     ax,0B800h
        mov     es,ax
        xor     di,di
        mov     ax,1F41h
        stosw
loop1: jmp loop1


db 55h, 0AAh
end start




It should print 'A' at boot time. I compiled it and then linked it using Lnk563's Link and then created the bin and copied it on the floppy's boot sector using a utility, but its not working. Can somebody plz help me with this?

Zulfi.

ecube

why don't you try taking out the loop1: jmp loop1 line and see if that works.

MichaelW

The problem is the:

.386
.model flat, stdcall

Which specifies 32-bit code with 32-bit address and operand sizes. What is needed here is 16-bit code, and one way to do this is to replace the above two lines with:

.model tiny

After I made this change the resulting boot disk worked as expected on my test system, but I have doubts that it would work on all systems because:

The 55AAh signature is in the wrong place. A system BIOS that looks for it would normally expect it to be at the end of the sector, at offsets 510 and 511, and without it the sector would not be recognized as a boot sector.

There is no near or far jump instruction at offset 0. AFAIK the system BIOS for some, probably early, systems required this, and without it the sector would not be recognized as a boot sector.
eschew obfuscation

zak100

Hi,
I have changed the code based upon your comments but still its not working. Kindly tell me whats the prob:




.386
.model tiny
.code
start:
jmp test1

test1:cld
        mov     ax,0B800h
        mov     es,ax
        xor     di,di
        mov     ax,1F41h
        stosw
;loop1: jmp loop1
org 510           ; Make the file 512 bytes long
  dw 0AA55h   ; Add the boot signature

end start



Zulfi.

MichaelW

The problem now is the .386 processor directive, which when it precedes the .model directive specifies a default segment word size of 32 bits, so the segments default to 32-bit address and operand sizes, and the code still has the same problem.
Quote
What is needed here is 16-bit code, and one way to do this is to replace the above two lines with:

.model tiny

BTW, you can still use a .386 or later processor directive, but you must place it below the .model directive.
eschew obfuscation

zak100

Hi,
Now its printing two A's. I want just one. Still request for help on this.

Zulfi.

MichaelW

For me it displays a single A. Try disassembling the bin with Debug to see what was actually assembled. Here is a disassembly of the code generated for your second source:

-u
0B10:0100 EB00          JMP     0102
0B10:0102 FC            CLD
0B10:0103 66            DB      66
0B10:0104 B800B8        MOV     AX,B800
0B10:0107 66            DB      66
0B10:0108 8EC0          MOV     ES,AX
0B10:010A 66            DB      66
0B10:010B 33FF          XOR     DI,DI
0B10:010D 66            DB      66
0B10:010E B8411F        MOV     AX,1F41
0B10:0111 66            DB      66
0B10:0112 AB            STOSW
0B10:0113 EBFE          JMP     0113


The DB 66 is an operand-size prefix that MASM added because it was assembling 16-bit instructions into a 32-bit segment. For a 32-bit segment the default address and operand sizes are 32 bits. The prefix overrides the default operand size, which in a 32-bit segment would force the processor to use the 16-bit form of the prefixed instruction. The problem is that at boot time the processor will be in 16-bit real mode, so the default address and operand sizes will be 16 bits, and the prefix will force the processor to use the 32-bit form of the prefixed instruction. So for example, STOSW would effectively be STOSD.
eschew obfuscation

zak100

Hi,
Thanks for your help on this. I found following when I debugged the bin


D:\masm prog>debug boot.bin
-u
0B0C:0100 EB00          JMP     0102
0B0C:0102 FC            CLD
0B0C:0103 B800B8        MOV     AX,B800
0B0C:0106 8EC0          MOV     ES,AX
0B0C:0108 33FF          XOR     DI,DI
0B0C:010A B8411F        MOV     AX,1F41
0B0C:010D AB            STOSW
0B0C:010E 0000          ADD     [BX+SI],AL
0B0C:0110 0000          ADD     [BX+SI],AL
0B0C:0112 0000          ADD     [BX+SI],AL
0B0C:0114 0000          ADD     [BX+SI],AL
0B0C:0116 0000          ADD     [BX+SI],AL
0B0C:0118 0000          ADD     [BX+SI],AL
0B0C:011A 0000          ADD     [BX+SI],AL
0B0C:011C 0000          ADD     [BX+SI],AL
0B0C:011E 0000          ADD     [BX+SI],AL
-q



If somebody has any clue, kindly help me.
Zulfi.

dedndave

now that it is working, put the dead loop back in
if you want to play with this stuff, it is a great idea to make a bootable DOS floppy and disassemble it's boot sector
it will show you the expected structure of the sector
it has bytes per sector, reserved sectors, number of copies of the FAT, etc (called a BIOS Parameter Block or BPB)
here is a link for you.....

http://www.geocities.com/thestarman3/asm/mbr/DOS50FDB.htm

the BPB is not a requirement for the floppy to boot, but it is a requirement if you want DOS to recognize the disk

zak100

Hi,
Thanks for your information. This program is still not working properly. It is printing two A's instead of one. Second 'A' is even blinking. Can somebody plz help me with this.

Zulfi.

MichaelW

This works for me, displays a single 'A' with an intensified white foreground and a blue background:

.model tiny
.386
.code
start:

    jmp test1
test1:
    cld
    mov ax,0B800h
    mov es,ax
    xor di,di
    mov ax,1F41h
    stosw
loop1:
    jmp loop1

org 510

    dw 0AA55h

end start


I used this batch file to assemble and link, and then copy it to the diskette with a debug script:

ml /c bootcode.asm
pause
link16 /tiny bootcode.obj,bootcode.bin;
pause
:debug bootcode.bin
:pause
debug < makeboot.txt
pause


makeboot.txt:

N
N This is a DEBUG script that will copy
N bootcode.bin to sector 0 of drive A.
N
N bootcode.bin
L 0
W 0 0 0 1
Q


Note that the diskette must start out with a valid format, and after the boot sector is modified it will no longer have a valid format (so it will need to be (quick ) formatted before you can repeat the process).
eschew obfuscation

zak100

Hi,

No change in the result. First 'A' is white colored with blue background while second one is white colored with black background. I am working on hp-Dx 2700. Earlier in my other prob., one person was telling about the bad version of masm.
What version are you people using??
You can see my version from this run:

D:\masm prog>debug nasmB_1.bin
-u
0B0C:0100 EB00          JMP     0102
0B0C:0102 FC            CLD
0B0C:0103 B800B8        MOV     AX,B800
0B0C:0106 8EC0          MOV     ES,AX
0B0C:0108 33FF          XOR     DI,DI
0B0C:010A B8411F        MOV     AX,1F41
0B0C:010D AB            STOSW
0B0C:010E 0000          ADD     [BX+SI],AL
0B0C:0110 0000          ADD     [BX+SI],AL
0B0C:0112 0000          ADD     [BX+SI],AL
0B0C:0114 0000          ADD     [BX+SI],AL
0B0C:0116 0000          ADD     [BX+SI],AL
0B0C:0118 0000          ADD     [BX+SI],AL
0B0C:011A 0000          ADD     [BX+SI],AL
0B0C:011C 0000          ADD     [BX+SI],AL
0B0C:011E 0000          ADD     [BX+SI],AL
-q

D:\MASMPR~1>ml /c nasmB_1.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: nasmB_1.asm

D:\MASMPR~1>


Thanks for your efforts.

Zulfi.

FORTRANS

#12
Hi,

   You left out the

loop1:
    jmp loop1"


If you still have the

org 510

    dw 0AA55h


The AA will be a STOSB and give you the extra "A".


Steve

zak100

Hi,
This time it has worked. Thanks for your support.

Zulfi.

dedndave

55h is a "push reg"
after that, you are off in la-la land - lol

it is still a good idea to do these 2 simple things:
1) put the BPB in there - you can still access the floppy under windows (or dos) that way
2) CLI - set the stack to someplace safe (i think DOS uses 0:7C00) - then STI to allow maskable interrupts

;bootable boot sector for a 1.4 mb floppy disk - by DednDave
;use windows or dos to format the floppy, then replace the boot sector

;----------------------------------------------------------------------------------

        .MODEL  TINY
        .CODE

;----------------------------------------------------------------------------------

        ORG     0

;code branch

boot0:  jmp short boot1

;----------------------------------------------------------------------------------

        ORG     3

;OEM identifier

boot03  db      'BootDisk'     ;always 8 characters

;----------------------------------------------------------------------------------

        ORG     0Bh

;BPB - BIOS Parameter Block
;later operating systems use an "extended BPB", which varies with the OS
;but BIOS only uses the original BPB

boot0B  dw      200h           ;bytes per sector
boot0D  db      1              ;sectors per cluster
boot0E  dw      1              ;reserved sectors (the boot sector is reserved)
boot10  db      2              ;number of copies of the FAT
boot11  dw      0E0h           ;root directory entries (224 for 1.4 mb)
boot13  dw      0B40h          ;total disk sectors
boot15  db      0F0h           ;media descriptor byte (F0 for 1.4 mb)
boot16  dw      9              ;sectors per FAT
boot18  dw      12h            ;sectors per cylinder
boot1A  dw      2              ;number of heads
boot1C  dw      0              ;hidden sectors

;----------------------------------------------------------------------------------

boot1: cli                     ;disable maskable interrupts
       xor     di,di
       mov     ss,di
       mov     sp,7C00h        ;SS:SP = 0000:7C00

;normally, a boot floppy would copy the first 11 bytes of the BPB to 0000:0522
;some timing related values are also set to complete the table for BIOS
;then, it would revector INT 1Eh to point to that BPB
;interrupts are left disabled until that task is complete
;once that table is set up, a disk reset is required for BIOS to recognize the new table location
;any boot code that wants to continue and load a boot-strap loader should do this
;for our simple booter, we do not need to do that as the floppy is done being read

       sti

       mov     ax,0B800h       ;notice that di is already 0
       mov     es,ax
       cld
       mov     ax,1F41h
       stosw

boot2: jmp     boot2

;----------------------------------------------------------------------------------

        ORG     1FEh

;validation marker

        dw      0AA55h

;----------------------------------------------------------------------------------

        END     boot0