Class Notes (1,100,000)
CA (620,000)
McGill (30,000)
COMP (700)
Lecture 8

COMP 273 Lecture Notes - Lecture 8: Dont, Data Segment, Louisiana Baptist University

Computer Science (Sci)
Course Code
COMP 273
Piotr Przytycki

of 11
COMP 273 8 - MIPS instructions 1 Feb. 3, 2016
High level language programs versus Machine code
You all have experience programming in a high level language. You know that when you write
a program in a language such as C or Java, the program is nothing more than a string of ASCII
( or UNICODE characters that you type on a
keyboard and that are stored in a file. If you are unfamiliar with ASCII and UNICODE, then you
should check out those URLs. Note that ASCII is a 7 bit code (the 8th bit of each byte is ignored),
and UNICODE is 16 bits.
You also know that in order to run a program that is stored in a file, you need to translate this
program into executable instructions (machine code, made of 0’s and 1’s) that specify what your
particular computer is supposed to do. Your particular computer has a processor (made by AMD,
Intel, etc) and your program must be translated into instructions for this particular machine.
When you write your program in a high level language like Java, you typically don’t want to
consider which machine you are going to run this program on. This flexibility allows your program
to be run on many different machines. Once you decide what machine you will use, though, you need
to translate the program into a language for that machine. This translation is done by a compiler.
A compiler is a program that takes a high level language program (say, in C) and translates it into
language that is written in machine code.
In Java, the situation is a bit more subtle. Compiling a .java file gives you a .class file which
is machine code. However, this machine code isn’t understandable by your computer’s processor.
Rather it is code that runs on a virtual (abstract) computer called a Java Virtual Machine. This
JVM code must be again translated – or more precisely, interpreted1– into machine code for your
particular processor.
Assembly language
When computers were first invented in the 1940s, programmers had to program in machine code –
they had to set 0’s and 1’s by flipping physical switches. For most people, it is not much fun (and
certainly not efficient) to program with 0’s and 1’s, and so a slightly higher level of code, called
assembly language was invented. Assembly language is just human readable machine code. The
MIPS language we will use in the next few weeks is an example.
An assembly language program is an ASCII file, and is not executable. It needs to be translated
into its machine code equivalent. This translation is relatively easy (compared with translating
from a high level language like C to machine code). The translation from assembly language into
machine code is done by a program called an assembler. We are not going to learn in this course
how exactly this is done.2Rather, we will just learn an assembly language and get experience
programming in it, and understand what this corresponds to at the level of the actual processor.
1I neglected to mention this in the lecture but I’ll mention it here for your interest. The program which interprets
the JVM code is called an interpreter. The interpreter simulates the JVM. Note that translating and interpreting
are not the same thing. Translation happens before the program runs. Interpreting happens while the program
is running. This is the same notion of translation versus interpretation that is used in natural languages: people
who work as translators take written documents in one language and create written documents in another language.
People who work as interpreters take spoken language in real time and convert it into another language. The two
jobs are very different. When politicians travel to foreign countries, they need an interpreter, not a translator.
2If you want to learn about this topic, then take the Compiler Design course COMP 520.
last updated: 5th Feb, 2016 1 lecture notes c
Michael Langer
COMP 273 8 - MIPS instructions 1 Feb. 3, 2016
In the next few weeks, we will look at a particular machine or computer processor: the MIPS R2000
(from here one, just known as MIPS). We will spend the next few weeks getting to know the MIPS
“instruction set” (set of MIPS instructions) and you will get some experience programming in the
MIPS assembly language.
Before we can introduce MIPS instructions, we need to know that these instructions can refer
to two types of memory. The first is the set of 32 MIPS registers that I described in lecture 6
(page 5). The second is a much larger memory that holds all the data and instructions of a user’s
programs, as well as the instructions and data of the kernel or operating system. If you wish to
write programs for an operating system for the given computer, then you need to make use of the
special instructions and data and memory locations of the kernel. We won’t do that in this course.
We’ll only write user programs.
If you are new to programming, then it may surprise you to learn that both instructions and
data sit in memory. This may be counterintuitive since you might think that instructions and data
are very different things. As we will see, though, both instructions and data ultimately need to
be encoded as 0’s and 1’s, and so there is no reason why they cannot both sit in memory. Your
intuition is correct, though, in that they are kept in separate places in memory, respecting the fact
that they are different types of things.
In MIPS, we write Memory (with a capital M) to refer to a set of 232 bytes (or 230 words,i.e.
a word is 4 bytes) that can be addressed by a MIPS program. These 232 bytes do not include the
32 MIPS registers. These 232 bytes should not be confused with an address on a physical memory
chip or a location on a disk. As we will see later in the course, MIPS Memory addresses need to be
translated into physical addresses.3
32 words
user data
user instructions
32 bits
2 words
(1 word)
= 2 bytes
kernel instructions
and data
program counter
3An analogy here is postal codes versus (latitude,longitude). Postal codes are used by the postal system to sort
and direct mail. (Latitude,longitude) coordinates specify where the mail goes in a physical sense. If this is confusing
to you, then ask yourself: do you believe that that postal code A0A0A0 is at the tip of Newfoundland and Z9Z9Z9
is at the western tip of Vancouver Island?
last updated: 5th Feb, 2016 2 lecture notes c
Michael Langer
COMP 273 8 - MIPS instructions 1 Feb. 3, 2016
You should have only a simple notion of how a MIPS computer works. This is illustrated in the
figure above. You should understand that instructions and data are stored in Memory. You should
also understand that operations such as addition and subtraction are implemented by selecting
two numbers that are stored in registers, running them throught the ALU, and then writing the
result back into a new register. The figure above shows another register which is called the program
counter. It holds the address of the instruction that is currently being executed. This instruction
is in Memory and so the address of the instruction is 32 bits.
In this lecture, we will see some examples of instructions for carrying out such arithmetic op-
erations. We will also see instructions for moving words to and from Memory and the registers.
Finally, we will see how a program goes from one instruction to the next, and how branches to other
instructions can be achieved.
Arithmetic instructions
Consider the C instruction:
In the MIPS assembly language, this instruction might be written:
add $16,$17,$18 #register 16 is assigned the sum of registers 18 and 19
The $symbol marks a register, e.g. $17 is register 17. The #symbol marks a comment. When this
symbol appears in a MIPS program, any characters to the right of this symbol are ignored.
Memory transfer instructions
In MIPS, we cannot perform arithmetic or logical operations on numbers that are stored in Memory.
The numbers must first be brought from Memory into registers and the operation performed from
there. To load a word (4 bytes) from Memory to a particular register, we use the instruction lw, or
“load word”. The lw instruction specifies the register that we are going to write to. It also specifies
the address in Memory of the word we want. The Memory address is the sum of a base address
plus an offest. The base address is a 32 bit number which is currently stored in a register, and this
register must be specified. The offset also must be specified. Here is an example:
lw $16, 40($17) # Bring a word from memory into register 16.
In this example, the address of the word in Memory is the value in register 17, plus 40. Allowing
for an offset (e.g. 40) gives the programmer more flexibility. If it happens that the address of the
word we want is already in $17, then then the offset would just be 0.
What about the opposite operation, namely taking a word that is in a register and putting it
into Memory? For this, MIPS has sw, which stands for “store word”.
sw $16, 40($17) # Put the word in $16 into Memory
Again, the address of the word in Memory is the value in register 17, plus 40.
last updated: 5th Feb, 2016 3 lecture notes c
Michael Langer