From e9309a7fb5a8f19d8a4908d8b506a1f304ae5067 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Wed, 13 Aug 2014 02:02:21 +0100 Subject: [PATCH] Document in english and optimize --- zx-brainfuck/main.asm | 117 ++++++++++++++++++++++-------------------- zx-brainfuck/main.tap | Bin 446 -> 422 bytes 2 files changed, 60 insertions(+), 57 deletions(-) diff --git a/zx-brainfuck/main.asm b/zx-brainfuck/main.asm index 4ce6e3d..9ed234f 100644 --- a/zx-brainfuck/main.asm +++ b/zx-brainfuck/main.asm @@ -1,8 +1,9 @@ org 30000 -tv_flag equ $5c3c ; Variavel das flags da tv -last_k equ $5c08 ; Contem a ultima tecla pressionada +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 "+" @@ -12,26 +13,30 @@ OP_IN equ "," OP_JMP_FWD equ "[" OP_JMP_BCK equ "]" +; Brainfuck source ;brainfuck db "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", 0 brainfuck db "++++[>++++++++++<-]>++.>+++++++++++++.<<++[>.<-]>>.<<+++[>.<-]>>.<<++++[>.<-]>>.<<+++++[>.<-]>>.", 0 -memory_pos db $0,$80 -source_pos db $0,$0 -start ; Começa em $75a2 - xor a ; O mesmo que LD a, 0 - ld (tv_flag), a ; Faz com que o rst $10 envie output pra tv - push bc ; Guarda BC na stack +; 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 + +start + xor a ; a = 0 + ld (tv_flag), a ; Enables rst $10 output to the TV + push bc ; Save BC on the stack main - ld hl, brainfuck - ld de, (source_pos) - - add hl, de - ld a, (hl) + 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 - cp $0 - jr z, end_main + cp $0 ; End of file + jr z, end_main ; Jump to the end ; > cp OP_INC_DP @@ -66,19 +71,19 @@ main jp z, F_JMP_BCK continue - ld de, (source_pos) + ld de, (source_pos) ; Increment source position inc de ld (source_pos), de - jr main + jr main ; Do it again end_main - pop bc ; Tira o BC da stack - ret ; Sai para o BASIC + pop bc ; Get BC out of the stack + ret ; Exit to BASIC ; ------------------------------------- F_INC_DP - ld de, (memory_pos) + ld de, (memory_pos) ; Increment memory position inc de ld (memory_pos), de jp continue @@ -86,7 +91,7 @@ F_INC_DP ; ------------------------------------- F_DEC_DP - ld de, (memory_pos) + ld de, (memory_pos) ; Decrement memory position dec de ld (memory_pos), de jp continue @@ -94,8 +99,8 @@ F_DEC_DP ; ------------------------------------- F_INC_VAL - ld de, (memory_pos) - ld a, (de) + ld de, (memory_pos) ; Increment value at the current + ld a, (de) ; memory position inc a ld (de), a jp continue @@ -103,8 +108,8 @@ F_INC_VAL ; ------------------------------------- F_DEC_VAL - ld de, (memory_pos) - ld a, (de) + ld de, (memory_pos) ; Decrement value at the current + ld a, (de) ; memory position dec a ld (de), a jp continue @@ -112,15 +117,16 @@ F_DEC_VAL ; ------------------------------------- F_OUT - ld de, (memory_pos) - ld a, (de) + ld de, (memory_pos) ; Print value at the current + ld a, (de) ; memory position cp $a - jr z, F_OUT_NEWLINE + jr z, F_OUT_FIX_NEWLINE ; Is it a $a ? Fix it! rst $10 jp continue -; $a is a new line on the PC but not on the Spectrum, use $d instead -F_OUT_NEWLINE +; $a is a NEWLINE on the PC but not on the Spectrum, use +; $d instead +F_OUT_FIX_NEWLINE ld a, $d rst $10 jp continue @@ -128,50 +134,47 @@ F_OUT_NEWLINE F_IN ld a, $0 - ld (last_k), a ; Limpa o valor da ultima tecla pressionada + ld (last_k), a ; Clear last pressed key F_IN_LOOP - ld a, (last_k) ; Se o valor da ultima tecla pressionada ainda - cp $0 ; for 0, é porque ainda não se pressionou nenhuma - jr z, F_IN_LOOP ; tecla, por isso... repete - ld de, (memory_pos) - ld (de), a + ld a, (last_k) ; If the value is still 0, repeat + cp $0 + jr z, F_IN_LOOP + ld de, (memory_pos) ; Set the read value at the current + ld (de), a ; memory position jp continue ; ------------------------------------- F_JMP_FWD - ld de, (memory_pos) - ld a, (de) - cp 0 + ld de, (memory_pos) ; If the value at current memory + ld a, (de) ; position is 0, skip until the next + cp 0 ; "]" jr z, SKIP_LOOP - ld de, (source_pos) - push de + ld de, (source_pos) ; Save the "[" source position on the + push de ; stack, and continue to next instruction jp continue - + +; Increments the source position until a "]" is found SKIP_LOOP - ld de, (source_pos) + ld de, (source_pos) ; Increment source position inc de ld (source_pos), de - ld hl, brainfuck - - add hl, de - ld a, (hl) - cp OP_JMP_BCK - jr nz, SKIP_LOOP - - ld de, (source_pos) - inc de - ld (source_pos), de - jp main + ld hl, brainfuck ; String first position + add hl, de ; First position + current position + ld a, (hl) ; Read opcode from source + cp OP_JMP_BCK ; Is it a "]"? + jr nz, SKIP_LOOP ; No? Then increment again + jp continue ; ------------------------------------- F_JMP_BCK - pop de - ld (source_pos), de - jp main + pop de ; Set the source_position as the last + ld (source_pos), de ; "[" position saved on the stack + jp main ; Jump to main so that it doesn't increment + ; the source_position ; ------------------------------------- diff --git a/zx-brainfuck/main.tap b/zx-brainfuck/main.tap index e3d594faa168babebdcf4b5f48001e5dbe7a3f9c..398cce600af9a445eadc58b48b0af9a7cf791464 100644 GIT binary patch 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+ literal 446 zcmYjLu}T9$6r7+!2wVyyL=%uR)#2sV%Y_6C=XMgTE$>nUv=EFDaj%s0{zM2iR{4c! zuy8vIvGWHc+6a=ujc?CObc%iR=DpcBIRK~K_R;Z$NoXR2CW;4`A!Fd%+a+Xy9RUMt zd3<`+dc`H75u)tXYQ$!s(dG(1N~NIzD~su<>Q*Xs8r_iVItMIu+9zl1vVCrv`zcqT zci&9S>;(dz(0Qd+Q*QeNr$wA`n