Thread: Need ASM to make BF compiler...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Need ASM to make BF compiler...

    hello,
    duno where to put this thread, so i'm putting this here.

    i need some basic asm knowledge and a minimalistic compiler in order to create a simple BF compiler. so far, i've done it in c#, but that's not the way to go...

    i just need a console with simple i/o written in ASM.
    i've tried MASM and NASM without any results as there are no tutorials, especially no ones with console i/o

    what i need is simple character input & output and a small compiler with commandline.

    thanks.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It would make more sense to write it in C and use inline assembly where necessary. However, I doubt assembly is necessary at all.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    well i could compile it to C code, but how do i compile C code?

    i could generate the C code myself, but i would need a C compiler which I can call easily and also redistribute...
    Last edited by Devils Child; 03-28-2010 at 08:46 AM.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    i found one. "tcc" seems to be a good compiler. it seems to be working.
    thanks.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You can call C standard library functions (printf, scanf, etc) from asm. Just link it to some driver C code.

    driver.c
    Code:
    int asm_main();
    
    int main() {
         return asm_main();
    }
    Then you can write the asm_main (or _asm_main, depending on your OS) function in assembly, assemble into an object file (say asm_main.o), then

    Code:
    gcc -o my_program driver.c asm_main.o
    Then you can use all C standard library functions, as long as you follow the calling convention.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Or just link the object file generated by assembler against -lc. You don't have to add some useless C code. If you use assembler used by your C compiler, you can even use your C compiler driver to compile your assembly and call linker for you with right flags.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Are you sure that will work? Doesn't the C compiler insert things into beginning of main? (that's why main cannot be called by your own code)

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    so far, i'm generating C code and compiling it with the tcc compiler.
    there's one thing left to do: how do I assign an icon the the exe file? are there any linkers? can tcc do it?

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by cyberfish View Post
    Are you sure that will work? Doesn't the C compiler insert things into beginning of main? (that's why main cannot be called by your own code)
    Yes, when program written in C is started, execution starts at entry point, _start by default among elven people, which gets argc and argv and environment and calls main with them. It doesn't do anything magical needed by -lc. If you need your command line arguments, you can fetch them, or call your main function main, link appropriate files (or let your C compiler do this) and let crt do the fetching. However, at least on *BSD you need to have symbols environ and __progname, don't remember about Solaris, haven't done anything like this on other operating systems (and only on SPARC).

    And I just realised I was stupid and didn't remember you could also assemble with your favourite assembler and use your C compiler only for linking.

  10. #10
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Wow, funny thing really, I was just looking at BF programs today (I do suppose you're talking about Brainf***, right?). And the whole point of BF was to make the smallest compiler for a turing-complete language possible - the smallest one I've seen is ~160 bytes. That's why all the BF compilers are in ASM.

    there's one thing left to do: how do I assign an icon the the exe file? are there any linkers? can tcc do it?
    I'll assume you're using windows since you asked - in linux you can set icons through the file manager, and mac... well, I don't know.

    You'll have to learn the Windows API, and how to use resources, and... well, it's a lot of work just for that little icon.
    Consider this post signed

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    well, it's a lot of work just for that little.....
    that's why i'm coding in C# and not in C++. you're not able to acomplish little things you could do in C# with a few lines of code.

    i already had that kind of problem with linked resourced. i asked in this forum. i managed to add a binary resource file for one use, but for the other purpose, i wasn't able to add an icon...


    btw: i'm calling my derivate "Brain........++"

    HelloWorld:
    Code:
    #inc <stdio.bf>
    
    // This function prints hello world
    _hello
      [-]>[-]>[-]>[-]>[-]<<<<++++++++++[
      >+++++++>++++++++++>+++>+<<<<-]>++
      .>+.+++++++..+++.>++.<<+++++++++++
      ++++.>.+++.------.--------.>+.>.
    _ret
    
    // Main entry point
    _main
      //Print ASCII characters
      [-]+[.+]
    
      //Hello world
      //Also call the "br" function from stdio.bf for line breaks
      (br)(br)(br)(hello)(br)(br)
    
      //Now decimal numbers using stdio functions
      [-]+[(dec)+>[-]++++++++++++++++++++++++++++++++.<]
    _ret
    it's downloadable here: http://www.dev-ch.de/upload/files/De...d/bfpp0.92.zip

  12. #12
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    that's why i'm coding in C# and not in C++. you're not able to acomplish little things you could do in C# with a few lines of code.
    Oh, sorry - the string of previous posts made me think you were making the compiler in C rather than using a C#-written program to convert BF to C.

    So I suppose the problem here is that you need to set an icon for the produced binary (the one you coded in BF, converted to C, and compiled with TCC) and not for the compiler. I still don't know any way to do that other than including <windows.h> and making a resource file.
    Consider this post signed

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    in previous versions, i used to compile BF code to C# code. the C# compiler is simple and redistributed with every .NET framework. setting an icon is not too hard, but now that i'm using C as language to convert to, it's hard...
    what do you mean by include windows.h and making a res file? can i accomplish that using my compiler?

  14. #14
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Ok, turns out you don't have to #include <windows.h>, my mistake. Here is an explanation of how - all it involves is creating a .rc file, which compiles (with a "resource compiler") and then you link to the compiled resource file.

    There's a resource compiler here.

    Unfortunately tcc doesn't support linking to .res files. However, it appears as though the compiler I linked to can make .o files from the .rc's; that should be no problem for tcc. However, I'm really shooting in the dark - I haven't tried this myself. But at least it's a start.
    Consider this post signed

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    ok. i created the .rc file with resource hacker, the .res and .obj file with that resource compiler and how tried to link it to my exe using the linker from their site

    http://www.dev-ch.de/upload/files/De.../Screen340.jpg

    it created an executable which is not working or somehow crappy...
    i guess i could use that linker for my compiler, but how?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  2. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  3. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM
  4. help me about make a compiler
    By Sither in forum C Programming
    Replies: 2
    Last Post: 09-09-2003, 02:40 AM
  5. Visual Basic compiler or whatever used to make vb progs...
    By Xmevs in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-22-2002, 03:01 AM