/logo.png

My personal low-level dev blog

Write string.h functions using string instructions in asm x86-64

Introduction

The C standard library offers a bunch of functions (whose declarations can be found in the string.h header) to manage NULL-terminated strings and arrays. These are ones of the most used C functions, often implemented as builtin by the C compiler as they are crucial to the speed of programs.

On the other hand, the x86 architecture contains “string instructions”, aimed at implementing operations on strings at the hardware level. Moreover, the x86 architecture was incrementally enhanced with SIMD instructions over the years which allows processing multiple bytes of data in one instruction.

Use nasm preprocessor to write clean x86 asm

Why should you use the nasm preprocessor ?

Writing assembly is a tedious work.

Some of my greatest issues with assembly language programming are

  • the lack of names for local variables
  • the need to write the same assembly code repetitively (for instance function’s prologue an epilogue)
  • and the lack of data structures.

These are three good reasons to learn a preprocessor language when you learn to write assembly and there are a lot of others like the need to adapt the code to the cpu features, or the need to generate large portions of code (like declaring an array of 1024 bytes filled with ones), etc.