2026-12-01 # Microbrot Fractal
open Introduction
Size optimizing binaries is one of my passions. In the age of more, why not less? Derived from an earlier attempt, I wrote a 128 byte binary that renders a mandelbrot fractal. This is just half the size of the old 256 byte program. No, it does not render in the Netpbm image format anymore, but at least it doesn't segfault once it is done! The original program was written in Zig, using a custom linker script. This program is written from the ground up in x86 assembly language. In specific, both code and the PHDR overlaps the ELF header. Many shortcuts were taken. I solemnly swear that I was up to no good when I made this. And yet, how exciting!
Here is a hexdump (hexyl) of the binary:
+--------+-------------------------+-------------------------+--------+--------+
|00000000| 7f 45 4c 46 01 00 00 00 | 00 00 00 00 00 00 01 00 |•ELF•⋄⋄⋄|⋄⋄⋄⋄⋄⋄•⋄|
|00000010| 02 00 03 00 00 10 00 00 | 20 00 01 00 04 00 00 00 |•⋄•⋄⋄•⋄⋄| ⋄•⋄•⋄⋄⋄|
|00000020| 94 89 c7 fd ba 18 06 00 | 00 90 20 00 01 00 bb 83 |××××ו•⋄|⋄× ⋄•⋄××|
|00000030| 04 00 00 b0 0a aa 89 de | 89 d5 b1 14 89 ec 0f af |•⋄⋄×_×××|××ו×ו×|
|00000040| e5 0f af ee c1 fd 09 01 | d5 0f af f6 70 09 29 e6 |ו××××_•|ו××p_)×|
|00000050| c1 fe 0a 01 de e2 e5 89 | c8 04 20 aa 83 eb 1e 80 |××_•××××|ו ××ו×|
|00000060| fb 73 75 d2 83 ea 3c 80 | fa e8 75 c2 31 db 43 b0 |×su×××<×|××u×1×C×|
|00000070| 04 89 f9 41 ba 94 18 00 | 00 cd 80 93 31 db cd 80 |•××A×ו⋄|⋄×××1×××|
+--------+-------------------------+-------------------------+--------+--------+
Here is the binary in base64. You can convert this to a binary by piping it through
base64 -d and redirecting into a file, like this:
echo "..." | base64 -d > tiny && chmod +x tiny && ./tiny
f0VMRgEAAAAAAAAAAAABAAIAAwAAEAAAIAABAAQAAACUicf9uhgGAACQIAABALuDBAAAsAqqid6J1bEUiewPr+UPr+7B/QkB1Q+v9nAJKebB/goB3uLlicgEIKqD6x6A+3N10oPqPID66HXCMdtDsASJ+UG6lBgAAM2AkzHbzYA=
Here is a 16x8 8-bit bitmap of the image... Hmm, I'm not sure if you actually can gain anything from this image, but it's included here anyways! Such a small number of pixels! That's not a lot of entropy, and a bunch of it is not even the program, but the ELF header / PHDR. Ain't it great?
sitelen ilo Output
When you run it, the program will print the fractal shown below. I particularly like how the outer values are all 0's and 1's; it reminds me of the pain I went through trying to squeeze out every last byte. I would consider my most enjoyable space saving to be how I represented the math itself. Fixed-point arithmetic is a pretty sweet deal. Honestly, more people need to learn how powerful it is. Most math doesn't need floating point numbers. Integers work - and if they don't, check again. :)
toki ilo Code
Listed below is the code I wrote for the mandelbrot renderer. It can be compressed more, if you
don't mind the side effects. If you don't care about exiting with zero, you can save two bytes by
removing xor ebx, ebx. If you don't care about segfaults, you can remove all the exit
code, saving another five bytes. If you don't care about undefined stack values, you can remove
inc ecx and change mov edx, BYTES to mov edx, BYTES + 1,
saving one more byte. I also found that if you get the low byte of ebx and edx to land on zero at
the end of your rows and columns, you can save another couple bytes by changing
cmp bl, STOP_RE and jne col to test bl, bl and
jnz col, as well as for the other loop end. You might also be able to save bytes by
writing bottom-up instead of top down in the stack, I have not tested that. The smallest I was able
to get the program was 116 bytes, but that had all the aforementioned side effects, and to save a
few more bytes, the image was shrunken so the number of output bytes matched what was already in the
edx register, or the limit to the imaginary value for C. Anyways, compile this code
with nasm -f bin tiny.asm -o tiny.
bits 32 ; 32-bit is often smaller than 64-bit, funny enough
org 0x10000 ; must be multiple of 0x10000 for alignment reasons
%assign C_RE_START -2445 ; -2.3876953125
%assign C_RE_END 1155 ; +1.1279296875
%assign STEP_RE 30
%assign C_IM_START -1560 ; -1.5234375000
%assign C_IM_END 1560 ; +1.4941406250
%assign STEP_IM 60
; hack some free bytes - GCD(256, 30) = 2
%assign STOP_RE (C_RE_START & 0xff)
%assign STOP_IM (C_IM_START & 0xff)
%assign COLS ((C_RE_END - C_RE_START) / STEP_RE)
%assign ROWS ((C_IM_END - C_IM_START) / STEP_IM)
%assign BYTES ((COLS + 1) * ROWS) ; newlines
headers:
db 0x7f, "ELF" ; magic bytes for executable format
dd 1 ; p_type = PT_LOAD, one loadable segment
dd 0 ; p_offset - loadable segment is at file start
dd 0x10000 ; p_vaddr - mapped at 0x10000 - matches org
dw 2 ; e_type = ET_EXEC, overlap p_paddr (ignored)
dw 3 ; e_machine = EM_386, overlap p_paddr (ignored)
dd 0x1000 ; e_version (ignored), overlap p_filesz = GTE code length
dd entry ; e_entry address, overlap p_memsz = GTE p_filesz
dd 4 ; e_phoff = 4, overlap p_flags = 4 (+R+X, linux is weird)
entry:
; on program load, EAX = 0 and ESP is the stack pointer - which is writable
; by swapping them, EAX is a writable address and ESP is a scratch register
xchg eax, esp
mov edi, eax ; set the output pointer to the top of the stack
std ; set the direction flag, each stosb moves edi down
; edx register holds Cim - moving from end to start because
; pixels will be rendered in the reverse order they are written
mov edx, C_IM_END
nop ; padding so header fields line up
; ELF header - e_phentsize = 32, program header table entry is 32 bytes
dw 0x20 ; executed as `and [eax],al`
; ELF header - e_phnum = 1, the program header table has only one entry
dw 0x01 ; executed as `add [eax],eax`
row:
; (Zre, Zim) is stored in (esi, ebp)
; (Cre, Cim) is stored in (ebx, edx)
; scratch register is esp
; ebx register holds Cre - moving from end to start because
; pixels will be rendered in the reverse order they are written
mov ebx, C_RE_END
; at the end of a line, write a newline
mov al, 10
stosb
col:
; set Zre = Cre and Zim = Cim... setting Z = C skips one iteration, and
; z = 0 would still cost 4 bytes with `xor esi, esi` and `xor ebp, ebp`
mov esi, ebx ; Zre <- Cre
mov ebp, edx ; Zim <- Cim
; cx is number of iterations per pixel - ch is already 0, so only set cl
mov cl, 20
iter:
; Z <- Z ^ 2 + C
; ... Zre <- Zre ^ 2 - Zim ^ 2 + Cre
; ... Zim <- 2 * Zre * Zim + Cim
; scratch <- Zim
mov esp, ebp
; scratch <- Zim ^ 2 (unnormalized, fixed point)
imul esp, ebp
; Zim <- Zre * Zim (unnormalized, fixed point)
imul ebp, esi
; Zim <- 2 * Zre * Zim
sar ebp, 9
; Zim <- 2 * Zre * Zim + Cim
add ebp, edx
; Zre <- Zre ^ 2 (unnormalized, fixed point)
imul esi, esi
; ESCAPE - If Zre ^ 2 overflows, assume the point is not part of the
; mandelbrot - this is cheaper than checking Zre ^ 2 + Zim ^ 2 >= 16
jo out
; Zre <- Zre ^ 2 - Zim ^ 2 (unnormalized, fixed point)
sub esi, esp
; Zre <- Zre ^ 2 - Zim ^ 2
sar esi, 10
; Zre <- Zre ^ 2 - Zim ^ 2 + Cre
add esi, ebx
; decrement ecx, and jump to iter if nonzero -
; ecx is the number of iterations per pixel
loop iter
out:
; determine remaining number of iterations
mov eax, ecx
; make value printable, inside set -> space
add al, ' '
; write character to [edi] and decrement edi
stosb
; advance Cre to the next col, and check if we are at the end of the
; line by checking it's lower byte - if so, advance to the next column
sub ebx, STEP_RE
cmp bl, STOP_RE
jne col
; advance Cim to the next row, and check if we are at the end of the
; fractal by checking it's lower byte - if so, advance to the next row
sub edx, STEP_IM
cmp dl, STOP_IM
jne row
xor ebx, ebx
inc ebx ; STDOUT
mov al, 4 ; SYS_WRITE
mov ecx, edi ; pointer
inc ecx ; std compensation
mov edx, BYTES ; count
int 0x80 ; write
xchg eax, ebx ; SYS_EXIT
xor ebx, ebx ; EXIT_SUCCESS
int 0x80 ; exit
© Eric Petersen. All rights reserved.