CSE/EEE 230 Project 1

Due Feb 17, 2011.   11:59 pm

 

Part I: Assignment

 

Problem 1: Write MIPS instructions for the following code fragment:

 

x = a * ((b + 2) << 2)

 

a and b are integers stored in registers $s0 and $s1 respectively, and the final result x should be saved to register $s2. You can use any temporary register(s) if needed. << represents logical shift left operator (sll). Part of the program is given as follows so that you can output your result in console window. Let us set a to 5 and b to 2. Then your result should be 80. Test with any other numbers.

 

#Problem 1

#Your Name ( your email )

mainli                      $s0,     17                    # initialize a = 17

            li                      $s1,     4                      # initialize b = 4

 

# put your code here

 

            li                      $v0,     1                      # print integer

     move              $a0,    $s2                  # what to print is stored at $s2

            syscall                                                # make system call

 

exit:     li                      $v0,     10                    # exit system call

     syscall           

#end of Problem 1

 

 

Note that the focus of this assignment is not the system call. Just use the above code segments to print out any integer number and at the end of program. For information on system calls, google “MIPS system call” or look here.

 

 

Problem 2:

Transform the following C code into a MIPS assembly code. The given program is to add every odd number less than an arbitrary even number. The result should be the square of the half of the number. The code below is for 20, so the result should be 100. Test with any large even number.

 

int x = 0;

for ( int i = 0; i < 20; i++ )

{

     if ( i % 2 != 0 )

                 x += i;

}

printf(“%d”, x);   // Use the code segment in Problem 1 for printing an integer.

 

Note:  There is no modulo operation in MIPS.

 

 

Problem 3:  
Write a MIPS assembly program that corresponds to the following C-like program. This is a practice to use various conditional branch instructions. The output should be “321”. 

 

int A[3] = {0, 1, 2};

int x;

for ( int i = 0; i < 3; i++ )

{

     if ( A[i] > 1 )

                 x = 1;

     else

{

                 switch ( A[i] )

                 {

                 case 0:

                             x = 3;

                 case 1:

                             x = 2;

                 }

}

     printf(“%d”, x);

}

 

 
Problem 4: -- challenge problem. Do not submit. No points for doing this.
 
In the above program (problem 3) if there is a large number of case statements, there will be a lot of labels. Write programs that use minimal labels using each of the methods outlines below:
1.      Use PC-relative branches (BEQ $0 $0 <relative address>) to implement the switch case statement.
2.      Use a jump table, that is an array in memory that contains the addresses of the jump points, and the switch on A[i] indexes into this table.

Part II: Submission instruction

Save your program files (one for each problem) with extension .s or .asm. Comment your code well and include your name and email in each source file. Zip all files into one file and send it to teaching assistant’s email. Please keep the following file naming rules.

 

            Source file: Problem1.asm, Problem2.asm, Problem3.asm

Zip file: LastName_FirstName_ASUID.zip

 

Attention:

Only last submission will be graded if multiple submissions are found.

The last time to submit is Feb 17th 11:59 PM.

Late submission is rarely accepted, and may carry penalties.

 

Grading Critea:

Program should work correctly (70%) – if the program results a wrong answer, you get no point of this 70%.

Comments and readability (30%) – However, you won’t need to write comments longer than total source code length. Writing a corresponding C code for a group of assembly instructions will be helpful.