The code for a function that is similar tofunction vfunct (a). We used vfunct toillustrate the use of a frame pointer in managing variable-sizestack frames. The new function aframe allocates space forlocal(a) C code1 #include <alloca.h>23 long aframe(long n, long idx, long *q) {4 long i;5 long **p = alloca(n * sizeof(long *));6 p[0] = &i;7 for (i = 1; i < n; i++)8 p[i] = q;9 return *p[idx];10 }(b) Portions of generated assembly codelong aframe(long n, long idx, long *q)n in %rdi, idx in %rsi, q in %rdx1 aframe:2 pushq %rbp3 movq %rsp, %rbp4 subq $16, %rsp Allocate space for i (%rsp = s )5 leaq 30(,%rdi,8), %rax6 andq $-16, %rax7 subq %rax, %rsp Allocate space for array p(%rsp = s )8 leaq 15(%rsp), %r89 andq $-16, %r8 Set %r8 to &p[0]⋮array p by calling library function alloca . This function issimilar to the more commonly used function malloc, except thatit allocates space on the run-time stack. The space isautomatically deallocated when the executing procedurereturns.(b) shows the part of the assembly code thatsets up the frame pointer and allocates space for localvariables i and p . It is very similar to the corresponding codefor vframe . Let us use the same notation as in Problem3.49 : The stack pointer is set to values s at line 4 and s atline 7. The start address of array p is set to value p at line 9.Extra space e may arise between s2 and p, and extra space emay arise between the end of array p and s1 .A. Explain, in mathematical terms, the logic in thecomputation of s2 .B. Explain, in mathematical terms, the logic in thecomputation of p.C. Find values of n and s1 that lead to minimum andmaximum values of e1 .D. What alignment properties does this code guarantee forthe values of s2 and p?
The code for a function that is similar to
function vfunct (a). We used vfunct to
illustrate the use of a frame pointer in managing variable-size
stack frames. The new function aframe allocates space for
local
(a) C code
1 #include <alloca.h>
2
3 long aframe(long n, long idx, long *q) {
4 long i;
5 long **p = alloca(n * sizeof(long *));
6 p[0] = &i;
7 for (i = 1; i < n; i++)
8 p[i] = q;
9 return *p[idx];
10 }
(b) Portions of generated assembly code
long aframe(long n, long idx, long *q)
n in %rdi, idx in %rsi, q in %rdx
1 aframe:
2 pushq %rbp
3 movq %rsp, %rbp
4 subq $16, %rsp Allocate space for i (%rsp = s )
5 leaq 30(,%rdi,8), %rax
6 andq $-16, %rax
7 subq %rax, %rsp Allocate space for array p
(%rsp = s )
8 leaq 15(%rsp), %r8
9 andq $-16, %r8 Set %r8 to &p[0]
⋮
array p by calling library function alloca . This function is
similar to the more commonly used function malloc, except that
it allocates space on the run-time stack. The space is
automatically deallocated when the executing procedure
returns.
(b) shows the part of the assembly code that
sets up the frame pointer and allocates space for local
variables i and p . It is very similar to the corresponding code
for vframe . Let us use the same notation as in Problem
3.49 : The stack pointer is set to values s at line 4 and s at
line 7. The start address of array p is set to value p at line 9.
Extra space e may arise between s2 and p, and extra space e
may arise between the end of array p and s1 .
A. Explain, in mathematical terms, the logic in the
computation of s2 .
B. Explain, in mathematical terms, the logic in the
computation of p.
C. Find values of n and s1 that lead to minimum and
maximum values of e1 .
D. What alignment properties does this code guarantee for
the values of s2 and p?
Trending now
This is a popular solution!
Step by step
Solved in 2 steps