Comparing C to machine language

83    10 Aug 2015 02:55 by u/roznak

18 comments

7

In order to write very efficient code in C/C++ it is useful to understand how the C gets translated into machine code.

In a lot of cases your C/C++ code can work faster when you reorganize your C/C++ code to a point where it creates the most optimal assembly code.

0

for clarification... machine code is a form of VHDL? Because that's kind of how I see him explaining since that's what I had to do to program a 8086 chip instruction set...

2

VHDL is an FPGA programming language, while machine codes are instructions that directly use hardware-based instruction hard-coded in to a chip, called the instruction set. You can use VHDL or Verilog (basically same thing) to define the behavior of an FPGA to give it an instruction set, that you could then write machine code to program in.

0

I think it is way more important to know the compiler you're working with than to know the underlying assembly. You have to write code that the compiler will be able to optimize.

Then, read the assembly for critical part and make sure it was optimized correctly by the compiler.

0

Buy by analyzing the underlying assembly code can deduce how your compiler is creating the assembly language.

It has been a while since I touched C++ but back then I suddenly realized that only the first 4 parameters are passed on as register memories, the fifth is pushed onto the stack. So if you wanted speed then any method you call should be limited to 4 parameters.

0

only the first 4 parameters are passed on as register memories, the fifth is pushed onto the stack.

For the specific compiler, OS and 32 bit vs. 64 bit, that's probably true.
http://www.agner.org/optimize/calling_conventions.pdf

2

A question: Why does "x = y" need two lines? Would not "movl -0xc(%rbp), -0x8(%rbp)" suffice?

-1

It only uses one line. It justs takes two lines to add a number.

1

I'm not talking about the adding, but the assigning part.

-1

I'm saying the assigning part is only one line.

1

But not the assigning to zero, the assigning to other variable. Lines ...000f57 and ...000f5a. Go to 8:42 in the video to see very clearly, that they're both collectively labeled "x = y", and my point is that that should be able to be done in one line, not two.

1

Reading and writing to memory at the same time might not be possible on that architecture. The difference with writing 0 and 0xFF is that those are constants/literals and not memory addresses.

7

x86 doesn't have a MOV that takes two memory addresses. You must use a temporary register to move data between memory addresses.

1

Ah, ok, thank you.

2

assembly != machine language

2

Nope but it directly translates to machine language almost 1 on 1.

0

What is the reason for putting your code into an infinite while(1) loop? Just to output multiple times? Also, in printf, shouldn't you be using printf(%i\n, x) since you are printing an integer rather than a double?