|
The general way to pass parameters and allocate local variables is using the runtime stack:
The (1) parameters. (2) return addresses and (3) local variables are stored in the program (runtime) stack:
We must be very careful and organize the data on the runtime (program) stack !!
When can/do we pass the parameters to a function ?
f1: +----> f2: ... | push {lr} | ... | ... | ... | ... | bl f2 ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ pop {pc} |
When can/do we pass the parameters to a function ?
f1:
+----> f2:
... | push {lr} |
... | ... |
pass params | ... |
bl f2 ----------+ ... |
... <--------------+ ... |
... | ... V
... +------ pop {pc}
|
Answer: it must happen before the bl f2 where we call the function !!!
(It's too late to pass parameters after the bl f2 instruction !)
When can/do we create the local variables for the function f2 ?
f1: +----> f2: ... | push {lr} | ... | ... | pass params | ... | bl f2 ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ pop {pc} |
When can/do we create the local variables for the function f2 ?
f1:
+----> f2:
... | push {lr} |
... | ... |
pass params | alloc local vars
bl f2 ----------+ ... |
... <--------------+ ... |
... | ... V
... +------ pop {pc}
|
Answer: immediately after we saved the return address
(Because the local variables are created when a function starts executing)
Due to the chronology of the events in the function invocation:
f1: +----> f2: ... | push {lr} | ... | push {fp} | pass params | alloc local vars bl f2 ----------+ ... | ... <--------------+ ... | ... | ... V ... +------ pop {pc} |
The order of of the items pushed/stored in the System Stack is:
|
Activation record or stack frame: ( click here)
|
Activation record or stack frame: ( click here)
|
|