Last active
March 15, 2020 14:58
-
-
Save inaz2/72de0fa471b207cb7a6b to your computer and use it in GitHub Desktop.
x86 brainfuck interpreter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* brainfuck.s */ | |
.intel_syntax noprefix | |
.globl _start | |
_start: | |
lea edx, mem | |
lea esi, bfcode | |
loop: | |
mov al, [esi] | |
cmp al, '>' | |
je inc | |
cmp al, '<' | |
je dec | |
cmp al, '+' | |
je plus | |
cmp al, '-' | |
je minus | |
cmp al, '.' | |
je write | |
cmp al, ',' | |
je read | |
cmp al, '[' | |
je left | |
cmp al, ']' | |
je right | |
jmp exit | |
inc: | |
inc edx | |
jmp next | |
dec: | |
dec edx | |
jmp next | |
plus: | |
incb [edx] | |
jmp next | |
minus: | |
decb [edx] | |
jmp next | |
write: | |
pushad | |
mov ecx, edx | |
mov edx, 1 | |
mov ebx, 1 | |
mov eax, 4 | |
int 0x80 | |
popad | |
jmp next | |
read: | |
pushad | |
mov ecx, edx | |
mov edx, 1 | |
mov ebx, 0 | |
mov eax, 3 | |
int 0x80 | |
popad | |
jmp next | |
left: | |
push esi | |
jmp next | |
right: | |
mov al, [edx] | |
test al, al | |
jz rightexit | |
mov esi, [esp] | |
jmp next | |
rightexit: | |
pop eax | |
jmp next | |
next: | |
inc esi | |
jmp loop | |
exit: | |
xor ebx, ebx | |
mov eax, 1 | |
int 0x80 | |
.data | |
.fill 64 | |
mem: | |
.fill 64 | |
bfcode: | |
.asciz "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcc -nostdlib brainfuck.s | |
$ ./a.out | |
Hello World! | |
$ objdump -s a.out | |
a.out: file format elf32-i386 | |
Contents of section .note.gnu.build-id: | |
8048094 04000000 14000000 03000000 474e5500 ............GNU. | |
80480a4 f930bf0a 585901aa 1ae6d8b7 6487120d .0..XY......d... | |
80480b4 ced4512f ..Q/ | |
Contents of section .text: | |
80480b8 8d158191 04088d35 c1910408 8a063c3e .......5......<> | |
80480c8 741e3c3c 741d3c2b 741c3c2d 741c3c2e t.<<t.<+t.<-t.<. | |
80480d8 741c3c2c 742f3c5b 74423c5d 7441eb50 t.<,t/<[tB<]tA.P | |
80480e8 42eb4a4a eb47fe02 eb43fe0a eb3f6089 B.JJ.G...C...?`. | |
80480f8 d1ba0100 0000bb01 000000b8 04000000 ................ | |
8048108 cd8061eb 286089d1 ba010000 00bb0000 ..a.(`.......... | |
8048118 0000b803 000000cd 8061eb11 56eb0e8a .........a..V... | |
8048128 0284c074 058b3424 eb0358eb 0046eb8c ...t..4$..X..F.. | |
8048138 31dbb801 000000cd 80 1........ | |
Contents of section .data: | |
8049141 00000000 00000000 00000000 00000000 ................ | |
8049151 00000000 00000000 00000000 00000000 ................ | |
8049161 00000000 00000000 00000000 00000000 ................ | |
8049171 00000000 00000000 00000000 00000000 ................ | |
8049181 00000000 00000000 00000000 00000000 ................ | |
8049191 00000000 00000000 00000000 00000000 ................ | |
80491a1 00000000 00000000 00000000 00000000 ................ | |
80491b1 00000000 00000000 00000000 00000000 ................ | |
80491c1 2b2b2b2b 2b2b2b2b 5b3e2b2b 2b2b5b3e ++++++++[>++++[> | |
80491d1 2b2b3e2b 2b2b3e2b 2b2b3e2b 3c3c3c3c ++>+++>+++>+<<<< | |
80491e1 2d5d3e2b 3e2b3e2d 3e3e2b5b 3c5d3c2d -]>+>+>->>+[<]<- | |
80491f1 5d3e3e2e 3e2d2d2d 2e2b2b2b 2b2b2b2b ]>>.>---.+++++++ | |
8049201 2e2e2b2b 2b2e3e3e 2e3c2d2e 3c2e2b2b ..+++.>>.<-.<.++ | |
8049211 2b2e2d2d 2d2d2d2d 2e2d2d2d 2d2d2d2d +.------.------- | |
8049221 2d2e3e3e 2b2e3e2b 2b2e00 -.>>+.>++.. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment