Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an "if" statement at the level of transistors?

I recently took a Digital Logic course and learned all about AND, OR, and various other kinds of logic. One thing we did not cover, which is entirely essential to programming, is if statements and it left me rather curious as to how they work.

My best guess is that it would simply be a 2:1 multiplexer, and as you add more else statements it gets to be 4:1 and 8:1 but that seems a little too complex for such a simple concept.

Anyone know what an if statement actually translates to?

like image 207
Nealon Avatar asked Jun 24 '13 18:06

Nealon


People also ask

What is the if condition used for?

The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

What is an if statement in computing?

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.

What are transistors in logic gates?

The use of transistors for the construction of logic gates depends upon their utility as fast switches. When the base-emitter diode is turned on enough to be driven into saturation, the collector voltage with respect to the emitter may be near zero and can be used to construct gates for the TTL logic family.

What is the relationship between transistors and gates?

"In a computer, a gate controls the flow of electric current through a circuit. The gate consists of transistors; the transistors are selected by the chip designer from two basic types (PMOS and NMOS transistors) that are found in the ubiquitous CMOS (complementary metal-oxide semiconductor) technology.


2 Answers

You're forgetting that programs are executed as individual instructions, and (at least in the simplest case) instructions execute sequentially.

So for if (a + 4 > 5) an instruction will load a into a register, another instruction will add 4, another instruction will compare the sum to 5. Then an instruction will test the "condition code" from the compare and either execute the immediately following instruction (executing the "if body") or "jump" to a location in memory several (or several dozen) instructions away (skipping over the "if body").

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

like image 183
Hot Licks Avatar answered Oct 01 '22 04:10

Hot Licks


A proper if statement translates to something a bit higher-level than transistor logic. if statements are generally implemented as conditional branches (assuming no optimizations), either incrementing the program counter by the default amount or setting it to the value specified in the branch based on whether the condition evaluated to true or false (usually 1 or 0).

like image 23
JAB Avatar answered Oct 01 '22 06:10

JAB