For the third lab in my SPO600 class, we create two programs in assembly, one for the x86_64 architecture, and one for the AArch64 architecture. Each program would loop from 0 to 30 and print a message with the number each time. If this were C, it would be 4 lines of code:

#include <stdio.h>

for (int i = 0; i <= 30; i++) {
    printf("Loop %d", i);
}

Assembly can't be that hard... can it? 0.0

Let's look at the way registers are manipulated in x86_64 first. When you program with assembly, it's not code like "x = y + 4;". It's more like "move y into a register because we'll need it, then add the register value and 4, that will go into another register... eventually, it's done". There are a lot more steps!

To move a hardcoded value into a specific register, you can do this:

mov  $42,%r10 // moves 42 into r10

To add r10 and r11 together, you can do this:

add %r10,%r11 // add r10 and r11, put result in r11

In order to do ifs, you do stuff like this:

comp %r10,%r11  // compare r10 with r11, the comparison sets a flag used by next conditional jump
je  next_  // jump to label "next_" if the previous comparison was equal

So basically, doing my program ending up involving moving a loop counter into a specific register, and then incrementing it (with an "inc" operation), then comparing the loop counter register with the max we knew we'd exit our "loop" at. If it was equal or greater, we'd jump to a certain label. This is how we emulated for loops.

These register examples were for the x86_64 architecture. The AArch64 architecture had similar steps, except the way some operators worked was different. For example, to do addition, x86_64 will overwrite one of the registers you were adding with the result. With AArch64, the result goes into a specific destination register. Therefore, the AArch64 reminds me more of C programming where I can do things like:

x = y + 42;  // add  y and 42 together, result into x

Whereas with AArch64 assembly, it would be:

add x11,x11,48  // add x11 and 42 together, result into x11

Based on my (admittedly limited) experience with programming assembly for both architectures, I think that the AArch64 style suits me more and that I prefer writing it over x86_64.The experience overall? Horrible. I find it very hard to think in assembly, and concepts like calling functions continue to confuse me no matter how hard I study it. I will have to spend a lot more time with it in order to understand it properly and make meaningful contributions to assembly codebases.

The experience overall? Horrible. I find it very hard to think in assembly, and concepts like calling functions continue to confuse me no matter how hard I study it. I will have to spend a lot more time with it in order to understand it properly and make meaningful contributions to assembly codebases.

The source code for my programs can be found here:

x86_64 - https://pastebin.com/raw/S2BumiiP
AArch64 - https://pastebin.com/yNa0tenL


This was originally posted on the blog I used for my SPO600 class while studying at Seneca College.