Lab 9: Logical Shifts

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.


Your assignment must be submitted electronically by 11:59pm on Wednesday, November 15. (This assignment is simpler than other recent assignments. Note that it is due on Wednesday, not Friday.)


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 8-bit binary representation of each number (one per line), until a 0 is read on the input. You can assume that the numbers entered will be positive and within the range of 8-bit binary numbers (0 to 255). For example, if the input consists of:

1
8
31
0

Then, the following output is produced:

00000001
00001000
00011111
00000000

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.

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/montek/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, November 15.

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:

% cd ~/comp411lab/lab9
% /home/montek/comp411/bin/selfchecklab9
% /home/montek/comp411/bin/submitlab9

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


8 November 2017, Montek Singh, montek@cs.unc.edu