[Ques] Dynamic Linking & Loading
- codemastersnake
- Member
- Posts: 148
- Joined: Sun Nov 07, 2004 12:00 am
- Contact:
[Ques] Dynamic Linking & Loading
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?
- codemastersnake
- Member
- Posts: 148
- Joined: Sun Nov 07, 2004 12:00 am
- Contact:
- codemastersnake
- Member
- Posts: 148
- Joined: Sun Nov 07, 2004 12:00 am
- Contact:
Re: [Ques] Dynamic Linking & Loading
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.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?
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!
- codemastersnake
- Member
- Posts: 148
- Joined: Sun Nov 07, 2004 12:00 am
- Contact:
- codemastersnake
- Member
- Posts: 148
- Joined: Sun Nov 07, 2004 12:00 am
- Contact:
-
- 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:
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)
Readsect is a simpler way of using BIOS interrupt 13h:
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!
(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
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