1. GNU ARM 彙編簡介
任何彙編行都是如下結構:
[<label>:] [<instruction or directive>} @ comment[<標籤>:] [<指令>} @ 註釋
GNU ARM 彙編中,任何以冒號結尾的都被認爲是一個標籤,而不一定非要在一行的開始。下面是一個簡單的例子,這段彙編程序定義了一個"add"的函數,該函數返回兩個參數的和:
- .section .text, “x”
- .global add @ give the symbol add external linkage
- add:
- ADD r0, r0, r1 @ add input arguments
- MOV pc, lr @ return from subroutine
- @ 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 |