匯編代碼語法官方文檔:
https://sourceware.org/binutils/docs-2.39/as.html
什么是匯編偽指令
1、沒有對應機器指令的匯編指令,主要用於協助匯編程序進行匯編。
2、在計算機中直接運行的程序所對應的語言叫機器語言(指令),如果直接按二進制表示出來就是一系列 0 和 1 。當然,用機器語言編寫程序的時代一般都使用八進制或十六進制,它們和二進制是3位對1位或4位對1位的關系,從而簡化機器語言程序代碼在書面(或屏幕)上的表示。即便如此,這種程序還是太令人望而生畏了,所以才有了“匯編”語言,其含義是用人類比較容易理解的符號來替代機器語言。假定一條加法指令的邏輯序列可用二進制表示為0110……1011,用八進制可表示為 3……3,用十六進制則表示為 6……B。而匯編指令則用 ADD x,y 這種形式來表示一條機器指令,即每一條機器指令都用一個對應的“匯編指令”來替代所形成的指令系統叫“匯編語言”,而將用匯編語言編寫的程序翻譯成機器語言的過程叫“匯編”過程。為了增加匯編語言的可讀性和協助翻譯程序對匯編源程序進行翻譯而增加的匯編指令就是“偽指令”。
3、匯編語言源程序必須翻譯成機器語言才能被計算機運行,而翻譯通常是由計算機通過匯編程序來實現,翻譯過程稱為匯編。在翻譯過程中需要匯編語言源程序向匯編程序提供相應的編譯信息,而這些信息是通過在匯編語言源程序中加入偽指令實現的。也就是說偽指令是放在匯編語言源程序中用於指示匯編程序如何對源程序進行匯編的指令。
GNU arm 匯編偽指令
所有的偽指令都是以 . 開頭命令,然后剩下的命名通常是小寫字母,比如 .section .type
.section
格式:.section name [, "flags "[, %type [,flag_specific_arguments ]]]
flags:
The optional flags argument is a quoted string which may contain any combination of the following characters:
a section is allocatable,可以將 allocatable 理解為 relocatable,也就是段可以被裝載到內存的任何位置並運行
w section is writable
x section is executable
M section is mergeable
S section contains zero terminated strings
G section is a member of a section group
T section is used for thread-local-storage
type:
The optional type argument may contain one of the following constants:
progbits: section contains data
nobits: section does not contain data (i.e., section only occupies space)
note: section contains data which is used by things other than the program
init_array: section contains an array of pointers to init functions
fini_array: section contains an array of pointers to finish functions
preinit_array: section contains an array of pointers to pre-init functions
實例:
.section .stack, "aw", %nobits /* 命名一個”.stack"段, 該段具有可分配和可寫屬性,該段不包含數據,該段用於保存堆棧值 */
.size
格式: .size name , expression
This directive sets the size associated with a symbol name. The size in bytes is computed from expression which can make use of label arithmetic. This directive is typically used to set the size of function symbols.
.type
This directive is used to set the type of a symbol.
格式有多種形式,如下:
.type <name> STT_<TYPE_IN_UPPER_CASE>
.type <name>,#<type>
.type <name>,@<type>
.type <name>,@<type>
.type <name>,%<type>
.type <name>,"<type>"
The types supported are:
STT_FUNC
function
Mark the symbol as being a function name.
STT_GNU_IFUNC
gnu_indirect_function
Mark the symbol as an indirect function when evaluated during reloc processing.
(This is only supported on Linux targeted assemblers).
STT_OBJECT
object
Mark the symbol as being a data object.
STT_TLS
tls_object
Mark the symbol as being a thead-local data object.
STT_COMMON
common
Mark the symbol as being a common data object.
STT_NOTYPE
notype
Does not mark the symbol in any way. It is supported just for completeness.
例子1
.section .text.Reset_Handler
.type Reset_Handler, %function Reset_Handler: ldr sp, =_estack /* set stack pointer */ bl entry bx lr .size Reset_Handler, .-Reset_Handler
例子2
.section .text.Reset_Handler
.type Reset_Handler, STT_FUNC Reset_Handler: ldr sp, =_estack /* set stack pointer */ bl entry bx lr .size Reset_Handler, .-Reset_Handler
例子3
.global g_pfnVectors .section .isr_vector,"a",%progbits .type g_pfnVectors, %object ;聲明一個 object 對象 .size g_pfnVectors, .-g_pfnVectors g_pfnVectors: .word _estack .word Reset_Handler .word NMI_Handler .word HardFault_Handler .word MemManage_Handler .word BusFault_Handler .word UsageFault_Handler
.global
.global makes the symbol visible to ld. If you define symbol in your partial program, its value is made available to other partial programs that are linked with it. Otherwise, symbol takes its attributes from a symbol of the same name from another file linked into the same program.
.global 用於聲明全局變量,是其讓ld可見。
.word
在當前地址放一個 32bit 的值
g_pfnVectors: .word _estack .word Reset_Handler .word NMI_Handler .word HardFault_Handler
上面的代碼表示,在連續相連的地址上,依次放各中斷服務函數指針