[Ques] Dynamic Linking & Loading

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
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

[Ques] Dynamic Linking & Loading

Post by codemastersnake »

I just wanted to know how does Operating Systems load executable files in the memory and transfer control to them and when finished get back the control?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I can post code, do you want code? My code loads from a FAT12 filesystem.
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

Sure if you can!
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I'll get it to you by the end of the 19th, as I am not currently able to access the code.
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

no probs
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Re: [Ques] Dynamic Linking & Loading

Post by INF1n1t »

Snake wrote:I just wanted to know how does Operating Systems load executable files in the memory and transfer control to them and when finished get back the control?
The whole thing is about jumping far to another location |which changes the CS register|. Loading a file into the memory isn't complicated, but you have to write file system and disk io code. For example, most people (like me) write a boot loader code to load their kernels and stuff into the memory.

So, you load the file and then change indirectly the CS:(E)IP registers, because otherwise the processor will continue executing what's on the next memory address and will 90% crash (if you have luck it won't) :)
I think, I have problems with Bochs. The biggest one: Bochs hates me!
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

Is this the way many commercial OS manages the child processes
Otter
Member
Member
Posts: 75
Joined: Sun Dec 31, 2006 11:56 am
Location: Germany

Post by Otter »

Is this the way many commercial OS manages the child processes
Most commercial OSs use paging and multi tasking. They create a new address space for the process, load the executable image and set up the page tables for the new process. After that, they create a new task and call it
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

Ok thanks I'll see to this topic after I finish other modules of my OS
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Post by INF1n1t »

I thought you're talking about loading a file into the memory. The multitasking is a different thing..however..Otter gave an explanation of it..
I think, I have problems with Bochs. The biggest one: Bochs hates me!
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

The following code is a jump to a file in sector 40 of the floppy drive. Note that this does not return back, I'm still trying to figure that out...

(NASM CODE)

Code: Select all

	push es

	mov ax,0x0050
	mov es,ax
	mov bx,0

	mov bx,llBuff					; the address of the location to load to
	mov ax,40					; sector to read from
	call readsect					; read it

	push ds ; save data segment
	call word 0x0050:0x0000 ; jumps to the place we just loaded it to
	pop ds ; restore data segment

	pop es
Readsect is a simpler way of using BIOS interrupt 13h:

Code: Select all

; ES:BX = Locationto load to, AX = Sector
readsect:
	mov si,[bsTrackSect]
	div si						; divide logical sect by track size
	inc dl						; sector # begins at 1
	mov [bsReserv],dl				; sector to read
	xor dx,dx					; logical track left in ax
	div word [bsHeadCnt]				; leaves head in dl, cyl in ax
	mov dh, [bsBootDrv]				;
	xchg dl,dh					; head to dh, drive to dl
	mov cx,ax					; cyl to cx
	xchg cl,ch					; low 8 bits of cyl to ch, hi 2 bits
	shl cl,6					; shifted to bits 6 and 7
	or cl, byte [bsReserv]				; or with sector number
	mov al,1					; number of sectors
	mov ah,2					; use read function of int 0x13
	int 0x13					; read sector
	jc rsfail					; display error message
	jmp readcomplete				; skip error handler
	rsfail:						; error handler, print msg
	mov si,llReadFail
	call putstr
	readcomplete:
	ret						; return to caller
The COM file that is run must exit itself by using 'retf', otherwise control will just flow straight through the memory locations causing unwanted code to execute - not good!
Post Reply