Snake ARM Assembler Game

After reading through half of an excellent ARM assembler tutorial (Think In Geek ARM Assembler), I wanted to put my new knowledge to work and code something just for fun. So here it is, a simple Snake game for Raspberry Pi in pure ARM assembler:

You move with wasd keys as directions and eat different bugs. As you progress, your snake grows in length, until you collide with a wall or yourself. The code for the program is in my GitHub.

Assembler (or assembly) is a low level programming language, meaning that the written code is relatively close to the actual computer’s machine code instructions. A lot of code is needed to perform anything but the simplest operations, and many advanced features, such as objects, or even for-loops don’t exist out of the box. On the other hand, the code should be very fast, as there’s minimal overhead. In practice, most of the code deals with operating with memory addresses, and storing and loading stuff into registers so that they can be operated with. Reminds me much of C-style pointers and their arithmetic.

In addition to the most common assembly stuff, the snake game has some additional interesting features:

  • Producing random number seeds with system call to Linux gettimeofday
  • Pseudo random number generator
  • How to do division and compute modulo (just that you need to do it the hard way in assembly…)
  • SVC system calls to read and write data
  • Getting immediate, nonblocking input from keyboard in Linux using ioctl syscalls and termios configuration structures (this was by far the hardest part in the game)
  • Using ANSI ASCII colors!

I highly recommend the Think In Geek ARM Assembler series. For more advanced people, there’s a decent crash course to ARM assembly also at Azeria Labs.

The ARM Assembly Snake Game at GitHub.

You May Also Like

Leave a Reply

Your email address will not be published.

Note: comments are moderated.