in & outportb in c++ and intel asm

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.
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

in & outportb in c++ and intel asm

Post by HJED »

hi
could anyone point me to a tutorial or tell me how to create the inportb() and outportb() methods using microsoft visual c++ 2005 and/or intel syntax
.................................. um what should i put here .............................?..........................................
..... :D................
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 »

Perhaps using the inb and outb instructions?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

hehe... I actually just written one for my new kernel, along with a few other usefull instructions... ;) Im not at my computer right now, so I cannot post it.

Either way, this is surprisingly simple. Do you have experience with inline assemby? All you need to do is store the parameters in the correct registers, and read/write from the port...
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Here you go (from my OS):

Code: Select all

inline	unsigned char _cdecl inportb (unsigned short portid) {

	unsigned char res=0;

	_asm {
		mov dx, portid
		in ax, dx
		mov [res], al
	}

	return res;
}

inline	void _cdecl outportb (unsigned short portid, unsigned char value) {

	_asm {
		mov	al, [value]
		mov	dx, [portid]
		out	dx, al
	}
}
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

Post by HJED »

thank you for the help 8)
.................................. um what should i put here .............................?..........................................
..... :D................
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Post by kataklinger »

And other useful information about using inline assembly in VC++:

Code: Select all

http://msdn2.microsoft.com/en-us/library/4ks26t93(VS.71).aspx
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

do you think that such a code will work probably in vc++ ?? i think under windows you can't do that because of the protection levels ...right? :D
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 »

mohammed wrote:do you think that such a code will work probably in vc++ ?? i think under windows you can't do that because of the protection levels ...right? :D
Under windows you can't... But this is under his OS, unless I am greatly mistaken.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I just wanted to add that MSVC++ inline assembler supports every opcode (including lgdt, lidt, et al.). As long as the code being executed is less then or equal to the Current Protection Level (CPL), the code will execute just fine.

Windows applications run at a greater protection level (ring 3) then the required ring 0 protection level, hence it will not work under windows, but it will work just fine under a ring 0 kernel.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

they can develop their oses with vc++ ?? how come ? i think this compiler can't produce an independent software you know what ? even when i was working in console applications my programs never worked under pure dos it can work only under windows although it is in text mode !!
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 »

You compiled with the CLR (common language runtime).

Look at the command line options and you can create "native" software.

In fact, there is information on how to make an OS with VC++ as your compiler in the wiki.
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

hmmm you destroyed an old deed i thought that there is two kinds of compilers ...some that can't make a separate programs because they add their optimizations and so on and others like gcc that can make it !
i am reading thw wiki right now !
http://www.osdev.org/wiki/Visual_Studio
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I'm not sure what you mean by different types of compiliers... All C++ compiliers produce object code for a specific platform or architecture.

Several members on this board in fact (Including me) is using MSVC++ for their compilier ;)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

i thought that every body here is using gcc !! another deed changed : ) )
OrOS
Member
Member
Posts: 143
Joined: Sat Sep 08, 2007 11:26 pm
Location: Canada

Post by OrOS »

Several members on this board in fact
Me now as well, since I like the IDE and its got great support once I add the runtime functions.
Post Reply