Value Return
1. Mechanisms
A function returns a value to the caller via an agreed slot: register (small values), frame slot (larger), or caller-provided pointer (structs). Convention is part of the ABI.
2. Typical Conventions
| Value kind | Where | Example |
|---|---|---|
| int/pointer | return register (eax/rax, r0) | return 42 → MOV eax,42 |
| float | float reg (xmm0) | return 3.14 |
| struct (large) | caller allocs space, passes hidden pointer as first param | s = f() → f(&tmp) then copy |
| void | nothing; only control returns | jump to return addr |
3. Sequence
callee: compute → store to return slot/reg → jump to return addr → (epilogue restores)
caller: call → (callee runs) → copy return slot to destination → continue
For y = f(a)+1: CALL f → result in eax → ADD eax,1 → MOV y,eax.
4. Pitfalls
- Returning address of local → dangling (frame popped).
- Type mismatch (return float as int) caught in semantic phase.
5. Exam Points
- Explain return-value slots + struct hidden pointer — 5 marks, frequent.
- Call/return value steps — 3 marks, frequent.
- Why returning local address is illegal — 3 marks, frequent.