Thread: how to make an executable file by ourselves?

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    how to make an executable file by ourselves?

    hello all, im curious to know how i can make my own executable files! i mean i made a simple simulator ! with my own made language! pretty simple! sth like this
    Code:
    RED 1 
    WRI 1
    HLT
    and yet a simpler version
    Code:
    1010  //read Memory location 10
    1210 //write To Memory Location 10
    9000 //Halt the program execution
    well the program now works only inside the simulator(actually you run the simulator and then you write your program in my new language! and then you press 1 e.g and simulator interpret the codes and will do the calculations!
    now im seeking a way to make an executable file when the user tries to run the emulated code on simulator! so that, after that the user gets an executable program! and wont need the simulator anymore to run the new language codes! !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You have to compile it into machine language. So you could output C code and use a C compiler (which is easier, but hacky), or you could output the binary into a file, and give it executable permissions.

    There are tables available for converting from Assembly Language to binary (I myself never figured out how to put operands into binary, though), and with some fancy bitwise operators in C, you could output machine code fairly easily from that point.

  3. #3
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    There are tables available for converting from Assembly Language to binary
    Dealing with executable file formats is a pain though, especially when you deal with it directly in your compiler.

    I would suggest just emitting a .s file filled with assembly code and passing that to something like GCC or GNU as to get a binary. It'll deal with all the linking and whatnot for you so you don't have to, but of course, this requires that GCC/etc. is installed.
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Mad_guy View Post
    Dealing with executable file formats is a pain though, especially when you deal with it directly in your compiler.

    I would suggest just emitting a .s file filled with assembly code and passing that to something like GCC or GNU as to get a binary. It'll deal with all the linking and whatnot for you so you don't have to, but of course, this requires that GCC/etc. is installed.
    There's a wide variety of assemblers, no need to use GCC.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just a side comment, here: If you're going to use a 3rd party assembler, you might as well output code in the language with which you are most familiar, and just use THAT compiler.

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    thanks guys , but im a bit confused! how am i supposed to convert sth like
    Code:
    while ( cpu is running )
    {
                  switch ( Opcode )
                  {
                                 case RED:
                                 {
                                          int number;  
                                          cout<<"Enter a number";
                                          cin>>number;
                                  }
                                ~~~~
                    }
    }
    into machine language!! please note that i want sth corss platform ( C++) , so that on each platform that the original simulator is compiled and run the platform specific executable gets produced!

    talking about gcc and stuff! do i have to inject gcc compilers into my simple simulator! in order to be able to make executables ? so what are makefiles! though? ive heard they are for making executable files out there!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A makefile is really just a script for compiling your project. You still have to have gcc to compile individual files and do the linking.

    If you want something cross platform, you have to output the source code and then compile it. Unless you want to write your own compiler (if you don't know the answer to that, the answer is definitely 'no'), there is no option but to use someone else's compiler.

    You could of course output batch files or shell scripts, but that isn't very cross-platform either.

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    tanx sean / are those recompilation stuff that we face in emulation , done with the same approach?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Emulation / interpretation is normally done by interpreting the commands, and then performing the action in a program. Normally interpretation is done on the fly, and must be redone completely every time. You would use another compiler (as above) only if you wanted to save the actions in a certain executable format.

    The other option is JIT compiling, where something is compiled to "byte codes" (eg. .NET and Java) that are easy to interpret and execute, and then they are interpreted on the fly.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Can we please! Not end every sentence! With an exclamation point!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    The C compiler translates C code to assembler code. The assembler translates assembler code to object code. The linker translates object code to executables. The CPU translates executables to actions.

    So you have several choices:
    - translate from your language to C
    - translate from your language to assembler
    - translate from your language to object code
    - translate from your language to executables

    These choices are sorted by portability.


    For a completely different approach, there are virtual machines and/or interpreters: a virtual machine is a program that translates bytecode/object code to actions, an interpreter is a program that translates your language to actions directly.

    Writing a virtual machine is fairly easy (but you will have to write a program that translates your language to bytecode), writing interpreters is clumsy.


    I'd recommend writing a compiler that translates your language to assembler code, or writing a virtual machine.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  12. #12
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Writing a virtual machine is fairly easy
    Wow. One can hardly expect that statement from a complete beginner.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since it is YOUR simulator, you can arrang your executable file in whatever way you like. The simplest form would be a simple hex file:
    Code:
    1000: 00 AA 83 12 18
    2000: 12 34 56 78
    Where the first hex number is the address you want data loaded at, the second set is the bytes to be loaded at that address.
    In this example, five bytes are loaded at 0x1000, and then another 4 bytes as address 2000.

    "Real" executable formats are of generally more complex because they have to solve many more problems (such as the address to load the file may vary depending on circumstances, or there are DLL's and other external references to cover).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Here's another option: target LLVM. LLVM can be used as a toolset, or as libraries, so you can easily combine LLVM parts with your own code to create a complete compiler, or even a JITting VM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you use .NET you can use Microsoft.CSharp, System.CodeDom.Compiler and System.Reflection to compile C# (or VB) code on the fly. You enter code in the form of a string and get an assembly back which you can either execute immediately or save to a file.

    In your case you could write a compiler taking your own language and constructs C# code from it, then compile it using the above system.

    Read http://www.ziggyware.com/readarticle.php?article_id=98 for more info.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM