The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: conio on July 27, 2009, 02:07:55 PM

Title: Writing bootcode
Post by: conio on July 27, 2009, 02:07:55 PM
Hello there,
I'm tryin' to write a boot sector with MASM.
The program, which is 512 bytes long, ending with 2 byte - special signature (as far as I remember it's a 0xAA55), is located at the first sector of a floppy. Than, BIOS reads it and puts into memory at address 0000:7C00. So, I have to ask general questions about MASM:
1) I'd like to make my file exact 512 bytes.  Should it look like this:

org 0

code of a program

org 510
dw 0xAA55

Or maybe there is some special variable which holds the size of a program, so that I would be able to write as much zereos as 510 - ProgramSize, using for example dup instruction.

2) The second question is basically about memory. I'd like to have my starting procedure loaded at certain address - at 0x7C00, while segment register CS=0. How can I make MASM to set entry point address? Or it is not necessary when it comes to dealing with bootsector - maybe BIOS will do all the job for me?
Is there any way to set exact address of a procedure/variable?
Like in C:
char* variable = 0x1234; // that makes this pointer to point on a certain address

Maybe is there something else I need to know/concern?
Any help will be greatly appreciated.
Thanks,
Konrad
Title: Re: Writing bootcode
Post by: dedndave on July 27, 2009, 03:38:31 PM
well - it has been a while since i have played with this stuff
i have not tried it with newer versions of masm
you need to write the program so that it may be assembled using the tiny memory model
then, the exe file needs to be converted to a bin by using exe2bin.exe (well, that's the old way, at least)

as for getting to 7c00, i suggest you make a bootable DOS floppy and disassemble their code
they do something like this

_LOAD SEGMENT AT 0
assume cs:_LOAD
org 7c00h

jmp far ptr LabelA
.
. or
.
mov eax,_BOOT  ;(7C0h)
push eax
mov eax,offset LabelA
push eax
retf

_LOAD ENDS

_BOOT SEGMENT AT 7C0h
assume cs:_BOOT

LabelA:

_BOOT ENDS
Title: Re: Writing bootcode
Post by: japheth on July 27, 2009, 04:12:15 PM
Hello,

this will create a 512 byte boot sector:



.286
.model tiny

.code

org 7C00h
start:
xor ax,ax
;       additional instructions

org 7E00h-2
dw 55aah

end start


And to create the binary run
ml -c boot.asm
link16 /TINY boot.obj,boot.bin;


Title: Re: Writing bootcode
Post by: redskull on July 27, 2009, 05:21:54 PM
This has been covered in several threads recently:

http://www.masm32.com/board/index.php?topic=11397.0

http://www.masm32.com/board/index.php?topic=11890.0

http://www.masm32.com/board/index.php?topic=11282.0
Title: Re: Writing bootcode
Post by: dedndave on July 27, 2009, 07:16:16 PM
so - the linker performs the function of old exe2bin, then - cool - should have always been that way
Title: Re: Writing bootcode
Post by: conio on July 28, 2009, 12:53:44 PM
Thank you very much for help - I'll give it a try and tell if it works :):) Thanks!!!