You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RL78G14平台汇编调用C函数疑问:参数存储偏移问题

Why Parameter 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 pushes c first, then b, then a.
  • Stack Frame Structure: When the CALL instruction executes, it automatically pushes the 16-bit return address onto the stack. Additionally, CubeSuite+ requires the caller to save certain 16-bit registers (like HL, BC, DE) to the stack before pushing parameters—this preserves their values for the caller after the function returns.
  • Data Size: int on 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:

  1. The caller saves 4 x 16-bit registers (e.g., HL, BC, DE, PSW) to the stack: that’s 8 total bytes.
  2. Next, the caller pushes parameters right-to-left: c (2 bytes), then b (2 bytes), then a (2 bytes).
  3. Finally, the CALL instruction 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] → Parameter a (2 bytes)
  • The 8 bytes between HL and HL+8 are the saved registers the caller pushed before parameters.
  • [HL+8] → Parameter b (2 bytes)
  • [HL+10] → Parameter c (2 bytes) (since b takes 2 bytes, c starts 2 bytes after b)

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è

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:14:27