Hello World, using only C syscalls

I’m currently working on implementing system calls in BlarbVM (see my previous post on it), so I am trying to get comfortable using only syscalls.

In Linux, you can change the heap size through brk (syscall #12). To write a string to standard output, you must provide a pointer to a location on the heap.

The following code:

  1. Get’s the end of the heap address
  2. Increments the end of the heap address by 6 bytes
  3. Set’s the value of those 6 bytes to a null terminated “hello\n” string
  4. Runs a syscall on the desired heap position to print “hello\n”

WearHacks Toronto 2016 Project: InfraViewer

At WearHacks Toronto 2016, we were trying to think of project ideas, but nothing came to mind that we all agreed on. So Adrian, Austin and I went out for coffee to take a mental break. While at the coffee shop, we were talking about all the types of light humans are blind to. Wouldn’t it be amazing if we could the whole spectrum of light, including infra red and ultra violet? … maybe we can!

When we got back to the Bitmaker offices (where the hackathon was held), we checked out the available hardware to see if there were any IR cameras. Unfortunately, the only IR camera we could find was on the Kinect, and it’s IR camera only has intensity (not spectrum data). So we made due with what we had and decided to use both the IR and RGB cameras of the Kinect to render both layers on top of each other.

Josh, Austin and I worked on the translation algorithm while Kulraj and Nick created an SFML GUI. The colorspace transformation algorithm we ended up designing and implemented was to shift the visible light by 10 degrees (hue), then shift the IR spectrum into the first 10 degrees of red hue. Essentially, we squeezed the visible light from the non-IR camera so there was no red left, then put a red layer on top of that image to display the infra red data.

The hardware turned out to be pretty difficult to overlay. The IR camera and visible light cameras had significantly different resolutions and aspect ratios. So we had to program some magic to translate the IR data to fit on top of the RGB data. Because of this, the IR layer was slightly offset from where it should have been. For instance, a slight red glow would appear around our bodies (from our body heat), but it would be shifted one side, since the camera perspectives were not identical.


You can check out the project here: InfraViewer. But be warned, the code base is pretty messy!


Esoteric programming languages are awesome. So I decided to create one, which I call BlardLang.

The goal of this language is to be the simplest possible systems programming language. It uses a stack for parameters, and every function must destruct its own stack frame, and push a return value, manually. The language “doesn’t care” about performance, only simplicity. It currently supports only 5 operations:

  • a b ~ Store the value of a in register b
  • a $ Push register a onto the stack
  • b a ! NANDS index a with index b and stores the result in index b
  • a ^ pop ‘a’ elements
  • a ? if index a is true (non-zero), execute the rest of the line

In one of my CS lectures (CSC258) I remembered learning that every gate can be created using NAND gates, so that is the only type of logical operation in the language! Using the above 5 operations, tons of different operations can be created, including adders, shifters, comparison operators, and more. https://github.com/elimirks/BlarbVM/blob/master/lib.blarb

There are 8 registers available for users. The 0th register is the line pointer, registers 1-3 are temporary registers (for swapping and stuff), and the 4-7 registers are “long term” registers, meant to be used outside of the standard library (by the user).

Labels allow creation of functions. For instance, the following code creates a function called “nandi” which NANDs two immediate stack values:

In the future, I want to add one more operation: the system call operation. Currently, the language can only modify data within the VM. That is, on the stack and in registers. However, using system calls, users will be able to allocate heap memory and perform useful tasks such as reading from STDIN and writing to STDOUT.

GDB Basics: Stack Traces

C beginners are often very lost when their program crashes from a segmentation fault or similar issues. Most of them know about GDB but are pretty intimidated by it.

Here is a super simple example about how to find a stack trace from a program crash.

Of course, this will result in the console simply printing “Segmentation Fault” which doesn’t help very much, especially in more complex programs.

Using GDB, you just have to remember 2 commands to get a stack trace: “run” and “backtrace”.

To compile, use the “-g” flag with gcc to include debugging symbols for gdb:

To run this in gdb, run:

Now all you have to do is type “run” into the gdb console. It will show you the line where it crashed, but to show the stack trace you must run the “backtrace” command:

GDB has a lot of other great features like breakpoints and memory analysis. If you are comfortable with assembly, you can even run commands while your program is running!

How to change login shell without chsh permissions

On the University of Toronto servers and Linux lab computers, the default shell is tcsh, and we don’t have chsh access *shakes fist*.

If you are in a similar circumstance, plop this in ~/.cshrc:

If you just run exec /bin/bash (without checking for the interactive shell), it will break non-interactive shells like scp and the GNOME startup scripts. If you just use bash you can ignore the first conditional. The reason why I have bash as a backup is because the lab computers have zsh installed, but the servers only have bash.

Bai :)

P.S. A little known fact about the UofT computing services is that you can SSH into lab computers directly. For instance, dh2026pc02.utm.utoronto.ca connects to “PC #02″ in DH2026.

UofT Hacks 2016

So last weekend, I spent 36 hours at UofT for their annual hackathon. If you have not been to a hackathon, I highly recommend it! There are tons of like minded individuals making awesome projects.

Check out the swag I collected:


I even got a new domain name for free: http://squabbit.tech/. Yes, SquabbitTech is our “company” name :3

The application my team built was a super-fuzzy note searching tool called Fuzzy Wuzzy. The idea is, several years after writing down notes, it is very difficult to remember exactly what you wrote. So we built an application that guesses what you want to search for. We designed three algorithms, but only had time to implement two of them: synonym, misspelled word, and idea group matchers. For instance, searching for “small” will return notes containing the word “tiny”. We didn’t have time to create the idea group algorithm, but we had it planned out. If you search for “car”, everything that is a car will be matched (e.g. “Honda Civic”).

We built the project as an API using Flask – a micro web framework for Python. It is extremely simple in comparison to other frameworks like Symfony. It provides templating, routing, and database engines that are all super easy to get started with.

Dabbling Around with Scala

Scala is a legendary language that allows both OO and functional programming. On top of that, it is compatible with Java and compiles to JVM bytecode. Hence, in theory, it could be used anywhere Java is used *cough cough* Android *cough cough*.

For a while, I have been meaning to try out Scala – and today I bit the bullet.

Closures are awesome. Here is a simple example which multiplies a list by 2 and prints the result:

Of course, this is a very trivial example of how closures can be used, but I find the syntax pretty cool nonetheless 😀

Here are some other features that I really like about Scala:

Just for kicks, I decided to build a simple regex matching program with character matching, “multiple”, and “or” functionality.

Switching from Arch to Debian

Last night was a sad night. After 5 years of using Arch Linux, I decided to leave it for Debian. While I learnt a lot while using Arch, it was often disruptive to my work when it required maintenance. I woke up last night to work on a Physics assignment, and sure enough, my kernel panicked. I don’t blame Arch per se. If I retained a legacy kernel, it would have been fine. But that is the reason why I left – it would be nice if my computer worked out of the box with minimal maintenance.

One thing I will really miss about Arch is yaourt – a package manager which connects with the Arch User Repository. Basically any package you could think of would be contained in there. As an alternative, I installed Linuxbrew. It doesn’t seem to have as many packages, but I am really digging how the formulae are easily modifiable in ruby! (expect to see a linuxbrew-formulae repository in my Github soon…).

Segmented sleep for 5 weeks

For the next 5 weeks, I will be sleeping from 23:00-2:00 and 4:00-7:00, totaling in 6 hours of sleep per day. This is known as the segmented sleep schedule. If it goes well, I will likely continue with this schedule. For some of you 6 hours probably sounds like a lot, but I usually get 8 hours, so cutting out 2 is a significant improvement.

My motive behind doing this is that I was recently offered a research opportunity that I could not refuse (I will likely post about it later). To make time for it, I had to sacrifice something – my choice was sleep. About a year ago I was on a similar routine and it worked well, but I haven’t had the motivation to start doing it again until now.


Week 1 Summary

During the 2:00-4:00 waking phase, I felt groggy for probably the first hour each night. Waking up was difficult as well, since my body was not adjusted to the schedule.

I had 3 successful trails and 4 unsuccessful trails. Since I was still trying to figure out the alarm clock and bar code scanning stuff, I guess this is not too bad.

Week 2 Summary

This week I started to feel less groggy in the 2:00-4:00 waking phase. I only needed about 10 minutes to wake up, as opposed to feeling groggy for the entire waking phase, as in week 1.

I had 5 successful trails and 2 unsuccessful trails. Despite the lack of sleep, I was able to function completely normal.

My goal for week 3 is to have a maximum of one failed trail.

Week 3 Summary

I think this week was when my body was really adapted to the schedule. I had no problems when waking up in the night or the morning.

I had 6 successful trails and 1 unsuccessful trail, which was intentional. Plus, I achieved my goal!

Week 4 Summary

This week, a bunch of things got in the way that messed up my schedule. Despite this, I was able to bounce back into the routine easily.

Night 1 (October 24 – October 25)

Changes from previous day:

  • Installed the “I Can’t Wake Up” app
  • Configured it so I have to scan a QR to wake up
  • Set up a sunlight lamp

In my waking period from 2:00-4:00, I felt a little groggy, but I ended up getting a lot of reading done.

After my second sleep cycle, I felt über tired and took several 5 minute naps until 7:30.

Night 2

Again, my waking period from 2:00-4:00 was productive. However, in the morning when I woke up at 7:00: I scanned the QR code, the alarm turned off, I crashed in my bed, and I slept in until 9:00. So yeah, I failed my second night :(

Despite the second night failing, I am going to be an optimist! My waking period in the middle of the night went well, so all I need to work on is the second wake up period.

Night 3

Changes from previous day:

  • Installed “Keep Me Awake”
  • Configured “I Can’t Wake Up” so I have to shake the device viciously after scanning the QR
  • Configured “I Can’t Wake Up” to auto start “Keep Me Awake”

It is currently 3:52. The alarm must have been too soothing, because I didn’t wake up for my waking period until 2:45. Once I got up, having to scan the QR code PLUS shake my device for about 30 seconds helped quite a bit. However, I need to get in the habit of not setting a 5 minute alarm to simulate snoozing.

So I forgot to set my 7:00 alarm… I guess I didn’t need to go to that stats class.

Night 4

So I should have read through the settings of “I Can’t Wake Up” more thoroughly. Apparently there is an option to make it repeat every day so I don’t have to remember to set them.

Changes from previous day:

  • Made “I Can’t Wake Up” repeat both alarms every day
  • Changed my alarm to a really annoying buzzing sound

The waking phase was really good last night. I had no problems getting up. However, the app appears to have issues doing what it was meant to do – be an alarm clock. It didn’t turn on at 7:00.

Night 5

In the past I have used Sleep as Android for my alarm clock. I stopped because I was dumb and didn’t configure the alarm properly, so the QR scanning wasn’t working. Needless to say, I figured out the issue (had to due with my change of address) and I plan to use that from now on.

Changes from previous day:

  • Switched to “Sleep as Android” for my alarm
  • Moved the QR code from my lamp to the opposite side of the room (to force me to turn on the main light)

It is now my waking period. Due to an assignment that was due at midnight, I had to push back my first sleeping phase to 11:30-2:30, at which point I immediately had a 5 minute nap after, so I woke up around 2:35. I always feel that naps will help me wake up (hint: they don’t). Since I shifted my first cycle, I decided to shift my second to 4:30-7:30.

Success! I woke up around 7:15, then felt inclined to do what I always do and snooze a couple times until 7:30. I won’t be missing stats today! Again, snoozing was probably just detrimental, but at the time I convinced myself that it would help (AsapSCIENCE has an interesting video on why snoozing is terrible).

Night 6

No complaints besides a bit of snoozing. That really needs to stop :/

Night 7

I ended up staying up too late and it screwed up by routine… Interestingly enough, I naturally woke up at 7:00 without an alarm. However, shortly thereafter I went to bed again. Despite this night being a complete failure, it will provide resiliency results for Night 8 when I go back to my routine.

Night 8 (October 31 – November 1)

Thanks to George Hudson, I only needed to sleep from 0:00-2:00 in my first cycle to get 3 hours of sleep – Happy daylight savings, everyone!

Despite having deviated from my routine on Night 7, I had no issues getting up after 3 hours for my waking phase. Yay!

Ugh. So of course, I woke up at 6:44 feeling chipper, then made the terrible decision that I should nap a bit to reduce my “sleep debt” by 10 minutes, and slept in until 8:00. I had a problem doing this even before I started my routine, so I don’t think it is anything related to the reduced sleep. To counter my groggy-selfed-argument, I am going to set a range from 6:45-7:15 for my alarm instead of 6:40-7:10 so that I know on average I will be getting 3 hours per sleep cycle. I also set up a second bar code that I will have to scan.

Night 9

Changes from previous day:

  • Adjusted sleep range (n:45 to (n+1):15)
  • Added an extra bar code to scan to disable my alarm

Success! For both alarms, looking at my university card (where the second bar code is located) probably reminded me why I started doing this whole experiment in the first place. And I didn’t snooze! 😀

Night 10

According to the sleep depth chart, Sleep as Android decided to wake me up in the middle of deep sleep :@ I am still going to stay awake until 4 for experimental purposes.

Strangely enough, when I woke up for my second alarm, I felt much better than normal.

Night 11

Something got messed up with my alarm and the maximum amplitude of the ring tone was reduced. Hence, I slept until 2:30. It goes without saying that I fixed this. Other than that, all is well.

Other than that anomaly, last night went well and I got a lot of work done in my waking phase.

Night 12

Just for fun I am going to try changing my alarm sound to shuffle from a set of random bird noises and random synthetic alarms.

Changes from previous day:

  • Changed the alarm to be a shuffled playlist of annoying tones

Turns out waking up to a seagull is a very effective way to wake up.

Tonight is the first time that I have been on my new sleep routine before writing a midterm in the morning. It will be interesting to see the manifested cognitive detriments – if they occur. Wish me luck!

I don’t think my cognition was affected by the new sleep schedule. I was able to focus just as well as normal during my test!

Night 13

I ended up going to bed at 00:15 last night due to an assignment that I was working on at the university. Hence I adjusted my wake up time to be 3:15 +- 15 minutes. Interestingly, I felt absolutely terrible when I woke up. I suppose this is to be expected since consistency is important with sleep schedules.

I decided to go back to bed at 4:00 so that my second sleep phase will note deviate from the normal time.

Night 14

So I stayed up later than I should have, and again screwed up my routine. Gur.

Night 15 (November 7 – November 8)

I ended up sleeping from 23:38-2:48 tonight because I couldn’t fall asleep at 23:00 (probably due to the previous night :|). Despite that, I had no detriments in my waking phase.

To get back on track with my routine, I slept again from 4:00-7:00. I feel fine this morning – yay!

Night 16

Argh. I woke up at 2:00 to discover that my OS was kernel panicking. I have an important assignment due tomorrow, so it looks like I won’t be getting my second sleep phase in.

I ended up sleeping initially from 23:41-2:49 with success and then took two 20 minute naps. Fun fact: this is basically the Everyman sleep schedule (the schedule I plan to do after this 5 week trial).

Night 17

Everything was normal this night, except I slept from 5:00 to 8:00 in my second sleep phase. Surprisingly, I didn’t feel the side effects of Night 16 where I only got 4 hours of sleep.

Night 18

Another mostly normal night – I slept from 1:00 to 4:00 and 5:00 to 8:00 instead, though. According to my sleep tracker, I had an average of 59% deep sleep, so I am thinking that maybe sleep times don’t matter as much as I thought. Just an idea.

Night 19

Nothing interesting on this night.

Night 20

So according to my sleep tracker, I have been waking up around 10-15 minutes after my alarms go off. This results in quite a bit of extra sleep. To compensate, I am going to change my alarms to go off at 1:50±15 and 6:50±15.

Something I forgot to mention: for those who don’t know, Sleep as Android is a sleep tracker which will set off an alarm within a certain range. It determines when to do so based on predicted sleep cycles – e.g. if you are rolling around, it will detect movement, which implies that you are in light sleep. Hence at that point it would wake you up.

Changes from previous day:

  • Changed my alarms to go off at 1:50±15 and 6:50±15.


Night 21

Tonight I ended up visiting my brother – so I had an excuse to stay up late and mess up my routine :3

Night 22 (November 14 – November 15)

Another ordinary success.

Night 23

I’m not exactly sure how… but I ended up waking up at 2:00, then setting another alarm for 4:00 followed by going back to bed – resulting in 5 hours of (basically) contiguous sleep. So here I am, awake at 4:00, needing only one more hour of sleep.

Sleeping for that extra 2 hours really threw off my sleep pattern, and I ended up sleeping in after trying to get only 1 more hour of sleep. Gur.

Night 24

Night 25

One thing I have been noticing is that it is easier to wake up at 2:00 than it is to wake up at 7:00. I think I am going to automate my lamp to turn on at 6:30 at some point to see if that helps.

Night 26

Night 27

So all went well until waking up at 7:00. I felt really groggy, so shortly thereafter I slept again until 8:00.

According to my sleep tracker, I was in deep sleep when it woke me up, but an hour before (at 6:00) I was in light sleep. I noticed a similar pattern among other nights, so I am wondering if moving my sleeping phase back would help.

Night 29

Night 30 (November 21 – November 22)

Changes from previous day:

  • Changed second sleeping phase to be from 3:30-6:30

It seemed like it was easier to wake up at 6:30 today. I will try this for a few more days to see if it helps.

Night 31

Night 32

Sleep as Android sucks. According to what I have read online, it occasionally decides to turn itself off… yep, you guessed it – I missed stats class again.

Night 33

So I heard good things about the Timely Alarm Clock, which includes wake-up challenges and alarm-sooth-in features. I decided to give this a whirl for the rest of my experiment.

Changes from previous day:

  • Switched to the Timely Alarm Clock
  • Changed the alarms back to 2:00 and 7:00

Stay tuned…