My Project
|
Makefile
to build C programs.
Makefile
to add files and options,
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.
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:
$ 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
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.
R3.tar
file.
$ make clean
$ make package
whatever
that echoes
"Running whatever...", and runs the 'date', 'whoami' and'uname -a'
commands, int that order, and verify that it works.
./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
You now have a functioning program. All the commands run,
however, only bin
will produce correct results at this point.
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:0The 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
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.
field.c
to the Checkin tab on the course website
to get credit for this lab.