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

enter image description here

If statements, and all other control flow statements, are implemented at the logic level as conditional jumps.

When you use an if statement, like this one:

int a = 1, b = 0
if (a > b)
{
    ...

Obviously, any smart compiler will optimize this out. If we specifically instruct our compiler to be as dumb as possible and generate instructions verbatim, we'll get something like the following out of it:

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 translate (roughly) directly to machine code opcodes. The processor does a bunch of additional stuff to support the process of loading and fetching instructions, which is relevant here. There is a special register called the program counter (referred to later as the PC register), which tracks the location of the next opcode that the processor is going to execute.

  1. 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 the arithmetic operation.
  2. Then, the JG instruction checks whether the GREATER flag in the FLAGS register is set. Since it is in our example (recall that 1 > 0), it performs a jump.
  3. The jump instruction modifies the program counter (PC), which is the register that controls where the CPU will read the next instruction from.
  4. The CPU then attempts to read the next instruction. Since we've jumped, the next instruction is not the one immediately following the previously processed one.

That's an overview of the process. If you want a more in depth explanation, I recommend you write a simple C program with an if statement, compile it, disassemble it (using linux objdump or an equivalent), and maybe 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

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

you probably have read digital design by Morris Mano, there a simple circuit is given for comparing two register, by the way, the logic is simple if you give a thought 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, that how actually it is implemented in the CPU then it goes through the 3 step cycle of Fetch, Decode and Execute.

After that the registers are compared as told earlier.

Hope it helps.. :)

if is a control flow operation within a programming language, not a concept expressable in digital logic. If your class covered the machinery associated with assembly instructions like jump (I'm not sure I ever learned that), that is what an if statement is made out of.

As the others above have stated, there are logic comparators that ultimately make these decisions. There are a ton of different implementations of comparators depending on the design needs (power, area, swing, clocked or not, ect.). The comparators are made out of a number of CMOS NMOS/CMOS gates.

Look on the wikipedia page for comparators, and look at dynamic latched comparator. I do not have enough rep to post one unfortunately.

This is an example of a dynamic latched comparator. Depending if implementation is active high or low, system would clock the architecture here to make comparison of two values and evaluate statement.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top