Load DOS exe

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
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Load DOS exe

Post by Firestryke31 »

I must admit, my Google-fu skills are poor today. I can't find how to do this.

I managed to find a couple of sites explaining what the various fields in the MZ header meant (though not to any great detail) but nothing on actually loading and running a MS-DOS MZ exe file. The reason I want this is because I want to have my bootloader run the MZ part of a PE exe, which will then parse and run the PE part. The PE part will be written in such a way that hopefully it will be loader-agnostic so I can use it with my loader, GRUB, and IIRC EFI (or UEFI or whatever it's called).

TL;DR: What's the bare minimum I need to run a MZ exe assuming it's being run from a boot sector?
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Load DOS exe

Post by Dex »

This will run a mz exe or com file, that is loaded at ImageLoadSeg

Code: Select all

;;;;;;;;;;;;;;;;;;;

;; Type checking ;;

;;;;;;;;;;;;;;;;;;;



	cli				; for stack adjustments

	mov	ax, ImageLoadSeg

	mov	es, ax

	cmp	word [es:0], 5A4Dh	; "MZ" signature?

	je	RelocateEXE		; yes, it's an EXE program



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Setup and Run COM program ;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



	mov	ax, es

	sub	ax, 10h 		; "org 100h" stuff :)

	mov	es, ax

	mov	ds, ax

	mov	ss, ax

	xor	sp, sp

	push	es

	push	word 100h

	jmp	Run



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Relocate, setup and run EXE program ;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



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

	mov	dh, 0xff		; let DexOS know it booted from bootprog

	sti

	retf


Also google "bootprog" for full code
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Load DOS exe

Post by Firestryke31 »

Thank you. I was forgetting the relocations as well as the image base adjustment, so hopefully now I can figure out how to reduce my FAT16 bootsector size enough to support finding the file, loading the file, and parsing/running the file. Then I get to do it again for FAT12 and FAT32, and maybe even NTFS. Fun! :mrgreen:
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply