Skip to content

Latest commit

 

History

History
78 lines (54 loc) · 2.38 KB

build.md

File metadata and controls

78 lines (54 loc) · 2.38 KB

Our code is in the first.asm file. It is a convention to use the .asm extension for assembly code files. There are two steps in the building process.

Build and Run

First we "assemble" the program using the nasm assembler. This is simpler than compiling because each line of assembly code translates almost directly to bytes of machine code. To assemble, from the project's root directory (not from the directory where the code is), type:

$ nasm -f elf64 tasks/first_prog/first.asm -o bin/first.o

This will generate the object file first.o in the bin directory (under the project's root). The object file needs to be linked before we can execute it:

$ ld bin/first.o -o bin/first

Now we created an executable file first in the bin directory. We can now execute our program, from the root directory, with:

$ bin/first

Check the result

The program should return 0 which means success. But since this is not printed, we need to use the following command, that checks the return value of the last executed program:

$ echo $?
0

You should see 0 in your terminal.

Try also the exit256.asm that uses another exit code. It turns out that the exit codes are represented with 1 byte (unsigned), in this case ranging from 0-255.

A Shortcut

If you are working from the local directory where first.asm is, we can do all these steps with just:

$ nasm -f elf64 first.asm -o first.o
$ ld first.o -o first
$ ./first
$ echo $?
0

Or even shorter:

$ nasm -felf64 first.asm -o first.o && ld first.o -o first && ./first
$ echo $?
0

But since the project will have lots of executables, we will leave them all at bin so we can easily clean up (and make git ignore them easily).

A Script for Quickly Building and Running the Programs

You can also use the run script:

$ ./run first_prog first
$ echo $?
0

This script takes as parameters the directory name under tasks that contains the program (in this case, first_prog) and the program name without the .asm extension (in this case, first). This way we can quickly run our assembly programs without typing all the compiling and linking instructions.

Reference for Syscalls

In this simple example we use a system call. The codes for all system calls in Linux x64 can be found here.