文章目录 一、基础语法
1、先看一个示例
2、分配和初始化全局变量
3、对齐aligning
4、函数
5、条件
6、include文件
7、宏Macros
二、指令介绍
三、Branch分支
★★★ 友情链接 : 个人博客导读首页—点击此处 ★★★
一、基础语法 1、先看一个示例 写一个简单的c语言程序 把它翻译成汇编语言是这样的: 把C语言编译成汇编语言,其实是这样的:
补充一下stp ldp的知识: stp ldp是双double-word操作
补充一下fp和sp的知识: x29是FP,x30是LR
为何要保存X29 X30
2、分配和初始化全局变量 (示例) 疑问: 如何定义一个未初始化的static静态局部变量和全局变量?
语法格式:
. byte expressions.2 byte expressions. hword expressions. short expressions.4 byte expressions. word expressions. long expressions.8 byte expressions. quad expressions. ascii "string" //带'\n' . asciz "string" //不带'\n' . string "string. float flonums. single flonums. double flonums
3、对齐aligning . align abs- expr, abs- expr, abs- expr 第一个abs- expr: 对齐的size 第二个abs- expr: 填充 第二个abs- expr: 可选,对齐应该跳过的最大字节数. balign[ lw] abs- expr, abs- expr, abs- expr. skip size, fill. space size, fill 分配一大块内存并将其全部初始化到相同的值,可以使用这两个指令. equ symbol, expression. set symbol, expression 宏定义,等价define. equiv symbol, expression 宏定义,如果已经定义过了,则产生error. global symbol. globl symbol 声明,声明后,所有文件都可以使用. comm symbol, length
4、函数 .size name,expression
.type name,type_description
5、条件 . if expression. ifdef symbol. ifndef symbol. else . endif
6、include文件 . include "file"
7、宏Macros . macro macname. macro macname macargs . . . . endm. exitm
(宏也是有递归宏的) ‘enum 0,5’相当于
二、指令介绍 1、instruction set architecture (ISA)框图
2、AArch64 general registers General purpose registers
Frame pointer —x29 Link register —x30 Stack pointer —sp Zero register —xzr Program counter —pc PSTATE register
3、condition flags
4、Load/store (1)、Load/store single register ldr Load Register str Store Register 在寄存器和memory之间传输double-word, single word, half-word, byte
语法: < op> { < size> } Rd, < addr>
The op are: b unsigned byte h unsigned half- word sb signed byte sh signed half- word sw signed word
示例: ldr w8, [ x4] strb x12, [ x2] ldr x5, [ x3, #7 ] ! 前变址寻址 strh w9, [ x6] , #7 后变址寻址 ldrsh x5, [ x0, 8 ] strb w1, [ x9] ldrsh x5, [ x0, 8 ] strb w1, [ x9]
(2)、Load/store single register (unscaled) ldur Load Register (Unscaled) stur Store Register (Unscaled). 特点别ldr str多了一个u而已,offset在 [−256, 256]之间
语法: < op> { < size> } Rd, [ Xn, #imm9]
The op are: b unsigned byte h unsigned half- word sb signed byte sh signed half- word sw signed word
示例: ldursb x4, [ x5, #255 ] stur x1, [ x2, #- 256 ]
(3)、Load/store pair 一次性读写两个寄存器 stp x29, x30, [ sp, #- 16 ] ! ldp x29, x30, [ sp] , #16
三、Branch分支 • Branch, • Branch to Register, • Branch and Link (subroutine call), • Compare and Branch, and • Form program-counter-relative Address.
1、Branch b Branch //无条件跳转, ±128 MB范围 b{} <target_label> //有条件跳转, ±1 MB范围
b main bvs overflow
2、Branch to Register br Xn ret {Xn}
3、Branch and link bl <target_address> blr Xn 其实就是在跳转之前,先将PC+4的地址保持到X30(LR)中
4、Compare and branch cbz Compare and Branch if Zero, cbnz Compare and Branch if Nonzero, tbz Test Bit and Branch if Zero, and tbnz Test Bit and Branch if Nonzero.
cb{n}z Rt, tb{n}z Rt, #imm6,
• The cbz and cbnz instructions have a range of ±1 MB (encoded in 19 bits). • The range of the tbz and tbnz instructions is ±32 KB (encoded in 14 bits).
5、Form PC-relative address adr Form PC-Relative Address adrp Form PC-Relative Address to 4 KB Page
它比ldr Rx,=label更加啊高效,它在两个cycle周期就可以计算这个64bit地址,无需访问memory