Laboratory: The Knob & Switch Computer

Background Information

The Knob & Switch Computer uses metaphor and an incremental approach to demonstrate the interactions between CPU datapath, CPU control, main memory, and machine language programs. We use it in this class to introduce the main concepts of computer organization and architecture before getting into the details of the Hack computer.

Part A: The Datapath

  1. Open The K&S Datapath Simulation. If the link doesn't open, then visit the Knob & Switch Computer and click the appropriate link. You should resize your windows so that you can see both the simulator and these lab exercises at the same time.

    Observe that this simple CPU contains four registers, but no control unit or memory. That is because you will be the control unit! You direct which registers are operated on, and which operations are performed.
  2. Take a look at the top box that says "Register Bank." The register values are the text boxes with a green background. Currently, the default value of zero is in all the registers.

    Set the value of R1 to 42 by clicking on the zero and typing in a new value. Then set the value of R2 to be 17.

  3. At the right are two command knobs, "A Bus Address" and "B Bus Address." These specify which registers should be read as inputs to the ALU. You can turn each of these knobs one notch by clicking on them. What digital logic components do these knobs correspond to?

    Set the A bus address to R1 and the B bus address to R2 by clicking each appropriately.

    Note: A bus is essentially a collection of wires used to transport data from one component to another. A better animation would show the four bits flowing along four wires.

  4. Find the ALU. What operation will the ALU perform?
  5. Find the C bus and the C bus address. Where do you think the result will be stored?
  6. Click the "Execute" button and watch the data flow through the datapath. Is the result what you expected?
  7. Now "program" the simulation to compute R3 = R1 - R2.

Part B: Main Memory

  1. Open The K&S Datapath Simulation with Main Memory

    There are two new components: main memory and  bus switches.

  2. Put the value 42 into memory location 1 (Mem[1] for short), and select it as the RW location.
  3. Find the main memory bus. Close the switch from the main memory bus to the C bus. Then open the switch from the ALU to the C bus. What do you think will happen when you execute the datapath?
  4. Click the "Execute" button and watch the data flow through the datapath. Is the result what you expected?

    You should notice two things: Reading from memory takes more time than reading from a register, and the ALU computed a result even though that result was ignored. If you didn't see both those things, put the value 43 into memory location 1 and click the "Execute" button again.

  5. How can we write data to memory? By closing the connection from the C bus to the main memory bus.

    Open the C bus so that nothing is read from memory, then close the switch from the C bus to the memory so that a result can be written to memory. Connect the ALU to the C bus by closing the switch that connects them. Open the switch that connects the C bus to the register bank.

    Set R0=8 and R1=9. Set the A Bus, B Bus, and ALU operation to calculate R0+R1.

    Now everything should be set to add our registers and send the output from the C bus along to memory. Where in memory will the result be stored? Verify this by clicking "Execute."

  6. Suppose we wish to subtract Mem[6] from Mem[7] and store the result in Mem[8]. How many cycles would this take?

Part C: Control

  1. Open The Knob & Switch Simulation with Microprogramming.

    This simulation adds a Microprogram Memory. This memory contains binary numbers representing the positions of all the knobs and switches.

  2. In the Microprogram Memory, click the first green arrow to store the knob and switch settings in the first microprogram location. You should see the x's disappear and binary numbers appear. Verify that they correspond to the default settings of the knobs and switches, as well as the memory RW address.
  3. Enter the value 42 in memory location 7. Then set up the datapath and memory to load this value into register 0. Click the first green arrow and notice how the binary values change.
  4. Set up the datapath to double the value in register 0, storing the result in memory location 8. Click the second green arrow to add this as a second instruction.
  5. Then, click the "Execute" button to run your program. Notice that the simulator changes the datapath settings according to what is stored in the microprogram memory.
  6. Write a microprogram to subtract Mem[6] from Mem[7] and store the result in Mem[8]. You can type directly in the microprogram memory, if you prefer.

    Then test your program. Before you click the "Execute" button, you may want to set the execution speed to "Fast".

Part D: The Fetch-Execute Cycle

  1. Open The Complete K&S Model 1 Computer.

    The complete K&S computer adds the ability to store machine language instructions in memory.

  2. Study the control unit. Find the instruction register (IR), the program counter (PC), and the micro-instruction register.
  3. In memory location 0, type the instruction
    add r1 r2 r3
  4. On the pull-down menu next to the instruction you just entered, change the view from "Auto" or "Inst" to "2", so that you can see the corresponding machine language instruction stored in memory. Then change the view back to "Inst" so you can see the instruction in assembly language.
  5. Enter the HALT instruction in memory location 1.
  6. Put some more interesting numbers in the registers, set the animation speed to "Fast", and execute the program. Observe the instruction flowing into the IR, the PC being updated, and the instruction executing in the datapath. Notice that the PC update happens in parallel with interpreting the instruction.
  7. Open the reference card for the K&S instruction set. Find the instructions for accessing memory.

    Write a program in memory locations 0-4 to subtract Mem[6] from Mem[7] and store the result in Mem[8]. Be sure to end your program with a HALT instruction.

    Then test your program. Before you click the "Execute" button, click the "Reset" button to reset the PC. You may also want to set the animation speed to "Fast" or "Fastest".

  8. What do you think the following program will do?
    0: SUB R0 R0 R0
    1: LOAD R1 5
    2: ADD R0 R0 R1
    3: BRANCH 2
    4: HALT
    5: 1

    The last line is not a typo: Mem[5] should contain the value 1.

    Enter this program in main memory, starting at memory location 0. Before you execute the program, be sure to reset the PC . As you execute the program, keep an eye on the PC. Does the program do what you expected?

  9. If you still have time, try the following:
    1. Write a program that doubles the number stored in Mem[10].
    2. Write a program that counts down from 10 to 0 and then halts.
    1. Write a program that finds the absolute value of the difference between Mem[10] and Mem[11] and stores the result in Mem[12].


Please write and then discuss with your partner:

This activity was shamelessly adapted from the work work of Professor Janet Davis (, with permission.

Created February 8, 2016
Last revisedFebruary 05, 2018, 07:46:18 AM PST

Acknowledgments: The Knob & Switch Computer was developed by Grant Braught at Dickinson College and Dave Reed at Creighton University. These instructions are adapted from laboratory exercises originally written by Jerod Weinman.

CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.