Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22035

About Variable-Size Stack Frames, about the alignment of stack frame,an example from CSAPP

$
0
0

C code:

long vframe(long n, long idx, long *q) {
    long i;
    long *p[n];
    p[0] = &i;

    for (i = 1; i < n; i++)
        p[i] = q;

    return *p[idx];
}

Portions of generated assembly code:

long vframe(long n, long idx, long *q)

n in %rdi, idx in %rsi, q in %rdx
Only portions of code shown
vframe:
pushq   %rbp            Save old %rbp
movq    %rsp, %rbp      Set frame pointer
subq    $16, %rsp       Allocate space for i (%rsp = s1)
leaq    22(,%rdi,8), %rax
andq    $-16, %rax
subq    %rax, %rsp      Allocate space for array p (%rsp = s2)
leaq    7(%rsp), %rax
shrq    $3, %rax
leaq    0(,%rax,8), %r8     Set %r8 to &p[0]
movq    %r8, %rcx       Set %rcx to &p[0] (%rcx = p)
...
Code for initialization loop
i in %rax and on stack, n in %rdi, p in %rcx, q in %rdx
.L3:                  loop:
movq    %rdx, (%rcx,%rax,8) Set p[i] to q
addq    $1, %rax        Increment i
movq    %rax, -8(%rbp)      Store on stack
.L2:
movq    -8(%rbp), %rax      Retrieve i from stack
cmpq    %rdi, %rax      Compare i:n
jl  .L3         If <, goto loop
...
Code for function exit
leave               Restore %rbp and %rsp
ret             Return

In the book the author says:

The leaq instruction in

leaq    22(,%rdi,8), %rax

computes the value 8n + 22, which is then rounded down to the nearest multiple of 16 by the andq instruction in

andq    $-16, %rax

The resulting value will be 8n + 8 when n is odd and 8n + 16 when n is even, and this value is subtracted from s1 to give s2.

What puzzles me is 8n + 22. why must it be 22, not 16, 17, 18, 19, 20, 21, 23?


Viewing all articles
Browse latest Browse all 22035

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>