Getting to Know PC Assembler Translators

Classics of the genre: NASM and GAS

NASM (Network Assembler) — is a free assembler for the Intel x86 architecture, known for its simplicity and power. It supports 16-, 32-, and 64-bit programs and offers a rich set of macros. NASM is often used to write high-performance code sections, device drivers, and operating systems.

  • Author: Simon Tatham and Julian Hall.

  • Repository: NASM GitHub

  • Instructions: NASM supports about 700+ instructions, including various extensions and specific commands.

GAS (GNU Assembler) — GNU Project assembler used by the GCC compiler. It is a cross-platform tool that supports multiple architectures. GAS has powerful macro and directive capabilities, making it an indispensable tool for developing system software.

  • Author: GNU Project.

  • Repository: GAS in GNU Binutils

  • Instructions: Supports over 1000 instructions as it covers many processor architectures

ReLax and AsmX

Relax — is not a traditional assembler, but its author positions it as a programming language that compiles to byte code. It is difficult to call it a full-fledged compiler, since it is more like a virtual machine.

AsmX — is a programming language based on NodeJS. AsmX G2 is the second generation of this platform. AsmX tries to offer assembler for developers familiar with web technologies.

Relax and AsmX: First Impressions

Relax, despite its claims of compilation, left me with more questions than answers. There is no clear documentation, and the compilation process itself is unclear. Although the author positions Relax as a compiler, in fact its implementation is more reminiscent of a byte machine. I couldn't find any information about specific compilation instructions, which seemed odd to me, since the product is supposed to be ready to use. The RVM and IRasm GitHub repositories list the license, but I couldn't find any documentation on how to use the language.

AsmX, unlike Relax, has a more structured approach. Documentation is available on GitHub to help you understand installation and usage. The AsmX Foundation has implemented a rather unusual system that is more like a JIT compiler, but is not a classic virtual machine. This is the first time I encounter such an approach, and it can be considered a kind of know-how. The author describes in detail the updates and new features of the language in the README files of the repositories.

Relax and AsmX Rating

Relax:

  • Pros:

    • The author is trying to create his own programming language in C++.

    • Written in C++.

  • Cons:

    • Lack of technical documentation.

    • It's hard to understand what you can write on Relax.

AsmX:

  • Pros:

    • Technical documentation is available.

    • Installation and use are easy to understand thanks to the manuals.

  • Cons:

Comparison tables

Comparison of comment syntax in different assemblers

For ease of reading and maintaining code, assemblers provide the ability to add comments — text that is ignored by the compiler. Let's look at how this is done in different assemblers:

GAS

NASM

Relax

AsmX

AsmX G2

#

;

;

#

;;

As you can see from the table, most assemblers use the symbol # or ; to indicate comments. However, AsmX G2 uses two symbols ;; for comments.

Comparison of presence and definition of entry point

The entry point is the place in the program from which its execution begins. Different assemblers may have different approaches to determining the entry point. Let's consider how this is implemented in each of the assemblers.

GAS

It needs to be defined manually (for example, main)

NASM

It needs to be defined manually (for example, _start)

Relax

MainClass.Main

AsmX

Has no obvious entry point

AsmX G2

main

In GAS and NASM, the developer must explicitly specify the entry point, which provides flexibility but requires additional configuration. ReLax uses an object-oriented approach, where the entry point is a method MainClass.Main. AsmX, on the other hand, does not have an explicitly defined entry point, which can be unusual for traditional assemblers. AsmX G2 returns to a more traditional approach, using main as an entry point, which may ease the transition for developers accustomed to C-like languages.

Presence of SIMD instructions in different assemblers

Now let's see how things are with support SIMD instructions (Single Instruction, Multiple Data) in different assemblers. These instructions play an important role in high-performance computing, such as multimedia applications or processing large arrays of data. The table below shows the supported SIMD instruction sets, such as MMX, AVX, SSE, and others.

Assembler

MMX

AVX

SSE

AVX2

SSE2

SSE3

SSE4

AVX-512

SSE4.1

SSE4.2

GAS

+

+

+

+

+

+

+

+

+

+

NASM

+

+

+

+

+

+

+

+

Relax

AsmX

AsmX G2

+

+

  • + means that the assembler supports the corresponding SIMD instruction set.

  • - means that the assembler does not support the corresponding SIMD instruction set.

Analysis of SIMD instruction support

  • GAS And NASM show full or nearly full support for modern SIMD instructions, making them preferred for developing high-performance applications. However, NASM does not support SSE4.1 and SSE4.2, which may be a limitation for some specific tasks.

  • Relax And AsmX do not support any SIMD instructions, which may limit their use in tasks that require high performance.

  • AsmX G2 supports basic MMX and SSE, which may be sufficient for some tasks, but the lack of support for newer instructions such as AVX or AVX-512 may limit its use in modern computing.

Results

If you need powerful SIMD instructions for multimedia or high-performance computing, GAS And NASM – these are your best friends. GAS Leads in SIMD support, including cutting-edge instruction sets such as AVX-512. NASM is also good, but with some limitations. ReLax and AsmX are not suitable for programs requiring SIMD instructions. AsmX G2 can be used for programs requiring only MMX and SSE.

It is important to note that support for SIMD instructions may depend on the specific assembler version and target platform. Before using SIMD instructions, it is recommended to read the documentation of the selected assembler.

Sample code to calculate the square of a number

square(x) = x^2

For comparison, I wrote a simple program in different assemblers: calculating the square of a number. It's not “Hello, World!” (it's too simple), but it's not a factorial either (it's too complicated for a first acquaintance with assembler).

Program in different assemblers

GAS:

.section .data
number: .int 500
result: .int 0

.section .text
.global main

main:
    movl number, %eax   # Загрузить число в регистр eax
    imull %eax, %eax    # Вычислить квадрат числа
    movl %eax, result   # Сохранить результат в переменную result

    # Завершение программы
    movl $1, %eax       # Системный вызов для выхода
    xorl %ebx, %ebx     # Код возврата 0
    int $0x80           # Вызов ядра

NASM:

section .data
number dd 500
result dd 0

section .text
global _start

_start:
    mov eax, [number]   ; Загрузить число в регистр eax
    imul eax, eax       ; Вычислить квадрат числа
    mov [result], eax   ; Сохранить результат в переменную result

    ; Завершение программы
    mov eax, 1          ; Системный вызов для выхода
    xor ebx, ebx        ; Код возврата 0
    int 0x80            ; Вызов ядра

AsmX:

@mul 500 500          # Вычислить квадрат числа

AsmX G2:

@function main {    ;; Точка входа C/C++
    @mul 500 500    ;; Вычислить квадрат числа
}

Benchmark: Calculating the square of a number

I decided to see how these assemblers behave in terms of performance and memory consumption by writing a simple program to calculate the square of a number. To do this, I ran the programs and measured the execution time and memory usage.

Assembler

memory

min

avg

max

GAS

1024kb

0.38s

0.4s

0.587s

NASM

256kb

0.36s

0.38s

0.505s

AsmX

?????

0.17s

0.21s

0.262s

AsmX G2

?????

0.15s

0.19s

0.259s

Unfortunately, it was not possible to benchmark Relax due to the lack of launch capability.

Note: Benchmarks are indicative only and may vary depending on system hardware and configuration.

Results

  • GAS and NASM — these are classic assemblers, perfectly suited for working with programming languages ​​C/C++ and others.

  • AsmX — an interesting project that can be useful for backend and frontend developers, thanks to the use of NodeJS.

  • NASM proved to be more economical in terms of memory consumption compared to GAS.

  • AsmX G2 has a syntax that resembles a mixture of C++ and assembler.

  • The benchmarks surprised us by showing high performance of AsmX and AsmX G2. However, due to the lack of memory information for these assemblers, the comparison cannot be complete.

Thank you for your attention! I hope the article was useful and interesting. Write in the comments about your experience with assemblers and what other little-known tools are worth considering.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *