Computers Like Rectangles

The other day I was at a friend’s house, and while waiting for the oven to preheat, I stumbled across a game I once played as a child, a triangular peg board where the goal is to remove pegs such that there is only one peg remaining. I was thinking of devising a program to attempt to solve the puzzle, much like my programming languages assignment last year to solve a slide puzzle. However, the more I thought about it, the more I realized how attached the conventional computer science paradigm is to rectangular data. It is almost too easy to write a program to play checkers (or at least manipulate the pieces on a board), but it is certainly non-trivial to write a simple and efficient algorithm the maintain the board state. I think it’s rather interesting how the problems we generally have to solve fit rather nicely into their little rectangles, and how few problems require other shapes.

For a solution to the peg board problem, I found this site that documents the author’s process and a code listing. If you’re interested in the game as much (or more) than that the programmatic solution, there are also a number of statistics for possible solutions on the site.

Peace and chow,

Ranok

Off on a Tangent: Calling Conventions

There are many things that we take for granted when using a computer: the operating system, hard ware drivers, and graphical interfaces. By learning about these tools, it gives a new awareness into how much work it takes to get even a simple system working. Computer programmers also take advantage of a number of software components: compiler, linker, operating system, memory management functions and debuggers. There is quite a bit of behind the scenes that goes on even in a simple program like:

int foo(int a, int b) {
return a + b;
}
int main() {
return foo(3, 4);
}

Still has many layers underneath the obvious, the one I want to mention briefly today is the calling conventions. I was curious what happens when you call a function, and looking on wikipedia, I found an article that very nicely shows how many possible things could happen.

Normally in with gcc, when you call a function, the generated code pushes the arguments to the function onto the stack in reverse order, that is, last argument first, and then pushes the address of the next instruction to execute and jumps to the function. That function then can access the arguments and put it’s return value in EAX and jump back to the pushed instruction address. The caller must then clear the stack and use the EAX return value.

However, a way to optimize your code with gcc is the -mregparm=N command, which will put the N < 4 first arguments in registers EAX, EDX, and ECX respectively and push the rest onto the stack. This is much quicker since it requires less memory access. However, you must make sure to compile all your code this way, otherwise you’ll have some strange interactions when the conventions are mixed.

Peace and chow,

Ranok

Using Inets – Erlang’s Builtin Web Daemon

A feature I’ve been meaning to add to Open Server Platform for a while is a web management system, where an administrator can login and manage the cluster and the servlets running on it. I’d like there to be a user friendly interface for administrators to start, stop and migrate servlets across the different nodes of the system and a way to upload a servlet file and have it compiled and distributed across the cluster.

The simplest way to start serving web content with Erlang is to use the inets server and the httpd service. This is a HTTP/1.1 server built into the Erlang distribution that supports some more advanced features, most interesting of all, the ability to use Erlang to dynamically generate content. It is however very poorly documented, and there are a few very annoying things I came across that I’m posting to hopefully help anyone else trying to get it working.

  1. The order of modules in the {modules, []} directive matters, if you want to have mod_dir work, it needs to be specified *AFTER* the mod_alias.
  2. The logging is rather horrid, the transfer.log will not log anything except for HTTP 200 for every request, even if it failed.
  3. You must specify {bind_address, any} in the configuration to use the httpd:reload_config function, otherwise it will return {error, not_started}
  4. If you just want to server static content, you will need at a minimum the following modules: mod_get, mod_head, mod_log, mod_actions and mod_range. However, adding mod_alias is recommended along with the {directory_index, ["index.html"]} directive to stop it from failing (HTTP 500) on a directory request.
  5. To use dynamic content, create a module that exports callbacks of the form: function(SessionID, _Env, _Input). To write Str back to the client, use the mod_esi:deliver(SessionID, Str) function.

I hope that this helps out!

Peace and chow,

Ranok