共有回帖数 0 个
- BrainFuck解释器 及 文本文件转BrainFuck的工具(C & ASM)
-
只看楼主
收藏
回复
-
C语言,BrainFuck解释器
#include stdio.h
#include conio.h
#define CELL_SIZE 30000
#define CODE_SIZE 30000
int main(int argc, char *argv[])
{
char cell[CELL_SIZE+1] = {0}, code[CODE_SIZE+1],
*pCode, *pCell;
FILE *f;
int CodeLen, t;
if (argc != 2)
return puts("MasterRay BrainFuck InterpreternBFI programnn program Specify the program file to run."), 0;
if ((f = fopen(argv[1], "rb")) == NULL)
return puts("Cannot open the program file specified."), 1;
fseek(f, 0, SEEK_END);
if ((CodeLen = ftell(f)) CODE_SIZE)
return puts("The program file is too large."), 2;
fseek(f, 0, SEEK_SET);
fread(code, CodeLen, 1, f);
code[CodeLen] = ' ';
for (pCode = code, t = 0; *pCode != ' '; ++pCode)
{
if (*pCode == '[')
++t;
else if (*pCode == ']')
--t;
if (t 0)
return puts("Unmatched ]."), 4;
}
if (t != 0)
return puts("Unmatched [."), 3;
pCode = code;
pCell = cell;
do
{
switch (*pCode)
{
case '+': ++*pCell; break;
case '-': --*pCell; break;
case '':
if (pCell != cell)
--pCell;
else
pCell += CELL_SIZE - 1;
break;
case '':
if (pCell cell + CELL_SIZE - 1)
++pCell;
else
pCell = cell;
break;
case '.': putchar(*pCell); break;
case ',': *pCell = getch(); break;
case '[':
if (*pCell == 0)
{
t = 1;
do
{
if (*++pCode == '[')
++t;
else if (*pCode == ']')
--t;
}while (t != 0);
}
break;
case ']':
if (*pCell != 0)
{
t = -1;
while (t != 0)
{
if (*--pCode == '[')
++t;
else if (*pCode == ']')
--t;
}
}
}
}while (*++pCode != ' ');
return 0;
}
C语言,文本文件转BrainFuck
#include stdio.h
int main(int argc, char *argv[])
{
static unsigned sqr[]={0, 0, 0, 0, 0, 25, 36, 49, 64, 81, 100, 121, 144};
unsigned char oldch, ch, i;
unsigned TextLen;
FILE *in, *out;
if (argc != 3)
return puts("MasterRay TextToBFnTextToBF source destinationnn source Specify the text file.n destination Specify the filename for the converted BrainFuck file."), 0;
if ((in = fopen(argv[1], "rb")) == NULL)
return puts("Cannot open the source file specified."), 1;
if ((out = fopen(argv[2], "wb")) == NULL)
return puts("Cannot write in the destination file specified."), 2;
fseek(in, 0, SEEK_END);
TextLen = ftell(in);
fseek(in, 0, SEEK_SET);
for (oldch = 0; TextLen != 0; --TextLen)
{
ch = fgetc(in);
if (ch == oldch)
fwrite(".", 1, 1, out);
else if ((unsigned char)(ch - oldch) = 127)
{
ch -= oldch;
if (ch 16)
{
fwrite("+++++++++++++++", ch, 1, out);
fwrite(".", 1, 1, out);
}
else
{
for (i = 5; sqr = ch; ++i);
fwrite("+++++++++++++++", i--, 1, out);
fwrite("[", 2, 1, out);
if (ch % i i / 2)
{
fwrite("+++++++++++++++", ch / i + 1, 1, out);
fwrite("-]", 4, 1, out);
fwrite("---------------", i - ch % i, 1, out);
}
else
{
fwrite("+++++++++++++++", ch / i, 1, out);
fwrite("-]", 4, 1, out);
fwrite("+++++++++++++++", ch % i, 1, out);
}
fwrite(".", 1, 1, out);
}
oldch += ch;
}
else if ((unsigned char)(ch - oldch) 127)
{
ch = oldch - ch;
if (ch 16)
{
fwrite("---------------", ch, 1, out);
fwrite(".", 1, 1, out);
}
else
{
for (i = 5; sqr = ch; ++i);
fwrite("+++++++++++++++", i--, 1, out);
fwrite("[", 2, 1, out);
if (ch % i i / 2)
{
fwrite("---------------", ch / i + 1, 1, out);
fwrite("-]", 4, 1, out);
fwrite("+++++++++++++++", i - ch % i, 1, out);
}
else
{
fwrite("---------------", ch / i, 1, out);
fwrite("-]", 4, 1, out);
fwrite("---------------", ch % i, 1, out);
}
fwrite(".", 1, 1, out);
}
oldch -= ch;
}
}
return 0;
}
ASM,BrainFuck解释器
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
includelib kernel32.lib
CELL_SIZE equ 30000
CODE_SIZE equ 30000
.const
sTitle db 'MasterRay BrainFuck Interpreter', 10, 'BFI program', 10, 10,
'program Specify the program file to run.'
len_sTitle equ $ - offset sTitle
sError1 db 'Cannot open the program file specified.'
len_sError1 equ $ - offset sError1
sError2 db 'The program file is too large.'
len_sError2 equ $ - offset sError2
sError3 db 'Unmatched [.'
len_sError3 equ $ - offset sError3
sError4 db 'Unmatched ].'
len_sError4 equ $ - offset sError4
.data?
cell db CELL_SIZE dup(0)
code db CODE_SIZE dup(0)
cKey db ?
hStdIn dd ?
hStdOut dd ?
t dd ?
.code
Start:
invoke GetStdHandle, STD_INPUT_HANDLE
mov [hStdIn], eax
invoke SetConsoleMode, eax, 0
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov [hStdOut], eax
;获取命令行参数
invoke GetCommandLine
xor ecx, ecx
xor edx, edx
.repeat
.if byte ptr [eax] == 32
xor edx, edx
mov byte ptr [eax], 0
.if byte ptr [eax-1] != 0
inc ecx
.endif
.elseif edx == 0
inc edx
mov [t], eax ;程序名
.endif
inc eax
.until byte ptr [eax] == 0
.if byte ptr [eax-1] != 0
inc ecx
.endif
.if ecx != 2 ;argc != 2
invoke WriteConsole, [hStdOut], offset sTitle, [len_sTitle], NULL, NULL
invoke ExitProcess, 0
.else
;打开文件
invoke CreateFile, [t], GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
.if eax == INVALID_HANDLE_VALUE
invoke WriteConsole, [hStdOut], offset sError1, [len_sError1], NULL, NULL
invoke ExitProcess, 0
.endif
mov [t], eax
;获取文件大小
invoke GetFileSize, eax, NULL
.if eax CODE_SIZE
invoke WriteConsole, [hStdOut], offset sError2, [len_sError2], NULL, NULL
invoke ExitProcess, 0
.endif
mov ebx, eax ;代码长度
;读取文件
push NULL
push offset [t]
push eax
push offset code
push [t]
call ReadFile
mov eax, offset code
add eax, ebx
mov byte ptr [eax], 0
.endif
;检查括号是否匹配
xor ecx, ecx
mov esi, offset code
.while byte ptr [esi] != 0
.if byte ptr [esi] == '['
inc ecx
.elseif byte ptr [esi] == ']'
dec ecx
.endif
test ecx, ecx
jl L_Error4
inc esi
.endw
jecxz @F
invoke WriteConsole, [hStdOut], offset sError3, len_sError3, NULL, NULL
invoke ExitProcess, 0
L_Error4:
invoke WriteConsole, [hStdOut], offset sError4, len_sError4, NULL, NULL
invoke ExitProcess, 0
@@:
;解释程序
mov esi, offset code
mov edi, offset cell
.repeat
mov al, [esi]
sub al, '+'
.if al 4
.if al == 0 ;+
inc byte ptr [edi]
.elseif al == 2 ;-
dec byte ptr [edi]
.elseif al == 1 ;,
invoke ReadConsole, [hStdIn], offset [cKey], 1, offset t, NULL
mov al, [cKey]
mov [edi], al
.else ;.
invoke WriteConsole, [hStdOut], edi, 1, NULL, NULL
.endif
.elseif al == 17 ;
.if edi != offset [cell]
dec edi
.else
add edi, CELL_SIZE - 1
.endif
.elseif al == 19 ;
.if edi offset [cell] + CELL_SIZE - 1
inc edi
.else
mov edi, offset [cell]
.endif
.elseif al == 48 ;[
.if byte ptr [edi] == 0
mov ecx, 1
.repeat
inc esi
.if byte ptr [esi] == '['
inc ecx
.elseif byte ptr [esi] == ']'
dec ecx
.endif
.until ecx == 0
.endif
.elseif al == 50 ;]
.if byte ptr [edi] != 0
mov ecx, -1
.repeat
dec esi
.if byte ptr [esi] == '['
inc ecx
.elseif byte ptr [esi] == ']'
dec ecx
.endif
.until ecx == 0
.endif
.endif
inc esi
.until byte ptr [esi] == 0
invoke ExitProcess, 0
end Start
ASM,文本文件转BrainFuck
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
includelib kernel32.lib
.const
sTitle db 'MasterRay TextToBF', 10, 'TextToBF source destination', 10, 10,
' source Specify the text file.', 10,
' destination Specify the filename for the converted BrainFuck file.'
len_sTitle equ $ - offset sTitle
sError1 db 'Cannot open the source file specified.'
len_sError1 equ $ - offset sError1
sError2 db 'Cannot write in the destination file specified.'
len_sError2 equ $ - offset sError2
cFullStop db '.'
sPlus db '+++++++++++++++'
sSubs db '---------------'
sLeft db '['
sRight db '-]'
sqr dd 25, 36, 49, 64, 81, 100, 121, 144
.data?
char db ?
oldch db ?
TextLen dd ?
fIn dd ?
fOut dd ?
hStdIn dd ?
hStdOut dd ?
pOutfileName dd ?
t dd ?
.code
Start:
invoke GetStdHandle, STD_INPUT_HANDLE
mov [hStdIn], eax
invoke SetConsoleMode, eax, 0
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov [hStdOut], eax
;获取命令行参数
invoke GetCommandLine
xor ecx, ecx
xor edx, edx
.repeat
.if byte ptr [eax] == 32
xor edx, edx
mov byte ptr [eax], 0
.if byte ptr [eax-1] != 0
inc ecx
.endif
.elseif edx == 0
inc edx
.if ecx == 1
mov [t], eax ;源文件名
.elseif ecx == 2
mov [pOutfileName], eax
.endif
.endif
inc eax
.until byte ptr [eax] == 0
.if byte ptr [eax-1] != 0
inc ecx
.endif
.if ecx != 3 ;argc != 3
invoke WriteConsole, [hStdOut], offset sTitle, [len_sTitle], NULL, NULL
invoke ExitProcess, 0
.else
;打开源文件
invoke CreateFile, [t], GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
.if eax == INVALID_HANDLE_VALUE
invoke WriteConsole, [hStdOut], offset sError1, [len_sError1], NULL, NULL
invoke ExitProcess, 0
.endif
mov [fIn], eax
;获取文件大小
invoke GetFileSize, eax, NULL
mov [TextLen], eax ;代码长度
;打开目标文件
invoke CreateFile, [pOutfileName], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.if eax == INVALID_HANDLE_VALUE
invoke WriteConsole, [hStdOut], offset sError2, [len_sError2], NULL, NULL
invoke ExitProcess, 0
.endif
mov [fOut], eax
.endif
;转换程序
;mov [oldch], 0
.while [TextLen] != 0
invoke ReadFile, [fIn], offset [char], 1, offset [t], NULL
mov al, [char]
mov ah, [oldch]
cmp al, ah
jnz @F
invoke WriteFile, [fOut], offset [cFullStop], 1, offset [t], NULL
jmp L1
@@:
sub al, ah
.if al 128
.if al 16
movzx eax, al
invoke WriteFile, [fOut], offset [sPlus] + 1, eax, offset [t], NULL
invoke WriteFile, [fOut], offset [cFullStop], 1, offset [t], NULL
.else
mov ebx, 5
.while byte ptr [ebx * 4 + offset [sqr] - 20] = al
inc ebx
.endw
invoke WriteFile, [fOut], offset [sPlus], ebx, offset [t], NULL
dec ebx
invoke WriteFile, [fOut], offset [sLeft], 2, offset [t], NULL
movzx ax, [char]
sub al, [oldch]
div bl
mov cl, bl
shr cl, 1
.if ah cl
sub bl, ah
movzx ebx, bl
inc al
movzx eax, al
push NULL
push offset [t]
push ebx
push offset [sSubs]
push [fOut]
invoke WriteFile, [fOut], offset [sPlus] + 1, eax, offset [t], NULL
invoke WriteFile, [fOut], offset [sRight], 4, offset [t], NULL
call WriteFile
.else
push eax
movzx eax, al
invoke WriteFile, [fOut], offset [sPlus] + 1, eax, offset [t], NULL
invoke WriteFile, [fOut], offset [sRight], 4, offset [t], NULL
pop eax
movzx eax, ah
invoke WriteFile, [fOut], offset [sPlus] + 1, eax, offset [t], NULL
.endif
invoke WriteFile, [fOut], offset [cFullStop], 1, offset [t], NULL
.endif
mov al, [char]
mov [oldch], al
.else
neg al
.if al 16
movzx eax, al
invoke WriteFile, [fOut], offset [sSubs], eax, offset [t], NULL
invoke WriteFile, [fOut], offset [cFullStop], 1, offset [t], NULL
.else
mov ebx, 5
.while byte ptr [ebx * 4 + offset [sqr] - 20] = al
inc ebx
.endw
invoke WriteFile, [fOut], offset [sPlus], ebx, offset [t], NULL
dec ebx
invoke WriteFile, [fOut], offset [sLeft], 2, offset [t], NULL
movzx ax, [oldch]
sub al, [char]
div bl
mov cl, bl
shr cl, 1
.if ah cl
sub bl, ah
movzx ebx, bl
inc al
movzx eax, al
push NULL
push offset [t]
push ebx
push offset [sPlus] + 1
push [fOut]
invoke WriteFile, [fOut], offset [sSubs], eax, offset [t], NULL
invoke WriteFile, [fOut], offset [sRight], 4, offset [t], NULL
call WriteFile
.else
push eax
movzx eax, al
invoke WriteFile, [fOut], offset [sSubs], eax, offset [t], NULL
invoke WriteFile, [fOut], offset [sRight], 4, offset [t], NULL
pop eax
movzx eax, ah
invoke WriteFile, [fOut], offset [sSubs], eax, offset [t], NULL
.endif
invoke WriteFile, [fOut], offset [cFullStop], 1, offset [t], NULL
.endif
mov al, [char]
mov [oldch], al
.endif
L1:
dec [TextLen]
.endw
invoke ExitProcess, 0
end Start
这些 BrainFuck 程序及其源代码可以在
http://groups.google.com/group/MasterRay
下载
楼主 2016-01-15 17:26 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知