咨询C3 Simplesem中函数参数设置:地址100处D[2]的配置方法
Hey there! Let's break down how to set up space for your local parameter D[2] in a C-like function using C3 Simplesem, starting at address 100.
First, let's recall the standard convention for handling local variables in Simplesem: they're almost always allocated on the stack, which typically grows downward (meaning we decrease the stack pointer to reserve space).
The Instruction at Address 100
To set up space for the 2-element local array D[2], you need an instruction that reserves 2 memory units on the stack. At address 100, this is the correct instruction:
SUB SP, #2
Here's why this works:
SP(stack pointer) points to the current top of the stack. By subtracting 2 fromSP, you're creating 2 unused slots on the stack specifically forD[2].- You can then access the elements via stack-relative addressing:
D[0]lives atSP+0andD[1]atSP+1(for example, useLD R1, SP+0to loadD[0]into registerR1).
Optional: Adding a Frame Pointer (For Robustness)
If your function will push/pop registers or modify SP later, setting a frame pointer (FP) right after allocating space makes local variable access more consistent. Add this instruction right after the SUB command:
MOV FP, SP
With a frame pointer, D[0] becomes FP+0 and D[1] becomes FP+1—these offsets won't change even if SP moves during the function's execution.
Full Function Entry Flow (Starting at 100)
- Address 100:
SUB SP, #2→ Reserve space forD[2] - (Optional) Address 101:
MOV FP, SP→ Set frame pointer for stable local access - Proceed with your function's core logic from there
That should cover the setup for your local parameter! Let me know if you need clarification on addressing or other Simplesem mechanics.
内容的提问来源于stack exchange,提问作者basil




