In x86_64 architecture it is possible to change some instruction-operands combinations with shorter ones to achieve the same effect, but smaller executable. for example, it is common to write:
xor eax, eax
instead of:
xor rax, rax
I wanted to test it, wrote simple program in assembly:
segment .text
global main
main:
push rbp
mov rbp, rsp
xor rax, rax ; line in question
leave
ret
built:
yasm -f elf64 -m amd64 -g dwarf2 main.asm; clang -o main main.o
checked size:
stat main
got:
....
Size: 9184
...
Ok, changed line in question to:
xor eax, eax
hoping to get smaller executable, but got the same 9184 bytes in size. Why size did not decrease with using shorter instruction form?
Use the size
command to find out how large the parts of a binary are. Using ls
or stat
is inaccurate as parts of the binary are padded to some power of 2 (e.g. to the next multiple of 16).
However, in your case there is still no difference because the text segment from main.o
is padded to a multiple of 16 bytes after which the startup code crt0.o
is linked. Thus there is no difference in code size.