Assembling Madness

Written by Hunter Jansen on October 03, 2014

My SPO class has started to get to the meat and potatoes of what the course is actually about,
but in order to get on with that, we’ve got to learn a little bit about assembly. A whole new world for me.

I’m not going to lie; it’s been about 2 years since I’ve used any type of C (except objective C), and Assembler is so far out of my wheelhouse that it’s almost silly. That said, I’m not one to back down from a challenge, and I’d certainly like to graduate, so on with it.

What’re we doing?

The link for this week’s lab can be found here.

Essentially we’ll be running a super simple program and looking at the differences between the assembler code on x86 vs aarch64.

So we have the contents of our hello program:

#include <stdio.h>

int main() {
	printf("Hello World!\n");
}

Super simple!

So now let’s look at what that outputs in each architecture:

x86:

section .text
global  _start

_start:
        mov     rdx,len                 ; message length
        mov     rcx,msg                 ; message location
        mov     rbx,1                   ; file descriptor stdout
        mov     rax,4                   ; syscall sys_write
        int     0x80

        mov     rax,1                   ; syscall sys_exit
        int     0x80

section .rodata

msg     db      'Hello, world!',0xa
len     equ     $ - msg

VS aarch:

.text
.globl _start
_start:

        mov     x2, len         /* message length (bytes) */
        adr     x1, msg         /* message location (memory address) */
        mov     x0, 1           /* file descriptor: 1 is stdout */

        mov     x8, 64          /* write is syscall #64 */
        svc     0               /* invoke syscall */

        mov     x0, 0           /* status -> 0 */
        mov     x8, 93          /* exit is syscall #93 */
        svc     0               /* invoke syscall */

.data
msg:    .ascii      "Hello, world!\n"
len=    . - msg
   

So what’s different here? They look PRETTY much the same, right? Well, of course they do, it’s a one line program! So let’s look at what’s the same.

They both have a start function, and a place to store data (.rodata vs .data), and they both end up doing the same thing, but with a bit of a different syntax.

ie: the very first block of instructions.

  • Different syntax for comments
  • Different way of defining variables
  • Different names for registers
  • aarch has a call to svc to invoke syscall
  • most calls (though, maybe not in the above example, it seems) have reversed order of instruction variables

So we’ve got a whole bunch of code to do a very simple hello world program - not the easiest thing in the world to do, but in fact quite interesting and a major change of pace from my JS life.

The next post will cover my progress on the latter half of the lab, writing some assembler of our own to loop and print output

Until Next time -Hunter