Page 1 of 1

[Ques] Dynamic Linking & Loading

Posted: Thu Jan 18, 2007 1:50 am
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?

Posted: Thu Jan 18, 2007 2:00 am
by pcmattman
I can post code, do you want code? My code loads from a FAT12 filesystem.

Posted: Thu Jan 18, 2007 2:03 am
by codemastersnake
Sure if you can!

Posted: Thu Jan 18, 2007 2:38 am
by pcmattman
I'll get it to you by the end of the 19th, as I am not currently able to access the code.

Posted: Thu Jan 18, 2007 3:21 am
by codemastersnake
no probs

Re: [Ques] Dynamic Linking & Loading

Posted: Thu Jan 18, 2007 3:44 am
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) :)

Posted: Thu Jan 18, 2007 7:21 am
by codemastersnake
Is this the way many commercial OS manages the child processes

Posted: Thu Jan 18, 2007 7:30 am
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

Posted: Thu Jan 18, 2007 7:36 am
by codemastersnake
Ok thanks I'll see to this topic after I finish other modules of my OS

Posted: Thu Jan 18, 2007 4:47 pm
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..

Posted: Thu Jan 18, 2007 6:20 pm
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!