Brainfuck?

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Brainfuck?

Post by Bobalandi »

Just wondering, but has anyone ported it to their OS, is it even possible? Just curious.
NULL
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Yes DexOS as a Brainfuck port, its the easiest to port out of all the languages

Note they can be very small, example of a 128byte dos BrainFuck compile.

Code: Select all

; bf.asm - Tiny BrainFuck compiler (128 bytes)
;
; Robert Oestling
; http://www.robos.org/

; nasm -f bin bf.asm -o bf.com
; bf <file.bf >file.com
;

; The length of the output file is fixed, programs larger than 19201 bytes
; will NOT run very well. Programs using more than 510 bytes of memory
; niether. Maximum length of jumps is about 128 bytes. Unlike certain other
; implementations, I do check for EOF. 8086 compatible code (I hope).
;
; Here are some other more or less tiny brainfuck compilers:
;
; Name              Size  Comments
; ==========================================================================
; Jeffry Johnston   123   '@' is EOF. Cheater. 16-bit jumps.
; INT-E             136   16-bit jumps, variable output size. 386+ - Cheater.
; Ben Olmstead      331   16-bit jumps (I think). 286+
; ==========================================================================


[section .text]

        org     0x100

main:
        mov     di,code_buffer+6        ; Where the compiled code goes.
.read_byte:
        xor     bx,bx                   ; stdin.
        mov     ah,0x3f                 ; Read from file.
        mov     cl,1                    ; One byte.
        mov     dx,di                   ; Right after the last compiled data
        int     0x21
        inc     bx                      ; stdout.
        dec     ax                      ; Not EOF? (i.e. one byte read?)
        jz      .not_eof
        mov     al,0xc3                 ; "ret"
        stosb                           ; Compile it.
        mov     ah,0x40                 ; Write to file.
        mov     dl,0x7a                 ; dx = code_buffer..
        ; Note, if anything doesn't work after you modified the file, check
        ; this number first!

        mov     ch,0x4b                 ; This byte means "dec bx". Ugly? :)
        int     0x21                    ; Write and quit.
        ret
.not_eof:
        cmp     byte[di],'['
        jz      .loop_start
        cmp     byte[di],']'
        jz      .loop_end
        mov     si,list                 ; Load instruction table into si.
.next_try:
        lodsw
        xchg    ax,cx                   ; cl = size, ch = instruction.
        lodsb                           ; al = address (low 8 bits).
        mov     ah,1                    ; High 8 bits of address always 1.
        or      ch,ch                   ; Last entry?
        jz      .read_byte              ; Yes, unknown instruction (comment).
        cmp     ch,[di]                 ; Is it the one we're looking for?
        jnz     .next_try               ; Nope, try again.
.found:
        xchg    ax,si                   ; Yes, address of code into si,
        mov     ch,0                    ; cx = cl (length of code).
        rep     movsb                   ; Copy code.
        jmp     short .read_byte

.loop_start:
        push    di                      ; Save current address.
        mov     al,0xeb
        stosw                           ; Compile short jump, address filled
        jmp     short .read_byte        ; in later.
.loop_end:
        pop     ax                      ; Get address to patch+maybe jump to.
        push    ax                      ; Save it again.
        mov     si,code_loop            ; Conditional jump code.
        movsw                           ; Copy first 2 bytes of it.
        sub     ax,di                   ; Calculate relative jump back.
        movsb                           ; Copy last byte of jump code.
        stosb                           ; Copy relative jump address.
        neg     al
        sub     al,4                    ; Relative jump _forward_ (for patch)
        pop     si                      ; si = address of jump to patch.
        mov     byte[si+bx],al          ; bx is always 1 here.
        jmp     short .read_byte

code_inc:
        inc     byte[bx]
code_dec:
        dec     byte[bx]
code_dot:
        mov     al,[bx]
        int     0x29

list:
        db      1,'>'
        db      0x0d
        db      1,'<'
        db      0x19
        db      2,'+'
        db      0x57
        db      2,'-'
        db      0x59
        db      4,'.'
        db      0x5b
        db      6,','
        db      0x71
code_comma:
        mov     ah,0x00                 ; 00 shows that the list is over.
        int     0x16
        mov     [bx],al
code_loop:
        cmp     [bx],ch                 ; ch is always 0 run-time.
        db      0x75                    ; jnz ...

code_buffer:                            ; Runtime initialization code:
        mov     bh,0x7f                 ; bx = 0x7f00 (middle of segment)
        mov     di,bx                   ; di = 0x7f00
        rep     stosw                   ; cx = 0x00ff, only 1/2kB is zeroed
Assembly is cool ;)
Last edited by Dex on Sun Dec 30, 2007 3:31 pm, edited 1 time in total.
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post by Bobalandi »

Oh? I didn't notice that when I looked, but I'll look again. Your OS is rather great by the way. :D
NULL
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Bobalandi wrote:Oh? I didn't notice that when I looked, but I'll look again. Your OS is rather great by the way. :D
Thanks Bobalandi :) .
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post by Bobalandi »

Ah, I found it. How long did it take you to port it to your OS? :)
NULL
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

I did not port it, it was a member of Team DexOS called Solidus117, i work 100% on the kernel and theres a Team of people who have add stuff.
This is because they all part own it, plus they can fit it in when they have spare time.
Note we do not only have coders, some member look after the forum, other translate
Doc etc.
Example www.dexos.cn
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post by Bobalandi »

Ah, got it.
NULL
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

The code for DexOS's brainfuck interpreter is here if it will help.
http://www.osdev.org/phpBB2/viewtopic.php?t=15794
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post by Bobalandi »

Thanks a bunch :D It's smaller than the one I have right now... hmmmm
NULL
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

May be it's a stupid question but may I ask
what is brainfuck?
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Its a minimalist programming language
http://en.wikipedia.org/wiki/Brainfuck
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post by Bobalandi »

Dex wrote:Its a minimalist programming language
http://en.wikipedia.org/wiki/Brainfuck
Very, Very Minimal :D
NULL
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

Thanx, I'm okay now...
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
Post Reply