I converted a very simple C program to an assembly file (Here, RISC-V ISA), and there were some operations done on the stack pointer that I did not understand.
The C program :
int main()
{
int a = 10;
return 0;
}
The associated assembly code :
.file "alternating_branches_2.c"
.option nopic
.text
.align 1
.globl main
.type main, @function
main:
addi sp,sp,-32
sd s0,24(sp)
addi s0,sp,32
li a5,10
sw a5,-20(s0)
li a5,0
mv a0,a5
ld s0,24(sp)
addi sp,sp,32
jr ra
.size main, .-main
.ident "GCC: (GNU) 8.3.0"
Here is my understanding.
sp contains the last address of the stack memory. Pushing to stack would, therefore, decrease the value of sp. s0 is the frame pointer, pointing to the previous value of sp.
In the first line, an offset of 32 is decreased from the stack pointer. Is this to create a stack frame? And usually, in a stack, a push operation would decrease the stack pointer. However, since a stack frame is already created, and sp now points to lower memory of stack, will pushing increase the value of sp?
-------------| <--sp
-------------|
-------------|
-------------|
-------------|
-------------|
-------------|
After the creation of the stack frame :
-------------| <--s0
-------------|
-------------|
-------------|
-------------|
-------------|
-------------| <--sp
And now, pushing to the stack must result in an increase in sp, correct? Or can pushing also be done using the frame pointer, s0? I apologize if this is a very basic question. Thank you.