# How do I do an If statement ?

13 messages
Open this post in threaded view
|

## How do I do an If statement ?

 An example would be if x is 0 then output is the input or if x is 1 then output is 0 thanks
Open this post in threaded view
|

## Re: How do I do an If statement ?

 Administrator In software, if statements make choices. What hardware part did you make in chapter 1 that chooses what to output? --Mark
Open this post in threaded view
|

## Re: How do I do an If statement ?

 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? Thank you for your help!
Open this post in threaded view
|

## Re: How do I do an If statement ?

 arjunyel, that's a great question. I tell myself, "hardware is not software," and in this case, there is no loss of "efficiency" by not using one of the computed values.
Open this post in threaded view
|

## Re: How do I do an If statement ?

 Administrator This post was updated on . In reply to this post by arjunyel 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 the sum and carry 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 using the microcomputer only needs 2 or 3 of the many interfaces that are provided. 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. --Mark
Open this post in threaded view
|

## Re: How do I do an If statement ?

 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.
Open this post in threaded view
|

## Re: How do I do an If statement ?

 Administrator This post was updated on . CMOS has problems with floating inputs so you would need pull-up resistors or "bus hold" circuits on all the inputs that might be tri-stated. When the input voltage gets close to midway between "0" and "1" both the pull-up and pull-down transistors begin to turn on and large amounts of current flow.   This image comes from an interesting app note from Texas Instruments. Warning, it's rather technical. There is a related problem with slow transitions between "0" and "1", which can cause oscillations in the gate's output. In this case "slow" means 200 nanoseconds!       This image comes from another TI app note. --Mark
Open this post in threaded view
|

## Re: How do I do an If statement ?

Open this post in threaded view
|

## Re: How do I do an If statement ?

Open this post in threaded view
|

## Re: How do I do an If statement ?

Open this post in threaded view
|

## Re: How do I do an If statement ?

 Ivant and Mark, Your explanations are helpful. I appreciate your responses.