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 |
||
movzbq (%rbx), %rax |
|||
incq %rbx |
incq %rbx |
||
movq %rbx, inst_ptr |
movq %rbx, inst_ptr |
||
decode: |
decode: |
||
#if DEBUGMODE |
|||
movb %al, debugChar |
|||
movq $1, %rdx |
|||
movq $debugChar, %rsi |
|||
movq STDERR, %rdi |
|||
movq SYS_WRITE, %rax |
|||
syscall |
|||
ERRCHECK -1 |
|||
movzbq debugChar, %rax |
|||
#endif |
|||
mov $branch_table, %rcx |
|||
jmp *(%rcx,%rax,8) |
|||
cmpb $'., %al |
|||
⚫ | |||
cmpb $',, %al |
|||
⚫ | |||
⚫ | |||
// execute: |
// execute: |
||
dp_left: |
dp_left: |
||
Line 138: | Line 131: | ||
dec_data: |
dec_data: |
||
movq data_ptr, %rbx |
movq data_ptr, %rbx |
||
decb (%rbx) |
|||
jmp fetch |
jmp fetch |
||
inc_data: |
inc_data: |
||
movq data_ptr, %rbx |
movq data_ptr, %rbx |
||
incb (%rbx) |
|||
jmp fetch |
jmp fetch |
||
out_data: |
out_data: |
||
Line 270: | Line 263: | ||
.data |
.data |
||
branch_table: |
|||
.rept 43 |
|||
⚫ | |||
.endr |
|||
.quad inc_data |
|||
⚫ | |||
.quad dec_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 |
.zero (1<<20) |
||
endtape: |
endtape: |
||
.zero 1 |
.zero 1 |