|
@@ -1,4 +1,4 @@
|
1
|
|
-; bfc : a brainfuck compiler & interpreter
|
|
1
|
+; bfc : a brainfuck compiler
|
2
|
2
|
; Copyright (C) 2018 Weber Yann
|
3
|
3
|
;
|
4
|
4
|
; This program is free software; you can redistribute it and/or modify
|
|
@@ -14,14 +14,13 @@
|
14
|
14
|
; You should have received a copy of the GNU General Public License
|
15
|
15
|
; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
16
|
;
|
17
|
|
-; A brainfuck compiler & interpreter :
|
|
17
|
+; A brainfuck compiler :
|
18
|
18
|
; Build : nasm -felf64 bfc.asm && ld -s -melf_x86_64 bfc.o -o bfc
|
19
|
19
|
;
|
20
|
|
-; ./bfc [-h] [-e [-o a.out]] FILE.bf
|
|
20
|
+; ./bfc [-h] [-o a.out] FILE.bf
|
21
|
21
|
; Options :
|
22
|
22
|
; -h print usage and exit
|
23
|
|
-; -e tell bfc to produce a elf file
|
24
|
|
-; -o with -e indicate the file to create
|
|
23
|
+; -o indicate the file to create default is a.out
|
25
|
24
|
; FILE.bf the brainfuck source file to compile
|
26
|
25
|
[bits 64]
|
27
|
26
|
%use smartalign
|
|
@@ -323,7 +322,7 @@ section .data
|
323
|
322
|
chr_list : db ": ", 0xA, 0x0
|
324
|
323
|
read_error: db "Error reading file "
|
325
|
324
|
read_error_sz: equ $ - read_error
|
326
|
|
- usage_err: db "Usage : [-e [-o a.out]] FILE.BF"
|
|
325
|
+ usage_err: db "Usage : [-o a.out] FILE.BF"
|
327
|
326
|
usage_err_sz: equ $ - usage_err
|
328
|
327
|
open_err: db "Error opening file", 0xa
|
329
|
328
|
open_err_sz: equ $ - open_err
|
|
@@ -334,9 +333,9 @@ _start:
|
334
|
333
|
; using heap to store arguments
|
335
|
334
|
%define bf_source [r13]
|
336
|
335
|
%define elf_file [r13+0x8]
|
337
|
|
-%define elf_out [r13+0x11]
|
338
|
|
-%define heap_size 0x12
|
|
336
|
+%define heap_size 0x10
|
339
|
337
|
|
|
338
|
+ ;heap init
|
340
|
339
|
mov rax, 0xc
|
341
|
340
|
xor rdi, rdi
|
342
|
341
|
syscall
|
|
@@ -346,12 +345,15 @@ _start:
|
346
|
345
|
mov rax, 0xc
|
347
|
346
|
syscall
|
348
|
347
|
|
|
348
|
+ mov rax, default_output
|
|
349
|
+ mov elf_file, rax
|
|
350
|
+
|
349
|
351
|
;argument parsing
|
350
|
352
|
mov rcx, [rsp] ; argc
|
351
|
353
|
cmp rcx, 2
|
352
|
354
|
jl .badarg
|
353
|
|
- je .init_1arg
|
354
|
|
- cmp rcx, 5
|
|
355
|
+ ;je .init_1arg
|
|
356
|
+ cmp rcx, 4
|
355
|
357
|
jg .badarg
|
356
|
358
|
|
357
|
359
|
mov rsi, rsp
|
|
@@ -370,8 +372,6 @@ _start:
|
370
|
372
|
mov al, [rdi+1]
|
371
|
373
|
cmp al, 0x68 ; '-h'
|
372
|
374
|
je .badarg
|
373
|
|
- cmp al, 0x65 ; '-e'
|
374
|
|
- je .elfout_arg
|
375
|
375
|
cmp al, 0x6f ; '-o'
|
376
|
376
|
jne .badarg
|
377
|
377
|
|
|
@@ -385,20 +385,6 @@ _start:
|
385
|
385
|
loop .argloop
|
386
|
386
|
jmp .init
|
387
|
387
|
|
388
|
|
- .elfout_arg:
|
389
|
|
- mov al, 0x1
|
390
|
|
- mov elf_out, al
|
391
|
|
- mov rax, elf_file
|
392
|
|
- test rax, rax
|
393
|
|
- jz .default_out
|
394
|
|
- .elfout_arg_end:
|
395
|
|
- loop .argloop
|
396
|
|
- jmp .init
|
397
|
|
- .default_out:
|
398
|
|
- mov rax, default_output
|
399
|
|
- mov elf_file, rax
|
400
|
|
- jmp .elfout_arg_end
|
401
|
|
-
|
402
|
388
|
.filearg:
|
403
|
389
|
mov rax, bf_source
|
404
|
390
|
cmp rax, 0
|
|
@@ -406,11 +392,7 @@ _start:
|
406
|
392
|
jnz .badarg ; file allready given
|
407
|
393
|
mov bf_source, rdi
|
408
|
394
|
loop .argloop
|
409
|
|
- jmp .init
|
410
|
|
-
|
411
|
|
- .init_1arg:
|
412
|
|
- mov rax, [rsp+16]
|
413
|
|
- mov bf_source, rax
|
|
395
|
+ jmp .init ; useless
|
414
|
396
|
.init:
|
415
|
397
|
; code map init
|
416
|
398
|
; rsi map size
|
|
@@ -439,27 +421,7 @@ _start:
|
439
|
421
|
mov rax, 0x3 ; close
|
440
|
422
|
syscall
|
441
|
423
|
|
442
|
|
- mov al, elf_out
|
443
|
|
- test al, al
|
444
|
|
- jnz .write_elf
|
445
|
|
-
|
446
|
|
-
|
447
|
|
- .code_jmp:
|
448
|
|
- ; restore heap
|
449
|
|
- mov rax, 0xc
|
450
|
|
- mov rdi, r13
|
451
|
|
- syscall
|
452
|
|
- ; set code map perm
|
453
|
|
- mov rax, 0xA ; mprotect
|
454
|
|
- mov rdi, r15
|
455
|
|
- mov rsi, r14
|
456
|
|
- mov rdx, 0x4 | 0x1 ; PROT_EXEC | PROT_READ
|
457
|
|
- syscall
|
458
|
|
-
|
459
|
|
- push r15
|
460
|
|
- jmp r15 ; end... jumping in bf code map
|
461
|
|
-
|
462
|
|
- .write_elf: ; writing elf file
|
|
424
|
+ ; writing elf file
|
463
|
425
|
mov rax, [rsp] ; map len
|
464
|
426
|
mov [elf_section_text_sz], rax
|
465
|
427
|
add rax, elf_head_sz ; elf head + map_ptr
|