What is an if at the transistor level?

I recently took a Digital Logic course and learned all about AND, OR, and various other kinds of logic. One thing that we did not consider, which is absolutely necessary for programming, is if , and I was very curious how they work.

My best guess is that it will just be a 2: 1 multiplexer, and since you add more else , it will be 4: 1 and 8: 1, but that seems too complicated for such a simple concept,

Does anyone know what the if really means?

+7
source share
7 answers

You forget that programs are executed as separate instructions, and (at least in the simplest case) instructions are executed sequentially.

So, for if (a + 4 > 5) command loads a into the register, another command adds 4 , another instruction compares the sum with 5 . Then the team will check the “condition code” from the comparison and either immediately execute the next instruction (executing the “body if ”), or “jump” to the memory location several (or several tens) instructions (skipping the “body if ”).

There is digital logic there, but it is at a lower level, deciding how to execute each individual instruction.

+1
source

The correct if translates to something higher level than transistor logic. if are usually implemented as conditional branches (in the absence of optimizations), either increasing the program counter by the default value, or setting it to the value specified in the branch, based on whether the condition is true or false (usually 1 or 0).

+1
source

It has been a while since I took the computer architecture class, so forgive me if I am a little vague with this answer.

All the instructions that your computer executes come from the memory of the commands, and I believe that there is a number that represents the address of the executable command.

A typical instruction has several sections, 1 for command code and usually separates up to 2 source registers, destination register and some other things that I have not had to think about for more than 4 years.

In any case, one of the command codes is usually a conditional jump, and if you can understand how your data path stores / retrieves values ​​in a regular bar, then it shouldn't be so difficult to extend this logic to change the address or literal value or value instruction stored in a particular register, depending on whether the other literal / register value is 0

enter image description here

+1
source

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 # intrinsically works by subtracting ebx from eax # eax and ebx are not changed, but the arithmetic flags are # if it was greater, jump to greater label JG my_if_statement_was_true # if it wasn't greater, we get here my_if_statement_was_false: # do something # we're now done, so jump to the end of the statement J my_if_statement_end my_if_statement_was_true: # do something else # now we're done with the if statement my_if_statement_end: # program continues 

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

+1
source

If you are just concerned about the implementation of the if condition, then its simple,

you probably read the digital design of Morris Mano, there is a simple scheme for comparing two registers, by the way, the logic is simple if you think about it.

 a>b OR a<b OR a==b all these 3 instructions can be implemented easily by just comparing the two registers 

Now, if you are concerned about how the if statement is, how it is implemented in the CPU , then it goes through a three-step cycle of Fetch, Decode, and Execute .

After that, the registers are compared, as mentioned earlier.

Hope this helps .. :)

+1
source

if is a control flow operation in a programming language, not a concept expressed in digital logic. If your class has considered mechanisms related to assembly instructions, such as jump (I'm not sure if I ever found out about this), this is what the if does.

0
source

As indicated above, there are logical comparators that ultimately make these decisions. There are many different implementations of comparators depending on the needs of the project (power, area, swing, synchronization or not, ect.). Comparators consist of a number of NMOS / CMOS CMOS motifs.

Take a look at the wikipedia page for comparators and look at the dynamic fixed comparator. I do not have enough reputation to publish them, unfortunately.

This is an example of a dynamic fixed comparator. Depending on whether the implementation is active high or low, the system will track the architecture here to match the two values ​​and the calculation operator.

-one
source

All Articles