Decrease NASM assembly executable size
03:55 06 Nov 2021

I just started learning assembly and want to make a really tiny hello world program. After compiling with NASM and then linking, the executable size is 4.5 kb. This is odd, as I thought the assembler basically just substitutes the instructions with binary equivalents. If somebody could tell me how to decrease the size or just a bit on how assemblers work, it would be appreciated.

Code

; hello.asm

section .text:

global _start
_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, 12
    int 0x80

    mov eax, 1
    mov ebx, 0
    int 0x80

section .data:
    msg: db "hello world"

Compilation

$ nasm -f elf32 hello.asm -o hello.o
$ ld -m elf_i386 hello.o -o hello

readelf -e output

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x8049000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          4332 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         5
  Section header string table index: 4

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08049000 001000 000021 00  AX  0   0 16
  [ 2] .symtab           SYMTAB          00000000 001024 000080 10      3   4  4
  [ 3] .strtab           STRTAB          00000000 0010a4 000027 00      0   0  1
  [ 4] .shstrtab         STRTAB          00000000 0010cb 000021 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  p (processor specific)

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0x00074 0x00074 R   0x1000
  LOAD           0x001000 0x08049000 0x08049000 0x00021 0x00021 R E 0x1000

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .text 
assembly nasm