NASM Coding: Constants and String Length

We can declare constant values in several ways in NASM:

  1. Using the
    equ
    pseudo-instruction:
    name equ expression
    Example:
    msg db "Hello", 10
    len equ $ - msg

    Explanation: we set

    msg
    to be the string
    "Hello\n"
    . Then, we set
    len
    to be the size of the string
    msg
    , which is
    6
    .
    Fun fact: the expression
    $ - msg
    is actually address subtraction: we subtract the start address of
    msg
    from the most recent address (which is stored at
    $
    ), which happens to be the end address of
    msg
    after running the previous instruction. This results in the length of the string
    msg
    .