Great question — and yes, this is a classic example of why we adjust the stack pointer and save registers in function calls. Let's break it down step by step and then describe what would happen if we didn’t do these things.
🧠 Understanding the Code
Original C code:
c複製編輯long f(long a, long b, long c, long d){
return g(g(a,b), c+d);
}
Simplified idea:
Compute g(a,b) → temp1
Compute g(temp1, c+d)
Return the result
Assembly translation (with annotations):
assembly複製編輯addi x2, x2, -16 # Adjust stack pointer (x2 = sp), make space on stack
sd x1, 0(x2) # Save return address (x1 = ra)
add x5, x12, x13 # x12 = a, x13 = b ⇒ x5 = c + d
sd x5, 8(x2) # Save c+d (needed after first call to g)
jal x1, g # Call g(a,b), result in x10
ld x11, 8(x2) # Load c+d into x11 (second arg for g)
jal x1, g # Call g(x10, x11)
ld x1, 0(x2) # Restore return address
addi x2, x2, 16 # Restore stack pointer
jalr x0, 0(x1) # Return
🔍 Why Save the Stack and Return Address?
- Why move the stack pointer?
When we do:
assembly複製編輯addi x2, x2, -16
We reserve 16 bytes of space on the stack. We need it to:
Save the return address (so we can return from f after calling g)
Save any temporary value we need across function calls (c+d here)
❗ If we don’t move the stack pointer, any attempt to write to the stack (sd x1, 0(x2)) may overwrite important data (like another function’s return address or local variables).
- Why save the return address (ra or x1)?
When f calls g, g will overwrite the return address (x1). So if f calls g twice, like here:
assembly複製編輯jal x1, g # 1st call → overwrites x1
...
jal x1, g # 2nd call → again overwrites x1
If we don't save x1 before the first call, we lose the address where f should return!
💥 If we don’t save it, when f finishes, it will try to return to the wrong place → CRASH or undefined behavior!
🧠 What Happens If We DON’T Save?
Let’s say we don’t do this:
assembly複製編輯addi x2, x2, -16
sd x1, 0(x2) # skipped
sd x5, 8(x2) # skipped
Then:
The second g call overwrites the return address → f doesn’t know where to return.
The value of c+d (x5) is not preserved, so when we load x11, it may be garbage or overwritten.
Stack pointer is not restored → you leak stack space or corrupt the caller’s frame.
🔧 How Do We Know What to Save?
In RISC-V calling convention:
Caller-saved registers (a0-a7, t0-t6, ra) must be saved by the caller if needed after a function call.
Callee-saved registers (s0-s11, sp) must be saved by the callee if they’re modified.
In this case:
We use ra (x1) → needs saving (caller-saved)
We need to use c+d after calling g(a, b) → must store it somewhere safe (stack)
So the function f must save:
ra because it's overwritten by each call to g
Intermediate value (c + d) because it's needed after the first call
📊 Diagram (Stack View)
Before calling g(a,b):
markdown複製編輯[high memory]
...
[ret addr from caller] <- x2 (stack pointer before)
[saved ra (x1)] <- x2 - 0
[c + d (x5)] <- x2 - 8
[low memory]
✅ Summary
Yes, this is exactly why we:
Adjust the stack pointer to make space
Save caller-saved registers like ra when calling other functions
Store temporary values that must survive across function calls
This protects the integrity of our function execution and allows nested function calls to work correctly.
Let me know if you want to see how this relates to callee-saved registers too!