CMSC216 HW08: Review for Exam 2
- Due: 11:59pm Sun 02-Nov-2025
- Approximately 0.83% of total grade
CODE DISTRIBUTION: hw08-code.zip
- Download the code distribution
- See further setup instructions below
CHANGELOG: Empty
1 Rationale
This HW reviews concepts from earlier exercises and lecture to prepare for an exam. Discussion leaders will give time to work, answer questions on the review problems here, and lead discussion of solutions to them. Note as well that there is an upcoming practice exam and you may discuss its solution to help prepare for the exam.
Associated Reading / Preparation
Review the following sections from Bryant and O'Hallaron:
- Ch 3.1-3.7 on assembly code basics, arithmetic, memory operations, control, and procedure calls
- Ch 3.8-9 on assembly code treatment of C structs and arrays
- Ch 3.10 on stack safety measures is useful for understanding certain classes of errors such as stack smashing
- Ch 3.11 on Floating Point operations in assembly will NOT be on the exam.
Grading Policy
Credit for this HW is earned by taking the associated HW Quiz which is
linked under Gradescope. The quiz will ask similar questions as
those that are present in the QUESTIONS.txt file and those that
complete all answers in QUESTIONS.txt should have no trouble with
the quiz.
Homework and Quizzes are open resource/open collaboration. You must submit your own work but you may freely discuss HW topics with other members of the class.
See the full policies in the course syllabus.
2 Codepack
The codepack for the HW contains the following files:
| File | State | Description |
|---|---|---|
QUESTIONS.txt |
EDIT | Questions to answer |
posneg_asm.s |
EDIT | Problem 1 assembly code to debug |
posneg_main.c |
Provided | Problem 1 main function used for debugging |
col_check_main.c |
Provided | Problem 2 main function for testing |
col_check.c |
Provided | Problem 2 C function to convert |
col_check_asm.s |
CREATE | |
badstack.c |
Provided | Problem 3 C file representing correct behavior |
badstack_asm.s |
EDIT | Problem 3 assembly file which contains bugs to analyze and correct |
3 What to Understand
Ensure that you have a good understanding of basic x86-64 assembly programming including register naming, ALU instructions, addressing in operands, control flow, procedure calls, and the relation of these to C code.
4 Questions
Analyze the files in the provided codepack and answer the questions
given in QUESTIONS.txt.
_________________
HW 08 QUESTIONS
_________________
Write your answers to the questions below directly in this text file to
prepare for the associated quiz. Credit for the HW is earned by
completing the associated online quiz on Gradescope.
PROBLEM 1: Movement Mistakes
============================
Analyze the files `posneg_main.c' and `posneg_asm.s'. The C code uses
a function in assembly and the assembly function has a common bug in
it.
A
~
Compile the files together and run the resulting program. Make sure
that you understand how to run a `gcc' command to compile the two
files, C and assembly code, to produce an executable.
After running the resulting program, explain why the output appears
strange based on the local variables defined in `posneg_main.c' and
the purpose of the function in `posneg_asm.s'.
B
~
Analyze the code in `posneg_asm.s' carefully and compare the `movX /
cmpX' instructions used in the first few lines against the types of
variables in the `posneg_main.c' code. You may want to step into this
function in GDB to look at the register values after the `movX'
instruction. Find a bug in this sequence and describe why it causes
the loaded value to appear negative.
C
~
Fix the bug in `posneg_asm.s' and paste your corrected code below.
PROBLEM 2: Convert C to Assembly
================================
Convert the C function in the file `col_check.c' to x86-64
assembly. Note that the parameter is a packed struct, not a pointer to
a struct.
,----
| typedef struct{
| int cur; // current value in collatz sequence
| int step; // step number in collatz sequence
| } colinfo_t;
| // | | Byte | Byte | Packed |
| // | Field | Size | Offset | Bits |
| // |-------+------+--------+--------|
| // | cur | 4 | +0 | 0-31 |
| // | step | 4 | +4 | 32-64 |
|
| int col_check(colinfo_t info){
| // Analyzes values in the packed struct arg
| // info to detect errors in it. An int
| // comprised of set error bits is
| // returned. Bit 0: cur field was 0 or
| // below, Bit 1: step was negative, Bit 2:
| // cur was 1 but step is negative.
|
| int cur = info.cur;
| int step = info.step;
| int errs = 0;
| if(cur <= 0){
| errs = errs | 0x1; // 0b0001
| }
| if(step < 0){
| errs = errs | 0x2; // 0b0010
| }
| if(cur==1 && step < 0){
| errs = errs | 0x4; // 0b0100
| }
|
| return errs;
| }
`----
PROBLEM 3: Valgrind Debugging Assembly
======================================
A
~
Study the C file `badstack.c' which presents a small `main()' function
which calls `inc_larger()'. Compile this file and run the resulting
executable.
After you are comfortable with the C version, examine similar
`badstack_asm.s' file which encodes the same two functions and
algorithm but has bugs. Compile and run this file and show the
results.
B
~
While there is output produced by `badstack_asm.s' in most cases, it
will not complete successfully. To gain insight into what is
happening, recompile the program with Debugging information by passing
an additional flag to `gcc'. Then run the program under Valgrind to
show more information on the nature of the problem. Show the output
from Valgrind below.
C
~
Analyze the Valgrind output carefully. It should contain one major
error. Copy and paste the output that pertains to the error below and
describe what you think might be going on. Relate anything you see in
the output to the values for variables that show up in
`badstack_asm.s', particularly anything that is labeled as a 'Invalid
Address' is relevant. As a hint, the problem lies with data that is in
the stack.
D
~
Use your knowledge of what has happened to repair the buggy `main()'
function in `badstack_asm.s'. Describe the changes required below.
After fixing the code, compile and run it to make sure that it
performs correctly and identically to `badstack.c' does. You will
likely need to make changes towards the beginning of `main()' and just
prior to its `ret' instruction.
Hint: You may also compile `badstack.c' to assembly code and examine
the first 5-8 instructions in `main()' to see how it manipulates the
stack to make space for local variables. Keep in mind that you should
compile with the `-Og' option to optimize for debugging and that the
setup generated by the compiler is somewhat more complex than required
in the hand-coded `badstack_asm.s'. You may also examine
complementary stack manipulations near `ret' at the end of `main()'.