Examples of using NASM language on Linux based system, some of the examples are calling c standard library functions using NASM.
- Linux machine, Ubuntu is recommended (Use only 64-bit version).
- Run
gcc -v
command, if version is under 7 do "gcc Upgrade process" - Install NASM by `sudo apt-get install nasm
- Check NASM installed correctly by useing : `nasm -v
- Add gcc repository :
sudo add-apt-repository ppa:jonathonf/gcc
- Update apt-get :
sudo apt-get update
- Install gcc7 :
sudo apt-get install gcc-7 g++-7
- Set gcc as default :
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7
- Test gcc version :
gcc -v
and see the version is 7 or above
Simple code example that using c standard library printf
function to print Hello World.
Compile command
Compiling the code with NASM.
nasm -f elf64 -o hello_world.o hello_world.asm
Linker proccess
gcc is our linker.
gcc -no-pie -m64 -o hello_world hello_world.o
Run the program
./hello_world
Using the command, we will compile, link and run the code:
nasm -f elf64 -o hello_world.o hello_world.asm && gcc -no-pie -m64 -o hello_world hello_world.o && ./hello_world
You can also just use the makefile in the folder and then use the make
command.
When three numbers are given: X, Y, Z. The program calculates the distance function - sqrt(x^2+y^2+z^2). In this example, you can see the use of XMM0
- XMM15
registers. Those can be used for floating point numbers and for more complex calculation such as mulsd
(Multiply Scalar Double-Precision Floating-Point).
In this example, the program is reading an external file and searching for integers & floating point numbers. In this example, you can see how to read an external file and also how to use atoll
& atof
functions for converting chars and strings to a numeric value.
In those examples, you can see how to use threads in NASM. In both examples, the program at first gets from the user the number of lines and threads to use. Then the program will fill up the memory with random numbers and calculate the average of the random number and the deviation.
In the 4th example, the memory filling & calculation is done by multiple threads but it contains a bottleneck in the calculation process due that only one thread can access the memory every time. In order to fix the bottleneck, In the 5th Example at first, the program is filling the memory with random numbers and only the calculation is done by parallel threads. Each thread gets a specific range of memory it can access and in this way, the program uses threads in a more efficient way.
This project is licensed under the MIT License.