I am curious - What is the difference between .equ and .word directives in ARM assembly, when defining constants?
NASM 2.10.09 ELF output:
.word is simple: it outputs 2 bytes to the object file no matter where we are.
Consequences of this:
.word is after a symbol x:, x will point to those bytes.word is in the text segment, those bytes might get executedIt has absolutely no other side effect. In particular, it does not set the st_size field of the symbol table entry (e.g. int often == 4 bytes), which is something sensible compilers should do. You need the .size x, 2 directive for that.
.equ does two things:
st_shndx == SHN_ABS and the given valueSample code:
.text
.equ x, 123
mov $x, %eax
/* eax == 123 */
.equ x, 456
mov $x, %eax
/* eax == 456 */
Now:
as --32 -o main.o main.S
objdump -Sr main.o
Gives:
00000000 <.text>:
0: b8 7b 00 00 00 mov $0x7b,%eax
5: b8 c8 01 00 00 mov $0x1c8,%eax
which confirms the macro-like effect, and:
readelf -s main.o
contains:
Num: Value Size Type Bind Vis Ndx Name
4: 000001c8 0 NOTYPE LOCAL DEFAULT ABS x
which confirms the SHN_ABS effect: a symbol was created, and it could be used from another file by linking if it were global. I have explained this in more detail at https://stackoverflow.com/a/33148242/895245
The situation is analogous for NASM's equ, except that the NASM version can only be used once per symbol.
.set and the equals sign = (source) are the same as .equ.
You should also look into .equiv https://sourceware.org/binutils/docs-2.25/as/Equiv.html , which prevents redefinition.