Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example: Binary Adder We want to construct a finite state machine that will add two numbers. The input is two binary numbers, (x n …x 1 x 0 ) 2 and (y.

Similar presentations


Presentation on theme: "Example: Binary Adder We want to construct a finite state machine that will add two numbers. The input is two binary numbers, (x n …x 1 x 0 ) 2 and (y."— Presentation transcript:

1 Example: Binary Adder We want to construct a finite state machine that will add two numbers. The input is two binary numbers, (x n …x 1 x 0 ) 2 and (y n …y 1 y 0 ) 2 At each step, we can compute (x i +y i ) starting with (x 0 +y 0 ). –If (x i +y i )=0, we output 0. –If (x i +y i )=1, we output 1. –If (x i +y i )=2, we have a problem. The problem is we need a carry bit. In fact, our computation needs to know the carry bit at each step (so we compute x i +y i +c i at each step), and be able to give it to the next step. We can take care of this by using states to represent the carry bit.

2 Binary Adder States We will use the following states –State S 0 occurs if the carry bit is 0 –State S 1 occurs if the carry bit is 1 Since when we begin the computation, there is no carry, we can use S 0 as the start state, So, how does which state we are in affect the output? If we are in state S 0 (we have a carry of 0) –If (x i +y i )=0, we output 0, and stay in state S 0 –If (x i +y i )=1, we output 1, and stay in state S 0 –If (x i +y i )=2, we output 0, and go to state S 1 If we are in state S 1 (we have a carry of 1) –If (x i +y i +1)=1, we output 1, and go to state S 0 –If (x i +y i +1)=2, we output 0, and stay in state S 1 –If (x i +y i +1)=3, we output 1, and stay in state S 1

3 Binary Adder State Table/Diagram From the previous observations, we can construct the state table and state diagrams for the binary adder Next StateOutput Input State S0S0 S0S0 S0S0 S0S0 S1S S1S1 S0S0 S1S1 S1S1 S1S1 1001

4 Construct a state table for the finite-state machine in Fig. 3. Find the output string for the input Answer: Input, output Output of ?

5 Example: Unit Delay In some electronic devices, it is necessary to use a unit-delay machine. That is, whatever is input into the machine should be output from the machine, but delayed by a specific amount of time. For instance, given a string of binary numbers x 1, x 2, ……, x n, the machine should produce the string 0, x 1, x 2, …, x n-1. We want to use a finite state machine to model the behavior of a unit-delay machine. What should a state in this machine represent? One possibility is that a state represents the last input bit. Thus we need a state for 1 and a state for 0. We also need start state.

6 Unit Delay States We will use the following states (that memorize last bit; except S 0 ) –State S 0 is the start state –State S 1 occurs if the previous input was 1 –State S 2 occurs if the previous input was 0 We can easily construct the state table for the unit delay machine by realizing that –When the input is 0, we always transition to state S 2 –When the input is 1, we always transition to state S 1 –When we are in state S 1 we always output 1 (since the previous input was 1) –When we are in state S 2 we always output 0 (since the previous input was 0) –When we are in state S 0 we always output 0 (since we always output 0 first)

7 Unit Delay State Table/Diagram Here is the state table and state diagram based on our previous observations. Next StateOutput Input State0101 S0S0 S2S2 S1S1 00 S1S1 S2S2 S1S1 11 S2S2 S2S2 S1S1 00 Whats output of ?

8 8 S0 – this state remembers that the previous input value (if it exists) was not a 1 S1 – this state remembers that the previous input value was a 1, but the input before (if it exists) was not a 1 S2 – this state remembers that the previous two input values were 1 Language Recognizer We want to construct an FSM that outputs 1 iff the string read so far has 111.

9 9 S0 – this state remembers that the previous input value (if it exists) was not a 1 S1 – this state remembers that the previous input value was a 1, but the input before (if it exists) was not a 1 S2 – this state remembers that the previous two input values were 1 S0S1S2 0,0 1,1 0,0 1,0 0,0 1,0 Language Recognizer This finite-state machine recognizes the set of bit strings that end in 111.

10 FSM as a Language Recognizer Definition: Let M=(S, I, O, f, g, S 0 ) be a FSM and L I*. We say that M recognizes (or accepts) L, iff for any input string x that belongs to L, M produces a 1 as an output

11 In-Class Exercise 1. Construct a finite-state machine that changes every other bit, starting with the second bit, of an input string, and leaves the other bits unchanged. 2. Construct a finite-state machine that gives an output of 1 if the number of bits read so far is divisible by 3 and an output of 0 otherwise. 3. Construct a finite-state machine that determines whether the input string has a 0 in the third to the last position read so far. (For instance, the output for the input string is: ) 11


Download ppt "Example: Binary Adder We want to construct a finite state machine that will add two numbers. The input is two binary numbers, (x n …x 1 x 0 ) 2 and (y."

Similar presentations


Ads by Google