Hey Mark, I love how much help you give on these fourms :) I know how to do the if statement with a chip, my question is It seems I need to compute both branches before going into the If statement, this seems inefficient as one branch is thrown away. Is there a way to to do if where it only computes the path chosen?
Or is this the point of CPU branch prediction I've heard about, without that you have to compute both paths?
The thing to realize is that the hardware is always there so its computation occurs in parallel (at the same time) as the other options. Power is used in modern ICs when signals change state, so there is a slight power cost if unused circuits are changing values.
There is, for instance, a lot going on in the Add16 in the ALU during operations that use the And function, so there is some power wastage. The circuitry to make the ALU not compute sums and carries would likely require more power than would be saved!
Many microcomputers for embedded systems have lots of interfaces for various communications buses and most of the time the product only needs 2 or 3 of the many interfaces that are provided, for example. These microcomputers often have built in mechanisms to power down sections of the chip that are not used. This is something that the firmware can do during initialization if there is a need to reduce power usage.
I was also thinking of another option for implementing if in hardware, namely to use 3-state logic. You put the "switch" first and it would forward the the signal to the correct branch. The other branch (or branches) would receive "floating". You can then just wire the outputs together.
To make that work all the parts need to be 3-state aware or hidden behind a controlled buffer or inverter.
I'm not sure if it has any advantages or drawbacks from the hardware stand point, but I think it should work. One advantage is, that it's closer to the way software developers think of if and switch statements.
N.B. This cannot be used in the Nand2tetris Hardware Simulator, because it doesn't support three-state logic.