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

STM32F091启动文件中.size伪指令作用及语法解析

Understanding the .size Directive in STM32 Cortex-M Startup Code

Great question—digging into startup scripts and assembler directives is a key part of mastering embedded systems, so let’s break this down step by step.

What’s the .size Assembler Directive?

The .size directive is part of the GNU Assembler (GAS), the standard toolchain for most ARM Cortex-M targets including your STM32F091. Its core purpose is to record the memory size associated with a symbol in the object file.

Linkers, debuggers, and memory map tools use this information to:

  • Correctly calculate how much RAM/Flash a function, variable, or section uses
  • Generate accurate debug symbols (so your debugger knows not to treat a marker as a block of memory)
  • Validate that your code and data fit within the MCU’s memory limits

Breaking Down .size __HeapBase, . - __HeapBase

Let’s unpack the syntax and context here, since this line is tied to how STM32 defines its heap region:

First, the key symbols:

  • __HeapBase: This is a linker-defined marker pointing to the start address of the heap (the RAM region used for dynamic allocation like malloc()). It’s not a variable that takes up memory—it’s just a reference point.
  • . (the dot): In assembler/linker scripts, this represents the current location counter—think of it as the address the assembler is currently "working at".

The .size directive follows this format:

.size <symbol>, <size_value>

In your specific line:

  1. The size_value is calculated as . - __HeapBase. Since this line is placed immediately after __HeapBase is defined, the current location counter (.) matches the address of __HeapBase exactly. This means the size evaluates to 0.
  2. We’re telling the assembler to mark __HeapBase as a zero-sized symbol. This is critical because __HeapBase isn’t a block of memory—it’s just an address marker. Setting its size to 0 ensures tools like debuggers and linkers don’t misinterpret it as a memory-occupying variable, keeping your memory map and debug information accurate.

Quick Summary

  • .size is a GAS directive that records a symbol’s memory size for toolchain tools.
  • Your specific line marks __HeapBase (the heap start marker) as a zero-sized symbol, correctly identifying it as an address reference rather than a variable that takes up space.

内容的提问来源于stack exchange,提问作者Gaurav Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:45:24