Sunday, February 04, 2007

Selling Out to C# For a While

I know all my programmer buddies may shun me for this, but I'm selling out to learn Microsoft's C# language for a while. I still hate Microsoft's strategy of developing only for Windows and being stuck with their products, but most project offers I'm getting these days require C# and related .Net technology knowledge.
C# itself is not bad - in fact, it takes the best ideas of the C/C++ standard and the Java programming model and combines them in a robust environment. What sucks is that you develop programs that'll run on Windows only (special consideration for Mac and Linux platforms) with their .Net framework. I hate being stuck in a box like that. But for the money I'm foregoing rejecting .Net-oriented programming, I'm caving in.
If at all, I'll take the best ideas from .Net and apply them to my Java programming experience. It's all a learning experience, and I come out more versatile than ever. The hate remains though ...
This is probably the best time to pick up this skill because:
  1. Microsoft finally offers free developer tools, such as Visual Studio C# 2005 Express. Small downloads and high integration with Windows (.Net Framework 2.0 must be installed).
  2. Enough available documentation: being a new language, I'm surprised at how many free tutorials are available, including code samples and sometimes complete solutions. Sweet ... I learn by example.
  3. I have a huge cache of ebooks, and some happen to be C#-oriented. Essentially, I can learn this technology for free. I recommend Sam's Teach Yourself Microsoft Visual C# 2005 in 24 Hours for beginners.
Slowly over the next couple of month, I'll steal time from my busy day to learn this language. The motivation is purely monetary - I have no desire to become a Microsoft/Windows programmer.

Saturday, January 27, 2007

The Quintessential MIPS Assembly Language Script

This small program demonstrates what you need to get started developing assembly language scripts for the MIPS architecture. You only need two tools: a SPIM simulator (e.g. PCSpim v2.03) and a text editor (notepad).

This script demonstrates:
  1. How to declare integer and string variables, and arrays.
  2. How to manipulate arrays.
  3. Using the la (load address), lw (load word), sw (save word), add (addition), addi (add immediate), sub (subtract) instructions.
  4. How to print string and integers to the console.
  5. How to organize a basic assembly language script.
  6. How to exit the program.
When you load the script into PCSpim, the .data section is executed. The data segment is an area in memory where data for the program is stored. In the script, I declare a variable called 'num' of type .word and assign it the value 7. An array is declared by providing a sequence of values, and a string by using the .asciiz type and providing a message in double quotes. PCSpim shows the data segment for our program as such:
[0x10010000]
0x00000007 0x00000002 0x00000004 0x00000006
[0x10010010]
0x00000008 0x0000000a 0x75736552 0x203a746c
These numbers are in hex. The first one ([0x10010000]) is the address in memory where the data is. On a 32-bit computer (like most we have these days), it means data is stored in 32-bit words (thirty two 1's and 0's). But we know that 8 bits = 1 byte, so 32 bits (1 word) = 4 bytes. This is the basis for showing the data as "0x00000007 0x00000002 0x00000004 0x00000006".
The first byte (0x00000007) is the number 7, the value of the variable 'num' in the script. The second byte (0x00000002) is the number 2, which is the first value of the variable named 'array', and so on. The second word is "[0x10010010] 0x00000008 0x0000000a 0x75736552 0x203a746c": it's at address 0x10010010 and contains 8 (0x00000008), 10 (0x0000000a), 'Resu' (0x75736552), and 'lt: ' (0x203a746c). If you notice, all data is loaded sequentially into data blocks the way it was written in the script.

Other observations:
  • la of a variable loads the address of the first element. If you lw a variable such as 'array', it'll load the value of the first element into the register.
  • Array values are dereferenced using 4 byte increments (4 bytes = 32 bits = 1 word). So if array address is loaded in register $s0, the first element's value is 0($s0), second is 4($s0), and third is 8($s0), etc.
  • In the above example, the address of array element #1 (2) is 0x10010000 + 4 bytes = 0x10010004. That's basic address arithmetic for you.
  • Each character is stored as 8 bits. So a word (32 bits) can only contain 4 characters (e.g 'Results' would have to be split into 'Resu' and 'lts', and stored in two bytes (even if 'lts' doesn't quite fill a byte).
Why the heck would anyone learn assembly language? If you are a serious programmer, you want to know that assembly is the final stage your Java or C code is translated to before being ported to machine language (binary). Learning efficiencies that can be acheived at this stage may help with better compiler design and debugging. I like it for my endeavors in reverse engineering - this is where you learn what a program really does when you don't have it's source code or know the language it was written in.

Thursday, January 18, 2007

How to Create Your Own Wiki

I'm sure you've heard of Wikipedia. But what if you could run your own such site? For me, I need a wiki for documentation of my projects internally, but I can see small companies or business units using such a solution to share information or document various policies and procedures. If you have a lot of static documents sitting around your network, a wiki might be a good idea.

The technology behind Wikipedia is Wikimedia, an open-source wiki engine. Wiki engines were originally built for Linux platforms running Apache webservers, open-source or free databases such as MySQL, and easy-to-use scripting languages such as PHP (and thus the acronym LAMP). As such, the most popular implementations of wikis require good knowledge of these 4 technologies. Fortunately, anyone can install and run their own wiki without knowing too much about them.


Prepare Your System
These steps demonstrate how to install Wikimedia on a Windows-based machine using the Saint AMP suite. I do not delve into issues involving having other web servers (such as IIS or application server containers) or databases already running on the system.


Download Software Required
(1)
The Saint AMP Suite (3.4.0): package that includes Apache webserver, MySQL database, and PHP - all designed to work together. Because it is installed as a single application (called a stack), you'll get running much sooner, and do not have to learn PHP or MySQL.
(2) Wikimedia (1.9.0): the wiki engine itself.

Install The Saint WAMP (TSW)
  • Run the TSW executable. Language=English [OK]. Review the welcome message and accept the licence.
    Choose Components: Full WAMP, everything selected. [Next].
  • Choose Install Location: Minimum space required is 160MB. Consider that the database will grow as users create pages and upload files. [Next].
  • Apache global options: provide an email, change the HTTP port for the webserver to use (81, for example, if you have another webserver listening on 80). Leave server name as 127.0.0.1 for portability. [Next].
  • Module Activation: Choose only mod_dav2, mod_gzip, and mod_status. We don't care about the others since we won't be using SSL or integrating with Python engines or IIS/ASP. [Next]
  • Database Options: mysqld-nt.exe. This is the service that'll run the database and listen on port 3306, the default MySQL port. [Next].
  • FileZilla FTP: defaults to listen on port 14147. Leave defaults and click [Next].
  • Start Menu Folder: cleanup the name as how it should appear in Start > Programs. [Install].
  • During installation, a "Connect to Server" dialog will popup. This is the FileZilla service request. Select Always connect to this server and click [OK]. You can close the window.
  • When installation finishes, ensure that the Do you want to start Apachemon now? checkbox is selected and click [Finish].
  • In the taskbar, double-click the Apache Monitor, highlight the TSW-Contentserver and click [Start]. Click [OK] to close the monitor dialog.
  • Ensure MySQL is started (Start > Programs > The Saint WAMP > DB > MySQL > Mysqld-start
  • Open your web browser and go to http://localhost:81/ (if you used port 81). A page should inform you on the success of the installation.

Configure Apache2

Configure the Apache webserver admin account:

  • Open the DOS console (Start > Run ... and type 'cmd').
  • Browse to $tsw$\Apache2\bin. ($tsw$ is the folder where you installed TSW above).
  • Run "htpasswd -cmd .htpasswd ". Replace with the username of the admin account. You are prompted to assign and confirm a password.
  • Run "copy .htpasswd ..\conf". Confirm a file replace. This replaces the default Apache admin account with one you just created.

Change the Apache DocumentRoot - where web documents are stored.

  • Open Windows Explorer and browse to $tsw$\Apache2\conf. Open the httpd.conf file.
  • Find 'DocumentRoot' and assign it an alternative directory, perhaps on a drive with more space or simply outside the folder context of the web server (security measure, maintenance ease).
  • Also change htdocs inside '' to the full name of the sirectory specified above.
    Stop and restart the TSW-Contentserver in the Apache Monitor for changes to take effect.
  • Open http://localhost:3000/ using the admin account iinfo you created above to view various aspects of the server. This is your system's administration console pretty much.

Install Wikimedia

  • Open the Wikimedia package with Winzip, and extract the included compressed file to the DocumentRoot folder of the Apache2 webserver (make sure you use folder structure while extracting).
  • Rename the extracted directory to wiki. (Called $wiki$).
  • Open http://localhost:81/wiki/ to configure the wiki engine for the first time.
    Site Config: provide a wiki name.
    Site Config: provide the admin username and password (for a new wiki account).
    Database Config: provide DB username/password (for a new MySQL account).
    Database Config: check the 'Use superuser account' option - used to create the above account.
    MySQL Options: provide a table prefix for all tables created in MySql for the wiki.
    Click [Install Wikimedia].
  • Move the $wiki$\config\LocalSettings.php to the $wiki$ folder.
  • The wiki can be accessed at http://localhost:81/wiki/.

At this point, your wiki works just like Wikipedia. If you are new to wikis, you should peruse the Wiki User Guide to learn how to add content.

Friday, January 12, 2007

IE Friendly HTTP Error Messages Not Useful for Debugging


I do a lot of my browser testing (client side for web applications) using Firefox. Whenever I experience an unexpected result in Firefox is when I check with IE. Internet Explorer has a feature that intercepts error-encoded messages and displays a user-friendly error page that hides the raw text of the error - useless when you want to debug with the browser. It'd be helpful if the "More Information" option actually showed the error, but it simply restates what is already mentioned elsewhere on the page. Dumb.

To disable this feature, open Internet Options -> Advanced Tab and uncheck the "Show friendly HTTP error messages" option. If you troubleshoot page display errors across a network, it might help to have this option cleared in group policy so it's distributed to all organization browsers. Or you could use the IEAK.

I've had to do this multiple times when users say they can't access pages and an error page is what appears.