汇编控制指令

1. GNU ARM 汇编简介

    任何汇编行都是如下结构:

[<label>:] [<instruction or directive>} @ comment
[<标签>:] [<指令>} @ 注释
 

     GNU ARM 汇编中,任何以冒号结尾的都被认为是一个标签,而不一定非要在一行的开始。下面是一个简单的例子,这段汇编程序定义了一个"add"的函数,该函数返回两个参数的和:

  1. .section .text, “x”  
  2. .global add                      @ give the symbol add external linkage  
  3. add:  
  4.          ADD r0, r0, r1          @ add input arguments  
  5.          MOV pc, lr              @ return from subroutine  
  6.                                  @ end of program  

2. GNU ARM汇编伪指令

    GNU ARM汇编伪指令均以.开始。常用汇编伪指如下所示:

汇编伪指令 描述 举例
.ascii “<string>” 插入字符串到目标文件,但不以NULL结束 .ascii "JNZ" @插入字节0x4A 0x4E 0x5A
.asciz “<string>” 与.ascii类似,但以NULL结束 .asciz "JNZ" @插入字节0x4A 0x4E 0x5A 0x00
.balign <power_of_2>
      {,<fill_value>
    {,<max_padding>} }
使下面的位置<power_of_2>对齐 .byte 0x55       @插入字节: 0x55
.balign 4,0      @插入3个对齐字节:0x00 0x00 0x00
.word 0xAA55EE11 @插入字节:0x11 0xEE 0x55 0xAA (LSB order)
.byte <byte1> {,<byte2>} … 插入字节到目标文件 .byte 64, 'A'         @ inserts the bytes 0x40 0x41
.byte 0x42            @ inserts the byte 0x42
.byte 0b1000011, 0104 @ inserts the bytes 0x43 0x44
.code <number_of_bits> 设置指令位数,16位Thumb或32位ARM .code 16    @设置以下汇编为Thumb指令
.else 与 .if 和 .endif 配合使用  
.end 标志汇编文件结束,汇编器不再读后面的内容  
.endif 怀.if 配合使用  
.endm 标志宏定义结束,与.macro配合使用  
.endr 结束一个loop,与.rept 和 .irp 配合使用  
.equ <symbol name>, <value> 设置一个符号的值 .equ adams, (5 * 8) + 2  @ set adams to 42
.set adams, 0x2A         @ set adams to 42
adams = 0b00101010       @ set adams to 42
.err 汇编时如遇到.err,则停止编译  
.exitm 中途退出宏定义  
.global <symbol> 标识symbol被程序的其它模块(汇编或C)访问,
相当于Kernel中的EXPORT_SYMBOL
.global _my_test_count  @它可被其它模块R/W
.hword <short1> {,<short2>}
插入16位(半字)值到目标文件 .hword 0xAA55, 12345 @ 插入字节 0x55 0xAA 0x39 0x30
.2byte 0x55AA, -1 @插入字节 0xAA 0x55 0xFF 0xFF
@ Least Significant Byte (LSB) ordering assumed
.if <logical_expression> 与 .endif 配合使用  
.ifdef <symbol> 与 .endif 配合使用  
.ifndef <symbol> 与 .endif 配合使用  
.include “<filename>” 与C的#include类似  
.irp <param> {,<val_1>}
{,<val_2>} …
Repeats a block of code, once for each value
 in the value list. Mark the end of the block 
using a .endr directive. In the repeated code block,
 use \<param> to substitute the associated 
value in the value list.
 
.macro <name> {<arg_1}
{,<arg_2>} … {,<arg_N>}
定义一个名为name的汇编宏,它有N个参数,
且必须以.endm结束。
.macro SHIFTLEFT a, b
    .if \b < 0
        MOV \a, \a, ASR #-\b
       .exitm
    .endif
    MOV \a, \a, LSL #\b
.endm
.section <section_name>
{,”<flags>”}
开始一个新的section, section_name可为:
   .text: 代码段
   .data: 已初始化的数据段
   .bss: 未初始化的数据段
flags可为:
   a allowable section
   w writable section
   x executable section
.section .text, “x”
.set <variable_name>,
<variable_value>
设置变化的值,与.equ一样 .set adams, 0x2A         @ set adams to 42
.space <number_of_bytes>
{,<fill_byte>}
预留给定字节空间,并设置为0或fill_byte .space 0x400,0
.word <word1> {,<word2>} … 插入32位字列表到目标文件 head_ptr: .word 0 @ Head pointer to within buffer (initially zero)
tail_ptr: .word 0 @ Tail pointer to within buffer (initially zero)
.word 0xDEADBEEF  @inserts the bytes 0xEF 0xBE 0xAD 0xDE
.4byte -42        @ inserts the bytes 0xD6 0xFF 0xFF 0xFF
         @ Least Significant Byte (LSB) ordering assumed
.rept <number_of_times> 重复执行代码块number_of_times次,与C中for
类似,以endr结束
 
<register_name> .req
<register_name>
寄存器重新命名 acc .req r0

3. ARM特殊字符和语法

@ 代码行中的注释符号
# 整行注释符号
; 语句分离符号
#或$ 直接操作数前缀
.arm
汇编使用ARM指令
.thumb
汇编使用Thumb指令
.code16
汇编使用Thumb指令
.code32
汇编使用ARM指令
.force_thumb
强制使用thumb模式,即使不支持
.thumb_func
Mark entry point as thumb coded (force bx entry)
.ltorg
Start a new literal pool(文字池:嵌入在代码中的用以存放常数的区域)

0 8进制
0x或0X 16进制
0b或0B 二进制
无前导符 10进制

.data 标识把随后的语句放入目标文件数据段
.text
标识把随后的语句放入目标文件的代码段
.extern symbol
从其它模块引入符号,类似C中的extern
.skip expression
 在目标文件中skip expression指定的字节数
buffer: .skip 512 @Buffer of 512 bytes, uninitialised

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章