LAB 11


Today’s lab will introduce a simplified machine language.

Software tools needed: web browser and a graphical editor, such as gEdit.

References:

In-class Quiz

During lab, there is a quiz. The password to access the quiz will be given during lab. To complete the quiz, log on to Blackboard (see Lab 1 for details on using Blackboard).

Simplified Machine Language

In today’s lab, we will explore a programming language that is very “low level” in that it maps very closely to the actual commands that are used by the computer’s processor.

We will use an emulator, WeMIPS, to emulate what a machine-level language would do for a popular class of computer processors, MIPS. Processors with MIPS are a Reduced Instructor Set Computer (RISC), meaning they have fewer different types of instructions that the processor knows (and thus fewer that have to be implemented, leading to faster processors).

Let’s start by looking at a program that will print “Hello World”:

If you would like to follow along, using the emulator, open the window and toggle the “Show/Hide Demos” button and then click on the “Hello World” demo.

Just as we did with PythonTutor, we can “step” or go through the code line-by-line to see what it does:

Try changing the program in the WeMIPS window to print out “Mihi cura futuri”. Once it does, copy the program into a text window:

#YOUR NAME HERE
#My first MIPS program that prints: Mihi cura futuri

... put your machine language program here ....

and see the Programming Problem List.

Loops in Machine Language

To create loops in our machine language, we use two additional instructions:

Here is a sample of setting up loops in the MIPS machine language (you can copy it into the emulator to step through the code):

#Sample program that loops from 10 down to 0
ADDI $s0, $zero, 10 #set s0 to 10
ADDI $s1, $zero, 1  #use to decrement counter, $s0
AGAIN: SUB $s0, $s0, $s1
BEQ $s0, $zero, DONE
J AGAIN
DONE:  #To break out of the loop

Here’s a translation of the code into pseudocode:

  1. Set $s0 to 10.
  2. Set $s1 to 1.
  3. Subtract $s1 from $s0 and store result in $s0 (i.e. $s0 = $s0 - $s1).
  4. If $s0 equals 0, go to line 6.
  5. Else, go back to Line 3 and repeat.
  6. Program ends here.

This program counts down from 10 to 0. How could you modify it to count from 1 to 10? When you have it running, see the Programming Problem List.

(Hint: store the value 10 in a register to use in the comparison)

Challenge

As a final machine language challenge, modify the “Interactive” demo (the first lines are in the image above) to use the current year when computing the ages. When you have it running, see the Programming Problem List.

A Note on Grading Simplified Machine Language

We’re going to use just a few commands that move values into registers (physical memory locations), do simple arithmetic, and jump (or branch) to another part of our program.

Explain stack pointer

To print out numbers from 10 to 0, loop with sub, plus print

There are many commands that can be used in the full MIPS machine language. We are working with only a few of them:

ADD, ADDI, ADDIU, BEQ, J, SB, SUB, SUBU, syscall

As such, the grading script only recognizes the commands above and the ‘#’ style comments. Anything else will confuse it greatly.

The general format for simplified machine language programs is:

#YOUR NAME HERE #My first MIPS program that prints: Mihi cura futuri

Code goes here…

To submit your program for grading:

What’s Next?

You must now complete programs 49 and 50 in the Programming Problem List. They are due tomorrow at 1pm!