Compilers

Discussion about everything. New games, 3d math, development tips...
Post Reply
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Compilers

Post by Alpha Omega »

Okay so I have been learning ASM, went with the NASM flavor just to get my feet wet with it. Now that I know how the C and ASM can interact, I have a question on the process of creating an .exe for this example win32.

So you write a C or C++ program in some fancy compiler. The compiler takes the source and runs various passes... and is one of those passes, taking the source to ASM code and then running an ASM assembler on that code to get machine code?

Isn't machine code CPU specific? If you compile a C++ program using an AMD processor can it run on an intel processor?

Does the win32 executable make it to machine code or just an assembler version?

I know C# exe's need the CLR to run them so they must be processor independent like java.

If you can give some incite, I would be happy to listen.
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Compilers

Post by CuteAlien »

Check section 11 (How the compiler works) to find out about compile-stages: http://www.network-theory.co.uk/docs/gccintro/
That manual is about gcc, but other compilers work similar.

AMD always makes it processors Intel-compatible (and once in a while Intel even follows AMD). Which means they both have the same command-set and so you can run programs on both processors. But if you compile for example for newer processor features those would indeed no longer run on older processors. Bascially you don't program for the processor, but for the command-set and expect that each processor handles those commands correctly.

Win32 exectuables are machine code (as are the .obj files and .lib files are just a bunch of .obj files put together so they also contain machine code).

And yes, C# and Java compile to code for a virtual machine. Which basically means your code compiles to a command-set that looks similar like assembler-commands - except that the commands are just a standard and not optimized for a specific processor. So a virtual machine is basically simulating a processor and because you can run the simulation on different sytems you get independent of the real processor platform. It would also be possible to create processors which directly have a Java or .NET command-set, but the only reason to do that would be some optimization for Java or .NET (while completely killing compatibility to Intel, etc).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: Compilers

Post by ACE247 »

C++ compiler doesn't turn it into machine code when you compile for win32 as far as I know. It is sort of a low level code that still requires the operating system kernel to use/convert the appropriate machine code for your system. You can though compile for machine code with C++. As far as I know to run machine code the computer only needs to have the same type of instruction set (X86_64 or X86_32 etc...) as it was compiled for. Don't know how varying memory addresses are handled across machines though.
Most basic = Binary. Abstracted version of Binary = Machine code. Abstraction of that = Assembly. And then there's the massive abstraction of assembly to C/C++.
Obviously nothing is done as binary these days, the cpu automatically converts ASCII Instruction strings from Machine code to complex Binary, using its pre-determined Instruction set, so its not compiled as Binary. If this sounds like gibberish, It might be, I'm not entirely certain of all this anymore these days. (Only once read a ASM/Machine code book)
Honestly though I might also benefit from some enlightenment as to what exactly happens... :)
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: Compilers

Post by Radikalizm »

@ACE247:

Native code (ie. non-managed code) does compile to machine code, it's the compiler which takes care of integrating kernel functions in your application. Maybe you're thinking of the rings in x86 processors, where ring 0 is the level the kernel executes at and where ring 3 is where user applications run, but this is purely for protection purposes.
Also, machine code is just a sequence of processor instructions provided in said processor's ISA, it's not an abstraction of 'Binary code'. When you analyze them they'll probably be shown in hexadecimal format since binary is just a painful representation for human reading
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Compilers

Post by ent1ty »

ACE247 wrote:... Honestly though I might also benefit from some enlightenment as to what exactly happens... :)
Sh!t happens.
True story.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Re: Compilers

Post by Alpha Omega »

I was just thinking about high frequency computations for work and so it brought me to Wirth's Law. If anyone has any input here it would nice. My idea as a newbie, would be to try to bring higher level abstractions closer to the hardware.

For example, my plan is to create a kernel built to solve differential equations which can compile files and run it straight into the cpu without all the higher level overhead. Possibly, base the kernel on a very paralleled approach so any further abstractions can process threads at the lowest level.

As we approach the hardware barrier, in which computer hardware will have reached its theoretical maximum, will we begin trying to rethink the way we program?

Or are we simply going to adopt quantum computing? How will that impact software developement?
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: Compilers

Post by ACE247 »

Thanks for the info guys! Yeah I must have been lost somewere with execution rings. Though I did once try coding one of those 8080 Chips from an apple 2board in Hex Machine instruction code, though luckily soon after I learned C++, probaly more usefull eh? :)
Quantum Computing = Low Level Code debugging nightmare :D As I've heard.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: Compilers

Post by Radikalizm »

ACE247 wrote:Thanks for the info guys! Yeah I must have been lost somewere with execution rings. Though I did once try coding one of those 8080 Chips from an apple 2board in Hex Machine instruction code, though luckily soon after I learned C++, probaly more usefull eh? :)
Quantum Computing = Low Level Code debugging nightmare :D As I've heard.
Don't know whether it's a nightmare, but I could imagine it being one for a 'conventional' programmer since you'd be working with qubits, which can hold values of '0', '1' and '0 and 1' (caused by the principle of superposition if I remember correctly). It'd just be a completely different way of working compared to current computers
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: Compilers

Post by ACE247 »

Actually the programming occurs on normal computers, the way the algorithms are written to the qubits though is the tricky part, and its presently also the biggest limiting factor in the usability and speed of quantum computers, though recently one did a Ramsey 2 colour equation in 270milliseconds and gave the correct answear of 8. :)
But heck nowadays to be a programmer you don't even have to really understand the fundamentals of all the technical bits going on in a processor.
Thats all going to change again with quantum computing, but once quantum becomes the norm we are probably going to end up with something like Q++ one day!

EDIT: Oh and I BET a quantum computer couldn't even crack that encryted string in my signature, self made and quite ridiculously simple Encryption that I used and Its 100% decipherable! IF you know how, its actually encrypted with itself and the ingeniuos thing is its reversible from the text you see there, and completely impervious to language consonant matching etc, maybe even brute force... :D I've been laughing since the day I came up with it, even challenged some cryptanalysts with it.
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: Compilers

Post by Cube_ »

I am assuming it isn't possible to decrypt due to the string length being the same as the length of the key?
I read some on that it is apparently impossible to crack.
"this is not the bottleneck you are looking for"
Post Reply