Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  1. Build the compiler

     $ c++ -o bfc bfc.cpp
    
  2. Compile a brainfuck program (exampleexample) to C

     $ ./bfc ascii.bf
    
  3. Compile the resulting C program to an executable.

     $ cc -g -O0 -o ascii ascii.bf.c
    
  4. Run the executable natively.

     $ ./ascii
    

    Or, you can also run it in a debugger. As long as you compiled with debugging symbols included and without optimizations, you should be able to step through the brainfuck source code line by line.

     $ gdb ascii
     [...]
     (gdb) break ascii.bf:1
     Breakpoint 1 at 0x1000010fc: file ascii.bf, line 1.
     (gdb) run
     [...]
     Breakpoint 1, main (argc=1, argv=0x7fff5fbffbd0) at ascii2.bf:1
     1   [-][
     (gdb) break ascii.bf:66
     Breakpoint 2 at 0x1000031c3: file ascii.bf, line 66.
     (gdb) cont
     Continuing.
    
     Breakpoint 2, main (argc=1, argv=0x7fff5fbffbd0) at ascii.bf:66
     66      >>>>> >>>>> >>> .                   (c_ascii)
     (gdb) display m.ptr
     1: m.ptr = 4
     Current language:  auto; currently minimal
     (gdb) display /d m.mem[0] @ 32
     2: /d m.mem[0] @ 32 = {0, 58, 32, 10, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 0, 0}
     (gdb) display /d m.mem[m.ptr]
     3: /d m.mem[m.ptr] = 0
    
  1. Build the compiler

     $ c++ -o bfc bfc.cpp
    
  2. Compile a brainfuck program (example) to C

     $ ./bfc ascii.bf
    
  3. Compile the resulting C program to an executable.

     $ cc -g -O0 -o ascii ascii.bf.c
    
  4. Run the executable natively.

     $ ./ascii
    

    Or, you can also run it in a debugger. As long as you compiled with debugging symbols included and without optimizations, you should be able to step through the brainfuck source code line by line.

     $ gdb ascii
     [...]
     (gdb) break ascii.bf:1
     Breakpoint 1 at 0x1000010fc: file ascii.bf, line 1.
     (gdb) run
     [...]
     Breakpoint 1, main (argc=1, argv=0x7fff5fbffbd0) at ascii2.bf:1
     1   [-][
     (gdb) break ascii.bf:66
     Breakpoint 2 at 0x1000031c3: file ascii.bf, line 66.
     (gdb) cont
     Continuing.
    
     Breakpoint 2, main (argc=1, argv=0x7fff5fbffbd0) at ascii.bf:66
     66      >>>>> >>>>> >>> .                   (c_ascii)
     (gdb) display m.ptr
     1: m.ptr = 4
     Current language:  auto; currently minimal
     (gdb) display /d m.mem[0] @ 32
     2: /d m.mem[0] @ 32 = {0, 58, 32, 10, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 0, 0}
     (gdb) display /d m.mem[m.ptr]
     3: /d m.mem[m.ptr] = 0
    
  1. Build the compiler

     $ c++ -o bfc bfc.cpp
    
  2. Compile a brainfuck program (example) to C

     $ ./bfc ascii.bf
    
  3. Compile the resulting C program to an executable.

     $ cc -g -O0 -o ascii ascii.bf.c
    
  4. Run the executable natively.

     $ ./ascii
    

    Or, you can also run it in a debugger. As long as you compiled with debugging symbols included and without optimizations, you should be able to step through the brainfuck source code line by line.

     $ gdb ascii
     [...]
     (gdb) break ascii.bf:1
     Breakpoint 1 at 0x1000010fc: file ascii.bf, line 1.
     (gdb) run
     [...]
     Breakpoint 1, main (argc=1, argv=0x7fff5fbffbd0) at ascii2.bf:1
     1   [-][
     (gdb) break ascii.bf:66
     Breakpoint 2 at 0x1000031c3: file ascii.bf, line 66.
     (gdb) cont
     Continuing.
    
     Breakpoint 2, main (argc=1, argv=0x7fff5fbffbd0) at ascii.bf:66
     66      >>>>> >>>>> >>> .                   (c_ascii)
     (gdb) display m.ptr
     1: m.ptr = 4
     Current language:  auto; currently minimal
     (gdb) display /d m.mem[0] @ 32
     2: /d m.mem[0] @ 32 = {0, 58, 32, 10, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 1, 0, 0, 0, 58, 48, 0, 0}
     (gdb) display /d m.mem[m.ptr]
     3: /d m.mem[m.ptr] = 0
    
added 29 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I'd like to know, how well does this technique work for debuggers other than GDB? Is there anything I should do to make either the compiler source code or the generated output more readable? How should I improve the error handling? Are there any other improvements you might suggest?:

  1. How well does this technique work for debuggers other than GDB?
  2. Is there anything I should do to make either the compiler source code or the generated output more readable?
  3. How should I improve the error handling?
  4. Are there any other improvements you might suggest?

I'd like to know, how well does this technique work for debuggers other than GDB? Is there anything I should do to make either the compiler source code or the generated output more readable? How should I improve the error handling? Are there any other improvements you might suggest?

I'd like to know:

  1. How well does this technique work for debuggers other than GDB?
  2. Is there anything I should do to make either the compiler source code or the generated output more readable?
  3. How should I improve the error handling?
  4. Are there any other improvements you might suggest?
Tweeted twitter.com/#!/StackCodeReview/status/423572699420844034
how to use the debugger
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading