Notice:
Make sure to bring the printf function into your final main.s program.
Copy the function code from printf.s, and put it in your main.s program.
Name your program: main.s
Due: Wednesday Feb. 12
Resources:
MIPS Overview
MIPS Textbook
Files Needed: Example programs for today's lab: Lab2
Background and review:
This text is repeated for the benefit of some students who may have missed the
first lab. If you don't want a refresher on how to use SPIM, skip to below.
In this part of the lab you will write, assemble and execute a MIPS assembly program using the SPIM MIPS simulator. You can download SPIM at:
https://sourceforge.net/projects/spimsimulator/files/
The PC/MAC versions of Spim run under a GUI - all lab instructions in this course will be for the command line version only.
Unlike a real assembler, which assembles MIPS source into an object file, SPIM
behaves like an interpreter for a scripting language. SPIM executes each
assembly statement one line at a time (it never produces an object file).
Because of this, SPIM can be a good learning tool. You can execute your code or check for
syntax errors with SPIM at the command line:
spim -f test.s
Load your file with:
load "file.s"
Run your code with:
run
Moving on with the lab:
Note that as you load your source code into SPIM, all files that you
previously attempted to load into SPIM will be linked (all source files loaded
will be joined). This includes source files that did not assemble properly.
So if you execute load "file.s" and it fails, you modify the source,
and you attempt to reload it by running load "file.s" without exiting SPIM both
files will be linked. This will cause a collision because you will have two
main blocks. You will get an error like so:
spim: (parser) Label is defined for the second time on line 9 of file error.asm main: ^
To fix this error run this command:
reinitialize
This may come up in today's lab because we are linking multiple ASM files.
Required Files The following files are required for this lab:
They are available at URL: https://cs.csub.edu/~eddie/cmps2240/code/lab2/
If you are working on sleipnir, you may copy the files like so:
cp /home/fac/eddie/public_html/cmps2240/code/lab2/* .
If you are working on a local machine, you may copy the files like so:
scp odin.cs.csub.edu:/home/fac/eddie/public_html/cmps2240/code/lab2/* .
Test The Files
Once you've downloaded the files, load all three files and run the code to see
what it does. Some example commands for how you would get this done:
(spim) load "printf.s"
(spim) load "main.s"
(spim) load "read.s"
(spim) run
The output you should get from the program should be:
Hello world. Register $a1 holds: 10 2 plus 3 is 5 The string is: I am a string The char is: a Enter a line of text [return]: Enter an integer [return]:
If you take a look at main.s you can see that printf has greatly reduced the amount of work you have to do to print a string. Note this:
jal printf
This is a function call. Each line of instruction in your code is associated with an address. Your main function starts at 0 (this is simulated, note that we talked about different starting addresses in practice during lecture). The program will march through each line in your code, line by line. It runs line 0. Then it runs line 1, and so on until it hits the sequence of commands to quit. In a high-level language we would alter the control flow with if, switch, for, etc. We don't have as much functionality in MIPS (but there are if branch commands). We must explicitly tell it to skip to a specific address or block. For example, the above line of code jumps to the block of code labeled printf which you can find in the file printf.s (look at this file and note the line printf:). How are arguments passed? Note that we load values into the registers $a0, $a1, etc. before calling printf.
Objectives For this part of the lab you should modify main.s to call printf and display the following:
Go Roadrunners in 2020!
Note: 3rd line calls printf with a format string and 1 argument.
Note: delete any lines of code you do not need.
To get 5 points for the lab you need to show me:
(1) that the output of your program matches the above output
(2) your source code for main.s.
Code Review For the rest of this lab and the following labs you may lose one or two points if the code is messy or the program is unnecessarily complex.
fname_buf: .space 32
lname_buf: .space 32
.space allocates an array of n consecutive bytes where n is the given value. E.g., the above code creates an array of 32 bytes. Note that characters are 1 byte long and integers are 4 bytes long, so this could hold either 32 characters or 8 integers.
Display the prompt for the user to enter first name. Load the address of fname_buf into $a0. Make the syscall (8) to read a string from the keyboard. This syscall grabs what the user types and stuffs it into the 32 bytes of space starting at address fname_buf. Do the same for lname_buf.
Prompt the user for a 3-digit ID (an integer). Read it. Move the value from this read from $v0 into a temp register:
move $t0, $a0 # move value from $a0 to $t0
Display all 3 values in the format shown below, using printf.
Note: the syscall read facility gets the newline from the screen input.
firstname: Mike lastname: Smith id: 756
Control your use of the newline character.
To get 5 points for the lab you need to show me:
(1) that your program can retrieve input from the keyboard and display the
results like above.
(2) your source code showing main.
What to turn in
Your lab program main.s must be located in the correct directory on Sleipnir. directory name: /home/stu/yourname/2240/2/ Replace yourname with your own Odin username!