Starting from:

$29.99

Lab 2 – Assembly Basic

COSC 211 Lab 2 – Assembly Basic
0.Introduction
In this lab, you will have the opportunity to start working with the SPIM simulator. You will explore its operation using
sample code. You will then have the opportunity to write some assembly code of your own. Additionally, there are a
series of questions from the text book. There is a total of thirty-four (34) marks available for this assignment.
The assignment is broken into two part: programming and written. The submission can be hand written and scanned or
typed. The written component can be submitted in a single txt file. Work must be clear with all work shown. The coding
part of the assignment requires multiple files to be submitted. Your code is to be submitted through Canvas before the
end of the day on Friday, September 29th
, 2023. Submissions will be accepted earlier. Late submissions will not be
accepted. Even if you do not complete all questions, please try them all and submit what you have for partial marks.
1.Programming Assignment
 In this part of the assignment, you will get practice using SPIM and writing simple assembly programs.
Getting Started with SPIM
QtSpim is a self-contained simulator as well as providing some simple debugging tools. Unlike previous versions,
QtSpim will run on Windows, Linux and MacOS X with the same UI. You can download a version of QtSpim at
http://spimsimulator.sourceforge.net/. Feel free to read more at this website if you are interested in the details
of the simulator.
To start QtSpim you will need to start it from the command line as the lab machines do not have a menu shortcut.
To access program, go to START->run… and enter QtSpim.exe and select ok. QtSpim will start with two windows.
The first window is the main window (left) that will provide all the menu options, along with a register view and
data and text tabs. The second is the console view (right) that is used to interact with your program is I/O is
required (you may have to adjust your program preferences to get the viewing mode you prefer as it may be whit
text on a black background).

You can select what windows are visible while you are working. In general, we will only be interested in using the
Internal Registers, Text Segment, Data Segment and Console windows. You can change your windows by going to
the Windows menu from the QtSpim main menu bar.
You can also select how much data you see in both the Text Segment and Data Segment. Select the Text tab in
the main QtSpim window to see code. In general, we are not interested in the kernel segment, so unselect the
Kernel Text option from the Text Segment menu on the QtSpim main menu bar.
Text Tab
To view you Data Segment, you can select the Data tab. You will find that there is a lot of data there that you
aren’t interested in at this time. The amount of data you see can be adjusted by selecting the Data Segment menu
from the QtSpim main menu bar. You can also display the number format used to display values. Select only User
Data and set the format to Hex.
Your window should now look like this.
Data Tab
The last thing that needs to be done before we are able to run code is to set the start address of where our code
will be found in memory when it is loaded in. As we recall from class, the Text Segment starts at 0x00400000,
but with QtSpim (and Spim in general), there is some support code located there for the simulator. Our code will
load and start at 0x00400024. This address needs to be set. From the QtSpim main menu select Simulator-
>Run Parameters.
A menu box will appear called Set Run Parameters. Here you can set the start address or label used to indicate
the start of your program as well as setting command line arguments to pass to the program. Set the start address
to be 0x00400024 and then click ok.
Loading and Running Your Program
To load a program, select File->Reinitialize and Load file. A menu will appear and you can navigate to where you
code is stored.
After you select open, you code will be loaded into QtSpim and the Data Segment and Text Segment will be visible
on the Data and Text tabs.
IMPORTANT: Every time you load a program, you will want to initialize the registers. If you do not do this, your
program may have unexpected results as the registers do not automatically clear when a new program is
loaded.
Running Your Program
There are two options for running your program. You can use Simulation->Run/Continue (F5) to run the program
all at once or you can single step through your code using Simulation->Single Step (F10). In general, it is a better
idea to single step through your code as you will be able to see what is going to. If you need to stop are restart
your code, press the stop button located on the main menu bar. You then need to clear the registers. This can
be done by selecting the Clear Registers button on the main menu bar or from Simulator->Clear Registers. You
can then start stepping through your program again. Be careful: if you are storing and using results in memory
and not initializing the locations first, you may get unexpected results when you rerun the program. If you
want to reinitialize the simulator, this can be done by selecting the Reinitialize Simulator button or from Simulator-
>Reinitialize Simulator. If you do this, the program you have loaded will be erased and you will have to reload it.
Single Step
Stop
Pause a Running Program
Run
Reinitialize Simulator
Clear Registers
Writing Your Assembly Program
As we did in Lab 1, we will be using Notepad (Windows) or TextEdit (macOS) to write our assembly programs. You
can use any numerous editors to do this. Many will give you command and syntax highlighting. On Windows,
Notepad can be found under Start->All Programs->Accessories. Alternately, you can go to Start->Run… and enter
notepad.exe to launch the program. Once you open notepad, you can enter your text. On Mac, TextEdit can be
found in your Applications folder. You can also use any other tools such as VSCode, Notepad++, nano, or VI, but
remember to follow the strict formatting guidelines covered in class. Use tabs to help keep your code aligned.
Don’t forget to include a header in your file with program title, your name and student number.
To save your code, you need to use Save As… (File->Save As…) as we need to change the extension type to .s.

By default, notepad will always save as *.txt and we would like to have the file saved as *.s. Make sure you change
the Save as type to All Files. Save all your code on your F: drive. Create a folder for lab 2 so you can refer back
to these materials.
Programming Exercise
1. (/4) Download the program s1.s from Canvas and load it into QtSpim.
• For this question only, set the start address to be 0x00400000. Click the button "run". If everything
goes well, a console window will appear, saying "Hello, Your ID = ". Type a string and press the "Enter" key
to observe the output.
• Reinitialize and reload the program. Play with this program several times to get an idea what it does.
Answer the following question.
Question 1: What does this program do? There are some instructions that we haven’t covered yet but consult
Appendix A.10 or ask your TA is you need to know what they do.
• Clear the program, and load it again. Press the button "step" to trace the program. Pay close attention
after the line jal 0x00400024 [main], and observe the change of the register $v0, $a0, $t0 - $t2, and
$s0. You can stop tracing at the line jr $31.
• Read the source code of s1.s to get familiar with it and then, use QtSpim’s "step" to figure out the answers
to the following questions:
Question 2: What are the memory addresses of the data labelled len, buffer, and str?
Question 3: Will your answer to Question 2 the same if the directive ".align 4" is commented out? Why?
Submit your answer to the above three questions in a file named excercise1.txt
2. (/10) Write a MIPS program that reads two integer numbers from the keyboard as
Enter number 1: x
Enter Number 2: y
where x and y are the numbers you have entered. Your program will store them in memory (in the data section
of your code). The code will them add the two numbers together and display the results on the console as
The sum of the two numbers is: ####
Where #### is the sum of the two numbers. Your program will them compute the difference between the two
numbers and display the results on the console as
The difference of the two numbers is: ####
Here is sample output:
Enter number 1: 25
Enter number 2: 40
The sum of the two numbers is: 65
The difference of the two numbers is: -15
Ensure that your program start address is set to 0x00400024.
You will need to use a syscall to access the input from the keyboard and print back to the console. Information
on utilizing syscall can be found in Appendix A.9. Include a program comment header that has your name,
student number and program name. Name your file excercise2.s.
3. (/10) Write a MIPS program that reads a string of 10 characters from the keyboard. Always enter 10 characters
as input. Your program will store them in memory (in the data section of your code). You code will then print out
the value of the 3rd, 5th, and 7th character is the string remembering that the first character is character 0. It should
be similar to the sample output:
Enter a 10 character string: this is ok
The third character is: s
The fifth character is: i
The seventh character is:
Ensure that your program start address is set to 0x00400024.
Include a program comment header that has your name, student number and program name. Name your file
excercise3.s.
Programming Submission
For questions 1, 2 and 3, submit both your .s files and .txt files through Canvas. All files should be in a single .zip
file. Submit your files through Canvas before the assignment due date.
2. Written Assignment
2. (/10) Operations and Operands (Show all work. Part marks will be assigned for all questions)
a) Exercise 2.1 (Textbook, page 164): (2 marks)
b) Exercise 2.2: (Textbook, pages 164): (2 marks)
c) Exercise 2.3: (Textbook, pages 165): (3 marks)
d) Exercise 2.9: (Textbook, pages 166): (3 marks)
Written Submission
Submit your written assignment through Canvas on the above indicated due date. Please take care and effort to make
your assignment answers as clear as possible.

More products