CMPS-2240 Lab 2
MIPS Function Calls

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals:
Write a MIPS program that makes function calls.
Understand la, li, lb, jal, and syscall statements.
Introduction to stack frames.

Resources:
MIPS Overview
MIPS Textbook

Files Needed:
Example programs for today's lab can be found at:
/home/fac/gordon/public_html/2240/code/lab2/

Do your work in your odin 2240/2 folder.

Background and review:
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:
http://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.

Part 1: printf

Required Files The following files are required for this lab:

If you are working on Odin, you may copy the files like so:
cp /home/fac/gordon/public_html/2240/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 "lab2.s"
(spim) load "read.s"
(spim) run

Output you should get from the program:


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 lab2.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. 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 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.

The output of the original lab2.s program is sloppy. Make sure the output of your finished work is neat and clean.

Objectives For this part of the lab you should modify lab2.s to call printf and display the following:


Go
CSUB Roadrunners
in 2024!

Note: 3rd line calls printf with a format string and 1 additional argument.
Note: delete any lines of code you do not need.

Checkoff: Demonstrate Ability to Use printf

To get full 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 lab2.s.

Code Review For the rest of this lab and the following labs you may lose points if your code is messy or your program is unnecessarily complex.

Part 2: Getting Input


Copy lab2.s to lab2a.s
Next modify lab2a.s to add functionality to read last name, first name and ID from the user at the keyboard. Take the chunks of code you need to do this from read.s. Insert the code into the main procedure in lab2a.s. Before doing this you must understand what read.s is doing so you know what to copy and where to put it. You also need to declare some chunks of memory in your .data segment to hold last name and first name. A chunk of 32 bytes for each should be enough. The data allocation instruction will look like this:

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 of this read operation from $v0 into a temp register:

    move $t0, $v0         # move value from $v0 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.

3.1 Demonstrate Ability to Get User Input

Please show me the following:

(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

The following files will be collected from your 2240/2 folder.

   lab2.s
   lab2a.s
   printf.s
   read.s
Make sure to bring the printf function into your lab2a.s program.

Copy the function code from printf.s, and put it in your lab2a.s program.
Also bring in code from read.s into your lab2a.s.