Ubuntu – How to compile & run assembly in Ubuntu 18.04

18.04assemblercompiling

So recently I've wanted to learn assembly, so I learnt a bit. I put this into nano and saved it as playground.asm. Now I'm wondering, how do I compile and run it? I've already searched everywhere and still cant find it. I'm really curious and there's no point learning a language if you can't even use it.

Best Answer

  • In all currently supported versions of Ubuntu open the terminal and type:

    sudo apt install as31 nasm  
    

    as31: Intel 8031/8051 assembler
    This is a fast, simple, easy to use Intel 8031/8051 assembler.

    nasm: General-purpose x86 assembler
    Netwide Assembler. NASM will currently output flat-form binary files, a.out, COFF and ELF Unix object files, and Microsoft 16-bit DOS and Win32 object files.

    This is the code for an assembly language program that prints Hello world.

    section     .text
    global      _start 
    _start: 
        mov     edx,len   
        mov     ecx,msg   
        mov     ebx,1   
        mov     eax,4   
        int     0x80   
        mov     eax,1  
        int     0x80   
    section     .data
    msg     db  'Hello world',0xa  
    len     equ $ - msg   
    

    If you are using NASM in Ubuntu 18.04, the commands to compile and run an .asm file named hello.asm are:

    nasm -f elf64 hello.asm # assemble the program  
    ld -s -o hello hello.o # link the object file nasm produced into an executable file  
    ./hello # hello is an executable file
    
  • Related Question