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?


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.


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).


It's been a while since I've taken a computer architecture class, so pardon me if I'm a bit vague with this answer.

All of the instructions that your computer executes comes from the instruction memory, and I believe that there's a number representing the address of the instruction to execute.

the typical instruction has a multiple sections, 1 for the command code, and normally sections for up to 2 source registers, the destination register, and some other stuff that I haven't had to think about for over 4 years.

Anyway, One of the command codes is normally a conditional jump, and If you can understand how your data path stores/retrieves values in the regular ram, than It shouldn't be that hard to extend that logic to modifying the instruction address to either a literal value, or the value stored in a particular register, based on whether another literal value/register value is equal to 0

在这里输入图像描述

链接地址: http://www.djcxy.com/p/72226.html

上一篇: vim中大小写不敏感的f键?

下一篇: 什么是晶体管级别的“if”声明?