Dabbling around with x86

I am going to work as an embedded software developer in May, so I thought I should feel cozy with x86. The only experience I have with x86 is in a dissassembler context… so be warned! This may be messy.

I wrote this little fellow which simply prints a given number in octal using recursion :]

To all the potential employers out there, here is a bonus… fizz buzz :3

PinkyCtrls

A couple months ago, I made the switch from Vim to Emacs. However, I’m using Evil mode, so I still have all the nice Vim keybindings.

One of my biggest complaints about Emacs is ergonomics – “Emacs Pinky” is a legitimate concern:

One solution is getting foot pedals or a fancy ergonomic keyboard like the Ergodox or Keyboardio, but I didn’t want to commit to the investment just yet. Thus, PinkyCtrls was born! I forked an existing project that turned the space key into a control key if you held it, but worked as space if you tapped it (Space2Ctrl). However, with Space2Ctrl I found that I generally mistyped spaces a lot, since I tend to mash the space key soon after typing words. So I decided to shove the functionality onto the Caps Lock & Minus key (where the quote key is on QWERTY layouts).

After about 2 weeks of using this mod, I find it uncomfortable to use the normal control keys, and I have no problems with all the finger stress when using Emacs.

Note: The code in my PinkyCtrls repo is currently pretty ugly… I may clean it up eventually, but for now it does the job. If you want to fork PinkyCtrls to work on another layout or with other keys, let me know and I’ll show you what you need to change!

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.

IMG_20160326_230643

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

BlarbVM/BlarbLang

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

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:

IMG_20160124_151456

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.