Laboratory: Machine Language
CSC 105 - The Digital Age

Goals:

This laboratory exercise provides practice with machine and assembly language instructions.

Part I: Interpreting Instructions

Exercise 1

Open this page 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.

Exercise 2

  1. In memory location 0, type the instruction
    ADD R2 R1 R0
  2. What is binary machine representation of this instruction? (You should calculate it manually from your notes.)
  3. 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.

Exercise 2

  1. On the pull-down menu next to memory location 1, change the value from "Auto" to "2".
  2. What is the assembly instruction for the following machine code?
    1000001001001010
    (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.)
  3. Copy this bit string into memory location 1 (be sure to delete any previous contents).
  4. 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 was.
  5. Enter the HALT instruction in memory location 2.

Exercise 3

  1. Change the contents of the registers (R0, R1, R2, and R3) to 1, 3, 5, and 7, respectively.
  2. Click "Reset" above the PC (Program Counter) to initialize it to zero.
  3. What do you expect to be the result when you click "Execute"?

Exercise 4

  1. What would happen if you had forgotten the HALT instruction? How would the control unit react?
  2. 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.
  3. Reset the PC and execute the program to test your prediction.

Part II: Writing An Assembly Program

Exercise 1

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 result.)

Exercise 2

  1. Enter your instructions from Exercise 1, starting at memory location 0. Don't forget to halt your program!
  2. Enter the values 11, 13, and 18 in memory locations 10, 11, and 12, respectively.
  3. What result do you expect your program to produce?
  4. Execute your program to verify its correctness.

For those with extra time

Exercise 3

  1. 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.

  2. Type your program into the simulator and execute it to verify its correctness.

Exercise 4

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.
  1. Assuming R1 is zero, write an assembly instruction that adds that copies the contents of R0 to R1.
  2. Assuming R3 is 1, write an assembly instruction that subtracts one from R2.
  3. 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?
  4. Using the new assembly instruction above, write another assembly instruction that branches to a HALT instruction when R2 is zero.
  5. Combine these instructions to write an assembly program that calculates R1=R2*R0 (using the value of R2 when the program starts), and then terminates.
Created: Jerod Weinman, March 16, 2009
Adapted from Dave Reed, "A Balanced Introduction to Computer Science," Exercises 14.9, 14.10, and 14.13.
Copyright © 2009-2011 Jerod Weinman.
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .