CS 35101 HW#01 Spring 2006

Due Date Friday, Feb 10th ( 100 Points)

MIPS and SPIM Reading and Tutorial Exercises ( Assigned ….)

1)Read text Appendix A.9, A.10

2)Become familiar with XSPIM on the CS department unix system.

3)If possible obtain PCSPIM at . Install it on your own PC and become familiar with it’s operation.

4)Go through Part 1-4 of the programmed introduction to assembly language programming using at . Parts 3 and 4 give practice with PCSPIM

SPIM Programming Exercises “Bare Metal” mode

For these exercises: start XSPIM with the –notrap and –nopseudo options. If using PCSPIM, select the bare machine option box from the Simulator::Settings… menu. Be sure to set the PC register to the starting address of your code before stepping through it. This will usually be 0x00400000.

5)(25)Convert the following C code to MIPS code and run it on XSPIM and PCSPIM (if possible)
D = A + B – C * 4;
Assume A, B, C, D are in registers $t1 – $t4. Give them initial values of 12, 33, 5 and 0. Remember you will not be able to use any pseudo-instructions like load_immediate
List your SPIM code and the contents (in decimal and hexadecimal) of registers PC, $t1-$t4 both before and after your code has been executed
Repeat the above using initial values of 1, 2, 3 and 0 for A, B, C and D.

6)(20)Write MIPS code to do the following sequence of actions:

  1. Put the hexadecimal value 0x41424344 in register $t1.
  2. Extract byte 0 from $t1 and place it in byte 3 of register $t2 ( bytes 0-2 should be all 0s)
  3. Extract byte 1 from $t1 and place it in byte 3 of register $t3 ( bytes 0-2 should be all 0s)
  4. Extract byte 2 from $t1 and place it in byte 3 of register $t4 ( bytes 0-2 should be all 0s)
  5. Extract byte 3 from $t1 and place it in byte 3 of register $t5 ( bytes 0-2 should be all 0s)
    Assume Big Endian format for byte sequencing.
  6. Run your code and show the contents of PC, $t1 - $t5 before and after the code has executed.

SPIM Programming Exercises “asm” mode.
For these exercises: start XSPIM in default mode. If using PCSPIM use the following settings:


7)(20)Write MIPS code which

  1. Prompts the user to enter a positive integer, N.
  2. Reads the integer into a variable labeled N
  3. Calculates the sum of the integers from 1…N ( use formula: sum = (N/2) x (N+1) )
  4. Outputs the result
  5. A sample of input / output is:
    Please enter a positive integer:10
    The sum of the integers from 1 to 10 is 30
  6. List your spim code

MIPS Instruction Questions

8)(10) <Section 2.5> The full MIPS instruction set has another logical operation not mentioned thus far: xor. The operation xor stands for exclusive OR. The table that follows defines this operation on a bit-by-bit basis. This instruction is useful inthe following exercise.

A / B / A xor B
0 / 0 / 0
0 / 1 / 1
1 / 0 / 1
1 / 1 / 0

a) Show the minimal MIPS instruction sequence for a new pseudo instruction called swap that exchanges two registers. After the sequence completes, the Destination register has the original value of the Source register, and the Source register has the original value of the Destination register. Convert this pseudo instruction: swap $s0,$s1 into MIPS instructions. The hard part is that this sequence must use only these two registers! [Hint: Itcan be done in three instructions if you use the new logical instruction. What is the value of (A xor B xor A)?]

b) Run your code sequence to verify that it works properly

9)(10) Convert the following MIPS assembly code instructions into MIPS machine code:

  1. add $s1, $t1, $t2
  2. lw $s3, 8($t8)
  3. sw $s3, 24($t9)
  4. sll $s2, $s0, 8
    Note: show each machine code instruction as a 32 bit binary numbers. Indicate the bits which belong to each field of the instruction

10)(5) What is the base 10 representation of 0x7fff fffa ?

11)(5) What is the hexadecimal representation of the base 2 number:
1100 1010 1111 1110 1111 1010 1100 1110two

12)(5) Why doesn’t MIPS have a subtract immediate instruction?