Let’s learn Assembly

November 13, 2019 0 By Rosa

It’s been quite a while since I’ve been able to spend some time into my own operating system. However, I’ve found some of it again to do what I love most! There is still a lot to research and to read, but I wanted to do something more practical. Hence, I’ve decided to learn Assembly!

Assembly is one of the lowest level programming languages in existence. The only “language” lower than Assembly would be Machine Code. The two languages are closely connected to one another; Assembly is the (somewhat) human-readable form of Machine Code. I say somewhat because it’s still tough to read. 

But what is Machine Code? To explain this, I want to talk about CPUs for a bit. CPU stands for Central Processing Unit, and you can find these in all electronic products. This unit makes all kinds of calculations so you can read this blog, write a document, make something happen with the press of a button. You name it; the CPU makes it happen. Well… Sort of. For it to function correctly, it needs instructions, so it knows what to do. However, the CPU doesn’t communicate in words but with specific sequences of 0s and 1s. This is where the Machine Code comes into play. With Machine Code, you can create a series of 0s and 1s to give the CPU instructions. An instruction comprises an opcode (operation code) and operands (memory address or data). 

However, writing Machine Code is difficult. You can easily make mistakes because you’re only working with 0s and 1s. Assembly makes this job a little easier. It created a small translation layer that allows you to use “words” for the opcodes, instead of the 0s and 1s. It also accepts hexadecimal input, since a computer can translate that to binary. Eventually, you’ll end up with something like the image below. Instead of having to remember that 6C is the opcode to jump to a different memory address, you can use JMP.

Machine Code vs Assembly

Still, Assembly is hard to read, and you’re working on the lowest level. Lots of mistakes will be made, and there is a lot to remember too. The instruction set for the Intel x86-64 architecture contains over 1500 instructions! That’s why my good friend Valentin interfered and suggested that I start easier. Instead of diving in the x86-64 CPU architecture, I should start small. Very, very small. With RISC, learning the concepts of Assembly  would already help a lot. RISC stands for Reduced Instruction Set Computer, and, as the name suggests, it has fewer instructions than the x86-64 but still well over 100 instructions. Yet, he wants me to go even smaller than that.

So, this new project has been born: I will build a computer from scratch! Yes, from scratch, meaning that I’ll put chips in a breadboard, create a clock module and everything around my new CPU: the 6502. This little CPU has 56 instructions which is manageable for me. After building this small computer, I can learn the concepts of Assembly with the 56 instructions. After I have mastered the concepts, I can work my way up to a Raspberry Pi (RISC) and ultimately to my laptop (x86-64). As still have to learn how to build the computer in the first place, I’ll be working my way through a couple tutorials from Ben Eater.

Learning Assembly will be a challenge, but a fun one for sure. I’m also thinking about making videos about this project, so you will see what problems I face and how I solve them. It seems like a good learning experience for me too!