College students: Attendance at section is required and recorded for all college students. All college students should fill out this attendance form when attending section. For more on the attendance policy and what to do if you miss section refer to the syllabus here and here.
Extension School students: Extension School students are required to self-report weekly section participation using this participation form. There are two ways to participate in section: (1) attend section live (for most students, this is one of the zoom sections, but local students are welcome to attend in-person sections) OR (2) watch a section recording (Canvas > Zoom > Published Recordings). Both participation methods are equally valid towards your participation grade.
In today’s section, we’ll walk through a selection of our data representation exercises and assembly exercises, which are problems taken from prior exams. Also take the opportunity to ask questions about the problem set and class material.
All exercises assume a 64-bit x86-64 architecture unless otherwise stated.
The main subjects of these exercises are:
- DATAREP-1: Size and alignment rules.
- DATAREP-38: Memory layout.
- ASM-1: Assembly properties.
- ASM-3: Assembly and data structures.
- ASM-4: Assembly control flow.
- ASM-12: Assembly and data structures.
DATAREP-1. Sizes and alignments
QUESTION DATAREP-1A. True or false: For any non-array type X, the
size of X (sizeof(X)
) is greater than or equal to the alignment of
type X (alignof(X)
).
QUESTION DATAREP-1B. True or false: For any type T
, the size of
struct Y { T a; char newc; }
is greater than the size of T
.
QUESTION DATAREP-1C. True or false: For any types T1
...Tn
(with
n
≥ 1), the size of struct Y
is greater than the size of struct X
,
given:
struct X { T1 m1; ... Tn mn; };
struct Y { T1 m1; ... Tn mn; char newc; };
QUESTION DATAREP-1D. True or false: For any types T1
...Tn
(with
n
≥ 1), the size of struct Y
is greater than the size of union X
,
given:
union X { T1 m1; ... Tn mn; };
struct Y { T1 m1; ... Tn mn; };
QUESTION DATAREP-1E. Assume that structure struct Y { ... }
contains K char
members and M int
members, with K≤M, and
nothing else. Write an expression defining the maximum
sizeof(struct Y)
.
QUESTION DATAREP-1F. Given struct Z { T1 a; T2 b; T3 c; }
, which contains no padding, what does (sizeof(T1) + sizeof(T2) + sizeof(T3)) % alignof(struct Z)
equal?
QUESTION DATAREP-1G. Arrange the following types in increasing order by size. Sample answer: “1 < 2 = 4 < 3” (choose this if #1 has smaller size than #2, which has equal size to #4, which has smaller size than #3).
char
struct minipoint { uint8_t x; uint8_t y; uint8_t z; }
int
unsigned short[1]
char**
double[0]
DATAREP-38. Memory layout
The following code computes a Fibonacci number.
#include <cerrno>
#include <getopt.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
// copied from <cstdio> for your reference:
#define EOF (-1)
// copied from <getopt.h> for your reference:
extern int optind;
static void usage() {
fprintf(stderr, "Usage: ./fib NUMBER\n");
exit(1);
}
unsigned long fib(unsigned long n) {
if (n < 2) {
return n;
} else {
unsigned long tmp1 = fib(n - 1);
unsigned long tmp2 = fib(n - 2);
return tmp1 + tmp2;
}
}
int main(int argc, char *argv[]) {
// process command line options
char ch;
while ((ch = getopt(argc, argv, "h")) != EOF) {
switch (ch) {
case 'h':
case '?':
default:
usage();
}
}
// skip processed options
argc -= optind;
argv += optind;
// require 1 argument
if (argc != 1) {
usage();
}
// parse argument
unsigned long n = strtoul(strdup(argv[0]), nullptr, 10);
if (n == 0 && errno == EINVAL) {
usage();
}
// print fib(n)
unsigned long f = fib(n);
printf("fib(%lu) = %lu\n", n, f);
}
QUESTION DATAREP-38A. What are fib(0)
, fib(1)
, and fib(2)
?
QUESTION DATAREP-38B. What are sizeof(fib(0))
, sizeof(fib(1))
, and sizeof(fib(2))
?
For parts C–H, select from the list below the part of memory that best describes where you will find the object.
- In the heap
- In the stack
- Between the heap and the stack
- In a read-only data segment
- In a code segment
- In a read/write data segment
QUESTION DATAREP-38C. The string "fib(%lu) = %lu\n"
(line 58).
QUESTION DATAREP-38D. optind
(line 12)
QUESTION DATAREP-38E. tmp1
(line 23)
QUESTION DATAREP-38F. fib
(lines 19-27)
QUESTION DATAREP-38G. strdup
(line 51)
QUESTION DATAREP-38H. The string being passed to strtoul
(line 51).
(Read the manual page for
strdup
first!)
QUESTION DATAREP-38I. Does this program contain a memory leak?
QUESTION DATAREP-38J. Consider a call to fib(10)
that was called from
fib(11)
. Will fib(10)
’s tmp1
variable have a larger, smaller, or equal
address as fib(11)
’s?
ASM-1. Assembly properties
Here’s some assembly produced by compiling a C program. (The instructions are labeled with byte offsets for your convenience.)
.globl f
.align 16, 0x90
.type f,@function
f:
0: movl $1, %r8d
6: jmp b <f+0xb>
8: incl %r8d
b: movl %r8d, %ecx
e: imull %ecx, %ecx
11: movl $1, %edx
16: movl %edx, %edi
18: imull %edi, %edi
1b: movl $1, %esi
20: movl %esi, %eax
22: imull %eax, %eax
25: addl %edi, %eax
27: cmpl %ecx, %eax
29: je 40 <f+0x40>
2b: cmpl %edx, %esi
2d: leal 1(%rsi), %eax
30: movl %eax, %esi
32: jl 20 <f+0x20>
34: cmpl %r8d, %edx
37: leal 1(%rdx), %eax
3a: movl %eax, %edx
3c: jl 16 <f+0x16>
3e: jmp 8 <f+0x8>
40: pushq %rax
41: movl $.L.str, %edi
46: xorl %eax, %eax
48: callq printf
4d: movl $1, %eax
52: popq %rcx
53: retq
.type .L.str,@object
.L.str:
.asciz "%d %d\n"
.size .L.str, 7
QUESTION ASM-1A. How many arguments might this function take? List all that apply.
- 0
- 1
- 2
- 3 or more
QUESTION ASM-1B. What might this function return? List all that apply.
- 0
- 1
- −1
- Its first argument, whatever that argument is
- A square number other than 0 or 1
- None of the above
QUESTION ASM-1C. Which callee-saved registers does this function save and restore? List all that apply.
%rax
%rbx
%rcx
%rdx
%rbp
%rsi
%rdi
- None of the above
QUESTION ASM-1D. This function handles signed integers. If we changed the C source to use unsigned integers instead, which instructions would change? List all that apply.
movl
imull
addl
cmpl
je
jl
popq
- None of the above
QUESTION ASM-1E. What might this function print? List all that apply.
0 0
1 1
3 4
4 5
6 8
- None of the above
ASM-3. Assembly and Data Structures
For each code sample below, indicate the most likely type of the data being accessed. (If multiple types are equally likely, just pick one.)
QUESTION ASM-3A. movzbl %al, %eax
QUESTION ASM-3B. movl -28(%rbp), %edx
QUESTION ASM-3C. movsbl -32(%rbp), %eax
QUESTION ASM-3D. movzwl -30(%rbp), %eax
For each code sample below, indicate the most likely data structure
being accessed (assume that g_var
is a global variable). Be as
specific as possible.
QUESTION ASM-3E. movzwl 6(%rdx,%rax,8), %eax
QUESTION ASM-3F. movl (%rdx,%rax,4), %eax
QUESTION ASM-3G.
movzbl 4(%rax), %eax
movsbl %al, %eax
For the remaining questions, indicate for what values of the register contents will the jump be taken.
QUESTION ASM-3H.
xorl %eax, %eax
jge LABEL
QUESTION ASM-3I.
testb $1, %eax
jne LABEL
QUESTION ASM-3J.
cmpl %edx, %eax
jl LABEL
ASM-4. Assembly control flow
The next four questions pertain to the following four code samples.
f1
f1: subq $8, %rsp call callfunc movl %eax, %edx leal 1(%rax,%rax,2), %eax testb $1, %dl jne .L3 movl %edx, %eax shrl $31, %eax addl %edx, %eax sarl %eax .L3: addq $8, %rsp ret
f2
f2: pushq %rbx xorl %ebx, %ebx .L3: movl %ebx, %edi addl $1, %ebx call callfunc cmpl $10, %ebx jne .L3 popq %rbx ret
f3
f3: subq $8, %rsp call callfunc subl $97, %eax cmpb $4, %al ja .L2 movzbl %al, %eax jmp *.L4(,%rax,8) .L4: .quad .L3 .quad .L9 .quad .L6 .quad .L7 .quad .L8 .L3: movl $42, %edx jmp .L5 .L6: movl $4096, %edx jmp .L5 .L7: movl $52, %edx jmp .L5 .L8: movl $6440, %edx jmp .L5 .L2: movl $0, %edx jmp .L5 .L9: movl $61, %edx .L5: movl $.LC0, %esi movl $1, %edi movl $0, %eax call __printf_chk addq $8, %rsp ret .LC0: .string "Sum = %d\n"
f4
f4: subq $40, %rsp movl $1, (%rsp) movl $0, 16(%rsp) .L2: leaq 16(%rsp), %rsi movq %rsp, %rdi call callfunc movl 16(%rsp), %eax cmpl %eax, (%rsp) jne .L2 addq $40, %rsp ret
Now answer the following questions. Pick the most likely sample; you will use each sample exactly once.
QUESTION ASM-4A. Which sample contains a for loop?
QUESTION ASM-4B. Which sample contains a switch statement?
QUESTION ASM-4C. Which sample contains only an if/else construct?
QUESTION ASM-4D. Which sample contains a while loop?
ASM-12. Assembly and data structures
Consider the following assembly function.
func:
xorl %eax, %eax
cmpb $0, (%rdi)
je .L27
.L26:
addq $1, %rdi
addl $1, %eax
cmpb $0, (%rdi)
jne .L26
.L27:
retq
QUESTION ASM-12A. How many parameters does this function appear to have?
QUESTION ASM-12B. What do you suppose the type of that parameter is?
QUESTION ASM-12C. Write C++ code that corresponds to it.