RL78G14平台汇编调用C函数疑问:参数存储偏移问题
b is at [HL+8] in Your RL78 Assembly Code Great question! Let's break down this behavior, which is directly tied to the calling convention and stack frame layout used by the CubeSuite+ compiler for the RL78G14 chip.
Key Background: RL78 Calling Convention (CubeSuite+ Default)
For C functions called from (or implemented in) assembly on the RL78G14:
- Parameter Passing Order: Parameters are pushed onto the stack right-to-left. For
Calculate_triangle(a, b, c), the caller pushescfirst, thenb, thena. - Stack Frame Structure: When the
CALLinstruction executes, it automatically pushes the 16-bit return address onto the stack. Additionally, CubeSuite+ requires the caller to save certain 16-bit registers (likeHL,BC,DE) to the stack before pushing parameters—this preserves their values for the caller after the function returns. - Data Size:
inton the RL78 is a 16-bit type, so each parameter occupies 2 bytes of stack space.
Breaking Down Your Stack Layout
You mentioned parameter a is at [HL]. Here's why b lands at [HL+8]:
Let’s assume the caller follows the full stack setup before invoking your function:
- The caller saves 4 x 16-bit registers (e.g.,
HL,BC,DE,PSW) to the stack: that’s 8 total bytes. - Next, the caller pushes parameters right-to-left:
c(2 bytes), thenb(2 bytes), thena(2 bytes). - Finally, the
CALLinstruction pushes the 16-bit return address (2 bytes).
When your assembly function starts, HL is set to point directly to the start of parameter a on the stack. From this starting point:
[HL]→ Parametera(2 bytes)- The 8 bytes between
HLandHL+8are the saved registers the caller pushed before parameters. [HL+8]→ Parameterb(2 bytes)[HL+10]→ Parameterc(2 bytes) (sincebtakes 2 bytes,cstarts 2 bytes afterb)
Quick Verification
Your assembly code correctly sums a + b + c by accessing these locations, which aligns with this stack layout. If you want to confirm, use the CubeSuite+ debugger: set a breakpoint at the start of your assembly function, check the value of HL, then inspect memory at HL, HL+8, and HL+10—you’ll see your parameter values (3, 4, 5) exactly where your code expects them.
内容的提问来源于stack exchange,提问作者Quang Minh Lè

