From a3db0d510a157a2379abc6d056fb57994955c09b Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Thu, 14 Aug 2014 02:41:56 +0100 Subject: [PATCH] Support "[[" --- zx-brainfuck/main.asm | 50 +++++++++++++++++++++++++++++++----------- zx-brainfuck/main.tap | Bin 422 -> 493 bytes 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/zx-brainfuck/main.asm b/zx-brainfuck/main.asm index 9ed234f..42380da 100644 --- a/zx-brainfuck/main.asm +++ b/zx-brainfuck/main.asm @@ -4,23 +4,31 @@ tv_flag equ $5c3c ; TV flags variable last_k equ $5c08 ; Last pressed key ; Brainfuck Operators -OP_INC_DP equ ">" -OP_DEC_DP equ "<" -OP_INC_VAL equ "+" -OP_DEC_VAL equ "-" -OP_OUT equ "." -OP_IN equ "," -OP_JMP_FWD equ "[" -OP_JMP_BCK equ "]" +OP_INC_DP equ ">" ; $3e - 62 +OP_DEC_DP equ "<" ; $3c - 60 +OP_INC_VAL equ "+" ; $2b - 43 +OP_DEC_VAL equ "-" ; $2d - 45 +OP_OUT equ "." ; $2e - 46 +OP_IN equ "," ; $2c - 44 +OP_JMP_FWD equ "[" ; $5b - 91 +OP_JMP_BCK equ "]" ; $5d - 93 -; Brainfuck source +; Hello World ;brainfuck db "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", 0 -brainfuck db "++++[>++++++++++<-]>++.>+++++++++++++.<<++[>.<-]>>.<<+++[>.<-]>>.<<++++[>.<-]>>.<<+++++[>.<-]>>.", 0 +; Arvorezinha +;brainfuck db "++++[>++++++++++<-]>++.>+++++++++++++.<<++[>.<-]>>.<<+++[>.<-]>>.<<++++[>.<-]>>.<<+++++[>.<-]>>.", 0 +; To test the "," operator +;brainfuck db "++++++++++>+[,<.>.]", 0 +; Bad Boy Hello world +brainfuck db ">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.", 0 ; Variables memory_pos db $0,$80 ; Current memory position ; Contains the address on where the BF memory begins source_pos db $0,$0 ; Current source position +branch_count db $0 +;lookup_tbl db F_INC_DP,F_INC_DP+1, F_DEC_DP, F_DEC_DP+1 + start xor a ; a = 0 @@ -31,7 +39,7 @@ main ld hl, brainfuck ; Get source first position ld de, (source_pos) ; add hl, de ; First position + current position - + ld a, (hl) ; Read opcode from source ; EOF @@ -74,6 +82,7 @@ continue ld de, (source_pos) ; Increment source position inc de ld (source_pos), de + jr main ; Do it again end_main @@ -148,7 +157,7 @@ F_IN_LOOP F_JMP_FWD ld de, (memory_pos) ; If the value at current memory ld a, (de) ; position is 0, skip until the next - cp 0 ; "]" + cp $0 ; "]" jr z, SKIP_LOOP ld de, (source_pos) ; Save the "[" source position on the @@ -157,6 +166,10 @@ F_JMP_FWD ; Increments the source position until a "]" is found SKIP_LOOP + ld a, (branch_count) ; Increment the number of branches + inc a + ld (branch_count), a +SKIP_LOOP_2 ld de, (source_pos) ; Increment source position inc de ld (source_pos), de @@ -164,8 +177,19 @@ SKIP_LOOP ld hl, brainfuck ; String first position add hl, de ; First position + current position ld a, (hl) ; Read opcode from source + + cp OP_JMP_FWD ; Is it a "[" ? + jr z, F_JMP_FWD ; Do it again + cp OP_JMP_BCK ; Is it a "]"? - jr nz, SKIP_LOOP ; No? Then increment again + jr nz, SKIP_LOOP_2 ; Repeat until found + + ld a, (branch_count) ; If the number of branches is not 0 + dec a ; we need to find the next "]" + ld (branch_count), a + cp $0 + jr nz, SKIP_LOOP_2 + jp continue ; ------------------------------------- diff --git a/zx-brainfuck/main.tap b/zx-brainfuck/main.tap index 398cce600af9a445eadc58b48b0af9a7cf791464..7e7bf8b7fb2ada083cade79b068eff06ccba91d7 100644 GIT binary patch literal 493 zcmYk2&q~8U5XPsdPzve63Tg`y3ql~xwoPfFWxLlRc=NakSkQyuKcu@y>4W$VeT9Zl z!S&=pJ$hG)2fg&**4Z>wzmUwAncwUTTL6Hw%l`4nwTj{Sp?5B?IbyY01WXaMm`NDyEQT8vDm)R;50N-mef>jOYN#XsXbMdny9wa6?Gsr zS4UEZ>QTUo literal 422 zcmZ8by-LGi6g@?SP)G+Os4YnBAc3}Nnoz+KyB240N-`955d1^D8KtjKN*5o&r7z%6 zDGq*%IEaf6;8I<>bnuJ!tHqX{;d0NpKj#<#&MuqBC)XyUfixP(9Uwtk$B(gC6PluJkxbHa?r^2HyUp*MW(s}dr!oF!8`)bQpL#|8~*dm>Apat|?KYZGDXc%;8^H#$0WLt%?>-wa^)xw$gR%IIzKVQR>6Icj8nvG}SI4DM*>7XYD^@Bjb+