RISC-V汇编

语法介绍

  • 一个完整的 RISC-V 汇编程序有多条 语句 (statement) 组成。
  • 一条典型的 RISC-V 汇编 语句 由 3 部分组成:[ label:][ operation] [ comment]
    • label(标号): GNU汇编中,任何以冒号结尾的标识符都被认为是一个标号。表示当前指令的位置标记。
    • operation 可以有以下多种类型:
      • instruction(指令): 直接对应二进制机器指令的字符串。例如addi指令、lw指令等。
      • pseudo-instruction(伪指令): 为了提高编写代码的效率,可以用一 条伪指令指示汇编器产生多条实际的指令(instructions)。
      • directive(指示/伪操作): , 通过类似指令的形式(以“.”开头),通知汇 编器如何控制代码的产生等,不对应具体的指令。
      • macro:采用 .macro/.endm 自定义的宏
    • comment(注释): 常用方式,“#” 开始到当前行结束。
image-20220802211800133

指令集合

risc-v汇编指令分类主要有以下几种:算数运算指令、逻辑运算指令、位移运算指令、内存读写指令、分支与跳转指令

image-20220802214220335

伪指令

伪指令不是真的机器指令,会被汇编器翻译为真实的物理指令,例如: ret 被汇编为:jalr x0, x1, 0

image-20220802214625069 image-20220802214801029

Pseudo Ops

Both the RISC-V-specific and GNU .-prefixed options.

The following table lists assembler directives:

Directive Arguments Description
.align integer align to power of 2 (alias for .p2align)
.file “filename” emit filename FILE LOCAL symbol table
.globl symbol_name emit symbol_name to symbol table (scope GLOBAL)
.local symbol_name emit symbol_name to symbol table (scope LOCAL)
.comm symbol_name,size,align emit common object to .bss section
.common symbol_name,size,align emit common object to .bss section
.ident “string” accepted for source compatibility
.section [{.text,.data,.rodata,.bss}] emit section (if not present, default .text) and make current
.size symbol, symbol accepted for source compatibility
.text emit .text section (if not present) and make current
.data emit .data section (if not present) and make current
.rodata emit .rodata section (if not present) and make current
.bss emit .bss section (if not present) and make current
.string “string” emit string
.asciz “string” emit string (alias for .string)
.equ name, value constant definition
.macro name arg1 [, argn] begin macro definition \argname to substitute
.endm end macro definition
.type symbol, @function accepted for source compatibility
.option {rvc,norvc,pic,nopic,relax,norelax,push,pop} RISC-V options. Refer to .option for a more detailed description.
.byte expression [, expression]* 8-bit comma separated words
.2byte expression [, expression]* 16-bit comma separated words
.half expression [, expression]* 16-bit comma separated words
.short expression [, expression]* 16-bit comma separated words
.4byte expression [, expression]* 32-bit comma separated words
.word expression [, expression]* 32-bit comma separated words
.long expression [, expression]* 32-bit comma separated words
.8byte expression [, expression]* 64-bit comma separated words
.dword expression [, expression]* 64-bit comma separated words
.quad expression [, expression]* 64-bit comma separated words
.dtprelword expression [, expression]* 32-bit thread local word
.dtpreldword expression [, expression]* 64-bit thread local word
.sleb128 expression signed little endian base 128, DWARF
.uleb128 expression unsigned little endian base 128, DWARF
.p2align p2,[pad_val=0],max align to power of 2
.balign b,[pad_val=0] byte align
.zero integer zero bytes
.variant_cc symbol_name annotate the symbol with variant calling convention

.option

rvc/norvc

Enable/disable the C-extension for the following code region.

pic/nopic

Set the code model to PIC (position independent code) or non-PIC. This will affect the expansion of the la pseudoinstruction, refer to listing of standard RISC-V pseudoinstructions.

relax/norelax

Enable/disable linker relaxation for the following code region.

NOTE: Code region follows by .option relax will emit R_RISCV_RELAX/R_RISCV_ALIGN even linker unsupport relaxation, suggested usage is using .option norelax with .option push/.option pop if you want to disable linker relaxation on specific code region.

NOTE: Recommended way to disable linker relaxation of specific code region is use .option push, .option norelax and .option pop, that prevent enabled linker relaxation accidentally if user already disable linker relaxation.

push/pop

Push/pop current options to/from the options stack.