cs33 midterm
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Q1 Alignment Ailment
9 Points
struct S {
char a;
short b;
union {
struct {
struct S* c;
struct S* d;
} x;
struct {
long e;
int* f;
} y;
} z;
char g[20];
} star[2];
Q1.1
2 Points
If %rdi holds the starting address of star and you want %rax to hold the value of star[0].z.y.e after the
instruction, which instruction should be used, and what should the value of N be in the chosen instruction?
Which instruction?
mov N(%rdi), %rax
mov (%rdi, N), (%rax)
lea N(%rdi), %rax
lea (%rdi, N), %rax
mov N(%rdi), (%rax)
lea (%rdi, N), (%rax)
lea N(%rdi), (%rax)
mov (%rdi, N), %rax
Q1.2
2 Points
Value of N for the chosen instruction (please answer in decimal)
8
Q1.3
2 Points
What is the alignment size of the defined structure type S? Please write in decimal.
8
Q1.4
3 Points
How many bytes in total does it take to store star in memory? Please write in decimal.
96
Q2 Switch Sanity
9 Points
Here is the assembly code for the function int foo(int x, int y);
000000000040052d
40052d: 8d 47 ea lea -0x16(%rdi),%eax
400530: 83 f8 06 cmp $0x6,%eax
400533: 77 2d ja 400562
400535: 89 c0 mov %eax,%eax
400537: ff 24 c5 30 06 40 00 jmpq *0x400630(,%rax,8)
40053e: 89 f8 mov %edi,%eax
400540: 83 e0 0f and $0xf,%eax
400543: eb 25 jmp 40056a
400545: 83 f7 fe xor $0xfffffffe,%edi
400548: 8d 04 3f lea (%rdi,%rdi,1),%eax
40054b: eb 1d jmp 40056a
40054d: f7 d6 not %esi
40054f: 8d 04 3e lea (%rsi,%rdi,1),%eax
400552: eb 16 jmp 40056a
400554: 85 ff test %edi,%edi
400556: 0f 95 c2 setne %dl
400559: c1 ee 1f shr $0x1f,%esi
40055c: 89 f0 mov %esi,%eax
40055e: 21 d0 and %edx,%eax
400560: eb 08 jmp 40056a
400562: 39 f7 cmp %esi,%edi
400564: 0f 94 c0 sete %al
400567: 0fb6 c0 movzbl %al,%eax
40056a: 83 c0 01 add $0x1,%eax
40056d: c3 retq
Here is the C source code of the function foo with some parts (i.e., the most important parts) missing.
int foo(int x, int y) {
switch(x) {
/* a few case statements are missing here! */
case 28:
x = /* Uh oh, what the expression here? */
break;
default:
x = /* Oh no, the expression here is missing as well! */
}
return x + 1;
}
With GDB, you dumped out the 64 bytes of content starting from address 0x400630. The leftmost column shows the starting address of the row, and the 8 columns to the right show the 8 bytes stored contiguously starting from that address.
(gdb) x/64xb 0x400630
0x400630: 0x3e 0x05 0x40 0x00 0x00 0x00 0x00 0x00
0x400638: |
0x62 |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400640: |
0x45 |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400648: |
0x48 |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400650: |
0x3e |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400658: |
0x4d |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400660: |
0x54 |
0x05 |
0x40 |
0x00 |
0x00 |
0x00 |
0x00 |
0x00 |
0x400668: |
0x25 |
0x64 |
0x0a |
0x00 |
0x01 |
0x1b |
0x03 |
0x3b |
Q2.1
2 Points
Fill in the missing expression in the default case .
x = (x == y)
Q2.2
2 Points
Fill in the missing expression in the case 28.
x = ((x != 0) && (y < 0))
Q2.3
2 Points
What is the return value of foo(28, -8) ? Please answer in decimal. If the value cannot be known from the information given in this question, please write X .
2
Q2.4
3 Points
What is the return value of foo(24, 24) ? Please answer in decimal. If the value cannot be known from the information given in this question, please write X .
3
Q3 Barred Bars
9 Points
You are using GDB to debug a program on a x86-64 machine. The program has a struct Bar and a variable bars defined as follow:
struct Bar { short a; int b; };
struct Bar* bars[8][8];
From your interactions with GDB, you find that the address of bars[0][0] is 0x6012a0 . Here is the memory dump of 1024 bytes of content starting from address 0x6012a0 . The leftmost column shows the starting address of the row, and the 8 columns to the right show the 8 bytes stored contiguously starting from that address.
(gdb) x/1024bx 0x6012a0
0x6012a0: 0xa8 0x6012a8: 0xb0 0x6012b0: 0xa0 0x6012b8: 0xa8 0x6012c0: 0x60 0x6012c8: 0x48 0x6012d0: 0x70 0x6012d8: 0x80 0x6012e0: 0x88 0x6012e8: 0x90 0x6012f0: 0x18 0x6012f8: 0xe8 0x601300: 0x28 0x601308: 0xd8 0x601310: 0xe8 0x601318: 0x60 0x601320: 0x88 0x601328: 0xa0 |
0x14 0x15 0x15 0x15 0x15 0x16 0x16 0x16 0x15 0x16 0x15 0x15 0x16 0x14 0x14 0x15 0x15 0x15 |
0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 0x60 |
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 |
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x601330: 0x30
0x601338: 0x00
0x601340: 0x58
0x601348: 0xe8
0x601350: 0xe0
0x601358: 0xe8
0x601360: 0xb0
0x601368: 0x60
0x601370: 0x10
0x601378: 0x18
0x601380: 0x80
0x601388: 0x58
0x601390: 0xe0
0x601398: 0x98
0x6013a0: 0xa0
0x6013a8: 0x30
0x6013b0: 0x58
0x6013b8: 0x20
0x6013c0: 0x60
0x6013c8: 0x38
0x6013d0: 0xe8
0x6013d8: 0xa0
0x6013e0: 0x38
0x6013e8: 0x90
0x6013f0: 0x18
0x6013f8: 0xf0
0x601400: 0x68
0x601408: 0x08
0x601410: 0x40
0x601418: 0xa8
0x601420: 0xf8
0x601428: 0xb0
0x601430: 0x30
0x601438: 0x70
0x601440: 0x88
0x16
0x15
0x15
0x14
0x14
0x15
0x14
0x16
0x15
0x15
0x16
0x16
0x15
0x16
0x14
0x16
0x15
0x16
0x16
0x15
0x14
0x15
0x16
0x16
0x15
0x15
0x15
0x15
0x15
0x15
0x15
0x15
0x15
0x15
0x15
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x60
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 |
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 |
2023-08-16