CS 270
|
The goal of the recitation is to implement a small C library (4 functions) that enables some useful operations on an integer 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.
R3
. Copy the four files below
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.
field.h
(do not modify)field.c
(complete this file)Makefile
(do not modify)testField.c
(do not modify)cd
command can be used for this.
Makefile.txt
as Makefile
, if the browser renamed it during download.
Next run 'make', and you should see the following output:
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
./testField
and read how to run the test program supplied with the recitation.
./testField bin 11259375
and you should see the output:
dec: 11259375 hex: 0xABCDEF bin: 0000-0000-1010-1011-1100-1101-1110-1111
This shows you the representation of decimal number 11259375 in hexadecimal and 32-bit binary.
You now have a functioning program. All the commands run.
However, only bin
will produce correct results at this point.
value = 0000 0000 0000 0000 0001 0010 0011 1100 mask = 0000 0000 0000 0000 0000 0000 0000 1000 --------------------------------------- value & mask = 0000 0000 0000 0000 0000 0000 0000 1000
Notice that the mask was built by placing a 1 in the position of interest. How would you compute such mask? If you know that the position you want is always bit 3, then you can hard code this mask:
int mask = 0x00000008;
Think about how you could build a mask where the position you are interested in is stored in a variable (hint: use a shift operator).
How about clearing a bit? The example below shows how to clear bit 4 in a value:
value = 0000 0000 0000 0000 0001 0010 0011 1100 mask = 1111 1111 1111 1111 1111 1111 1110 1111 --------------------------------------- value & mask = 0000 0000 0000 0000 0001 0010 0010 1100
Notice that the mask was built by placing a 0 in the position of interest and 0 elsewhere. Again, how would you compute such mask if the position of interest is stored in a variable? (hint: look at how this mask is related to the mask used for extracting a bit)
value = 0000 0000 0000 0000 0001 0010 0011 1100 mask = 0000 0000 0000 0000 0000 0000 1000 0000 --------------------------------------- value | mask = 0000 0000 0000 0000 0001 0010 1011 1100
Notice that the mask was built by placing a 1 in the position of interest. How would you compute such mask if the position of interest is stored in a variable? (hint: refer to the mask computation for extracting a bit)
The previous examples dealt with one bit at a time. Sometimes, however, you
may need to deal with multiple bits at once (as in the getField
function). The most common obstacle when doing this is getting a group of
contiguous 1's. For example, suppose you want to create the following mask without
hard coding it:
mask = 0000 0000 0000 0000 0000 0000 0111 0000
How would you go about doing this without using loops? (hint: start from 1 and manipulate it using the shift and subtraction operators).
Note: in GCC, when you shift a signed value to the
right, the compiler will do an arithmetic shift. This means that it will
preserve the sign. For example, if you shift a negative number to the right,
it will bring 1's on the right instead of 0's. If you shift a positive number
to the right, it will bring 0's on the right. This will not happen if you
shift an unsigned number (for example, unsigned int
). In this case,
it will always bring 0's on the right (this is called a logical shift). Try the
following program (compile with gcc -o test test.c
):
#include <stdio.h>
int main() {
int a = 0xABCD0123; // Negative number
a = a >> 4; // Arithmetic shift
printf("%08X\n", a); // Print in hexadecimal
int b = 0x7BCD0123; // Positive number
b = b >> 4; // Arithmetic shift
printf("%08X\n", b); // Print in hexadecimal
unsigned int c = 0xFBCD0123; // Unsigned number
c = c >> 4; // Logical shift
printf("%08X\n", c); // Print in hexadecimal
return 0;
}
COMMAND: ./testField getField 0x000012B4 7 4 0 ARGUMENTS: old = 0x000012B4 = 0000 0000 0000 0000 0001 0010 1011 0100 hi = 7 lo = 4 sign = 0 (positive) RETURN: = 0x0000000B = 0000 0000 0000 0000 0000 0000 0000 1011
Notice how the field is all the way to the right and no further modifications were made (because sign = 0). Here is another example:
COMMAND: ./testField getField 0x000012B4 7 4 1 ARGUMENTS: old = 0x000012B4 = 0000 0000 0000 0000 0001 0010 1011 0100 hi = 7 lo = 4 sign = 1 (negative) RETURN: = 0xFFFFFFF5 = 1111 1111 1111 1111 1111 1111 1111 0101
The field in question is the same as in the previous example. However, we had to take the 2's complement because sign = 1.
Before attempting to write any of the functions of field.c
, study
the documentation 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.
Here are the line counts for my implementation:
getBit()
- 1 linesetBit()
- 1 linesclearBit()
- 1 linesgetField()
- 4 lines