Laboratory: Machine Language
CSC 105 - The Digital Age
This laboratory exercise provides practice with machine and assembly
Part I: Interpreting Instructions
in a new window by right-clicking on the link and
choosing "Open Link in New Window" and then come back to this
lab. This will bring up the full Knob & Switch Machine
Simulator. In addition to the usual Data Path and Main Memory
regions, you now have a complete and working Control Unit.
In memory location 0, type the instruction
ADD R2 R1 R0
What is binary machine representation of this instruction? (You
should calculate it manually from your notes.)
On the pull-down menu next to the instruction you just entered,
change the value from "Auto" or "Instr" to "2" to reveal the
binary executable instruction. Were you correct? If not, verify
where your mistake was.
On the pull-down menu next to memory location 1,
change the value from "Auto" to "2".
What is the assembly instruction for the following machine code?
(It may help to add some formatting spaces to help you parse the
code. Start at the left and work your way to the right.)
Copy this bit string into memory location 1 (be sure to delete any
Using the pull-down menu next to address 1, change the formatting
from "2" to "Instr" to to reveal the binary executable
instruction. Were you correct? If not, verify where your mistake
HALT instruction in memory location 2.
Change the contents of the registers (R0, R1, R2, and R3) to 1, 3,
5, and 7, respectively.
Click "Reset" above the PC (Program Counter) to initialize it to zero.
What do you expect to be the result when you click "Execute"?
What would happen if you had forgotten the
instruction? How would the control unit react?
Change the HALT in memory address 2 to something else (anything
you like). You may need to change the "View as" pulldown to
something other than "Instr" first.
Reset the PC and execute the program to test your prediction.
Part II: Writing An Assembly Program
Write a sequence of assembly instructions to add the
contents of memory locations 10, 11, and 12, then store the result
in memory location 13. (Note: this will take several steps to load
the values into the registers, compute the sum, and store the
Enter your instructions from Exercise 1, starting at memory
location 0. Don't forget to halt your program!
Enter the values 11, 13, and 18 in memory locations 10, 11, and
What result do you expect your program to produce?
Execute your program to verify its correctness.
For those with extra time
Write a sequence of assembly instructions to multiply the value at
memory address 11 by 4.
For example, if the contents of memory address 11 was the number
7, your instructions should store the number 28 there. While the
ALU has no multiplication setting on its knob, multiplication can
be accomplished via repeated addition: 7*4 = 7 + 7 + 7 + 7.
Type your program into the simulator and execute it to verify its
What if you had the following assembly instruction:
BREZ RR MMMMM
which means, if register RR is equal to zero, set the program counter
to the memory address MMMMM. That is, "BRanch if Equal to Zero." Then
you could exhibit looping behavior you've seen in Python programs.
Assuming R1 is zero, write an assembly instruction that adds that
copies the contents of R0 to R1.
Assuming R3 is 1, write an assembly instruction that subtracts one from R2.
Assume R2 is initialized to a desired multiplier for the original
value in R0, how many times would you need to execute the previous
two instructions to calculate R1 = R2*R0?
Using the new assembly instruction above, write another assembly
instruction that branches to a HALT instruction when R2 is zero.
Combine these instructions to write an assembly program that calculates
R1=R2*R0 (using the value of R2 when the program starts), and then