Linked Code in external File

Help and discussing programming in C and C++.

Moderator: Moderators

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Linked Code in external File

Post by erazzzor » Sat Nov 07, 2009 7:09 pm

Hy guys,

I'm developing OSs for 3 Jears now, and I've red all ur Tut's with Interrest.
Now first: a praise for the Tutorial Creators.

2nd: My Question:
I am trying to develop a System, who gets a piece of code out of another File, and
executes it. Like the LoadLib Functions of Windows. I try to make Std. Librarys, so
that a app doesn't have to carry the whole System functions with it. Like writing on a display
or drawing a BMP. I dont come forward. Its embrasing. Can anybody give me a lift?

PhillB

PS: Sorry for my (very) bad english. I'm austrian ^^.
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Re: Linked Code in external File

Post by Andyhhp » Sun Nov 08, 2009 1:49 pm

ok

what you are describing is dynamic loading and linking and is a very complicated topic

i suggest you read http://en.wikipedia.org/wiki/Dynamic_loading as a start.

do you have any more specific questions?

~Andrew
Image

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Re: Linked Code in external File

Post by erazzzor » Sun Nov 08, 2009 5:03 pm

Thanx for ur fast reply.

Yea I've two another Questions.
1.
I just tried to add this inline Assembly in ur Demo21 Project:

Code: Select all

void CursorHidden ()
{
  _asm mov cx, 0x2000
  _asm mov ah, 1
  _asm int 0x10
}

but the Curser just dont want to DIE.

2.
I tried to change the Keyboard Template in ur Projects to German(IBM) Layout:

Code: Select all

qwertzuiopü+
asdfghjklöä#
yxcvbnm,.-


BUT
if I compile the Kernel, Bochs dont want to Start.
no Idea why.

THX
Birkl Philipp
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Re: Linked Code in external File

Post by Andyhhp » Sun Nov 08, 2009 6:23 pm

1) You are now in Protected Mode (if you have been following the tutorials) which means you cant use any of the BIOS interrupts any more. Calling int 0x10 in Protected mode will execute whatever you have allocated for your exception 0x10 handler (Invalid TSS exception)

The way around this is to develop task scheduling then create an idleing task that runs in real mode which you use to use the BIOS interrupts. More info is here http://wiki.osdev.org/Virtual_8086_Mode


2) Not certain about what is causing this but you must ensure that the kernel is being compiled as an ASCII binary rather than a UTF16 binary. (MSVS messes about with this behind your back and causes problems). This is not to say that you cant have unicode in an ASCII binary. All this means is that you have to rewrite all your string related functions to accept UTF8 strings rather than ASCII strings.

~Andrew
Image

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Linked Code in external File

Post by Mike » Mon Nov 09, 2009 12:24 pm

Hello,

Dynamic linking and loading is not too complicated assuming that you use existing formats. I personally have only implemented dynamic linking and calling (like a GetProcAddress() routine) using DLLs and PE executables. If you are interested, I can provide my solution here.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Re: Linked Code in external File

Post by erazzzor » Mon Nov 09, 2009 5:09 pm

Thx to Andrew and Mike,

2 Things to ensure that we are meaning the same.

1. I dont want to insert öäü or é è or smth like these letters.
i only want to change the z & the y letter.
Why can you print smth on the Monitor, if you dont use Interrupts?
I realy dont understand...I cant read all of your Tutorials properly, becouse I only have student english. And you can forget about google Translator.

2. @Mike: Do you have Written a rutine, that provides a Dynamic linking & Calling a extern Function(with Params/Maybe for C++)^^?

Whow thats hard work, I think. I would be honored if U share this source.

mfg
Birkl Philipp
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Linked Code in external File

Post by Mike » Mon Nov 09, 2009 6:27 pm

erazzzor wrote:2. @Mike: Do you have Written a rutine, that provides a Dynamic linking & Calling a extern Function(with Params/Maybe for C++)^^?
It is in C. Its more of an interface then a routine that provides alot of different PE functionality. For what you are requesting, it provides GetProcAddress and methods for finding the DLLs imported into a PE (can be helpful to auto-load all DLLs that the program uses.) GetProcAddress works basically the same way that the Win32 APIs GetProcAddress works that allows you to call exported functions from the DLL or PE file.

If you are still interested, please let me know and Ill see what I can do about posting the routines for you to port.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Re: Linked Code in external File

Post by erazzzor » Mon Nov 09, 2009 6:32 pm

Yea i'm very interrested.

THX phill
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Linked Code in external File

Post by Mike » Tue Nov 10, 2009 12:52 am

Hello,

You mention that you have been programming operating systems for three years so I will have to assume that I do not need to explain much of the code.

Here are the routines. Code taken from Neptune's PE library.

You will also be needing:

Code: Select all

#define PE_FILE_MAGIC 0x4550

//! size of nt signiture
#define SIZE_OF_NT_SIGNATURE  sizeof (DWORD)

//! location of PE_FILE_MAGIC in file
#define NTSIGNATURE(a) ((void*)((BYTE *)a + \
                        ((IMAGE_DOS_HEADER*)a)->e_lfanew))

//! location of the pe file header offset
#define PEFHDROFFSET(a) ((void*)((BYTE *)a + \
    ((IMAGE_DOS_HEADER*)a)->e_lfanew + SIZE_OF_NT_SIGNATURE))

//! location of optional header offset
#define OPTHDROFFSET(a) ((void*)((BYTE *)a + \
    ((IMAGE_DOS_HEADER*)a)->e_lfanew + SIZE_OF_NT_SIGNATURE + \
    sizeof (IMAGE_FILE_HEADER)))
The structures used - IMAGE_OPTIONAL_HEADER, IMAGE_DATA_DIRECTORY, IMAGE_EXPORT_DIRECTORY, IMAGE_DOS_HEADER, IMAGE_FILE_HEADER can be found on Microsofts web site via a Google search. These are standard Win32 structures.

This should be all of the information that is needed - you should be able to build your own routines from this information.

Just in case you are not used to the coding convention used, the types are:

Code: Select all

PWORD - unsigned short*
PDWORD - unsigned int*
P-structure-, for example PIMAGE_OPTIONAL_HEADER - structure* object. For example, IMAGE_OPTIONAL_HEADER*
To use, just call GetProcAddress like GetProcAddress (DLLBaseAddress, "myfunction"); and it will return the address of the function from the loaded DLL. The function must be exported from the DLL, using _declspec (dllexport)

I cannot cover everything in the code here. Well, I -can- but rather not in a forum post. If you want to better understand how the code works, feel free to ask or search online and learn more about the PE file format.

This code cannot be compiled or used "as-is". You will need to write your own version of the routine, however you can use the code as a guide.

Hope this helps!
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Re: Linked Code in external File

Post by erazzzor » Tue Nov 10, 2009 4:05 pm

Thx, this helped a lot.

2 Thing to ask left:
Yes I know, I'm onerous. Sorry :S

1. Is this NeptuneOS a OS to execute Dos Apps?

2. This attributes you're Using: INIAPI & INISYSAPI, what are they about?


THX
phillB
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Re: Linked Code in external File

Post by Andyhhp » Tue Nov 10, 2009 4:52 pm

Why can you print smth on the Monitor, if you dont use Interrupts?
I realy dont understand...I cant read all of your Tutorials properly, becouse I only have student english. And you can forget about google Translator.


This is to do with magic known as Memory Mapped Hardware.

In protected mode you can write to the screen by writing a byte to the memory address 0xB8000 (this being the top left character). Instead of being normal memory, that address range is monitored by the VGA card which automatically draws it to the screen for you.

I should also point out that you can do this in Real mode as well. The interrupts do some extra formatting for you (specifically looking after the cursor position for you)

~Andrew
Image

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Linked Code in external File

Post by Mike » Tue Nov 10, 2009 6:38 pm

1. Is this NeptuneOS a OS to execute Dos Apps?
2. This attributes you're Using: INIAPI & INISYSAPI, what are they about?

1) No.
2) Those constants are used to control the import/exporting of the routines and calling conventions. INISYSAPI can either be nothing, _declspec (dllexport) or _declspec (dllimport) it can technically be something else as well. INIAPI is the calling convention, can be a number of different things. For our case, its _cdecl. (Please note: These constants are specific to Neptune's code.)
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
erazzzor
Posts: 6
Joined: Sat Nov 07, 2009 6:40 pm

Re: Linked Code in external File

Post by erazzzor » Wed Nov 11, 2009 1:45 pm

@andrew
Ok I understood...
BUT
If I want do draw pixels on the screen:

Code: Select all


name "pixel"

org  100h

mov ah, 0   ; set display mode function.
mov al, 13h ; mode 13h = 320x200 pixels, 256 colors.
int 10h

mov cx, 10  ; column
mov dx, 20  ; row
mov al, 15  ; white
mov ah, 0ch ; put pixel
int 10h
ret


How can I manage this in PMODE?

thx
Philipp
99.9 % of Errors get caused in front of the Monitor.
The speed of Ketchup from the Bottle is 40 km/year.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Linked Code in external File

Post by Mike » Wed Nov 11, 2009 11:24 pm

Hello,

Assuming that you are already in a video mode, and I am assuming a standard VGA mode (Like Mode 13h and lower), video memory is always in bit planes. It is always mapped into the address space at linear address 0xa0000. (Take note: This is only graphics modes. Text modes can be 0xb0000 or 0xb8000) Just grab a pointer to this address and start writing to it to draw pixels :)

Mode 13h is easy as the bit planes are "hidden", i.e., you can just treat it as a linear frame buffer. Other modes you may have to work with them (like Mode 12h).

For example, in mode 13h, this will render a pixel at an x / y location on screen. 320 is just the width of video display:

Code: Select all

unsigned char* p (unsigned char*)0xa0000;
p [ x+y*320 ] = pixel;
If you want to set the video mode directly in protected mode, you will need to set up the VGA registers to build up Mode 13h. This involves setting 50+ registers to 5 micro-controllers. I can write a small code sample with standard Mode 13h settings if you like.

The easier method ise using the BIOS before going into protected mode.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

User avatar
djsilence
Posts: 30
Joined: Sun Feb 15, 2009 8:49 pm
Location: Kyiv, Ukraine
Contact:

Re: Linked Code in external File

Post by djsilence » Thu Nov 12, 2009 3:54 pm

Actually everything is simple if you gonna use dlls. see, dll have some part where the names of functions listed. First of all it has name of library. then after one character functions names' take place. using this you can get known is your function in this library. after that you have to get the address of that functions and base address. after that you can execute it. I do it in my os. One problem: all functions' declarations have to be written in alphabetic way. end of function is declared via 0xC3. so, that is all. but best of all for you is to look on dll library via Hex-editor. You should see everything yourself.

Daniel.
Thinking of great - thinking of little, thinking of little - thinking of great.

Post Reply