NASM Coding: Constants and String Length

  1. Using the
    %define
    directive:
    %define name expression
    Example:
    %define WRITE_SYSCALL_NUM 1
    %define PRETTY_NUM 12345
    %define EXIT_SYSCALL mov rax, 60 \ syscall ; We can put entire code segments in the macro!!!
    %define MYREG rax                          ; We can even create register aliases!

    Explanation: we can turn any piece of NASM code into a constant. This is similar to creating a macro, whose name you just need to write somewhere in your program: you can write (repeat) it as many times as wanted! During compilation, a special software called the pre-processor will replace every instance of your constant name with the actual code behind it before converting the program into machine code (e.g., every

    WRITE_SYSCALL_NUM
    will be replaced with a
    1
    .)