I' ve written some machine code: how do I execute it?

Started by
31 comments, last by GameDev.net 19 years, 1 month ago
Its more a question of "is it *theoretically* possible to write a text file with only zeros and ones, then change the extension in .exe and make it do something?"

Of course this has no practical use in programming anything, it just would be cool
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Advertisement
Quote:Original post by The Najdorf
Its more a question of "is it *theoretically* possible to write a text file with only zeros and ones, then change the extension in .exe and make it do something?"

Of course this has no practical use in programming anything, it just would be cool


No, a normal executable as Windows knows it has a special PE Header. You'll need to write the PE Header from the top of your head before adding any executable code.

Toolmaker

Quote:Original post by Toolmaker
No, a normal executable as Windows knows it has a special PE Header. You'll need to write the PE Header from the top of your head before adding any executable code.
Toolmaker


Ok, but if I knew the right PE header in binary form I could? I mean the question is "does there exist a sequence of 1s and 0s such that saved in a text file, and changed the extension, it does *something*?"
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Quote:Original post by The Najdorf
Ok, but if I knew the right PE header in 0101 form I could?


Yes. I remember a while back how you could create a very simple .com executable (7 bytes) just by typing in the sequence. However, 0's and 1's were not used, but rather the ascii characters alt + num. I have long lost that link though.
You know, you can't save some text representing a binary number to a file and expect anything to happen. It's just text, it isn't actual binary data. You need a hex editor for that kind of thing.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thx...

I found out that if I have an "exe", I can somewhat open it as a .txt file. But if I save it, then I cant get it back to work the in the .exe form... there seems to be some information loss...

Instead, if I open it in a hex editor, I see the actual machine code, right? just that it's not in binary but in "hex"...

Does any body know what happens when a .txt file is converted into an .exe and viceversa?

Oh... I think I got it! You cant really use a text editor because it does not have a 1-1 relation with bytes! Ascii encoding might encode two different bits in the same character, so there is no way to go back!
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Text files are usually encoded as ASCII. Each byte represents a character or has a special meaning. See here.

The problem is that there isn't an ASCII code for every possible value a byte can hold, and most of the special codes will get translated or removed by your text editor, and in the process breaks your executable.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I don't think text files normally support all the nessicary bit combinations. Also, some hex editors will show you the hex and binary. Why do you want to know this, anyway?
ok, I saw your messages, got it, thx for coping with my ignorance :-)

yeah that was pretty stupid thinking that a .txt 011010 would produce that machine code :-P
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Here's a (more) serious explanation.

Assuming ASCII characters and big-endian binary, a text file full of ones and zeroes would actually be a sequence of 00110000 and 00110001 (I think). That's because 00110000 binary is 48 in decimal, which is the code for the "0" character in ASCII encoding. Binary is simply base 2. Decimal, what most people are used to working with, is base 10. Also used are octal (base 8) and hexadecimal (base 16), primarily because they make it easier to read machine code.

Basically, that's as much machine code as opening a jpg file in notepad is a text file; as far as a human is concerned, the letters are "garbage" because jpg viewers interpret the binary data as image data, but notepad interprets it as a sequence of 8-bit character codes (for example).

Executable binary is a sequence of numbers representing basic commands. Imagine there's a list of numbered instructions somewhere, each number corresponding to a different action - that's what's happening here. There are also data values in there, such as memory addresses and literal values. And in the same way "100576" can be interpreted as either 1, 0, 0, 5, 7, 6 or 10, 05, 76 or 100, 576 or 100576 there is a set way, defined by the particular computer, of defining how many binary digits each number actually is (so that you can tell where one number ends and the next begins).

Almost nobody writes in binary. At the lowest level, people write in assembly language which is pretty much binary level commands except instead of having to write strings of 0 and 1 you write short instructions. When you want to run that, you run it through a program that converts the instructions into their binary form. So rather than write 00000001 to represent instruction one, you might write PUSH, which then gets converted into 00000001 by the compiler.

People rarely write in assembly language, however. Most modern languages are much higher level, and get converted into machine code by programs called compilers.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)

This topic is closed to new replies.

Advertisement