Article 54V1K Mystery address present: Callee stack asm instruction block.

Mystery address present: Callee stack asm instruction block.

by
andrew.comly
from LinuxQuestions.org on (#54V1K)
Take the following extremely simple program:
Code: #include <stdio.h>

//PROTOTYPES
int sumOfTwo(int, int);

//Main
int main()
{
int x, y, z;
x = 5;
y = 13;
z = sumOfTwo(x, y);
printf("z = %d", z);
return 0;
}

//FUNCTIONS
int sumOfTwo(int a, int b)
{
int sum = a + b;
return sum;
}when running
Code:gcc -S add.cproduces the following ASM code:
Code: .file "add.c"
.section .rodata
.LC0:
.string "z = %d"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $5, -12(%rbp)
movl $13, -8(%rbp)
movl -8(%rbp), %edx
movl -12(%rbp), %eax
movl %edx, %esi
movl %eax, %edi
call sumOfTwo
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.globl sumOfTwo
.type sumOfTwo, @function
sumOfTwo:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -20(%rbp)
movl %esi, -24(%rbp)
movl -20(%rbp), %edx
movl -24(%rbp), %eax
addl %edx, %eax
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size sumOfTwo, .-sumOfTwo
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbitsor in simplfied ASM code:
Code:main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $5, -12(%rbp)
movl $13, -8(%rbp)
movl -8(%rbp), %edx
movl -12(%rbp), %eax
movl %edx, %esi
movl %eax, %edi
call sumOfTwo
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
sumOfTwo:
pushq %rbp
movq %rsp, %rbp
movl %edi, -20(%rbp)
movl %esi, -24(%rbp)
movl -20(%rbp), %edx
movl -24(%rbp), %eax
addl %edx, %eax
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
popq %rbp
retI would like to focus on the following asm instruction block, it is seen quite often in asm code:
Code: pushq %rbp
movq %rsp, %rbpI would like to understand the specifics of the above. What I understand so far is:
Code:pushq %rbpmeans first:
Code:subq $8, %rspThe above is equivilent to rsp = rsp - 8; Anotherwords increment stack pointer by 8(q) bytes (stack memory increments/grows downwards, therefore "-8"). This is quite significant because this new 8 byte block will be the first block of the new stack(callee stack).

And second:
Code:movq %rbp, (%rsp)This means move the 8 bytes in "stack(caller stack) base pointer" register into the block of memory(5 different types of memory, this specific memory is stack memory) in stack pointed to by the memory address currently in register %rsp.
***(memory address in %rsp??)***

Notice how above instruction contains (%rsp) as its destination. To my knowledge when instructions other than "lea" use parentesis, this denotes Indirect Mode Addressing, which means the register surrounded by "()"'s content is an address in memory(stack memory). Please correct me if this understanding is incorrect.

Finally I will interpret the second line of instuction above:
Code:movq %rsp, %rbpmeans to move the contents of stack pointer register(%rsp) into %rbp register. Anotherwords, this will move the memory address of the previous stack's base pointer into the base pointer register.

Please feel free to correct my above understanding/interpretation.

QUESTION:
But wait! Look again above where I previously said: ***(memory address in %rsp??)***.
How/Why in the first place is there a stack memory address currently in stack pointer register (%rsp)? How do you know there isn't an actual value in %rsp prior to the pushq %rbp instruction? Can %rsp, %rbp registers only have memory addresses in them?

If this isn't an appropriate forum to post this question, any suggestions to where should I post this question?latest?d=yIl2AUoC8zA latest?i=nVb5eXfQZYs:vz7c_BkFw9g:F7zBnMy latest?i=nVb5eXfQZYs:vz7c_BkFw9g:V_sGLiP latest?d=qj6IDK7rITs latest?i=nVb5eXfQZYs:vz7c_BkFw9g:gIN9vFwnVb5eXfQZYs
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments