COMP 411: Lab 9: Logical Shifts

Due Wednesday, March 28, 2018, 11:59 PM

This assignment consists of two exercises, both of which provide practice in logical and shift operations. The first exercise converts decimal numbers read from the input into binary numbers. The second exercise implements a pseudo-random number generator using the well-known linear feedback shift register (LFSR) method.


Exercise 1: Printing numbers in binary

In this exercise, you are to write a program that reads a sequence of decimal numbers on the standard input (one per line), and prints the 16-bit binary representation of each number (one per line), until a 0 is read on the input. The numbers entered may be positive or negative, but you can assume they will be within the range of 16-bit binary numbers (-2^15 to 2^15-1). For example, if the input consists of:

1
8
31
-1
0

Then, the following output is produced:

0000000000000001
0000000000001000
0000000000011111
1111111111111111
0000000000000000

Name the file with your C code ex1.c. Test it using your own inputs, and also the sample inputs provided.

Once your C code is working correctly, write the equivalent MIPS assembly program. Name it ex1.asm. Test it similarly.

Note: Integer values within the C and MIPS programs are already internally represented in binary form. For this exercise, you simply need to examine the appropriate bits using only logical and shift operations! That is, no division or multiplication or power operations are needed. Shift and AND operations are all you need to generate the binary representation of an integer. Carefully study, in particular, srl, srlv, and andi instructions before proceeding.

Exercise 2: Pseudo-random number generator

In this exercise, you are to write a pseudo-random number generator (PRNG) using the classic linear feedback shift-register (LFSR) technique (see Wikipedia article).

We will focus on random numbers in the range 1 to 31 only. Given a starting value (sometimes called the "seed") in the range 1..31, the algorithm generates a succession of random numbers in that range, going through all the 31 values, until the starting value is reached again, at which time the program terminates.

The algorithm works as follows. Suppose the current random number is R. Let us represent its binary form using 5 bits, as R4R3R2R1R0. The next random number in the sequence is produced by shifting R to the left by one bit position, and inserting a new bit B into the LSB position. The value of B is given by the XOR of R4 and R2.

That is:

B = R4 XOR R2
next R = R3R2R1R0B

A C language implementation of the PRNG is provided (ex2.c). Your task is to write an equivalent MIPS program. Name it ex2.asm. Test it by giving it different starting numbers; each time, the output should be a permutation of the numbers 1 to 31, each appearing only once, until the starting number is reached again. You can also test it using the sample input/output files provided.


Test Inputs, Due Date and Submission Procedure

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab9. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.

Your assignment must be submitted electronically by 11:59pm on Wednesday, March 28.

First copy the files ex1.c, ex1.asm and ex2.asm to the CS Dept server (classroom.cs.unc.edu) under the appropriate folder in your home directory (e.g., comp411lab/lab9). Then, run the submit script:

 % /home/porter/comp411/bin/comp411start
 % cd ~/comp411lab/lab9
 % selfchecklab9
 % submitlab9

In case of any problems, please contact the instructor or the TAs.


20 March 2018, Montek Singh, montek@cs.unc.edu

Last updated: 2018-03-22 20:14:23 -0400 [validate xhtml]