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

6. Consider the following assembly code: 00000000 <canefunc>: 0: e92d40f8 push {

ID: 3694159 • Letter: 6

Question

6. Consider the following assembly code:

00000000 <canefunc>:
0: e92d40f8 push {r3, r4, r5, r6, r7, lr}
4: e1a04000 mov r4, r0
8: e1a05001 mov r5, r1
c: e1a00080 lsl r0, r0, #1
10: e2800001 add r0, r0, #1
14: e2846001 add r6, r4, #1
18: e1a06086 lsl r6, r6, #1
1c: e7913100 ldr r3, [r1, r0, lsl #2]
20: e3530000 cmp r3, #0
24: 03a07000 moveq r7, #0
28: 0a000001 beq 34 <canefunc+0x34>
2c: ebfffffe bl 0 <canefunc>
30: e1a07000 mov r7, r0
34: e7953106 ldr r3, [r5, r6, lsl #2]
38: e3530000 cmp r3, #0
3c: 03a00000 moveq r0, #0
40: 0a000002 beq 50 <canefunc+0x50>
44: e1a00006 mov r0, r6
48: e1a01005 mov r1, r5
4c: ebfffffe bl 0 <canefunc>
50: e3540000 cmp r4, #0
54: 05953000 ldreq r3, [r5]
58: 00877003 addeq r7, r7, r3
5c: 00870000 addeq r0, r7, r0
60: 10870000 addne r0, r7, r0
64: e8bd80f8 pop {r3, r4, r5, r6, r7, pc}

(a) Fill in the blanks in the following C decompilation of this code based on the assembly:
int canefunc(int n, int *a) {
int b = ____________;
int c = ____________;
int d = 0;
int e = 0;
if (__________)
d = ______________;
if (__________)
e = ______________;
if (___________)
return (_____________);
return (__________);
}

(b) What does this function do?

(c) For the previous question, draw the stack frame for three recursive calls to the function:

(d) Where (if at all) is the variable n stored on the stack? If it is stored, why? If it isn’t stored, why?

(e) Explain lines 38, 3c, and 40. Describe how the N,C,V, and Z flags are set and used in these operations.

Explanation / Answer

a)
int canefunc(int n, int *a) {
int b = 1;
int c = 1;
int d = 0;
int e = 0;
if (n>0)
d = d+a;
if (n>0)
e = e+a;
if (n==0)
return 0;
return c + canefunc(n-1,a+1);
}

-----------------------------------------------------------------------------------
b)
this function is to get sum of numbers upto 1 to n numbers

--------------------------------------------------------------------
c)
consider n = 3
n = 3, a = 1
n = 2, a = 1+2
n = 1, a = 1+2+3
n = 0, return a = (1+2+3) == 6

------------------------------------------------------------------------------------------
d)
n variable stored in top of stack. On every call, the recent n value is replaced with new one.

---------------------------------------------------------------------------------------------------
e)
in 38th line .. we are comparing whether n=0
in 3c line, as n reaches 0 we are returning 0
in 40, until n equals 50, we are calling canefunc recursively

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote