My Project
cs270 Recitation R3 - Bit Fields in C
Recitation R3: CS 270 Computer Organization

Recitation R3 - Bit Fields in C


Programming done in lab week of Feb. 1, due Friday, Feb. 5 at 11:59pm.


This recitation has six objectives:
  1. To understand how to use a Makefile to build C programs.
  2. to learn how to modify the Makefile to add files and options,
  3. to write a C program that manipulates the bits of integer values,
  4. to learn the C language operators for binary numbers,
  5. to build a more complex C program with multiple files,
  6. to see if you can follow directions!

About The Recitation

This recitation may be helpful for future assignments. You will learn how to use the C language operators for binary and (&), binary or (|), and binary not (~). You will use the C language bit shift operators (<< and >>). We will also introduce Makefiles.

The goal of the assignment is to implement a small C library (5 functions) that enables getting and setting bits and fields in a binary number. This is especially useful for playing around with numerical representations, for example you could build a new floating point number from scratch by setting the sign bit, exponent, and mantissa, or you could analyze an existing floating point number by extracting the same fields. We will use it later in this class for understanding number representations and for converting LC3 assembly code into machine code. To get started, read the Getting Started section below and then study the documentation for field.h in the Files tab to understand the details of the assignment.


Getting Started

Perform the following steps
  1. Copy the four files into this directory. It is easiest to right click on the link, and use Save Link As ... to save the file in your directory. While you may use copy/paste to save the file, it may convert the required tabs of Makefile into spaces.
    1. field.h (do not modify)
    2. field.c (complete this file)
    3. Makefile (do not modify)
    4. testField.c (do not modify)
  2. Open a terminal and make sure you are in the directory you created. The cd command can be used for this.
  3. In the terminal, do an 'ls' command. You may need to rename Makefile.txt as Makefile, if the browser renamed it during download. Next run 'make', and you should see the following output:
    
        $ make
        Compiling each C source file separately ...
        c11 -g -Wall -c field.c
    
        Compiling each C source file separately ...
        c11 -g -Wall -c testField.c
    
        Linking all object modules ...
        c11 -g -Wall field.o testField.o  -o testField
        
  4. After you build the program using the make command as shown above, use the 'touch' command to update the field.c source file and rebuild the program using make. Note which files are recompiled and linked. This is the purpose of a Makefile, to figure out based on dependencies which modules need to be rebuilt, without always building everything.
  5. Type the following command to clean up the directory, and note which files are cleaned. Change the clean target to remove the R3.tar file.
            $ make clean
        
  6. Type the following command to package the directory, and note how the package is built.
            $ make package
        
  7. Add a target to the Makefile called whatever that echoes "Running whatever...", and runs the 'date', 'whoami' and'uname -a' commands, int that order, and verify that it works.
  8. Now we're done with Makefiles, so in the terminal type ./testField and read how to run the test program supplied with the recitation.
  9. For example, type ./testField bin 11259375 and you should see the output:
    
        dec: 11259375  hex: 0xABCDEF  bin: 0000-0000-1010-1011-1100-1101-1110-1111
        

You now have a functioning program. All the commands run, however, only bin will produce correct results at this point.

Computing Bit Masks

In this recitation you will need masks whose values (in binary) have a variable number of consecutive 1's. Here are some examples of masks:
    0xFFFF0000 // 1's in 31:16, 0's in 15:0
    0x000000FF // 1's in 7:0, 0's in 31:8
    0x7F800000 // 1's in 30:23, 0's elsewhere
    0x80000000 // 1 in 31, 0's in 30:0
The following is an example of replacing bits 4-7 in a 32-bit integer with a new value, i.e. setField:
COMMAND:
./testField setField 0x00001234 7 4 9

ARGUMENTS:
old         0x00001234  0000 0000 0000 0000 0001 0010 0011 0100 
new         0x00000009  0000 0000 0000 0000 0000 0000 0000 1001
hi = 7
lo = 4

STEP 1) Create a mask for bits 4-7:

mask        0x000000F0  0000 0000 0000 0000 0000 0000 1111 0000

STEP 2) Use inverted mask to clear bits 4-7 in old value:

mask        0x000000F0  0000 0000 0000 0000 0000 0000 1111 0000
~mask       0xFFFFFF0F  1111 1111 1111 1111 1111 1111 0000 1111
old         0x00001234  0000 0000 0000 0000 0001 0010 0011 0100 
old & ~mask 0x00001204  0000 0000 0000 0000 0001 0010 0000 0100 

STEP 3) Shift the new value to be in the bits 4-7:

new         0x00000009  0000 0000 0000 0000 0000 0000 0000 1001
new <<      0x00000090  0000 0000 0000 0000 0000 0000 1001 0000

STEP 4) Clear any other bits besides 4-7 in new value:

mask        0x000000F0  0000 0000 0000 0000 0000 0000 1111 0000
new         0x00000090  0000 0000 0000 0000 0000 0000 1001 0000
new & mask  0x00000090  0000 0000 0000 0000 0000 0000 1001 0000

STEP 5) Combine the old and new values, and return the result:

old         0x00001204  0000 0000 0000 0000 0001 0010 0000 0100 
new         0x00000090  0000 0000 0000 0000 0000 0000 1001 0000
old | new   0x00001294  0000 0000 0000 0000 0001 0010 1001 0100 

RESULT
setField: set bits 4:7 in 0x1234 to 0x9 = 0x1294

Completing the Code

Before attempting to write any of the functions of field.c, study the documentation in found in the files tab. Plan what you need to do before writing code. The best way to be successful is to write, compile, and test a single function at a time.

Grading Criteria

Submit the single file field.c to the Checkin tab on the course website to get credit for this lab.
© 2016 CS270 Colorado State University. All Rights Reserved.