Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

( I )Type in the following C program. Save it as “swap_n_add.c”. int swap_n_add(

ID: 3624931 • Letter: #

Question

( I )Type in the following C program. Save it as “swap_n_add.c”.

int swap_n_add(int *xp, int *yp)
{
int x = *xp;
int y = *yp;

*xp = y;
*yp = x;
return x + y;
}

int main() {
int a1 = 534;
int a2 = 1057;
int sum = swap_n_add(&a1, &a2);
int diff = a1 - a2;

return sum * diff;
}



(II) Compile the program and load it in gdb, as in the previous exercise. Note that swap_n_add returns sum but also modifies a1 and a2 as a side effect.
Using breakpoints, stack inspection and local variable inspection, answer the following questions with reference to your session.

Q4. What is the value of both a1 and a2 before swap_n_add? After? What would be the value of diff if the call to swap_n_add did not occur?
(Show this on your GDB session.)
Q5. Show the return value of main through register inspection.
Q6. Issue the following commands.
(gdb) set disassembly-flavor intel
(gdb) list swap_n_add
(gdb) disas swap_n_add
Group the assembly code instructions with their corresponding statements in swap_n_add to the best of your ability.

Explanation / Answer

Dear, I.Pasted and saved in swap_n_add.c 4.           The values of a1, a2 is 534 1057          After function call the values are 1057 534 5. The value returned to main is 1591 Hope this will help you