打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
GNU ARM 汇编伪指令备忘录
userphoto

2021.03.12

关注

GNU arm 汇编伪指令

所有的汇编指令都以period('.')开头命名,而剩余的命名通常用小写字母

1. .section

 .section "name "[, flags ...]

Use the .section directive to assemble the following code into a section named name

Note that the section name is quoted. There may be a sequence of comma separated
flags:
#alloc section is allocatable
#write section is writable
#execinstr
section is executable
#tls

section is used for thread local storage

实例:

.section ".start", #alloc, #execinstr  @命名一个“.start"段,该段具有可分配和可执行属性

 .section name [, "flags "[, @type [,flag_specific_arguments ]]]
The optional flags argument is a quoted string which may contain any combination of
the following characters:
a section is allocatable
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
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"段, 该段具有可分配和可写属性,该段不包含数据,该段用于保存堆栈值

2. .text

 .text subsection
Tells as to assemble the following statements onto the end of the text subsection numbered
subsection, which is an absolute expression. If subsection is omitted, subsection number
zero is used.

实例:

.text @正文段包括指令的代码段, 当“subsection”忽略时, 缺省值用0。

3. .size

This directive is used to set the size associated with a symbol.

 .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.

4. .space

.space size , fill
This directive emits size bytes, each of value fill. Both size and fill are absolute expressions.
If the comma and fill are omitted, fill is assumed to be zero. This is the same as '.skip’.

实例:

.space    4096 @ 分配4096bytes = 4KB,fill 缺省用“0” 填充

5. .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.

实例:

E1 :

start:
        .type    start,STT_FUNC         @声明start函数:

E2:

start:
        .type    start,#function       @声明start函数,与E1例子等价子子

E3:

        .type    LC0, #object  @声明一个object对象
LC0:        .word    LC0            @ r1
        .word    __bss_start        @ r2
        .word    _end            @ r3
        .word    _edata            @ r6
        .word    input_data_end - 4    @ r10 (inflated size location)
        .word    _got_start        @ r11
        .word    _got_end        @ ip
        .word    .L_user_stack_end    @ sp
        .size    LC0, . - LC0

        .type    phexbuf,#object 声明一个phebuf对象
phexbuf:    .space    12
        .size    phexbuf, . - phexbuf

6.  .macro .endm .exitm

宏指令和子程序有些类似,可以使用参数,节约代码存储,但macro调用时不需要保护现场,从而节约了系统的开销;

当代码较短或输入参数较多时,可考虑使用宏指令代替子程序。

.macro 和 .endm 配对使用, 而.exitm 为提早退出宏定义

格式:

.macro macname

.macro macname macargs ...

定义宏名为“macname“, 如果需要参量将他们定义在宏名后,用逗号或空格键分开。

You can qualify the macro argument to indicate whether
all invocations must specify a non-blank value (through ':req’), or whether it
takes all of the remaining arguments (through ':vararg’). You can supply a
default value for any macro argument by following the name with '=deflt ’.

定义示例:

.macro comm

定义了一个“comm" 宏名,它不带参数

.macro plus1 p, p1

.macro plus1 p p1

这两个宏定义比相同,定义了一个”plus1“宏,带有两个参数分别为p、p1

.macro reserve_str p1=0 p2

定义”reserve_str"宏, 两个参数p1和p2, p1有个缺省值为0, p2没,

如果调用这个宏

为 'reserve_str a, b' ==> \p1 = a, \p= b

为 'reserve_str ,b' ==> \p1=0, \p=b.

.macro m p1:req, p2=0, p3:vararg

定义宏“m”,三个参数, p1必须要赋值, p2不强制要求它有个缺省值,p3在宏调用时才被赋值。

Note that since each of the macargs can be an identifier exactly as any other
one permitted by the target architecture, there may be occasional problems if
the target hand-crafts special meanings to certain characters when they occur
in a special position.

There are several possible ways around this problem:

Insert white space
If it is possible to use white space characters then this is the simplest
solution. eg:
.macro label l
\l :
.endm
Use '\()’ The string '\()’ can be used to separate the end of a macro argu-
ment from the following text. eg:
.macro opcode base length
\base\().\length
.endm
Use the alternate macro syntax mode
In the alternative macro syntax mode the ampersand character ('&’)
can be used as a separator. eg:
.altmacro
.macro label l
l&:
.endm

实例:

定义一个sum宏用于将一系列数存入内存中

    .marco sum from=5, to=5
    .long    \from
    .if    \to-\from
    sum    "(\from+1)", \to
    .endif
    .endm

通过这样定义,“SUM 0,5”等价于这样的汇编输入
    .long 0
    .long 1
    .long 2
    .long 3

    .long 4

    .long 5

7. .algin

.align abs-expr , abs-expr , abs-expr

Pad the location counter (in the current subsection) to a particular storage boundary.

The first expression (which must be absolute) is the alignment required, as described below.

The second expression (also absolute) gives the fill value to be stored in the padding
bytes. It (and the comma) may be omitted. If it is omitted, the padding bytes are normally
zero. However, on some systems, if the section is marked as containing code and the fill
value is omitted, the space is filled with no-op instructions.

The third expression is also absolute, and is also optional. If it is present, it is the
maximum number of bytes that should be skipped by this alignment directive. If doing
the alignment would require skipping more bytes than the specified maximum, then the
alignment is not done at all. You can omit the fill value (the second argument) entirely by
simply using two commas after the required alignment; this can be useful if you want the
alignment to be filled with no-op instructions when appropriate.

实例

.align 3 @填充“0”,使其内存边界值,对齐于2^3 = 8倍数。

.align  @缺省为2^2=4,对齐

8. .global

.global symbol , .globl symbol
.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可视。

9. .ltorg

This directive causes the current contents of the literal pool to be dumped into
the current section (which is assumed to be the .text section) at the current 

location (aligned to a word boundary). GAS maintains a separate literal pool
for each section and each sub-section. The .ltorg directive will only affect the
literal pool of the current section and sub-section. At the end of assembly all
remaining, un-empty literal pools will automatically be dumped.
Note - older versions of GAS would dump the current literal pool any time a
section change occurred. This is no longer done, since it prevents accurate
control of the placement of literal pools.

当 LDR Rd,=const 伪指令要求将常数放入文字池时,汇编器会执行下列操作:
· 检查以前任何文字池中的常数是否可用以及是否可寻址。 如果是,则会对
现有常数进行寻址。
· 如果以前的文字池已经不可用,则会尝试将常数放入下一个文字池中。
如果下一个文字池超出范围,汇编器会生成一条错误消息。 在这种情况下,必
须使用 LTORG 指令在代码中放置一个附加的文字池。 LTORG 指令应放在失败的 LDR
伪指令之后,并位于范围 ±4KB (ARM, 32 位 Thumb-2)或范围 0 到 +1KB
(Thumb-2 以前的 Thumb、 16 位 Thumb-2)内。

LTORG 指令指示汇编器立即汇编当前文字池。
语法
LTORG
用法
汇编器在每个代码节末尾汇编当前文字池。 代码节的结束位置由后续节开始处
的 AREA 指令确定,或由汇编代码的结束位置确定。
这些缺省文字池有时会超出某些 LDR 、 VLDR 和 WLDR 伪指令的范围。 使用 LTORG 可
确保在指定范围内汇编文字池。
大型程序可能需要多个文字池。 将 LTORG 指令放在无条件跳转或子例程返回指令
之后,以使处理器不会试图将常数作为指令来执行。
汇编器对文字池中的数据进行字对齐

文字池必须放在处理器不会试图将其当作指令来执行的位置上。 它们应放在无
条件跳转指令的后面,或者放在子例程末尾处的返回指令的后面。

上段中文摘自Real View 编译工具 《汇编器指南》

文字池 literal pool 定义: https://en.wikipedia.org/wiki/Literal_pool

Literal pools :来自ARM定义:http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473c/Bgbccbdi.html

The assembler uses literal pools to hold certain constant values that are to be loaded into registers. The assembler places a literal pool at the end of each section. The end of a section is defined either by the END directive at the end of the assembly or by the AREA directive at the start of the following section. The END directive at the end of an included file does not signal the end of a section.

10. .int .long

.int expressions

.long expressions

Expect zero or more expressions, of any section, separated by commas. For each expression,
emit a number that, at run time, is the value of that expression.

.long 0x12345678, 0x87654321 @声明两个值,定义4个字节数据

11. .byte .short .long .quad

分别定义 单字节,双字节, 4字节, 8字节数据

12. .string "str", .string8 "str", .string16 "str", .string32 "str", .string64 "str"

Copy the characters in str to the object file.You may specify more than one string
to copy, separated by commas. Unless otherwise specified for a particular machine, the
assembler marks the end of each string with a 0 byte.

The variants string16, string32 and string64 differ from the string pseudo opcode
in that each 8-bit character from str is copied and expanded to 16, 32 or 64 bits respectively.
The expanded characters are stored in target endianness byte order.
实例:
.string32 "BYE"
expands to:
.string "B\0\0\0Y\0\0\0E\0\0\0"/* On little endian targets. */
.string  "\0\0\0B\0\0\0Y\0\0\0E"/* On big endian targets. */

13. .ascii .asciz

.ascii "string ". . .
.ascii expects zero or more string literals separated by commas. It assembles each string (with no automatic trailing zero byte) into consecutive
addresses.

.asciz is just like .ascii, but each string is followed by a zero byte. The “z” in '.asciz’ stands for “zero”.

14. .rept .endr

Repeat the sequence of lines between the .rept directive and the next .endr directive count
times.
For example, assembling
.rept 3
.long 0
.endr

is equivalent to assembling
.long 0
.long 0
.long 0

15. .bss

.bss symbol , size [, [blocking_flag ] [,alignment_flag ]]
Reserve space for symbol in the .bss section. size is in words. If present, block-
ing flag indicates the allocated space should be aligned on a page boundary if
it would otherwise cross a page boundary. If present, alignment flag causes the
assembler to allocate size on a long word boundary.

16. .save

.save reglist
Generate unwinder annotations to restore the registers in reglist. The format
of reglist is the same as the corresponding store-multiple instruction.
core registers
.save {r4, r5, r6, lr}
stmfd sp!, {r4, r5, r6, lr}

参考:

Using as The gnu Assembler (Sourcery G++ Lite 2010q1-188) Version 2.19.51

备注:

初步完成于20160204, wiwa

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
QT:QString、QByteArray和char *的转换。
C#中如何将字符串转换byte[],同时如何将byte[]换成字符串?
java生成md5值
Lua struct
从 Go 分析 Struct 对齐如何影响内存使用量
Qt中QString,char,int,QByteArray之间到转换
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服