Skip to content

teimor/NASM-Examples

Repository files navigation

NASM Examples

Examples of using NASM language on Linux based system, some of the examples are calling c standard library functions using NASM.

Getting Started

Requerments

  • 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

gcc Upgrade process

  1. Add gcc repository : sudo add-apt-repository ppa:jonathonf/gcc
  2. Update apt-get : sudo apt-get update
  3. Install gcc7 : sudo apt-get install gcc-7 g++-7
  4. 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
  5. Test gcc version : gcc -v and see the version is 7 or above

Examples

1st Example - "Hello World"

Simple code example that using c standard library printf function to print Hello World.

How to complie and run

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

One line command

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

Makefile

You can also just use the makefile in the folder and then use the make command.

2nd Example - Square

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).

3rd Example - Find Integers and Floating point numbers

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.

4th and 5th Examples - Threads Calcualtion

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.

License

This project is licensed under the MIT License.

Additional Information and links

Github Projects with good Examples

About

Code examples in NASM - Netwide Assembler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published