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.
Asked
Active
Viewed 9.9k times
31
2 Answers
37
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
- 122,695
- 134
- 305
- 337
-
2Why would the
as31package 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
as31is not needed. Inspired by the question I googled a Hello World in assembler, installed onlynasmand it worked. :-) I think karel just mentioned it as an alternative. – PerlDuck Aug 12 '18 at 12:46 -
1
-
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
-
1This 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(orasin 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
-
If I wanted to create a DOS executable on Linux, I might start your example with
nasm hello.asm. Is a linker still then needed? If so, what would the invocation be? – user643722 Dec 06 '24 at 16:05 -
The executable format of the asm code at Creating a tiny 'Hello World' executable in assembly is still supported by modern Windows OSes, that run it inside a DOS emulator. In Ubuntu it runs successfully in dosbox with the command
dosbox hello.com. Please note that I had to use the commandnasm -f bin hello.asm -o hello.comto create the file named hello.com in my Ubuntu 24.04. The 'Hello World' code example from the website is without comments, but you can ask ChatGPT to add comments to it. – karel Dec 07 '24 at 01:47 -
That was the easy way. Here is the hard way. 1. Assemble the code into an object file with
nasm -f obj hello.asm -o hello.obj2. Link the object file using jwlink withjwlink format dos file hello.objThis command will create an executable DOS program named hello.exe. 3. Run the DOS program with dosbox or dosemu (example:dosbox hello.exe). – karel Dec 07 '24 at 09:02
13
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
-
Thanks, this worked but I'm confused why I can't get it working with gcc... You wouldn't know how to do this with the gcc also would you? – Enjoy87 Feb 08 '25 at 11:24
.sand the command to compile should begcc myprog.s– FedKad May 01 '19 at 11:16