Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } pcount_r: movl $0, $eax testa frdi, $rdi je .L6 pusha frbx mova frdi, &rbx andi $1, %ebx shra %rdi call pcountur adda rbx, frax popa %rbx .L6: rep; ret # if x==0 # done # save $rbx # x -> 8 rbx # x & 1 # x >> 1 # recurse # retval # restore rbx Problem 3. This problem considers the growth of the stack in the execution of a recursive function. Consider the recursive version of the "popcount" function (pcount.r()), as described in Lecture L10 (beginning on slide 6). Suppose that function is called with actual parameter 0x00000012, from a point where register %rbx contains 0x0000ffff, and where the address of the next instruction is Oxfff60548. Assume the address of the (recursive) call instruction is 0xffff703000, and it is a three-byte instruction. a. Including that original invocation, what is the maximum number of frames for pcounts that will be on the stack as a result? b. Draw a diagram showing the contents of the stack at the point of maximum depth of recursion. Include the value of all the items pushed onto the stack for each invocation. /* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); } pcount_r: movl $0, $eax testa frdi, $rdi je .L6 pusha frbx mova frdi, &rbx andi $1, %ebx shra %rdi call pcountur adda rbx, frax popa %rbx .L6: rep; ret # if x==0 # done # save $rbx # x -> 8 rbx # x & 1 # x >> 1 # recurse # retval # restore rbx Problem 3. This problem considers the growth of the stack in the execution of a recursive function. Consider the recursive version of the "popcount" function (pcount.r()), as described in Lecture L10 (beginning on slide 6). Suppose that function is called with actual parameter 0x00000012, from a point where register %rbx contains 0x0000ffff, and where the address of the next instruction is Oxfff60548. Assume the address of the (recursive) call instruction is 0xffff703000, and it is a three-byte instruction. a. Including that original invocation, what is the maximum number of frames for pcounts that will be on the stack as a result? b. Draw a diagram showing the contents of the stack at the point of maximum depth of recursion. Include the value of all the items pushed onto the stack for each invocation
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started