CS-343 Assignment 6

Submission

Submit this assignment by email by midnight of March 21. Be sure to use "CS-343 Assignment 6" as the subject, and include your name in the message body. Send your program as an attached text file and answer the questions in the body of your email message.

Don't forget that you can get help for this and other assignments at the Discussion Forum for the course.

The Assignment

  1. Write a MIPS assembly language program that adds several numbers together using a loop. See below for help. Verify that it is correct by stepping through the program one instruction at a time.
  2. Here are two lines from the Text Segment of the sample program:
      [0x00400024] 0x3c011001 lui $1, 4097  [aNumber] ; 7: lw $t0, aNumber
    [0x00400028] 0x8c28000c lw $8, 12($1) [aNumber]
    1. What are the hexadecimal numbers in square brackets at the left end of each line?
    2. What is the next hexadecimal number (0x3c011001, etc) on each line?
    3. Explain the difference between the assembly language code to the left and to the right of the semicolon?
    4. Explain how these two lines of code implement the first lw instruction in sample.s.
  3. Explain how the branch instruction in your program was translated to machine language.

Help

Installing (PC/X)SPIM

There are three versions of the SPIM simulator on the CD that comes with the book. (And there are updated versions on the SPIM author's website if you are interested.) The simplest version is called spim, but it works only from the command line, which makes it less useful than the two graphical versions. The graphical version for Windows is called pcspim and the graphical version for Unix and Macintosh is called xspim.

To install the PC/Windows version, use WinZip to unzip the pcspim.zip file that's in the Content/Software/Spim directory of the CD. Then run setup.exe, and the program will be installed and ready to go.

For Linux/Unix/Macintosh, you have to build and install the program yourself. The file to use is spim.tar.gz from the same directory as pcspim.zip. Unzip it (tar xvzf spim.tar.gz) and you will get a directory named something like Spim-7.0. Change to that directory and edit Imakefile to tell where you want everything installed. Then run ./Configure, then xmkmf, then make, make xspim, and make install. These instructions are spelled out more carefully in the README file. Ask on the course forum if you need help getting set up.

Writing an Assembly Language Program

You have to use a text editor (not a word processor) to prepare your program. Windows has Notepad, but if you are a CS student you undoubtedly have a programmer's text editor that you prefer by now, like vim or nedit or maybe a commercial one. Create a text file with a .s extension. Here is a sample program you can use as a model:

sample.s
  # Add two numbers and print their sum.
  # C. Vickery
  # March 18, 2005

                  .text
                  .globl  main
  main:           lw    $t0, aNumber
                  lw    $t1, anotherNumber
                  add   $t0, $t0, $t1
                  sw    $t0, answer
  
                  li    $v0, 4    # print string
                  la    $a0, msg
                  syscall
                  li    $v0, 1    # print int
                  lw    $a0, answer
                  syscall
                  li    $v0,  10  # exit
                  syscall

                  .data
  msg:            .asciiz "The sum is "
  aNumber:        .word 5
  anotherNumber:  .word -123
  answer:         .space 4
                  .end

  

Comments are introduced by the '#' character.

Labels (symbols that represent memory addresses) appear at the beginning of a line, and end with a ':'. Like a C program, your code starts executing at main, so use that as the label of your first instruction.

Memory is divided into three regions: text, where the instructions go, data, where the data goes, and kernel where the operating system resides. The SPIM simulator comes with a small operating system kernel in a file called exception.s. If the simulator is installed correctly, exception.s is loaded into kernel memory automatically whenever you run it. The text area starts at address 0x400000 and the data area starts at 0x10000000.

The instructions that start with dots, (.text, .globl, .data, .asciiz, .word, .space, and .end) are "assembler directives."