CS计算机代考程序代写 mips database gui assembly assembler 2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 1/6

Week 04 Laboratory Exercises
Objectives

give you practice with simple MIPS assembler programming
give you practice debugging with qtspim

Admin

Marks Lab Assessment Guide

Demo in the Week04 Lab or at the start of the Week05 Lab

Submit give dp1092 lab04 percentage.s negative.s weather.s asterisks.s

Deadline must be submitted by 11:59pm Tuesday

Background

MIPS assembly language provides a small number of simple instructions which lend themselves to an efficient implementation in
hardware, but require more instructions to render typical high-level programming language constructs.

We’ll be running our MIPS programs on the SPIM emulator, using either

the ~dp1092/bin/spim command-line interface
the /usr/bin/qtspim graphical interface

Both systems use the same backend, so programs that work on one should work without change on the other. The qtspim interface is
the best one to use for debugging, but requires you to be running in an environment that supports windowing (e.g. CSE Labs, any
modern operating system) Note that there may be other (older) versions of spim installed in CSE; make sure that you’re using the version
above (you can check using the command which spim and which qtspim).

Setting Up

Setting Up
Create a directory for this lab; let’s call it Lab04Dir.

Change into your Lab04Dir directory and run the following command:

$ unzip ~dp1092/web/public/tlb/04/lab04.zip

If you’re working at home, download lab04.zip by right-clicking on the above link and then run the above command on your local
machine.

Also, if you want to work at home, you’ll need to download the MIPS emulator. Executables for qtspim are available for most operating
systems from http://spimsimulator.sourceforge.net/. If you want to use the spim command-line interface on your own machine, you’ll
need to download the source code from SourceForge and compile it yourself, after modifying the Makefile in the spim/ subdirectory of
the source code to tell make where to install the executable and the exception handling code. After doing that, installation is as simple as
running make; make install. This is true on MacOS (with gccinstalled) and Linux. I haven’t tried to install spim on Windows.

If you’ve done the above correctly, you should now find the following in the directory:

warmup.s interactive program that adds 2 numbers

percentage.s template for exercise 1

negative.s template for exercise 2

weather s a buggy solution for exercise 3

https://cgi.cse.unsw.edu.au/~dp1092/21T3/resources/lab_assessment.html
https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/lab04.zip
http://spimsimulator.sourceforge.net/

2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 2/6

weather.s a buggy solution for exercise 3

asterisks.c a template for exercise 4

tests/ directory containing test cases

Note that you run these tests slightly differently to others, because there’s no binary executable involved. Run the check script in the
Lab04Dir directory using the command:

$ ~dp1092/bin/check

In order to run these programs manually, you can use spim via e.g.

$ 1092 spim -file warmup.s
Loaded: /home/dp1092/lib/spim/exceptions.s
First number: 9
Second number: 4
13

This is a quick and easy way to run working SPIM programs, but is a poor environment to try and debug them.

For the first exercise in this lab and for debugging, you should use qtspim.

$ /usr/bin/qtspim

or just

$ qtspim

This will start the GUI interface from within which you can load and execute SPIM programs and monitor the execution (e.g. step by
step).

A very quick tutorial on using qtspim is available on YouTube.

Exercise #0: qtspim
Note: There is no submission for this exercise but your tutor will ask you to demonstrate the use of qtspim when marking your lab.

Part 1: Running a program in qtspim
1. Run the command

$ qtspim

This should bring up a main window. The window is divided into a number of sections:

Buttons across the top that can be used to load, run and debug your code

The Register tabs on the left display the content of all registers. There is a tab for integer registers and floating point
registers. We will mainly be using the integer registers in this course. Note: the Registers menu allows you to change the
format to decimal,hexadecimal or binary.

The Text tab displays the MIPS instructions loaded into memory.

The columns from left to right show:

the memory address of an instruction
the opcode MIPS instruction at that address in hexadecimal
the MIPS instruction at that address in symbolic format
the actual spim code you wrote along with any comments

The Data tab displays memory addresses and their values in the data and stack segments of the memory. The columns from
left to right show:

the memory address
the data in hexadecimal (or decimal or binary can be chosen from the Data Segment Menu)
the data in ascii representation

At the moment you have not loaded in any code, so in the text window at address [00400014] (on the around 7th line down) you
should see something like

[00400014] 0c000000 jal 0x00000000 [main] ; 188: jal main

Note: actual addresses may vary depending on your system.

2. Go to the button (or the menu item File->Load File) and load the file warmup.s

You should notice that the text window has been updated with your code. The line stored in address [00400014] should be
updated to something like:

[00400014] 0c100009 jal 0x00400024 [main] ; 188: jal main

This means the main program has been loaded into address 0x00400024.

2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 3/6

3. To run the program simply press the button. Note: Running this program should result in a console window popping up for

you to enter input in. If it doesn’t, check whether the console window is hidden behind the main window.

4. Congratulations. You have run your first program in qtspim. Don’t quit qtspim yet. It is time to look at the debugging tools

Part 2: Debugging tools in qtspim
1. To run the program again, you’ll want to reset the registers. Click on the button (or the menu item

Simulator->Clear Registers). This resets the registers including the PC (program counter) so that the program will start again at
the beginning the next time you run it.

2. Next press the button (or the menu item Simulator->Single Step) to single step through the program. Notice how the
highlighted instruction in the text window changes.

Step through the program line by line. As you step through the code, notice how the values stored in the registers change when
your instruction stores a new value into them. You may notice that in the memory tab, you can see the data segment being
modified as the values for x and y are read in too. All of this information can be very helpful when it comes to debugging code.

3. This time instead of single stepping all the way through the program, we are going to use a break point. The first step is to make
sure you reset the registers again (press the button).

4. Now we are going to set a break point on the following line of MIPS code.

[00400074] 01092020 add $4, $8, $9 ; 39: add $a0, $t0, $t1 # reg[a0] = reg[t0] + reg[t1]

To do this simply right click on the instruction that you want to set a break point on in the text tab. A menu that looks like

should pop up. Press the Set Breakpoint menu item.

5. Now press the run and the simulator will stop just before it executes the line with the breakpoint. From there you can choose to
single step or press run again to continue execution.

Part 3: Modifying the Source Code and qtspim
Suppose we wanted to modify our code because we found a bug. We would need to modify our code using our normal text editor and
reload the file using the reinitialise and load command.

1. Modify the your code to subtract instead of add the two numbers together. Make sure you save your code.

2. Reinitialise and load the code into qtspim using the button (or the File->Reinitialize and Load File menu item) and run

the program to check that it works.

Exercise #1: Calculations in MIPS: Percentage
You have been given a template file called percentage.s. The template provideds some MIPS code that you need to complete, along
with a full C solution of the problem in the comments.

It should then print out what percentage of the marks the student was awarded for that exam, with no decimal places.

Your program should read in two integers: the total number of marks in the exam, and how many marks the student was awarded.

Your program should behave as follows:

2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 4/6

$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 10
Enter the number of marks the student was awarded: 5
The student scored 50% in this exam.
$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 10
Enter the number of marks the student was awarded: 1
The student scored 10% in this exam.
$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 5
Enter the number of marks the student was awarded: 2
The student scored 40% in this exam.
$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 1
Enter the number of marks the student was awarded: 1
The student scored 100% in this exam.
$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 3
Enter the number of marks the student was awarded: 1
The student scored 33% in this exam.
$ 1092 spim -file percentage.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter the total number of marks in the exam: 20
Enter the number of marks the student was awarded: 0
The student scored 0% in this exam.

Note: If you try the following case where the user enters 0 for the total number of marks in the exam you will get something like the
following, due to a divide by zero error.

Enter the total number of marks in the exam: 0
Enter the number of marks the student was awarded: 100
Exception occurred at PC=0x0040008c
Exception 9 [Breakpoint] occurred and ignored
The student scored 10000% in this exam.

You do not have to fix your code to handle this case to get marks for the lab, but it is good practice to try to do it when you have
finished the rest of the lab.

Exercise #2: If Statements in MIPS: Negative
You have been given a template file called negative.s. The template provideds some MIPS code that you need to complete, along with a
full C solution of the problem in the comments.

You must complete the MIPS program so that gets a number from a user and prints “Don’t be so negative!” if they entered a negative
number.

If the number is positive, the program should print “You have entered a positive number.”

If the user enters the number 0, the program should print “You have entered zero.”

Your program should behave as follows

$ 1092 spim -file negative.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter a number: 5
You have entered a positive number.
$ 1092 spim -file negative.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter a number: -3
Don’t be so negative!
$ 1092 spim -file negative.s
Loaded: /home/dp1092/lib/spim/exceptions.s
Enter a number: 0
You have entered zero.

Exercise #3: Debugging if staments in MIPS: Weather
You have been given a template file called weather.s. The template provideds some buggy MIPS code that you need to debug, along
with a full C solution of the problem in the comments.

2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises

https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 5/6

Once your program has been debugged, it should read in a temperature in degrees celsius. If the temperature is > 25 it should print out
“I wish it was n degrees cooler”, where n is the temperature – 25. If the temperature is >= 40 it should also print “It is boiling!\n”.If the
temperature < 25 it should print out "I wish it was n degrees warmer", where n is 25 - the temperature. If the temperature is <=10 it should also print "I am freezing\n". Otherwise it should print "Nice weather we are having\n". Your debugged program should behave as follows: $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 25 Nice weather we are having. $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 35 I wish it was 10 degrees cooler. $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 24 I wish it was 1 degrees warmer. $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 40 I wish it was 15 degrees cooler. I am boiling! $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 10 I wish it was 15 degrees warmer. I am freezing! $ 1092 spim -file weather.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the temperature: 100 I wish it was 75 degrees cooler. I am boiling! Exercise #4: A while loop in MIPS: Asterisks You have been given a template file called asterisks.s. The template provideds some MIPS code that you need to complete, along with a full C solution of the problem in the comments. You must complete the MIPS program so it prompts the user for an input n, and prints out n '*' characters, each on a new line. Your program should behave as follows: $ 1092 spim -file asterisks.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the number of asterisks: 5 * * * * * $ 1092 spim -file asterisks.s Loaded: /home/dp1092/lib/spim/exceptions.s Enter the number of asterisks: 3 * * * Hint: There is a standard mapping from a C while loop to MIPS, which is illustrated by the following two versions of a while loop in C. The second version is very close to what you need to do in MIPS. What you need to work out is how to implement the test goto in MIPS ... and, of course, how to implement the initialisation, processing and update steps in MIPS. Standard C version C version closer to MIPS initialise MIPS code for initialise while (Condition) { TopOfLoop: do some processing if (!Condition) goto BottomOfLoop; update loop variables do some processing } update loop variables goto TopOfLoop; BottomOfLoop: Reminder: You can use the check script to run some simple tests via the command: 2021/9/22 下午12:23 DPST1092 21T3 — Week 04 Laboratory Exercises https://cgi.cse.unsw.edu.au/~dp1092/21T3/lab/04/questions 6/6 Reminder: You can use the check script to run some simple tests via the command: $ ~dp1092/bin/check while you are in the directory for this lab. Submission You need to submit the files: percentage.s, negative.s, weather.s and asterisks.s. You can submit this via the command line using give. After submitting the code, show your tutor, who'll give you feedback on your work and award a grade. To check your lab marks, you can run the following command on vlab: $ 1092 classrun -sturec Note: There is often a delay between when your tutor marks you lab and when it appears in the database