Page 1 of 1

Brainfuck interpreter

Posted: Mon Dec 31, 2007 5:09 pm
by matias_beretta
I moved my topic to this place because it was in a wrong place.

I've coded my interpreter, here is the code:

Code: Select all

org 100h

include 'if.inc'

mov si, a
mov di, b

.while byte [si] <> 0
   .if byte [si] = '>'
      inc di
      inc si
   .elseif byte [si] = '<'
      dec di
      inc si
   .elseif byte [si] = '+'
      inc byte [di]
      inc si
   .elseif byte [si] = '-'
      dec byte [di]
      inc si
   .elseif byte [si] = '.'
      mov ah, 0eh
      mov al, [di]
      int 10h
      inc si
   .elseif byte [si] = ','
      mov ah, 0h
      int 16h
      mov [di], al
      inc si
   .elseif byte [si] = '['
      .if byte [di] = 0
	 .while byte [si] <> ']'
	    inc si
	 .endw
	 inc si
      .else
	 push si
	 inc si
      .endif
   .elseif byte [si] = ']'
      pop si
   .endif
.endw

ret

a:
   db "++++++[<,.>-],", 0
b:
   times 30000 db 0
I used IF.INC (from FASM) because of simplicity.