Johns Hopkins University
Department of Computer Science
Fall 2010

Computer Systems Fundamentals
(Course 600.333/433)

Programming assignment 1:
Introduction to MIPS programming
and debugging with SPIM
(Due on Friday October 8, 2010, by midnight.)


1. Objectives

2. General Instructions

    For this assignment you are required to implement several programs using the MIPS programming language and test them with the simulator SPIM. There are several questions you must answer and submit along with your programs.

    Your assigment has to be submitted electronically by midnight (i.e., 11:59 PM) on the due date to he e-mail address cs333 [at] cs.jhu.edu. Very important: Include in the subject your name and programming assignment number.

    Notes:

    Additional resources:

3. Programming Activities

  1. Test the example provided in the SPIM guide.

    Questions

    1. What is the meaning of the SPIM directive .asciiz?
    2. What is the purpose of the SPIM directives .data and .text?
    3. What is a label?

  2. Create the well-known "Hello world" program using MIPS instructions.

    Questions

    1. What system call do you use to send messages to the screen?
    2. How do you implement a "carriage return" in your program?

  3. Create a program to add two integers entered from the keyboard.

    Questions

    1. What system call do you use to request data from the keyboard?
    2. Once data has been retrieved from the keyboard, where it is placed?
    3. How do you implement a small dialogue with the user?

  4. Make a MIPS template to implement each (programming) control structure.

  5. Only for 433 students: Modify the program given at the first part of this lab to (iteratively) perform the addition of the first 100 natural numbers, but squaring all the odd ones.

  6. Translate the following C programs to MIPS and explain your reasoning.

    main () {
    int i;
    for (i=1;i<=10;i=i+1)
       printf ("%d\n",i);
    }
    
    
    main () {
    int i;
    
    printf ("Input a number: ");
    scanf  ("%d",i)
    
    if (i%2==0) 
       printf ("your number is: 2*%d\n",i/2);
    else
       printf ("your number is: 2*%d+1\n",i/2);
    }
    
    main () {
    int i,s;
    
    s=0;
    while (s<10) {
       printf ("Input a number: ");
       scanf  ("%d",i)
       s=s+i;
       }
    }
    

    Questions:

    1. Indicate what control structure is illustrated in each case.
    2. State the purpose of each program.
    3. What is the difference between a variable and a register.
    4. Once data has been retrieved from the keyboard, where is it placed?

4. Tracking instructions

  1. Test the program shown in section 3 of Working with SPIM and use SPIM to answer the following questions.

    Questions

    1. What is the content of the registers $s2, $s4 and $2 when $17 reaches the value of 5?
    2. What is the content of the registers $s2, $s4 and $2 when $17 reaches the value of A?
    3. How does SPIM translates the instruction move?
    4. How does SPIM translates the instruction la?
    5. How does SPIM encodes the instruction nop?
    6. How does SPIM encodes the instruction beq $18, $0, end_lop?
    7. What memory address is associated to the label loop.

  2. Only for 433 students: Make a MIPS program to iteratively perform the addition of the first 10 natural numbers and track it with SPIM.
    Make a list of the registers your program uses (including the program counter) and record their value on each iteration.

5. Debugging programs

  1. The following program executes fine, however it has some logical errors. Test the code, find the error(s), correct it(them), and then, answer the questions. (Note: The arithmetic operations inside the loop are correct.
    .data
    message:  .asciiz "...and the result is: " 
    .text
    .globl main
    main:
          la $4, message       
          li $v0, 4            
          syscall              
          nop                  
          move $20, $zero      
          move $17, $0         
                               
    loop: slti $18, $17, 10   
          beq $18, $0, end_lop
          mul $20, $20, $17   
          add $17, $17, 1     
          j loop              
          nop
    end_lop: move $4, $20      
          li $v0,1             
          syscall              
    _exit:                     
          li $v0,10            
          syscall              
    

    Questions

    1. What result does the program produce before any modification?
    2. Why do you think this result is produced?
    3. What result does the program produce after modifications?
    4. Describe how did you solve the problem.
    5. What do you think was the intended purpose of the program?

  2. Only for 433 students: The following program was meant to add four numbers; however, it has not been completed and has several bugs.
    Correct and complete the program, add comments, test it, and answer the final questions.
                .data
                message:  .asciiz "Welcome to the world of MIPS programming\n"
                message2: .asciiz "Iteration: "
                message3: .asciiz "\n"
                first:    .word 21
                          .word 14
                          .word 26
                          .word 39        
                          
                .text
                .globl main
    
                _start:
                main:
                la $4, message
                jal putstr
                nop
                la $16, first
                ori $20, 0x0
                move $17, $0
                loop:slt $18, $17, 0x03
                beq $18, $0, end_lop
                nop
                lw $19, 0($16)
                nop
                add $20, $20, $19
                add $16, $16, 0x4
                addi $17, $17, 1
                la $4, message2
                jal putstr
                nop
                move $4, $17
                jal writeint
                nop
                la $4, message3
                jal putstr
                nop
                j loop
                nop
                end_lop: move $4, $20
                jal writeint
                j _exit
                nop
    

    Questions

    1. How many errors did you find in the program?
    2. Why the program did not perform all the iterations?
    3. What is the purpose of the label first: ?
    4. What was the last error?
    5. What is the purpose of the label _start: ?
    6. What is the purpose of the label message: ?
    7. What is the purpose of the label putstr ?

    Any concern about the goals, questions, or wording of this document, please send a message to Jorge Vasconcelos.
    Return to the CSF Programming homepage