Page 1 of 1

In the C function that follows, we have omitted the body of the switch statement. In the C code, the case labels did not

Posted: Sat May 14, 2022 4:51 pm
by answerhappygod
In the C function that follows, we have omitted the body of
the switch statement. In the C code, the case labels did
not span a contiguous range, and some cases had multiple
labels.
void switch(short x, short \*dest) {
short val = 0;
switch (x) {
.
.
Body of switch statement omitted
}
\*dest = val;
}
In compiling the function, gcc generates the assembly
code that follows for the initial part of the procedure, with
variable x in %rdi:
; void switch(short x, short *dest)
; x in %rdi
switch:
addq $2, %rdi
cmpq $8, %rdi
ja .L2
jmp *.L4(, %rdi, 8)
Based on this information, answer the following questions:
Question 13
What is the maximum case
label in the above code?
For example, in the following code,
the maximum case label is 7.
void switcher(long a, long b, long c, long *dest)
{
long val;
switch(a) {
case 5:
c = b ^ 15;
/* Fall through */
case 0:
val = c + 112;
break;
case 2:
case 7:
val = (c + b) << 2;
break;
case 4:
val = a;
break;
default:
val = b;
}
*dest = val;
}
Answer:
Question 14
What is the minimum case
label in the above code?
For example, in the following code,
the minimum case label is 0.
{
long val;
switch(a) {
case 5:
c = b ^ 15;
/* Fall through */
case 0:
val = c + 112;
break;
case 2:
case 7:
val = (c + b) << 2;
break;
case 4:
val = a;
break;
default:
val = b;
}
*dest = val;
}
Answer: