Notice: Constant FORCE_SSL_ADMIN already defined in /nfs/unixspace/linux/accounts/COURSES/cs206/public_html/sp17/wp-config.php on line 94
Lab 7 – Prelab | CSCI 206 Computer Organization & Programming

Lab 7 – Prelab

Introduction to the MIPS system

Objectives

  • Learn how to access the mips machine, write, compile, and execute MIPS programs
  • Practice MIPS coding and debugging

Exercise 0: zyBook 9.3, 9.4, 9.5, 9.6

Read and complete all activities in the given sections.

Exercise 1: Login and exit

Now that you’ve had time to get used to programming MIPS in assembly language using the MARS simulator, we’re going to move to writing programs on a real MIPS machine. This machine lives on our network and is accessible via the hostname mips.bucknell.edu. To login from the lab machine you just have to type:

The .bucknell.edu part is added automatically by our machines (this is the default search domain). When you press enter it will ask you to confirm the RSA key fingerprint (say yes) and then enter your password (this is your usual Bucknell password).

When you log in it will look very similar to any other terminal. To verify it is a real MIPS processor, you can view the cpuinfo:

system type		: MIPS Malta
processor		: 0
cpu model		: MIPS 24Kc V0.0  FPU V0.0
BogoMIPS		: 823.29
wait instruction	: yes
microsecond timers	: yes
tlb_entries		: 16
extra interrupt vector	: yes
hardware watchpoint	: yes, count: 1, address/irw mask: [0x0ff8]
ASEs implemented	: mips16
shadow register sets	: 1
kscratch registers	: 0
core			: 0
VCED exceptions		: not available
VCEI exceptions		: not available

You should get the same output as shown above. Notice the cpu model is a MIPS 24Kc. The measured MIPS rating is 823.29. This processor has a minimum CPI of around 1, so it is a safe bet that this processor is running at about 800 MHz. This is why it does not run any graphical applications. You might even notice that compiling code is rather slow compared to more recent machines.

After you login, do an ls to view the current directory. It should be empty except for one folder “unixspace”. If you cd into unixspace it will be your usual linux home directory. You should do all of your work inside unixspace. The local drive on mips is rather small and we don’t want to fill it up. This means, you should be able to cd into your cs206 labs folder by doing cd unixspace/csci206/Labs. Change to your labs folder and create a folder for Lab07.

To make git work properly, you must copy your ssh and git config files using the following commands:

Change your working directory into unixspace/csci206/Labs. (Notice that your entire Linux file structure is available under unixspace on the MIPS machine you are using.) Create the working directory for this week, Lab07. Inside your Lab07 folder, create the file exit.s using your text editor with the contents below. Note if you setup your editor with special startup scripts you will have to copy those from ~/unixspace to ~/ for them to be found.

To discover what it does, compile and run the program as shown below:

You should observe the message:

ld: warning: cannot find entry symbol __start; defaulting to 0000000000400090.

Think about what this might mean. The linker (ld) is looking for a symbol named __start. This gives us a hint that the real entry point to our program is actually __start (not main). The C compiler has some startup code that is executed before it calls your main program, this is called the C run time (crt).

Change the label main to __start and add the directive .global __start at the top of your program to ask the assembly to expose this symbol outside of the file. Your program should now look like:

When you once again compile the code it should complete without any errors or warnings.

The content of this program should be familiar. It loads 99 into $a0, 4001 into $v0, and performs a syscall just like you would do in MARS. However, now the OS (in this case Debian Linux) handles the syscall. The Linux syscalls are very different from the MARS syscalls. To see an introduction, including a list of syscalls, type man 2 syscalls at the command prompt. The values (for $v0) are defined in /usr/include/mips-linux-gnu/asm/unistd.h, go ahead and open this in your text editor. Near the beginning you will see the lines below; __NR_Linux is 4000, which means syscall __NR_Linux + 1 == 4001 and is exit! The exit status returned is the value of $a0 when the syscall is executed. In our case, 99. The command echo $? prints the exit code of the last command, which is why we see 99 printed to the terminal. Modify this program to set the exit code to 42 and exit.

When you are done with this problem, make sure to add the modified exit.s to your git repo and push to gitlab. (Note: always remember do a git pull first.)

Exercise 2: Benchmark primes in c

In the lab we will explore the possibility of using assembly language to make our programs faster. To do this, we need a benchmark that we can implement in C and assembly. Many commercial solutions exist (SPEC), but these are very complicated. Computing prime numbers can be implemented easily and is computationally intensive so makes a good benchmark.

Below (and in the file ~cs206/Labs/Lab07/primes.c) is a skeleton for our prime finding benchmark program. The main loop will print MAX prime numbers.

In your primes.c, write the body of the is_prime function (in C) using the brute-force method to verify the primality of a number: for a given number n, test if it is divisible by any of the numbers in the range [2, n-1].  Once you complete this function, the output of primes.c should be the first 80 primes organized in a table exactly like the one below.

Once you have verified your program is correct on the first 80 primes, change the #define to print the first 10,020 primes. Use the time command to measure the time your program takes to run. It should take around 10-60 seconds to run on the mips machine. Note these times (real, user, and sys) in a comment at the beginning of your primes.c file. If your run time is much faster (<2 seconds) you are most likely running the benchmark on your local machine and not mips! Log into mips, recompile, and run.

When done, commit your primes.c file to git and push to gitlab.

Grading

[prelab: 25 points total]:

  1. [10 points] Exercise 0: zyBook activities >80% complete.
  2. [5 points] Exercise 1; successfully modified exit.s.
  3. [10 points] Exercise 2: primes C created with correct is_prime function [5 points]. Benchmark results from mips machine for 10,020 primes added to primes.c as a comment [5 points].
Print Friendly
Posted in Lab Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

*