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

Content added Content deleted
(Not quite right.)
Line 1: Line 1:
==x86_64 Assembly==
==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.
GAS Syntax for Linux. Called it "Brainkrieg" after Teen Girl Squad. Operating principle is a little like a multi-cycle processor.


===Implementation:===
===Implementation (using jump table):===


<lang>// Assembly brainf*** interpreter
<lang>// Assembly brainf*** interpreter
Line 34: Line 29:
#define STDIN $0
#define STDIN $0
#define STDOUT $1
#define STDOUT $1
#define STDERR $2

#define DEBUGMODE 0


.global _start
.global _start
Line 104: Line 102:
cmpq end, %rbx
cmpq end, %rbx
ja shutdown // End of code
ja shutdown // End of code
movb (%rbx), %al
movzbq (%rbx), %rax
incq %rbx
incq %rbx
movq %rbx, inst_ptr
movq %rbx, inst_ptr
decode:
decode:
cmpb $'+, %al
#if DEBUGMODE
je inc_data
movb %al, debugChar
cmpb $'-, %al
movq $1, %rdx
je dec_data
movq $debugChar, %rsi
cmpb $'>, %al
movq STDERR, %rdi
je dp_right
movq SYS_WRITE, %rax
cmpb $'<, %al
syscall
je dp_left
ERRCHECK -1
cmpb $'[, %al
movzbq debugChar, %rax
je brf
#endif
cmpb $'], %al
mov $branch_table, %rcx
je brb
jmp *(%rcx,%rax,8)
cmpb $'., %al
je out_data
cmpb $',, %al
je in_data
jmp fetch
// execute:
// execute:
dp_left:
dp_left:
Line 138: Line 131:
dec_data:
dec_data:
movq data_ptr, %rbx
movq data_ptr, %rbx
decq (%rbx)
decb (%rbx)
jmp fetch
jmp fetch
inc_data:
inc_data:
movq data_ptr, %rbx
movq data_ptr, %rbx
incq (%rbx)
incb (%rbx)
jmp fetch
jmp fetch
out_data:
out_data:
Line 270: Line 263:


.data
.data
branch_table:
.rept 43
.quad fetch
.endr
.quad inc_data
.quad in_data
.quad dec_data
.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
brack_smph: // Bracket matching semaphore
.quad 0
.quad 0
Line 275: Line 298:
junkChar: // Also used to check if input was used.
junkChar: // Also used to check if input was used.
.byte 0
.byte 0

#if DEBUGMODE
debugChar:
.byte 0
#endif


err_val:
err_val:
.byte 0
.byte 0
// fstat:
statstruct: // This struct is 144 bytes. Only want size (+48)
statstruct: // This struct is 144 bytes. Only want size (+48)
.zero FSIZEOFF
.zero FSIZEOFF
Line 297: Line 326:
.quad 0
.quad 0
tape:
tape:
.zero 30000
.zero (1<<20)
endtape:
endtape:
.zero 1
.zero 1