Hackernoon logoThinking Algorithmic In Assembly by@max-kaplan

Thinking Algorithmic In Assembly

Author profile picture

@max-kaplanMax Kaplan

Data structures and algorithms are essential to computer science. Yes, it is true that most software engineers donโ€™t implement them on an every day basis. However, I do strongly believe that a good foundation of data structures and algorithms can help make you a better, overall engineer.

Today Iโ€™m going to be demoing an implementation of bubble sort in assembly. To most people, the thought of this probably sounds insane. Youโ€™re probably thinking, โ€œWhy on earth would you implement an algorithm in assembly?โ€ Well to start off, I would probably never write a production application in assembly. Compilers do a WAY better job of writing assembly than I do. However, I do think it is important to understand assembly. Most of us run commands to compile our code, and I think itโ€™s really easy to take for granted the work the compiler is doing for us. We all use loops, conditionals, and functions on a daily basis. Have you ever taken the time to understand how these are implemented at the assembly level? Chances are, if you majored in computer science, you might have explored assembly. However, if not, keep reading!

The second question you might be asking is โ€œWhy bubble sort?โ€ For those not familiar with algorithms, bubble sort is generally considered one of the slowest sorting algorithms out there. The only main reason I chose bubble sort is because itโ€™s fairly easy to implement and writing these algorithms out in assembly can be pretty tricky.

The implementation below isnโ€™t perfect. Like I said, compilers do a way better job of writing assembly than I do. The goal here was not to write a perfectly optimized algorithm in assembly. Itโ€™s simply just a demonstration and a fun weekend project if youโ€™re a computer science geek like myself. I chose MIPS as I have the most experience writing assembly for that architecture. MIPS isnโ€™t widely used versus things like x86 and ARM, however itโ€™s still widely taught today.

Okay, enough talking for now. Hereโ€™s the code:

What do you think? Comment below.

Follow me on Twitter: @maxekaplan


Join Hacker Noon

Create your free account to unlock your custom reading experience.