Skip to main content
gamedev.net
🔒 Locked

how do compilers make object code/ binary code?

Started by Cipher3D 8 replies 500+ views
Original Post
Cipher3D
Cipher3D
how do compilers convert statements like: a + b = c; to machine code? i understand it parses it, converts the stuff to tokens, etc, but what next? for example, does it fwrite(blah) to make the exe file?
I eat heart attacks
dr_slash_uh
dr_slash_uh
it converts the code into assembly language. the equivilant code in assembly coud be something like this

mov ax, 6 ;ax can be a
mov bx, 3 ;bx can be b
add ax, bx ;add them together and store in ax
mov cx, ax ;mov answer to cx wchich can be c

than the code above is converted into hexadacimal
so the first line(mov ax, 7)will look like this:
B860600

from there thats all I can tell you, i am sure someone else can finish or give a better explanation.

cowsarenotevil
cowsarenotevil
quote:
Original post by Cipher3D
how do compilers convert statements like:

a + b = c;

to machine code?

i understand it parses it, converts the stuff to tokens, etc, but what next? for example, does it fwrite(blah) to make the exe file?


Basically, yes. EXE''s are just like any other document format, they just happen to give instructions to the processor.

<- Cow Soft, free software I''ve made
"Unfortunatly, when I sent the email, instead of writing "Best Regards" I ended up writing "Best Retards." - boolean
-~-The Cow of Darkness-~-
WilyCoder
WilyCoder
First of all, expressions usually take the form of
[LefthandSide] = [expression]

as in

c = a + b;

To find your answer, you should read up on Assemblers first,
as they are easier to understand, and have less steps involved.

Basically a compiler is a tool that is used to turn your
statements into abstract concepts(like expressions, production rules and tokens)

c=a+b;

might get translated to

[variable]=[expression]

and if by using the production rules for [expression], you can derive a+b , then the statement is considered a valid one, and compilation continues to the next line.

I have barely touched the surface of the entire answer to your question, but I am at work and have little time.

I'm sure others will sound in with their input.

Compilers are a really detailed subject (usually graduate level stuff).

As a side note, the answer to your question will vary depending on what CPU you are building for.

DOS and Win32 EXE files follow a standardized format, something like

[Header]
[instructions]
[data]
[end of file]

Im sure someone here can fill in the details

[edited by - wilycoder on March 3, 2004 7:24:01 PM]

[edited by - wilycoder on March 3, 2004 7:24:41 PM]
Cipher3D
Cipher3D
so if i open an exe up in a hex editor, do all those hex values stand for an instruction (mov, jmp, cmp, etc) or values? interesting...

so a compiler would look like this:

if (instruction == "mov")
{
convertToHex(FFFFFF);
fprintf("%x",&value);
}


?

I eat heart attacks
LessBread
LessBread
quote:
Original post by Cipher3D
so if i open an exe up in a hex editor, do all those hex values stand for an instruction (mov, jmp, cmp, etc) or values? interesting...


Many of them would, but many of them would also constitute data values - constant numbers, strings and the like. The instructions are usually stored in the ".text" section of the exe. The format of exe files is named "PE" for "Portable Executable", but they really aren''t that portable.

quote:
Original post by Cipher3D
so a compiler would look like this:

if (instruction == "mov")
{
convertToHex(FFFFFF);
fprintf("%x",&value);
}

?



Sort of, but not quite .



"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Cipher3D
Cipher3D
also, i don''t want to start another thread, i also have another question:

using sockets, how would you send files?

I understand you might do this:

FILE* fp = fopen("file.dat","rb");

char buffer[CHUNK_SIZE];

fread(buffer,CHUNK_SIZE,whatever);

send(buffer);

the problem is, how would you know how much to send (of the chunk)? Do you determine if the other user has a fast connection and send appropriately? Iknow send returns the number of bytes sent, so do you send the whole file, see if you sent the whole file, if not, then send the remaining parts of the file, ad infinitum?

I eat heart attacks
mattnewport
mattnewport
If you want all the gory details of exactly how instructions are represented, check out The IA-32 Intel(R) Architecture Software Developer''s Manual, Volume 2A: Instruction Set Reference, A-M and The IA-32 Intel Architecture Software Developer''s Manual, Volume 2B: Instruction Set Reference, N-Z. For details on the format of an .exe there''s the Microsoft Portable Executable and Common Object File Format Specification
blueEbola
blueEbola
For the compiler question, usually the compiler parses the source code into a tree structure, performs optimization, semantic checks, etc.

Then it will convert the expressions in the tree to their assembly equivalents, pass this output to the assembler, and then link the code and the data together using a linker and the result is the PE .exe file.

quote:

also, i don''t want to start another thread, i also have another question:

using sockets, how would you send files?

I understand you might do this:

FILE* fp = fopen("file.dat","rb");

char buffer[CHUNK_SIZE];

fread(buffer,CHUNK_SIZE,whatever);

send(buffer);

the problem is, how would you know how much to send (of the chunk)? Do you determine if the other user has a fast connection and send appropriately? Iknow send returns the number of bytes sent, so do you send the whole file, see if you sent the whole file, if not, then send the remaining parts of the file, ad infinitum?



You''ve got the right idea, you just send the file in chunks. The way I sent a file in the past was split it into 2k chunks and attach this chunk to a header. The header would contain information like the size of the chunk, the CRC of the data sent (to check the integrity of the data), and any other information the receiver needs.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.