Scott Hanselman

Rescuing the Tiny OS in C#

April 02, 2007 Comment on this post [15] Posted in Learning .NET | Programming
Sponsored By

About four years ago (in the middle of the last fifteen years of writing software for money) I graduated from college. I took a round-about way through school at OIT - my four-year degree took eleven years (I actually had to make deals with four different deans to allow me to keep my credits from expiring) but it turned out OK.

Some where in the middle of going to school at night we were asked in an Operating Systems class to write a "Virtual CPU and OS." More of an interpreter, we were given a description of the OS, some op-codes and an ASM-like machine language. We were to write the OS, and feed test programs into the system. It was a fun exercise and a good way to teach folks how virtual memory and memory mapping works, threads vs. processes, etc. I was the only student who chose to write it in managed code. Quite the coup for 2002.

I turned in the project and threw it up on GotDotNet (this link will be dead in July of 2007), but of course, GDN will be gone soon. Just so it wouldn't be lost, it's now available here in C#. It's actually kind of fun, I think, as I put in some "jokes" - funny to me at least. For example, the OS pages it's virtual memory to XML files when it runs out of "physical" memory. It behaves like a real little OS. It'll thrash with low memory, and get fragmented if a lot is going on.

A tiny virtual CPU and OS written entirely in C#. The TinyOS simulates the scheduling, memory management (including paging and virtual memory) and other operations of theoretical Operating System. You’ll see many Framework classes and techniques in use including Regular Expressions, XML Serialization, generated Strongly Typed Collections, XML Comments and a generated Help file. You won’t get much useful work out of the TinyOS itself, but the techniques you’ll learn can be applied to real life.

Since this little ditty, others who really know what they are doing have written more serious OS's in C# that are worth looking at.

I presented on my little CPU/OS at TechEd Malaysia 2002, and the PPT slides are available as well. I also did it again in VB.NET if you like with a little trouble.

I looked at this code in 2005 and was shocked and offended with myself - well, not really, but it was enlightening to say the least. 

Go find some code that you wrote at LEAST FIVE years ago and tell me how it looks today...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 02, 2007 8:39
Just had to download it, to see if this was an april fool's joke...
April 02, 2007 9:34
Okay -- i've worked how to tinker with this (i've upgraded it from vs2002 to 2005 with no glitches, which was nice).

When no parameters are entered, it tells you:
"Usage: OS membytes [files]"

So you run it like this:
OS.exe 1024 scott1.txt
for example -- meaning, allocating 1024 bytes, and execute the file "scott1.txt" (which you'll find provided with it).

Okay -- when you run these files, which are short machine-code programs, a lot of stuff scrolls up the screen... you need fast eyes ;-)

I found what the instructions mean:
public enum InstructionType {
Noop = 0,
Jmp (Good old Jmp!),
So first up i've written up a suite of Nunit tests to test if these instructions each display correct behaviour. (Mostly passed -- one suprise mistake in there!)

Then I wrote up a short assembly language compiler (in powershell) that lets you assign and use aliases for these commands, and compile aliased code into machine at compile time.

Next I wrapped some obvious functions (some 2d analysis, bitblt, and the like) into a small library that gets preloaded into the virtual os, to see if we can create a higher level abstraction as a basis for future

And then as a demonstration, i've written a gif to ascii converter based on this OS, and tried to out converting a picture of anakin skywalker into a 64*64 ascii picture

Check out all this and more here:
April 02, 2007 10:26
Hey Scott, nice new theme! I apparently missed the rollout of it since I usually read everything via my feed reader.

I have to take the bait on your comment about telling you how our five year old code looks. Five years ago, I would have been at Commerce Bank for a little over a year, and hoo-boy, did I write some embarrassingly bad classic ASP and VB in that era. Those first couple years at Commerce were a crash course for me in a lot of things - web technology, pseudo-OOP (realizing that we're talking about VB 6), COM, enterprise development, etc. Five years later, I can say with confidence that this code is amazingly bad; Especially anything where I tried to use XML for no reason other than because it's what the cool kids were doing at the time. Any of the code I wrote in that timeframe should be treated as hazardous material and properly disposed of. Thankfully, the programmers who maintain that application today have long since replaced most of my offending code - except the nifty little VB app I wrote to bridge our contact center softphones to our ASP-based web app via DDE.

Prior to coming to Commerce, my start was as a subcontractor for an independent consultant who had a thing for XBase languages. When I started as his apprentice at the age of 12, it was Clipper. Then FoxPro 2.6 for Windows. Then Visual FoxPro 3.0, 5.0, and, by the time I graduated from college and went to work for him full time, Visual FoxPro 6.0. If I were to look around my home office today, I could probably find some code left over from that bygone era, but that begs the question: Why on Earth would I want to? :)

(Disclaimer: These comments are solely my opinion and do not represent or express the position of Commerce Bancshares, Inc.)
April 02, 2007 10:48
Thank goodness you've saved the Tiny Abstract OS from the doom of GotDotNet!
April 02, 2007 14:22
i did wrote a 8+ years old VB program, and now couldn't find anything just the program name and description on one of downloads sites and the link even broken. :( .
April 02, 2007 16:13
+1 to the new theme. I read via a reader too, so I missed the roll-out date (but I do remember you talking about it).

Anyhow, I remember doing this project in class as well. I did mine in VB6. And it was a pain.
April 02, 2007 16:37

I've been stalking your blog for some time now and it's a great lunch break activity! One quick question... what tool did you use to compile the generated help file? Do you recommend it?
April 02, 2007 17:50
What was your grade for this project?
April 02, 2007 19:50
I believe I got an A for this project.
April 02, 2007 20:33

It's time to grab what you might need from GDN. I don't think their stuff will be relocated once the site is gone.
April 02, 2007 21:34
Justin - I used NDoc in this example.
April 02, 2007 23:29
I had to write an OS in java for my CSS degree (I only took 7 years to graduate though). The highlite project of my degree was we had to write a 68000 series dissassembler in assembly, it was a PITA but I got an A and my friend and I were the only group of 2 in the class (most others were groups of 3 or 4). After doing this project, I will NEVER forget how to work with 68000 series assembly language. ever. I also did write an OS in Java during my degree, same idea as what you did except no virtual PC because the JVM effectively provided that. It didn't do too much but it was a great excerise in learning about threads, processes, scheduling, and file system management with disk I/O (simulated with a text file, I assume yours probably does something similar).

Even looking back, the code I wrote for that isn't too bad, although there are a few minor things I would have done differently. Most of my other old code is horrible, however. My first real programming experience came on the TI-85 graphing calculator (my friend wrote a nifty little RPG called CalcRPG back then, it was pretty cool in 7th grade). Eventually, we attempted to port CalcRPG (now graphical on the TI-92) into VB and created VBRPG which I think has some of the worst code in the history of the world. It's gotten much better over the years and the stuff I did in the last two years really isn't all that bad.
April 03, 2007 0:59
I found code that I wrote less than a year ago that I am shocked by.

It's amazing how quickly your techniques and problem solving can change or go a way depending on their use. I would like think playing with my electronic Sudoku (day and night) has helped with it. When I started with that, I was taking about 10-15 minutes on an easy level puzzle. Now that's down to less than 3 on average. The more difficult ones would take anywhere from 1-2 hours. I'm now down to 15-20 minutes.

One other thing that I noticed was the amount of comments I left myself (and others). There are MANY more now as I am figuring out that they are truly needed. One of them I know I wrote, but I just don't remember it.

I'm going to play with TinyOS now and try the 2002 - 2005 conversion. Then learn a bit more about OSes. Thanks for your work and I'm glad you were able to actually use something from college in the real world! ;-)

Love then new design for your blog by the way!
April 04, 2007 21:14
Looks cool, Scott. Not to be confused with the other, though.

April 09, 2007 9:51
I started looking at this again and came up wiht a tiny challenge, one that I amy just take myself up on. Now that we have an idea of how a single CPU would process data, what about 2+? It would give a little insight into how those kind of processors work and how the OS handles them...

That might take me a little while to figure out since I am pretty much clueless on that concept right now. But your work here jsut might give me the starting point to look at that.

Thanks again!

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.