How do I compile an asm file and run it on Linux?
I created a file asm
file and compiled it using the command
nasm file -o outfile
How do I run the program in the terminal now? (Run outfile)?
2 answers
By default, nasm
(usually) compiles to bin
- format, which is equivalent to normal flat code. Generally speaking, such files are not executable for almost any OS. But if the file is formed in a special way, a block of 256 bytes is skipped at the beginning of the asm
file.:
org 0x100
Then this file will be equivalent to the COM
-DOS format. It can be run in the DOS emulator, for example dosbox
.
To compile an executable file for linux, you need to explicitly specify the format:
nasm file -f elf64 -o file.o # создаст объектный файл (указать elf32 для x86)
ld -s file.o -o outfile # слинкует объектный файл в исполняемый
But to do this, the code must be written directly for linux (use its system calls, etc.).
You can run it as usual:
./outfile
For a list of available formats, see nasm -hf
Like all programs in Linux:
./outfile
Or
<path_to_file>/outfile