GNU ARM 汇编伪指令

汇编代码语法官方文档:

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

 

上面的代码表示,在连续相连的地址上,依次放各中断服务函数指针

 

posted @ 2021-06-12 20:40  流水灯  阅读(1147)  评论(0编辑  收藏  举报