Tutorials/Assembler Tutorial

From ThorstensHome
Revision as of 09:16, 3 December 2014 by ThorstenStaerk (Talk | contribs)

Jump to: navigation, search

Machine language is everywhere. Whether you are playing Call of Duty, surf in the internet or write a document - it is machine language that is being executed inside your computer. No matter if you wrote your software in C, BASIC or Ruby, at execution time it has been translated to machine language. Machine language is the godfather of programming languages. Its commands are binary, for example "wait" is 90h for x86. This is why assembler exists - it maps the machine language commands to mnemonics like "jmp" for "jump" or "nop" for "wait". You see this is very low-level and I like low-level topics. So here I show you how I deal with machine language and assembler. I am using x86 Linux in the examples.


Endless loop

A "hello world" program in assembler is already advanced. So as a first lesson we will take a look at a program that does nothing but an endless loop. Here is it:


global _start
jmp _start

This assembler source code contains two commands, "nop" for "no operation" and "jmp" for "jump". The other two lines is a label (_start:) and meta-information (global _start saying that "start" is where the program starts).

compile it

nasm -f elf64 endless.asm

link it

ld -s -o endless endless.o

call it


Hello world

We now create a hello world program in C. Then we compile and disassemble it. So we have the C compiler translate it into machine language and then we use a disassembler to translate it into assembler. This is the program:


#include <stdio.h> 

int main()
  int i=0x23;
  printf("hello world");

Now we compile it:

gcc hello.c -o hello

and see that it runs:

hello world

To disassemble it, say

objdump -M intel -d hello

And the result for the main section is:

000000000040053c <main>:
 40053c:       55                      push   rbp
 40053d:       48 89 e5                mov    rbp,rsp
 400540:       48 83 ec 20             sub    rsp,0x20
 400544:       c7 45 fc 23 00 00 00    mov    DWORD PTR [rbp-0x4],0x23
 40054b:       bf 4c 06 40 00          mov    edi,0x40064c            
 400550:       b8 00 00 00 00          mov    eax,0x0                 
 400555:       e8 d6 fe ff ff          call   400430 <printf@plt>     
 40055a:       c9                      leave                          
 40055b:       c3                      ret

To understand this you should know that every processor has a set of registers. eax, edi, rbp and rsp are such registers. The "push rbp" command is only one byte, 55 hexadecimal and means that the processor will take its register rbp and store it in memory so it can always be restored using the pop command. The "mov" command stands for "move" and says that one register's value is moved into another register, or a value is moved into a register, or a value is moved into ram. Note that this command ("mov") translates - depending on its exact meaning to quite some different bytes in machine language, in the above example b8, bf, c7 and 48 89. b8 requires 4 bytes as parameters, 48 89 only one. sub stands for "subtract", ret stands for "return". It will end the program and return to the calling program which is the operating system. "call" will do exactly this - call a library function that is in memory, in this case it will call printf. The actual "hello world" string is stored not in the <main> section but in the data section. Note that the "text" section is the "code" section; it is the section that will be executed:

tweedleburg:~ # strings hello
hello world

translate C to assembler

To learn the syntax of a gcc assembler program, let's write a C program and compile it without assembling it. Here is the C program, hello.c:

#include <stdio.h>

int main()
  int i=0x23;
  printf("hello world");

Now we compile this without assembling it:

# gcc -o hello.asm -S hello.c

Now we have the program transformed to assembler and take a look at it:

# cat hello.asm              
        .file   "hello.c"                        
        .section        .rodata                  
        .string "hello world"                    
.globl main                                      
        .type   main, @function                  
        pushq   %rbp                             
        movq    %rsp, %rbp                       
        subq    $32, %rsp                        
        movl    $35, -4(%rbp)                    
        movl    $.LC0, %edi                      
        movl    $0, %eax                         
        call    printf  

Now we know the syntax of gcc assembler and we can finally write a program that consists of an endless loop:

.globl main
  jmp start

Create a boot sector

Under program your own OS I show how to create a boot sector for your own operating system. The challenge here is that the executable code must not be longer than 512 bytes. Here is how we do it:

  • create a file hello.s


; this should print H
    mov ax, 0xe48
    mov bx, 7
    int 0x10
; E
    mov ax, 0xe45
    int 0x10
; L
    mov ax, 0xe4C
    int 0x10
; L
    mov ax, 0xe4C
    int 0x10
; O
    mov ax, 0xe4F
    int 0x10
    jmp .ende

You may note that we say here "mov ax,..." while in the previous example we have seen "mov eax,...". The reason is that there are so many assembler dialects.

  • translate this assembler code into machine language:
nasm kernel.s
  • the result is the file kernel. Let's look at it:
tweedleburg:~ # ll kernel
-rw-r--r-- 1 root root 30 Nov 27 21:29 kernel
tweedleburg:~ # hexdump -C kernel
00000000  b8 48 0e bb 07 00 cd 10  b8 45 0e cd 10 b8 4c 0e  |.H.......E....L.|
00000010  cd 10 b8 4c 0e cd 10 b8  4f 0e cd 10 eb fe        |...L....O.....|
tweedleburg:~ # 

You see the mov ax (or mov eax) assembler command is again translated to b8 as a byte in machine language. You see all assembler commands are translated and there is nothing but machine language in that file. If you want to use this, see programming your own OS.

x86 assembler

In all of this article we are talking about Intel x86 assembler. Only here I want to give you an example for ARM assembler that I got from http://linuxintro.org/wiki/objDump:

Nokia-N810-43-7:~# objdump -d a.out | head

a.out:     file format elf32-littlearm

Disassembly of section .init:

000084f8 <_init>:
    84f8:	e52de004 	str	lr, [sp, #-4]!
    84fc:	e24dd004 	sub	sp, sp, #4	; 0x4
    8500:	eb000035 	bl	85dc <call_gmon_start>
    8504:	e28dd004 	add	sp, sp, #4	; 0x4

The main difference is, as you can see, that this machine language is has a fixed-width command set - every command consists of 4 bytes. That makes it easier to jump to the command that is one, two or whatever commands apart. While the machine language is completely different, there are many assembler mnemonics that also exist in x86 assembler.

See also