Laboratory: Machine & Assembly Language
CSC 105 - The Digital Age

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

Contents

Part I: Interpreting Instructions

Exercise 1: Getting started

  1. Open this page in a new tab by right-clicking on the link and choosing "Open Link in New Tab" and then come back to this lab. The page shows the assembly and machine instructions for the Knob & Switch Machine simulator
  2. 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.

    Enable JavaScript on this page by clicking the "Options" button in the lower-right corner of the window and selecting either Temporarily allow all this page or Allow dave-reed.com, depending on your preferences.

Exercise 2: Assembly to machine language

  1. In memory location 0, type the instruction
    ADD R2 R1 R0
  2. What is the binary machine language representation of this instruction? (You should calculate it manually from the Instruction Set you opened in Exercise 1.a.)
  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 3: Machine to assembly language

  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 as you interpret the bits.)
  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 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 4: Executing a program

  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"? (Don't do it yet, just note your predictions.)
  4. Ok, now click "Execute" to run the simulation and verify your prediction.

Exercise 5: The HALT instruction

  1. What would happen if you had forgotten the HALT instruction? How would the control unit react?
  2. Let's test your theory. First, 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. Next, reset the PC to have value zero and then execute the program to test your prediction.

Part II: Writing Assembly Programs

Exercise 1: Adding three numbers

  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.)
  2. Enter your instructions from part a (above), starting at memory location 0. Don't forget to halt your program!
  3. Enter the values 11, 13, and 18 in memory locations 10, 11, and 12, respectively.
  4. What result do you expect your program to produce?
  5. Execute your program to verify its correctness. (Don't forget to reset the PC!)

Exercise 2: Zeroing and negating registers

  1. Write a single instruction that sets the value of R3 to zero. Do not load the value zero from memory. Remember to halt your program afterwards!
  2. Type your instruction at location 0 in memory. Reset the PC and execute the program to test your prediction.
  3. Write three instructions to load a value from memory and then negate that value.
  4. Type your program instructions at locations 0-3 in memory. Reset the PC and execute the program to test your prediction.

Congratulations!

By the time you finish this lab, you've written several programs in assembly language—something even some CS majors never do. You should feel proud of yourself.

For those with extra time

Exercise 4: Multiplying by 4

  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 5: Branching

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
Revised: Janet Davis, April 27, 2012
Revised: Janet Davis, March 12, 2013
Revised: Jerod Weinman, 4 April 2014
Adapted from Dave Reed, "A Balanced Introduction to Computer Science," Exercises 14.9, 14.10, and 14.13.