Starting from:

$30

Assignment 1 Numbers and Memory

CPSC 213 – Assignment 1
Numbers and Memory

After a 8-hour grace period, no late assignments will be accepted.
Overview
One preliminary goal for this assignment is for you to familiarize yourself with the computing
environment we will be using this term. You will learn about the UNIX command line, how to
transfer files to the department UNIX servers, and how to submit assignments using the handin
program. You will also setup an IntelliJ project for the Simple Machine processor simulator.
The assignment consists of five parts. The marks for each part and for each question within the
parts are listed as a percentage as like this [xx%].
You may do this assignment in groups of 2 if you like. If you do, be sure that both of you work
every problem — don’t split up the work. See the How to Hand In Section for important details
on how you do this.
What You Need to Do
Part 1: The UNIX Command Line
You can get a UNIX Command Line (i.e., a shell) in one of three ways.
1. You can login to one of the department server machines directly in the lab or remotely from
your on machine. To do this from the Mac use ssh. To do this from Windows use putty or
xshell.
2. If you have a Mac (or run Linux) you can get a shell directly on your computer.
3. If you run Windows there is a poor version of a UNIX-like environment that you can use,
but it isn’t great. Its call Cygwin and must be installed separately. Windows 10 provides a
bash command-line directly without Cygwin using an optional module that must be
downloaded. Non of these configurations are recommended.
Pick your environment. Then do the following.
1. Create a file called HelloWorld.java and edit it so that it will print “Hello World”
when it runs. You’ll do this by creating a static method called main that prints that string.
2. Compile this class from the command line using the javac command.
3. Run this class from the command line using the java command.
4. Follow the instructions in the last section to use handin to submit what you’ve done so far.
Then use handin again to verify that your code was submitted properly. The goal here is to
workout the process of running handin. You’ll run handin again later when you finish the
rest of the assignment.
Part 2: Install the Simple Machine
Most of you will want to use IntelliJ to edit and run the simple machine simulator we will use
throughout the term. It is also possible to use Eclipse or another other IDE or editor you like
(emacs and vi are possibilities).
To install the simulator in IntelliJ, download:
• www.ugrad.cs.ubc.ca/~cs213/cur/resources/sm-student-213-intellij.zip
To install in Eclipse, download
• www.ugrad.cs.ubc.ca/~cs213/cur/resources/sm-student-213-eclipse.zip.
Then follow instructions in Companion Section B.1 for IntelliJ or B.2 for Eclipse. Note that
you’ll find a link to the Companion readings on the course Piazza resources page.
To use an IDE other than IntelliJ or Eclipse, unpack one of these zip files to select the files
SimpleMachineStudent.jar, SimpleMachineStudentSrc.zip, and
SimpleMachineStudentDoc213.zip. Then install them into your IDE. The first file
contains jar files you need to include in your path, the second contains the source of the
simulator, and the third its Java Doc.
Finally, download the solution / reference implementation of the simulator from
• www.ugrad.cs.ubc.ca/~cs213/cur/resources/SimpleMachine213.jar.
Save this file for later assignments. Companion Section B.5 describes how to run this version.
You can, for example, run it from the UNIX command line by typing:
java -jar SimpleMachine213.jar
Now, check to see whether your installation was successful by following the instructions in
Companion Section B.4 to run the simulator. If all went well, the simulator will start and show a
window like this:
Part 3: Endianness and Hex Questions [20%]
Answer these questions.
1. [4%] What is the value (in hex) of the little-endian, 4-byte integer stored at location
0x70d8 in Table 1?
2. [4%] What is the value (in hex) of the big-endian, 8-byte long stored at location 0x7020 in
the table above?
3. [4%] Give an example of a 4-byte integer whose big- and little-endian representations are
identical. Can you generalize this example?
4. [8%] The Hubble Space Telescope labels the image data it collects with sky positions using
right ascension / declination coordinates. It downloads this data as binary files that are
accessible on the Internet. You’ve decided that you’d like to take an up-close look at
Proxima Centauri, the nearest star to earth after the sun, whose position is RA 14h 29m
42.9s, D -62 40 46.1
Hubble image files encode position coordinates using two 4-byte integers, one for right
ascension and the other for declination. And so the position of Proximate Centauri would
be labeled as RA=521,829 and D=-2,207,359.
RA = 14 * 36000 + 29 * 600 + 42.9 * 10 = 521,829
D = -62 * 36000 + 40 * 600 + 46.1 * 10 = -2,207,359
Table 1: Contents of Memory from Address 0x7000 to
So you write a program to download a portion of the Hubble dataset and search it for images
containing these coordinates. You discover, however, that Hubble apparently never took any
images of Proximate Centauri. You call the head of NASA to complain bitterly. She tells
you that they have taken thousands of pictures of Proxima Centauri and suggest that
perhaps you are an idiot.
Then you note that the computer on the Hubble that generated the coordinates is the DF-224
manufactured by Rockwell Autonetics in the 1980’s and the computer on which your
program is running uses an Intel Core i7 processor that you recently purchased — and then
you realize that something you learned in CPSC 213 might actually be useful.
What did you realize and what are the correct values of the two integers that you should use
in your program to search for Proxima Centauri?
HINT: Use a calculator or a program to convert the numbers 521,829 and -2,207,359 to
hex. Then think about how you might need to manipulate these hex values. You can give
your answer in hex or convert it back to decimal; your choice. To print the hex value of a
number in Java:
System.out.printf(“0x%x\n”, i);
Part 4: Programming Test of Endianness [30%]
Download the file www.ugrad.cs.ubc.ca/~cs213/cur/assignments/a1/code.zip. It contains the
skeleton of an executable Java class in the file Endianness.java. Place this file directory on
a UNIX machine (e.g., one of the lab machines, a Mac, or Windows running Cygwin) and
compile it from the UNIX command line like this:
javac Endianness.java
You can now run this program from the command line. The program takes four command-line
arguments. These arguments are the values of four consecutive bytes (in hex) of memory from
which the program will construct both big-endian and the little-endian integers. For example, to
see the value of the integer whose byte values are 0x01, 0x02, 0x03, and 0x04, in that order, you
would type:
java Endianness 1 2 3 4
[25%] Write the code that transforms this memory into integers by replacing the TODOs with an
implementation of bigEndianValue and littleEndianValue.
[5%] Test your program carefully by calling it from the command line with various memory
values. Ensure that your tests provide good coverage and be sure to include some tests with
bytes that have bit-eight set to 1 (e.g., 0xff or 0x80). Your mark here will be based the list of
tests you ran, as described in the P4.txt file you create.
Part 5: Implement the Simple Machine MainMemory Class [50%]
Like a real processor, the simulator has a memory and a CPU. You will implement both of them
as java classes. This week you will implement the memory.
Some portions of the memory are already implemented. Your job is to implement and test the
five methods of MainMemory.java labeled with TODO’s. You will find this file in your
IntelliJ environment in the arch.sm213.machine.student package.
[35%] Implement the following methods.
• [10%] isAccessAligned that determines whether an address is aligned.
• [10%] bytestoInteger and integerToBytes that translate between an array of
bytes and a big endian integer. You can use your solution to Part 2 for this.
• [15%] get and set that provide array-of-byte access to memory used by the CPU to fetch
and to store data. Follow the specification listed in the javadoc comments carefully. Note,
for example, that the address provided to these methods is not required to be aligned.
[15%] Create a set of JUnit tests to test your implementation. Place all of your tests in a class
named MainMemoryTest in the same package as the MainMemory class. Note that you will
not actually be able to run the simulator itself yet beyond the initial screen, because you will still
lack a CPU implementation.
Ensure that your tests provide good test coverage for each of five methods you implemented.
Comment each test to explain what it is testing.
Plan Text Files
THIS IS IMPORTANT
All of the files you submit for this assignment and all future assignments must be plain text files.
Java files produced by IntelliJ are examples of this sort of file.
In this assignment you are also turning in several files ending in “.txt” that contains textual
information. You might be tempted to created and edit these files in MS Word, for example.
But, Word does not create plain text files. The files it creates contain formatting information in
addition to the text and end in “.docx”, “.doc”, or maybe even “.rtf”. These will not work!
Be sure that you create your files in a plain text editor or export them to plain text format from
whatever editor you use.
Summary of What to Hand In
Create a directory named a1 that contains the following files:
1. PARTNER.txt – containing your partner’s CS login id and nothing else (i.e., the 4- or
5- digit id in the form a0z1). Your partner should not submit anything. If you do not
have a partner, do not include this file. Remember PLAIN TEXT.
Note that if you both partners accidentally hand something in — the hand auto grader will
warn you — one of you must delete their submission by handing in an empty directory.
2. HelloWorld.java
3. Q1.txt … Q4.txt – containing your answers to Questions 1-4, respectively.
4. Endianness.java – your solution to Part 4.
5. P4.txt containing your test description for Part 4.
6. MainMemory.java – your MainMemory solution
7. MainMemoryTest.java – your JUnit tests for Part 5.
Be sure this directory contains these files and nothing else and then follow the instructions in the
next section to submit your assignment.
How to Hand In
You must use a program called handin to submit your assignment. To use this program you
need a CS login id. If you don’t have one you must get one. Your partner needs one too. To get
a CS id following the online instructions here:
www.cs.ubc.ca/students/undergrad/services/account.
You should run handin program from the UNIX command line. Instructions for using the
command-line version are here:
my.cs.ubc.ca/docs/handin-instructions
As indicated in the instructions, place your assignment files in a directory called a1.
You can hand in as many times as you want up to the grace-period deadline. The last hand in
wins.
The instructions also tell you how to very that your hand in was successful. You should always
double check.
The hand-in script will perform certain automated marking tasks and assign a tentative mark for
certain parts of the assignment. But, since automated marking has limitations, TAs will review
these automated marks and make corrections where appropriate.

More products