If the operators and all other control flow operators are implemented at the logical level as conditional branches.
When you use an if statement, like this one:
int a = 1, b = 0 if (a > b) { ...
Obviously, any smart compiler optimizes this. If we specifically instruct our compiler as deeply as possible and generate instructions verbatim, we get something like the following:
my_if_statement: CMP eax, ebx
These are assembly instructions, each of which is converted (roughly) directly into machine code codes. The processor performs a bunch of additional materials to support the download process and receive instructions, which is relevant here. There is a special register called the software counter (later referred to as the PC register) that tracks the location of the next operation code that the processor will execute.
- First, the CMP instruction subtracts the second operand from the first and discards the result. However, the FLAGS register is updated with the results of an arithmetic operation.
- The JG team then checks to see if the GREATER flag is set in the FLAGS register. Since this is in our example (recall that 1> 0), it performs a jump.
- The jump command changes the program counter (PC), which is a register that controls where the CPU will read the next instruction.
- The CPU then tries to read the following instruction. As we jumped, the next instruction is not the one immediately after the previously processed one.
This is an overview of the process. If you want a more detailed explanation, I recommend that you write a simple C program with an if statement, compile it, parse it (using linux objdump or the equivalent), and possibly attach a debugger to it and run it.
linux objdump manual
To show the next instruction to be executed in gdb , use display/i $pc
Wug
source share