Execute Brain****/x86 Assembly: Difference between revisions

(Not quite right.)
Line 1:
==x86_64 Assembly==
 
{{incorrect|X86_Assembly|This example is not interpreted correctly.<pre>
>++++++++[-<+++++++++>]<.>[][<-]>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.
>->+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.
</pre>}}
 
GAS Syntax for Linux. Called it "Brainkrieg" after Teen Girl Squad. Operating principle is a little like a multi-cycle processor.
 
===Implementation (using jump table):===
 
<lang>// Assembly brainf*** interpreter
Line 34 ⟶ 29:
#define STDIN $0
#define STDOUT $1
#define STDERR $2
 
#define DEBUGMODE 0
 
.global _start
Line 104 ⟶ 102:
cmpq end, %rbx
ja shutdown // End of code
movb movzbq (%rbx), %alrax
incq %rbx
movq %rbx, inst_ptr
decode:
cmpb#if $'+, %alDEBUGMODE
je movb inc_data %al, debugChar
cmpb movq $'-1, %alrdx
je movq dec_data $debugChar, %rsi
cmpb $'>movq STDERR, %alrdi
je movq dp_right SYS_WRITE, %rax
cmpb $'<, %alsyscall
je ERRCHECK dp_left -1
cmpb $'[movzbq debugChar, %alrax
je brf#endif
cmpbmov $']branch_table, %alrcx
jejmp brb*(%rcx,%rax,8)
cmpb $'., %al
je out_data
cmpb $',, %al
je in_data
jmp fetch
// execute:
dp_left:
Line 138 ⟶ 131:
dec_data:
movq data_ptr, %rbx
decqdecb (%rbx)
jmp fetch
inc_data:
movq data_ptr, %rbx
incqincb (%rbx)
jmp fetch
out_data:
Line 270 ⟶ 263:
 
.data
branch_table:
.rept 43
jmp .quad fetch
.endr
.quad inc_data
je .quad in_data
.quad dec_data
je .quad out_data
.rept 13
.quad fetch
.endr
.quad dp_left
.quad fetch
.quad dp_right
.rept 28
.quad fetch
.endr
.quad brf
.quad fetch
.quad brb
.rept 162
.quad fetch
.endr
 
brack_smph: // Bracket matching semaphore
.quad 0
Line 275 ⟶ 298:
junkChar: // Also used to check if input was used.
.byte 0
 
#if DEBUGMODE
debugChar:
.byte 0
#endif
 
err_val:
.byte 0
// fstat:
statstruct: // This struct is 144 bytes. Only want size (+48)
.zero FSIZEOFF
Line 297 ⟶ 326:
.quad 0
tape:
.zero 30000(1<<20)
endtape:
.zero 1