21 July 2010

Learning basic assembler.

I've been learning assembler, so I can write a code generator for Obelisk. I'm at page 98 of (the PDF version) of Dr Paul Carter's PC Assembly Language.

So far, I've learned how to call C functions. I need to do this because the Obelisk runtime is written in C!

Anyway, it aint much, but here's a 'hello world', written for NASM, the netwide assembler..

hello.asm:




; Hello world program!

; The data segment contains data that is always in memory (unlike the stack)
segment .data
format db 'Hello world!', 10, 0 ; The format string for printf

; The text segment contains the code the machine will execute
segment .text
   global main ; Main must be global for gcc to find it!
   extern printf ; We must declare that printf is defined in another object file.

main: push dword format ; Push the argument to printf on to the stack.
      call printf ; Call printf
      pop eax ; Remove the argument from the stack



This will need to be linked to libc because it uses printf. I'm using gcc because it's simple.
So, in the shell, assemble and link and run with:




$ nasm -f elf hello.asm && gcc hello.o -o hello && ./hello
> Hello world!



On my computer, the object file hell.o is only 608 bytes long

20 July 2010

Garbage collection

Obelisk has a simple copying garbage collector now :)

18 July 2010

Obelisk

I've started a new programming language project: Obelisk.

Check out what I have so far at

Obelisk github repository

That is the place to go if you want to understand the syntax and semantics of Obelisk.

The rest of this post is to outline the reasons why it's going to work! Some may consider it a bit of a rant, but I assure you that I am not a crackpot!

Firstly, about my last programming language project, Delve. It petered out for a number of reasons:
I didn't have a proper lexer.
I wrote my own (buggy) parser.
I was trying to write a type-checker for a language where new methods can be added to an object, or an object can change its class, at any time.
I wrote the runtime in Haskell, which processed high-level bytecodes. It was very slow.
My approach was a rather chaotic, and I didn't produce very much documentation (thank you beer).

On the plus side, I did learn a lot about programming language design, and the process definitely improved my programming skills .

However, Obelisk is different!

Obelisk is a strongly typed, systems programming language. Obelisk is theoretically based on the lambda calculus, but has a class based object system to create its data types.

Well, it will be and have all these things; and it will work out this time! I know this because:

I have a solid lexer, based on Text.Parsec.Token

The parser is generated by happy, which also has the benefit of showing that the grammar is unambiguous. This is A Good Thing.

Obelisk's semantics are much simpler, so I already have a type-checker for non-polymorphic non-higher order (lower order?) functions.

I am writing the run-time in C, and initial tests indicate it should be quite fast. And because the semantics of Obelisk are again quite simple, I am able to adapt much material in The Dragon Book to suit its needs.

Also my approach is better:

I have divided development into iterations. An iteration is a phase of development where a large proportion of the code-base must be changed in order to support new features. For instance, I am not going to worry about classes and objects, until I have a full working system for compiling simple-functions. The latter will take place in iteration #1, the former in iteration #2.

I also have ~1500 words of documentation so far, in a html file. This doesn't sound like a lot, but since I'm only concerned with iteration #1 objectives (simple functions) there isn't so much needed. Still, it documents all scoping and type rules, and gives examples.

So aye, you might say that I'm rather a chufty at this juncture. That's about all for now. Look at the repository for more information!

25 June 2010

Wheee! ....and a small utility

"Hello,

      We have received your appeal regarding your blog http://killersmurf.blogspot.com/. Upon further review we have determined that your blog was mistakenly marked as a TOS violator by our automated system and, as such, we have reinstated your blog. We apologise for any inconvenience this may have caused in the meantime and thank you for your patience as we completed our review process.

      Thank you for understanding.

      Sincerely,

      The Blogger Team"

ACE. From reading other's posts on the subject, I was worried I'd never see this blog again.

I hope they don't see my copy of the email above and think I'm leeching data and spamming it out again!

Anyway, here's a small utility, in haskell, for creating c/c++ header files, using #define to prevent multiple inclusion:

mkheader

See the README for details on how to use.

I wrote it because I realized I did this for every single header file I wrote! Using this saves a couple of minutes every time.

20 June 2010

Car wash

Bry and Hanner and I took the car through the wash today.

I've always loved it since I was small: I was fascinated with the whirling brushes, the track that drags your car; every part of the squeaky clean, shiny automated washing process.

So we laughed at the whooshing brushes and we bonded and the car was cleaned. Then I ran over a dead rabbit on the way out.

9 June 2010

Transcribed some Silly Wizards banter

Andy Stewart: "Thanks very much indeed. Och well, you're not as tough as you thought!

I'm gonna sing you a maybe a different kind of a song. It's not really a love song as such, maybe it is, I dunno."

John Cunningham: "It's kinda, it's like a lust song."

Andy Stewart: "That was Johnny Cunningham there. You'll eh... you'll hear a lot more of him before the night is out, no doubt.

The song we're gonna sing you right now is, eh, a song from the North of Ireland, and it tells the story about how a young girl decides one day she's gonna go out on a nice, well, one evening infact, she's gonna go out on a nice summers evening and take a walk, down beside the river Bann.

And um, the first time I heard this song, I thought to myself: now, if this woman has heard any of the other five hundred songs that are all called 'The Banks of the Bann', and she's still going down there, then she's out of her mind, because there's always some young cad there. You know sorta cutting around and there's always a bit of misbehaviour (well, quite a lot of misbehaviour actually) and tears and recriminations and maybe a kid involved somewhere or...

It's an age old story but distressing nonetheless. You know, that's how all these songs tend to go and I just couldn't help thinking: there's tonnes of space, you can go anywhere you... you don't have to go down there. It's just not worth it."

Transcribed from the video: Silly Wizards Live - Willie Archer and the Banks of the Bann

28 May 2010

A circular ring of electrodes with a power gradient facing North

Lepht wants to know where North is. However, instead of buying a compass, she's decided to turn herself into one. What sort of self-respecting transhumanist wouldn't?

I have some tcheuchter friends who claim to always know the direction of North, but that may just be because they've never left the Moray coast.

This post starts with some further ideas for making Lepht's invention run smoothly, and ends with edited comments from Lephts original post, in order to provide a record for them. If you have no freaking idea what I'm talking about, then it may be an idea to start reading from below the horizontal rule. Otherwise: onwards!

If you are going to have some sort of gradient, it may be wise to write a small program which would allow you to see graphically the power output at each electrode.

Otherwise, you might end up with all sorts of problems. For example, using my solution you might find that too many electrodes are powered on, which doesn't really indicate north very well. You might want to vary the steepness of the parabola by multiplying the quadratic term by a constant. The effect of this would be that electrodes further from North may receive more or less power.

Writing a small program would allow you to experiment with the variables, until you find something nice, a comfortable time before lots of blood is involved.

That's my thoughts at least.



The rest of this post is derived from comments on Lepht's blog, though it isn't a rip-off - I wanted to summarise the thoughts we had so far in its own blog post, so that anyone who wanted to see what we were thinking could do so without looking through comments.

Lepht proposed the following Microcontroller code:


while (poweron)
get north direction from compass module;
cast to a degree out of 360;
figure out which electrode's "domain" that number falls into;
activate that electrode;


However Max had an idea for improvement:

"the whole thing could be a lot better if you could get it to produce a gradient of current between electrodes, so that when north is between two electrodes, you have both firing at half power instead of the signal snapping to one of them."

My comment was a proposed solution:
The sort of function we might use may be a quadratic with a parabolic graph.

y = P - (x - n)^2

Here P is the maximum power, n is north's angle round the leg, and x is the electrodes angle round the leg. y is the power of the electrode at position x.

This graph has a maximum at (n, P) which is what we're looking for.

That then might be a suitable function. y is the power of an electrode at the angle x round your leg. Or in (untested) C

/* Return the power of an electrode at the angle round your leg 'theta', given that the direction North is at the angle round your leg 'north', and the max power to be emitted by an electrode is 'power' */

float electrode_power(float theta, float north, float power)
{
   return power - (theta - north)^2;
}