Computer Science – How Do Lines of Code Get Executed by the CPU?

computer sciencecpu

I'm trying to really understand how exactly a high-level language is converted into machine code and then executed by the cpu.

I understand that the code is compiled into machine code, which is the low level code that a CPU can use. If I have an assignment statement say:

x = x + 5;
y = x - 3;

Does the CPU execute each line one at a time? So it will first execute the x = x + 5; instruction and then the next instruction the CPU will execute is the y = x- 3; I'm really trying to understand the execution process and how the code I write is actually execute by the CPU.

Best Answer

The lines of code have nothing to do with how the CPU executes it. I'd recommend reading up on assembler, because that will teach you a lot about how the hardware actually does things. You can also get assembler output from many compilers.

That code might compile into something like (in a made up assembly language):

load R1, [x] ; meaning load the data stored at memory location x into register 1
add R1, 5
store [x], R1 ; store the modified value into the memory location x
sub R1, 3
store R1, [y]

However, if the compiler knows that a variable isn't used again, the store operation may not be emitted.

Now for the debugger to know what machine code corresponds to a line of program source, annotations are added by the compiler to show what line corresponds to where in the machine code.

Related Topic