0

So I was reading chapter 3 of CSAPP (3.7.4 Local Storage on the Stack).

In Figure 3.31, there is this snippet of code:

long caller() {
    long arg1 = 534;
    long arg2 = 1057;
    long sum = swap_add(&arg1, &arg2);
    long diff = arg1 - arg2;
    return sum * diff;
}

and its generated assembly:

caller:
  subq    $16, %rsp
  movq    $534, (%rsp)
  movq    $1057, 8(%rsp)
  leaq    8(%rsp), %rsi
  movq    %rsp, %rdi
  call    swap_add
  movq    (%rsp), %rdx
  subq    8(%rsp), %rdx
  imulq   %rdx, %rax
  addq    $16, %rsp
  ret

My question is that why not use pushq $1057 then pushq $534 here.

I'm also aware that if we want to allocate space for some variable and leave it uninitialized, we should decreament $rsp directly as here. But I can't relate this fact with my question here.

manticeo
  • 27
  • 4
  • That's how compilers normally do things, but yes it's a missed optimization. [What C/C++ compiler can use push pop instructions for creating local variables, instead of just increasing esp once?](https://stackoverflow.com/q/49485395). Many CS:APP examples are *actual* compiler output, not hand-written asm. – Peter Cordes Nov 19 '21 at 05:39
  • Thanks for the link and this useful knowledge about CS:APP, @PeterCordes – manticeo Nov 19 '21 at 15:44

0 Answers0