GNU ARM汇编快速入门

前言:

以前用ARM的 IDE工具,使用的是ARM标准的汇编语言。现在要使用GNU的工具,当然要了解一点GNU ARM汇编的不同之处。其实非常的简单,浏览一下文档然后再看看程序就完全可以搞定了,或者你硬着头皮看GNU ARM的汇编程序,用不了多少时间你就就可以无师自通了。个人比较健忘,还是把文档翻译了一下,算是给自己一个避免遗忘的理由吧。

ARM汇编语言源程序语句,一般由指令,伪操作,宏指令和伪指令作成.ARM汇编语言的设计基础是汇编伪指令,汇编伪操作和宏指令.

目前常用的ARM编译环境有2种:
ARMASM: ARM公司的IDE中使用了CodeWarrior的编译器,绝大多数windows下的开发者都在使用这一环境,完全按照ARM的规定;
GNU ARM ASM: GNU工具的ARM版本,与ARMASM略有不同;

关于CodeWarriror ARM汇编的书和文章很多,本文假定你已经完全了解ARMASM,这里只说明GNU ARM汇编,并针对ARMASM给出说明。本文翻译自:GNU ARM Assembler Quick Reference,本人水平有限,错误难免,转载随意,请注明出处。英文原文地址不详。


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

 


GNU ARM汇编伪指令


下面列出了一些GNU ARM汇编伪指令,并给出了相应说明。


.ascii “<string>” 在汇编中定义字符串并为之分配存储空间(与armasm中的DCB功能类似)。
.asciz “<string>” 和.ascii类似, 但不分配存储空间。

 

.balign <power_of_2> {,<fill_value> {,<max_padding>} }
以某种排列方式在内存中填充数值。 (该指令与armasm中的ALIGN类似)。
power_of_2表示排列方式,其值可为4,8,16或32,单位是byte;
fill_value是要填充的值;
max_padding最大的填充界限,请求填充的bytes数超过该值,将被忽略。

 

.byte <byte1> {,<byte2>} … 定义一个或多个Byte,并为之分配空间(与armasm的DCB类似)。

.code <number_of_bits> 设定指令宽度,16表示Thumb,32表示ARM assembly
(和armasm中的CODE16,CODE32相同)。

 

.if
.else
.endif
预编译宏(与armasm中的IF ELSE ENDIF相同)。

 

.end 汇编文件结束标志,常常省略不用。


.endm 宏结束标志。
.exitm 宏跳出。
.macro <name> {<arg_1} {,<arg_2>} … {,<arg_N>}
定义一段名为name的宏,arg_xxx为参数。
必须有对应的.endm结尾。
可以使用.exitm从中间跳出宏。(与armasm中的MACRO, MEND, MEXIT相同)。
在使用宏参数时必须这样使用:“\<arg>”。

例如:
[CODE].macro SHIFTLEFT a, b
.if \b < 0
MOV \a, \a, ASR #-\b
.exitm
.endif
MOV \a, \a, LSL #\b
.endm


.rept <number_of_times> 循环执行.endr前的代码段number_of_times次。
(与armasm中的WEN相似)


.irp <param> {,<val_1>} {,<val_2>} …
循环执行.endr前的代码段,param依次取后面给出的值。
在循环执行的代码段中必须以“\<param> ”表示参数。


.endr 结束循环(与armasm中的WEND相似).

 

.equ <symbol name>, <value> 为一个标号赋值,类似C中的#define。(与armasm中的EQU相同)

 

.err 编译错误报告,将引起编译的终止。

 

.global <symbol> 全局声明标志,这样声明的标号将可以被外部使用。(与armasm中的EXPORT相同)。

 

.hword <short1> {,<short2>} …
插入一个16-bit的数据队列。(与armasm中的DCW相同)

 

.ifdef <symbol> 如果 <symbol>被定义,该快代码将被编译。以 .endif结束。
.ifndef <symbol> 如果 <symbol>未被定义,该快代码将被编译。以 .endif结束。

 

.include “<filename>” 包含文件。(与armasm中的INCLUDE 或者C中的#i nclude一样)

 

<register_name> .req <register_name>
定义一个寄存器,.req的左边是定义的寄存器名,右边是使用的真正使用的寄存器。
(与armasm中的RN类似)
例如:acc .req r0

 

[CODE].section <section_name> {,”<flags>”}
开始一个新的代码或数据段。.text, 代码段;.data, 初始化数据段;.bss, 未初始化数据段。
这些段都有缺省的标志(flags),联接器可以识别这些标志。(与armasm中的AREA相同)。

For ELF targets, the .section directive is used like this:

.section name [, "flags"[, @type[, @entsize]]]

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

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 on targets where the @ character is the start of a comment (eg ARM) then another character is used instead. For example the ARM port uses the % character.

如:.section .text._start, "ax", %progbits

     If flags contains M flag, type argument must be specified as well as entsize argument. Sections with M flag but not S flag must contain fixed size constants, each entsize octets long. Sections with both M and S must contain zero terminated strings where each character is entsize bytes long. The linker may remove duplicates within sections with the same name, same entity size and same flags.
    If no flags are specified, the default flags depend upon the section name. If the section name is not recognized, the default will be for the section to have none of the above flags: it will not be allocated in memory, nor writable, nor executable. The section will contain data.

For ELF targets, the assembler supports another type of .section directive for compatibility with the Solaris assembler:

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

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

This directive replaces the current section and subsection. The replaced section and subsection are pushed onto the section stack. See the contents of the gas testsuite directory gas/testsuite/gas/elf for some examples of how this directive and the other section stack directives work.

 

.set <variable_name>, <variable_value> 变量赋值。(与armasm中的SETA相同)

 

.space <number_of_bytes> {,<fill_byte>}
分配number_of_bytes字节的数据空间,并填充其值为fill_byte,若未指定该值,缺省填充0。
(与armasm中的SPACE功能相同)

 

.word <word1> {,<word2>} …
插入一个32-bit的数据队列。(与armasm中的DCD功能相同)


GNU ARM汇编特殊字符和语法

 

代码行中的注释符号: ‘@’
整行注释符号: ‘#’
语句分离符号: ‘;’
直接操作数前缀: ‘#’ 或 ‘$’

 

.arm 以arm格式编译,同code32
.thumb 以thumb格式编译,同code16
.code16 以thumb格式编译
.code32 以arm格式编译

篇后语:


更详细的使用说明请参照:ARM Architecture Reference Manual, Addison-Wesley ISBN 0-201-73719-1 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值