created 06/20/2003; modified 06/06/2009, 07/25/15


Chapter 12 Programming Exercises


For these programming exercises, use only those instructions that have been discussed so far in these notes:

and andi nor or ori sll srl xor xori

In the Simulator/Settings/MIPS menu of SPIM set Bare Machine ON, Accept Pseudo Instructions OFF, Enable Delayed Branches ON, Enable Delayed Loads ON, Enable Mapped I/O OFF, Load Exception Handler OFF.

Run the programs by single stepping (pushing F10). Observing the results in the SPIM Int Regs window. Check the Registers menu that the registers are displayed in HEX.


*Exercise 1

Put the following bit pattern into register $1 :

DEADBEEF

A. Easy program: do this using just three instructions.

B. Somewhat harder: do this one letter at a time, using ori to load each letter (each nibble) into a register, then shifting it into position. You will need to use ori, or, and sll instructions. Look at the contents of register $1 as you push F10 to single step the program.

Click here to go back to the main menu.


*Exercise 2

In each register $1 through $7 set the corresponding bit. That is, in register 1 set bit 1 (and clear the rest to zero), in $2 set bit 2 (and clear the rest to zero), and so on. Use only one ori instruction in your program, to set the bit in register $1.

ori   $1,$0,0x01

Don't use any ori instructions other than that one. Note: bit 1 of a register is the second from the right, the one that (in unsigned binary) corresponds to the first power of two.

Click here to go back to the main menu.


*Exercise 3

Start a program with the instruction that puts a single one-bit into register one:

ori   $1,$0,0x01

Now, by using only shift instructions and register to register logic instructions, put the pattern 0xFFFFFFFF into register $1. Don't use another ori after the first. You will need to use more registers than $1. See how few instructions you can do this in. My program has 11 instructions.

Click here to go back to the main menu.


*Exercise 4

Put the bit pattern 0x55555555 in register $1. (Do this with three instructions.)

Now shift the pattern left one position into register $2 (leave $1 unchanged).

Put the the bit-wise OR of $1 and $2 into register $3. Put the the bit-wise AND of $1 and $2 into register $4. Put the the bit-wise XOR of $1 and $2 into register $5.

Examine the results.

Click here to go back to the main menu.


**Exercise 5

Put the bit pattern 0x0000FACE into register $1. This is just an example pattern; assume that $1 can start out with any pattern at all in the low 16 bits (but assume that the high 16 bits are all zero).

Now, using only register-to-register logic and shift instructions, rearrange the bit pattern so that register $2 gets the bit pattern 0x0000CAFE.

Write this program so that after the low 16 bits of $1 have been set up with any bit pattern, no matter what bit pattern it is, the nibbles in $2 are the same rearrangement of the nibbles of $1 shown with the example pattern. For example, if $1 starts out with 0x00003210 it will end up with the pattern 0x00001230

A. Moderately Easy program: do this using ori instructions to create masks, then use and and or instructions to mask in and mask out the various nibbles. You will also need shift instructions.

B. Somewhat Harder program: Use only and, or, and shift instructions.

Click here to go back to the main menu.


***Exercise 6

Start out register $1 with any 32-bit pattern, such as 0x76543210. Now, put the reverse of that pattern into register $2, for the example, 0x01234567.

Click here to go back to the main menu.


   * == easy program
  ** == moderately easy program
 *** == harder program
**** == project

End of Exercises