MZ EXE help

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

MZ EXE help

Post by renovatio »

Well, now that my kernel is written, i would like to boot it. I know i can boot it because i didn't used functions that needed DOS interrupts. My kernel is an MZ EXE created by Turbo C++ 3.0. I had been googling for a long time, and found the MZ EXE header format, and a lot of boot sectors that loaded EXEs, but there are not good information about how to load them. Really i don't know anything about EXE files. Can anyone explain me how to load them?

Thanks for helping me and sorry for my bad english ;).
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: MZ EXE help

Post by eddyb »

your kernel is exe?
if you want to can boot it with a common bootloader(eg:grub), it should be in ELF or BIN format.
you can use exe, but it's not so used, and are a few bootloaders that support it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: MZ EXE help

Post by Combuster »

First of all, use google. You can get the MZ format off there easily.
Basically, what you need to do is copy the binary part of the exe into memory, then jump to the entry point as specified in the header (compute the starting CS:IP depending on where the app was put in memory)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: MZ EXE help

Post by renovatio »

I googled for a long time. I can't understand, for example, what is a paragraph, a block, the overlay number?

Code: Select all

RelocateEXE:
        mov     ds, ax

        add     ax, [ds:08h]            ; ax = image base
        mov     cx, [ds:06h]            ; cx = reloc items
        mov     bx, [ds:18h]            ; bx = reloc table pointer

        jcxz    RelocationDone

ReloCycle:
        mov     di, [ds:bx]             ; di = item ofs
        mov     dx, [ds:bx+2]           ; dx = item seg (rel)
        add     dx, ax                  ; dx = item seg (abs)

        push    ds
        mov     ds, dx                  ; ds = dx
        add     [ds:di], ax             ; fixup
        pop     ds

        add     bx, 4                   ; point to next entry
        loop    ReloCycle

RelocationDone:

        mov     bx, ax
        add     bx, [ds:0Eh]
        mov     ss, bx                  ; ss for EXE
        mov     sp, [ds:10h]            ; sp for EXE

        add     ax, [ds:16h]            ; cs
        push    ax
        push    word [ds:14h]           ; ip
Run:
        mov     dl, [cs:bsDriveNumber]  ; let program know boot drive
        sti
        retf

Code: Select all

00-01 	0x4d, 0x5a. This is the "magic number" of an EXE file. The first byte of the file is 0x4d and the second is 0x5a.
02-03 	The number of bytes in the last block of the program that are actually used. If this value is zero, that means the entire last block is used (i.e. the effective value is 512).
04-05 	Number of blocks in the file that are part of the EXE file. If [02-03] is non-zero, only that much of the last block is used.
06-07 	Number of relocation entries stored after the header. May be zero.
08-09 	Number of paragraphs in the header. The program's data begins just after the header, and this field can be used to calculate the appropriate file offset. The header includes the relocation entries. Note that some OSs and/or programs may fail if the header is not a multiple of 512 bytes.
0A-0B 	Number of paragraphs of additional memory that the program will need. This is the equivalent of the BSS size in a Unix program. The program can't be loaded if there isn't at least this much memory available to it.
0C-0D 	Maximum number of paragraphs of additional memory. Normally, the OS reserves all the remaining conventional memory for your program, but you can limit it with this field.
0E-0F 	Relative value of the stack segment. This value is added to the segment the program was loaded at, and the result is used to initialize the SS register.
10-11 	Initial value of the SP register.
12-13 	Word checksum. If set properly, the 16-bit sum of all words in the file should be zero. Usually, this isn't filled in.
14-15 	Initial value of the IP register.
16-17 	Initial value of the CS register, relative to the segment the program was loaded at.
18-19 	Offset of the first relocation item in the file.
1A-1B 	Overlay number. Normally zero, meaning that it's the main program.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: MZ EXE help

Post by Solar »

Google search for "MZ exe" turned up the Wikipedia entry for "DOS executable", which links to http://www.delorie.com/djgpp/doc/exe/, where I find - among other things - that table you posted.

And at the top of that page: "One block is 512 bytes, one paragraph is 16 bytes."
Every good solution is obvious once you've found it.
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: MZ EXE help

Post by renovatio »

and what is the overlay number?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: MZ EXE help

Post by Solar »

A nasty remnant from RealMode times. Unless a RealMode OS with MS-DOS compatibility is what you want, you can ignore that number.
Every good solution is obvious once you've found it.
Post Reply