├── LICENSE ├── Makefile ├── README.md ├── bf256.s └── rt.s /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Brian Callahan 2 | 3 | Permission to use, copy, modify, and distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # bf256 Makefile 2 | 3 | all: 4 | gas -g -o rt.o rt.s 5 | gas -g -o bf256.o bf256.s 6 | ld -e main -nostdlib -nopie -o bf256 rt.o bf256.o 7 | strip bf256 8 | strip -R .bss bf256 9 | strip -R .comment bf256 10 | strip -R .data bf256 11 | 12 | clean: 13 | rm -rf bf256 bf256.o rt.o bf256.core 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bf256 2 | ===== 3 | `bf256` is a Brainfuck compiler under 256 bytes in size. 4 | 5 | It probably only works on 6 | [OpenBSD](https://www.openbsd.org/)/amd64 7 | as-is. 8 | 9 | Write-up 10 | -------- 11 | See the 12 | [blog post](https://briancallahan.net/blog/20210710.html). 13 | 14 | Building 15 | -------- 16 | Just run `make`. 17 | 18 | Running 19 | ------- 20 | ``` 21 | $ bf256 < input.bf > output.c 22 | ``` 23 | Alternatively: 24 | ``` 25 | $ bf256 < input.bf | cc -x c - 26 | ``` 27 | 28 | Size 29 | ---- 30 | Compiler alone: 31 | ``` 32 | $ size bf256.o 33 | text data bss dec hex 34 | 207 0 0 207 cf 35 | ``` 36 | 37 | With overhead: 38 | ``` 39 | $ size bf256 40 | text data bss dec hex 41 | 231 0 0 231 e7 42 | ``` 43 | 44 | License 45 | ------- 46 | ISC License. See `LICENSE` for details. 47 | -------------------------------------------------------------------------------- /bf256.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Brian Callahan 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | .text 18 | .globl main 19 | main: 20 | incl %edi # Write prologue 21 | leal .LSprologue, %esi 22 | movb $21, %dl 23 | jmp .Lwrite 24 | .Lparse: 25 | movb $3, %al # Load read(2) system call 26 | xchg %edi, %edx # Read one character 27 | xorl %edi, %edi # Read from stdin 28 | leaq (%rsp), %rsi # Read into top of stack 29 | syscall # read(0, (%rsp), 1); 30 | incl %edi # Set %edi to 1, for write 31 | xchg %ebp, %eax # Store return value in %ebp 32 | movb (%rsi), %al # cmpb imm, %al is the smallest cmp 33 | leal .LS, %esi # Preload string 34 | cmpl %edx, %ebp # EOF ? (%ebp < 1) 35 | jl .Leof 36 | cmpb $60, %al # '<' ? 37 | je .Lleft 38 | cmpb $62, %al # '>' ? 39 | je .Lright 40 | cmpb $45, %al # '-' ? 41 | je .Ldec 42 | cmpb $43, %al # '+' ? 43 | je .Linc 44 | cmpb $44, %al # ',' ? 45 | je .Lgetchar 46 | cmpb $46, %al # '.' ? 47 | je .Lputchar 48 | cmpb $91, %al # '[' ? 49 | je .Lopenloop 50 | cmpb $93, %al # ']' ? 51 | jne .Lparse # Comment character, skip 52 | decl %ebx # Decrement loop counter 53 | js .Lexit # %ebx < 0 ? (%rdi == 1 from the write(2) call) 54 | addl $47, %esi 55 | jmp .Lwrite 56 | .Linc: 57 | addl $6, %esi 58 | .Ldec: 59 | movb $6, %dl 60 | .Lwrite: 61 | movb $4, %al 62 | syscall # write(1, string, length); 63 | jmp .Lparse 64 | .Leof: 65 | cmpl %edx, %ebx # Loop counter < 1 ? (i.e., 0) 66 | jge .Lexit 67 | addl $47, %esi 68 | movb $4, %al 69 | syscall 70 | xorl %edi, %edi # Get ready to exit 71 | .Lexit: 72 | xchg %edx, %eax # _exit(%edi); 73 | syscall 74 | .Lleft: 75 | subl $6, %esi # 7 - 6 = 1 76 | .Lright: 77 | addl $7, %esi 78 | movb $5, %dl 79 | jmp .Lwrite 80 | .Lgetchar: 81 | subl $12, %esi # 24 - 12 = 12 82 | .Lputchar: 83 | addl $24, %esi 84 | movb $13, %dl 85 | jmp .Lwrite 86 | .Lopenloop: 87 | incl %ebx # Increment loop counter 88 | addl $37, %esi 89 | movb $10, %dl 90 | jmp .Lwrite 91 | 92 | .LSprologue: 93 | .ascii "a[65536],*p=a;main(){" # 21 94 | 95 | .LS: 96 | .ascii "*p-=1;" # 6 97 | .ascii "*p+=1;" # 6 98 | .ascii "*p=getchar();" # 13 99 | .ascii "putchar(*p);" # 12 100 | .ascii "while(*p){" # 10 101 | .ascii "}" # 1 102 | -------------------------------------------------------------------------------- /rt.s: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 Brian Callahan 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | .section ".note.openbsd.ident", "a" 18 | .p2align 2 19 | .long 0x8 20 | .long 0x4 21 | .long 0x1 22 | .asciz "OpenBSD" 23 | .long 0x0 24 | .previous 25 | --------------------------------------------------------------------------------