You can also directly assign symbols with the equals sign `=`:
S = 40
which the GNU as manual says is equivalent to using `.set`
[To see links please register here]
> A symbol can be given an arbitrary value by writing a symbol, followed by an equals sign `=', followed by an expression (see Expressions). This is equivalent to using the .set directive. See .set. In the same way, using a double equals sign `='`=' here represents an equivalent of the .eqv directive. See .eqv.
`.equ` is yet another synonym...
.equ S, 40
One typical use case of such constants, is to calculate the length of static strings, e.g. the Linux x86_64 hello world can be written as:
hello_world.S
.data
hello_world:
.ascii "hello world\n"
hello_world_len = . - hello_world
.text
.global _start
_start:
/* write */
mov $1, %rax
mov $1, %rdi
mov $hello_world, %rsi
mov $hello_world_len, %rdx
syscall
/* exit */
mov $60, %rax
mov $0, %rdi
syscall
which you can compile and run wth:
as -o hello_world.o hello_world.S
ld -o hello_world.out hello_world.o
./hello_world.out
[GitHub upstream](
[To see links please register here]
).
`%rdx` will then contain the length of the string to be written.
The `$` is required just like for any regular address label, otherwise you would be trying to access that memory address instead of moving an immediate.