Assembly and C#

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
Unkn0wn1
Member
Member
Posts: 37
Joined: Fri Jan 13, 2012 11:18 am

Assembly and C#

Post by Unkn0wn1 »

I only very recently found you can develop an OS using C#, and with that being my 'Native' language, I was rather pleased. However, while following the Barebones C# tutorial to get a start, I realised that some tasks need assembly, and that I can't use it directly in C#. So, as I was saying, how do you use assembly with C#?
Not sane
Just remember, FIND Is Not DOS :)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Assembly and C#

Post by jnc100 »

I am not aware of any language that natively supports inline assembly. Even C implements it through compiler-specific mechanism. I think the Cosmos compiler contains methods for directly injecting arbritrary assembler instructions into the output, but tysila (which is used in the Barebones tutorial) does not. However there are actually very few assembly routines required once you are already in protected mode with a working kernel. For those which are required there are methods to call in the libsupcs library (see the libsupcs.OtherOperations class and its static methods e.g. Read/WriteCR3()). These still require a concrete implementation to be provided in assembly though (e.g. see tysos/cpu.asm within the repository), but I am working on removing this limitation, and indeed the class/method names may change in future (as they are currently x86 centric).

edit: you can obviously link assembler/c/any other language modules with the output of compiling c# modules with tysila to provide additional functionality.

Regards,
John.
Rew
Member
Member
Posts: 28
Joined: Mon Oct 29, 2012 2:26 pm

Re: Assembly and C#

Post by Rew »

As jnc100 said, it is compiler dependent. Letting us know which compiler you are using would help. The 3 main (mostly functional) native C# compilers that I know of are:

1. COSMOS
2. MOSA
3. TYSILA

MOSA and tysila both use a similar model. There is a "x86 native dll" reference that you add that contains some pre-established assembly routines. The methods in the reference are just stubs that are recognized by the assembler, which already knows about the routines and inserts the appropriate assembly. COSMOS uses a method that they term "plugging" methods. First you can write a method stub yourself and use it in your kernel. Then you can "plug" the methods by writing a class and/or methods that use the plug attribute. The attributed methods are not actually compiled as c#, but are parsed using their syntax and used to plug the original method.

In all of them you can write a x86/x87 kernel without having to write assembly yourself if you chose to use the tools they have provided. COSMOS is targeted at speeding up early development by providing a platform where you don't need to know a ton about low level x86 processor. You can initialize your kernel off their builtin x86 kernel and start using standard .NET functions (they plugged Console.Writeline to write out to text VGA for example).

A quick search of the code repositories for MOSA and tysila has very obvious intrinsic x86 dlls where you can see what is included in the compiler and if you want more information on assembly in COSMOS you can find a tutorial here: http://www.codeproject.com/Articles/220 ... stem-Intro. It mostly shows their old syntax as opposed to their X# assembly syntax, but the concept remains the same.
Post Reply