【RISC-V】.pushsection .popsection和.section .previous的区别

规则如下:

.pushsection.popsection维护一个私有的段栈

1、.pushsection行为:

  1. 将当前段(current section)压入段栈。

  2. 切换到指定的name段(如果不存在则创建)。

  3. 后续的汇编代码写入新段。

2、.popscetion行为:

  1. 从段栈弹出最顶层的段名。

  2. 直接切换回该段(作为当前段current section)。

复制代码
.text
    nop
.pushsection .data  # 压栈text
    .byte 42
.pushsection .rodata # 压栈data
    .ascii "hello"
.popsection        # 回到 .data
    .byte 43
.popsection        # 回到 .text
    nop

⚠️⚠️⚠️规则如下:

GAS(GNU Assembler)内部维护previous_section 变量和current_section变量,当

复制代码
.text                # current=.text,    previous=?
    nop
.section .data       # previous=.text,   current=.data
    .byte 42
.section .rodata     # previous=.data,   current=.rodata
    .ascii "hello"
.previous            # 交换:previous=.rodata,current=.data。回到.data段
    .byte 43
.previous            # 再次交换: previous=.data,current=.rodata。回到.rodata段

.text              # previous=?,        current=.text    
    nop
.section .data     # previous=.text,    current=.data    
    .byte 42
.section .rodata   # previous=.data,    current=.rodata   
    .ascii "hello"
.previous          # 交换,previous=.rodata,current=.data。回到data段
.section .init     # 此时在data段,所以previous=.data,    current=.init  
    .byte 43
.previous          # 交换,previous=.init,current=.data。回到data段

自测习题:

回答:最终 .byte 99(或 .quad 99 等)位于 .xxx 段。

1、基础

复制代码
.text
    nop
.section .data
    .byte 1
.previous
    .byte 99

2、稍复杂

复制代码
.text
.section .rodata
.section .data
.previous
.previous
    .byte 99

3、陷阱题

复制代码
.text
.section .init
.section .fini
.previous
.section .data
.previous
    .byte 99

4、混合使用

复制代码
.text
.pushsection .data
.section .rodata
.previous
.popsection
    .byte 99

5、嵌套 push/pop

复制代码
.text
.pushsection .init
.pushsection .data
.section .rodata
.popsection
.previous
    .quad 99

答案:

第一题:.text,第二题:.data,第三题:.init,第四题:.text,第五题:.data

相关推荐
高新打工人1 天前
RISC-V(五):xceptions, Traps, and Interrupts介绍
risc-v
m0_747124534 天前
RISC-V 基础知识扫盲
risc-v
W_LuYi1856 天前
手撸极简zkEVM验证器:RISC-V电路实践
java·risc-v
大唐游子14 天前
MIT 6.1810 开发环境搭建(Xv6)
risc-v
nvd1115 天前
绝地求生:如何在 2026 年把 OpenAI Codex 强行交叉编译到 RISC-V 架构
架构·risc-v
Eloudy16 天前
可在开源 RISC-V 上的裸机操作系统
开源·risc-v
hai31524754316 天前
RISC-V核E203核前向旁路的架构性顽疾
驱动开发·架构·硬件架构·硬件工程·risc-v
时光飞逝的日子17 天前
基于 RISC-V 架构的边缘 AI 推理引擎优化设计
risc-v·模型量化·推理引擎·边缘 ai·向量扩展·低功耗优化
国科安芯18 天前
基于RISC-V架构的商业航天级MCU国产化技术路径与产业生态研究
网络·分布式·单片机·嵌入式硬件·架构·risc-v·安全性测试
国科安芯18 天前
AS32S601商业航天级抗辐照MCU芯片:架构设计与技术特性研究
单片机·嵌入式硬件·算法·安全·架构·risc-v