29

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.

Glorfindel
  • 965
  • 3
  • 13
  • 20
Scorch
  • 431
  • 1
  • 7
  • 13
  • 8
    Nice to read such a question in times of gigabyte sized frameworks for all kind of problems :-) – PerlDuck Aug 12 '18 at 11:31
  • 4
    Take note that there are *two* major assembly "flavours" which have a different syntax: AT&T(gas) and Intel(nasm). Before choosing an assembler, you should decide which syntax you want to learn and use. See a detailed comparison chart [here](https://www.imada.sdu.dk/~kslarsen/Courses/dm18-2007-spring/Litteratur/IntelnATT.htm). If still unsure, go with Intel/nasm. – undercat Aug 12 '18 at 13:10
  • 2
    You can use _gcc_ which should be installed on a standard Ubuntu machine by default. The file name extension is `.s` and the command to compile should be `gcc myprog.s` – FedKad May 01 '19 at 11:16

2 Answers2

35

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
karel
  • 110,292
  • 102
  • 269
  • 299
  • 2
    Why would the `as31` package be needed? From the question and the description of the package it doesn't sound like it would server a purpose. – kasperd Aug 12 '18 at 12:42
  • 5
    @kasperd Actually `as31` is not needed. Inspired by the question I googled a _Hello World_ in assembler, installed only `nasm` and it worked. :-) I think karel just mentioned it as an alternative. – PerlDuck Aug 12 '18 at 12:46
  • 1
    as31 is an alternative if you also want to learn assembly with as31. – karel Aug 12 '18 at 12:50
  • 2
    @kasperd: the OP did neither specify which ISA he is writing for, nor what assembly syntax he is using, so it makes sense to include as many options as possible. – Jörg W Mittag Aug 12 '18 at 14:02
  • @JörgWMittag The question does indeed not specify an ISA, it does however specify Ubuntu 18.04 which limits the options. Is Intel 8031/8051 among the CPUs which Ubuntu 18.04 can run on? – kasperd Aug 12 '18 at 16:07
  • 1
    This answer would benefit from splitting the installation command into nasm, and another for 8051 assembler + emulator (there seems to be at least one). Since the purpose is to learn assembly, actually running emulator for simple(r) architecture than x86 might make a lot of sense. – hyde Aug 12 '18 at 19:37
  • you have listed one proprietary assembler and one nonstandard, why not include a mention of `gas` (or `as` in general)? – cat Aug 12 '18 at 19:46
  • @cat NASM is under the BSD-Two Clause / FreeBSD, also most people perfer using the Intel syntax which is default for Nasm – Shipof123 Sep 10 '20 at 21:34
12

Ubuntu comes with as (the portable GNU assembler)

as file.s -o file.out
ld file.out -e main -o file
./file

-o: Tells where to send the output
-e: Tells ld the start symbol