Last Updated: 2026-02-22 Sun 21:59

CMSC216 Lab04: Debugger Usage and Masking

CODE DISTRIBUTION: lab04-code.zip

CHANGELOG: None

1 Rationale

As C programs become more complex, it is useful to have tools that support debugging their problems. GDB is a traditional terminal-based debugger that handles C programs well. This labs contains an exercise introducing GDB and to inspect a program with behavior that is not easily understood from its source code alone.

Certain applications utilize bits at an individual level. This is enabled by support for bit-level operations in most processors which are presented in C programs as bit-wise logical and shift operations. This lab explores the basic mechanics of these operations in preparation for project work which will provide a concrete application for them. It also includes a callback to GDB usage to remind students of the insight the debugger can have.

Associated Reading

  • Practical use of GDB is covered in The Quick Guide to GDB with the complete account of its capabilities covered in Debugging with GDB, its official manual.
  • Bryant and O'Hallaron Sections 2.1.6 to 2.1.9 discuss bit-level operations in C. Numerous examples are provided that complement material presented here.

Grading Policy

Credit for this lab is earned by completing the code/answers in the Lab codepack and submitting a Zip of the work to Gradescope preferably via running make submit. Students are responsible to check that the results produced locally are reflected on Gradescope after submitting their completed Zip.

Lab Exercises are Free Collaboration and students are encouraged to cooperate on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.

No late submissions are accepted for Lab work but the lowest two lab scores for the semester will be dropped including zeros due to missing submissions. See the full policies in the course syllabus.

2 Codepack

The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.

File Use Description
QUESTIONS.txt EDIT Questions to answer: fill in the multiple choice selections in this file.
debugger_secrets.c Provided Problem 1 program to analyze under GDB
debugger_input.txt EDIT Problem 1 input file to edit as input to debugger_secrets.c
masking.c EDIT Problem 2 C file to edit and complete; TODO sections are marked in the code
masking_input.txt EDIT Problem 2 Input file for masking.c which should be edited to contain correct inputs.
Makefile Build Enables make test and make zip
QUESTIONS.txt.bk Backup Backup copy of the original file to help revert if needed
QUESTIONS.md5 Testing Checksum for answers in questions file
test_quiz_filter Testing Filter to extract answers from Questions file, used in testing
test_lab04.org Testing Tests for this lab
test_lab04_code.org Testing Tests for the code portion of the lab to allow individual problem testing
testy Testing Test running scripts
gradescope-submit Misc Allows submission to Gradescope from the command line

3 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the lab. Follow the instructions in it to complete the QUIZ and CODE questions for the lab.

                           _________________

                            LAB03 QUESTIONS
                           _________________


Exercise Instructions
=====================

  Follow the instructions below to experiment with topics related to
  this exercise.
  - For sections marked QUIZ, fill in an (X) for the appropriate
    response in this file. Use the command `make test-quiz' to see if
    all of your answers are correct.
  - For sections marked CODE, complete the code indicated. Use the
    command `make test-code' to check if your code is complete.
  - DO NOT CHANGE any parts of this file except the QUIZ sections as it
    may interfere with the tests otherwise.
  - If your `QUESTIONS.txt' file seems corrupted, restore it by copying
    over the `QUESTIONS.txt.bk' backup file.
  - When you complete the exercises, check your answers with `make test'
    and if all is well. Create a zip file and submit it to Gradescope
    with `make submit'. Ensure that the Autograder there reflects your
    local results.
  - IF YOU WORK IN A GROUP only one member needs to submit and then add
    the names of their group on Gradescope.


PROBLEM 1: GDB and Debugger Secrets
===================================

  Staff will survey the `debugger_secrets.c' file which reads input from
  a text file specified on the command line.  A sample input file is in
  `debugger_input.txt' and students will nee to put start by putting
  their terpmail email address on the first line. The file will need to
  have additional inputs BUT determining exactly what they are is quite
  challenging even though the source code for the program is available.

  Instead, staff will illustrate how to start the program in a debugging
  session using GDB, the GNU Debugger. A large amount of detail about
  how to do so is available on GDB Quick guide which is produced by
  staff:

  <https://kauffman77.github.io/tutorials/gdb.html>

  A typical session will start with
  ,----
  | >> gdb -tui debugger_secrets
  | 
  | (gdb) set args debugger_input.txt
  | (gdb) break main
  | (gdb) run
  | ...
  | (gdb) next
  | (gdb) print something
  | ...
  | (gdb) quit
  | >> 
  `----


QUIZ Questions
~~~~~~~~~~~~~~

  To set a breakpoint to stop execution at a specific spot in code one
  can use the GDB command
  - ( ) `break 15' which will stop at line 15 in the current file
  - ( ) `break file.c:15' which will stop at line 15 of `file.c' when it
    is reached
  - ( ) `break func_name' which will stop when the function `func_name'
    is reached
  - ( ) `break' which will stop when the current line is reached again
  - ( ) Actually all of these are valid: it's nice to have options!

  The following command will continue executing code until the next
  break point is reached.
  - ( ) `run' or `r'
  - ( ) `continue' or `c'
  - ( ) `break' or `b'
  - ( ) `execute' or `e'

  The following lines in the `debugger_secretes.c' do something peculiar
  and should be analyzed and discussed.
  ,----
  | char userid[10] = ...;
  | ...;
  | int magic1 = *((int *) userid);
  `----
  Which of the following best describes how the value for `magic1' is
  set?
  - ( ) The number stored in `userid' is converted from a string to an
    integer and stored in `magic1'
  - ( ) The memory address of userid is used as an integer to set
    `magic1'
  - ( ) userid is cast as an integer pointer and the 4 bytes at its
    beginning of it are treated as an int and used to set `magic1'
  - ( ) This line has unpredictable behavior and changes from one run to
    the next making it very difficult to get the input correct.

  Which of the following lines that appears later in
  `debugger_secretes.c' is most related to how `magic1' is set?
  - ( ) `int hash = magic1 ^ magic2;' because it also uses the same
    variable
  - ( ) `int mod1 = pb_rand() % quote_len;' because it involves an
    unpredictable result that changes from run to run
  - ( ) `quote_actual[mod1] = '_';' because it involves a character
    array
  - ( ) `int *quote_as_int = (int *) quote_actual; quote_as_int[mod3] =
      1600085855;' because this combination similarly subverts the
      normal type system treating character data as integers.


CODE
~~~~

  By analyzing the code in `debugger_secrets' and using GDB, determine
  the correct input needed in `debugger_input.txt' to run the program to
  completion.

  NOTE: students should use their own @terpmail.umd.edu addresses at the
  beginning of the input which will lead to slightly different answers
  being required than other students. Email addresses will be checked on
  the Gradescope autograder.

  Test that the code behaves correctly via the command
  ,----
  | make test-code testnum=1
  `----


PROBLEM 2: Background on masking.c
==================================

  Study the code in `masking.c' which uses a number of bit-wise
  operations that have recently been discussed in lecture. These
  include:
  ,----
  | | Operation | Description          |
  | |-----------+----------------------|
  | | x & y     | Bit-wise And         |
  | | x | y     | Bit-wise Or          |
  | | x ^ y     | Bit-wise Xor         |
  | | ~x        | Bit-wise inversion   |
  | | x << y    | Bit-wise Left-shift  |
  | | x >> y    | Bit-wise Right-shift |
  `----
  Study the examples provided in `masking.c' and ensure you understand
  their meaning according to the context provided. Then answer the
  following questions.


Terminology
~~~~~~~~~~~

  - "Low order bits" or simply "the low bits" are written on the right
    side of binary numbers while "high order bits" appear on the
    left. The low/high distinction relates to the power of two
    associated with bits in that position.
  - "Bit indexes" start with 0 for the low order bit (right-most) and
    number up when moving to the left.  For example the number
    `0b10110001' has bits at the following indices:
    ,----
    | Value: 1011 0001
    | Index: 7654 3210  
    `----

    Bit index 0 is a "1" (the lowest order bit) and bit index 7 is also
    a 1 (the highest order bit).
  - "Setting" a given bit means to place a 1 in that position; "Set bit
    7" means to ensure that the 7th bit from the right is a 1
  - "Clearing" a give bit means to place a 0 at that position; "Clear
    bit 7" means to ensure that the 7th bit from the left is a 0


PROBLEM 2: QUIZ on masking.c
============================

Shift with Or
~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | x = x | (1 << 19);
  `----

  - ( ) Changes `x' so that only its 19th bit is a 1, all other bits are
    0.
  - ( ) Changes `x' so that only its 19th bit is a 0, all other bits are
    unchanged.
  - ( ) Changes `x' so that its 19th bit is set to 1, all other bits
    unchanged.
  - ( ) Changes `x' so that its bits are shifted left by 19 places.


Shift with Or with Pattern
~~~~~~~~~~~~~~~~~~~~~~~~~~

  If `x' is cleared (value all 0's), what is the effect of the following
  line of code:
  ,----
  | x = x | (0b110101 << 8);
  `----

  - ( ) Sets the bits of `x' to the given pattern `0b110101' starting
    with the 8th position.
  - ( ) Checks that `x' has the given pattern `0b110101' starting with
    the 8th position.
  - ( ) Copies the given pattern `0b110101' 8 times throughout `x'
  - ( ) Adds 53 on the value of `x' faster than normal addition.


Shift with Invert with And
~~~~~~~~~~~~~~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | x = x & ~(1 << 8);
  `----

  - ( ) Changes `x' so that only its 8th bit is a 1, all other bits are
    0.
  - ( ) Changes `x' so that only its 8th bit is a 0, all other bits are
    unchanged.
  - ( ) Changes `x' so that its 8th bit is set to 1, all other bits
    unchanged.
  - ( ) Changes `x' so that its bits are shifted left by 8 places.


Shift with And
~~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | if( x & (1 << 13) ){ ... }
  `----

  - ( ) Conditionally execute only if the 13th bit of `x' is 0 and set
    that bit subsequently.
  - ( ) Conditionally execute only if the 13th bit of `x' is 0 but leave
    `x' unchanged.
  - ( ) Conditionally execute only if the 13th bit of `x' is 1 and set
    that bit subsequently.
  - ( ) Conditionally execute only if the 13th bit of `x' is 1 but leave
    `x' unchanged.


PROBLEM 2: CODE Complete masking.c
==================================

  Complete the TODO items in the `masking.c' file so that the missing
  blocks produce the effect mentioned in the `printf()' statements.

  The `masking.c' program ends with an interesting arrangement of bits
  that must be matched by writing some numbers in an `masking_input.txt'
  file.  Use GDB to help determine what numbers are appropriate and how
  they should be placed so that the "shot" is formed correctly to match
  the "target" leading to a program exit code of 0.

  Test that the code behaves correctly via the command
  ,----
  | make test-code testnum=2
  `----

4 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.


Web Accessibility
Author: Chris Kauffman (profk@umd.edu)
Date: 2026-02-22 Sun 21:59