[ARM-assembly]-ARM64汇编语言学习笔记

273 篇文章 421 订阅

快速链接:
.
👉👉👉 个人博客笔记导读目录(全部) 👈👈👈

在这里插入图片描述


★★★ 友情链接 : 个人博客导读首页—点击此处 ★★★

一、基础语法

1、先看一个示例

写一个简单的c语言程序
在这里插入图片描述
把它翻译成汇编语言是这样的:
在这里插入图片描述
把C语言编译成汇编语言,其实是这样的:
在这里插入图片描述

补充一下stp ldp的知识:
stp ldp是双double-word操作

  • ARM64 开始就取消了32位的 LDM,STM,PUSH,POP指令,取而代之的是str\stp、ldr\ldp
  • ARM64 里面对栈的操作是16字节对齐

补充一下fp和sp的知识:
x29是FP,x30是LR

  • sp寄存器在任意时刻会保存栈顶的地址
  • fp寄存器也称为x29寄存器属于通用寄存器,但是在某些时刻我们利用它保存栈底的地址

为何要保存X29 X30

  • 当此函数为叶子函数时,就不需要在对 x29 和 x30 寄存器的保护。 叶子函数:函数里面不再调用其他函数。
  • 当函数中的参数还有其他函数的引用时,需要对参数入栈,进行保护,以防引起数据错误。
2、分配和初始化全局变量

(示例)
在这里插入图片描述
在这里插入图片描述
疑问: 如何定义一个未初始化的static静态局部变量和全局变量?

语法格式:

.byte expressions

.2byte expressions
.hword expressions
.short expressions

.4byte expressions
.word expressions
.long expressions

.8byte 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,

• 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
在这里插入图片描述
在这里插入图片描述


2) Who uses ARM? Currently ARM CPU is licensed and produced by more than 100 companies and is the dominant CPU chip in both cell phones and tablets. Given its RISC architecture and powerful 32-bit instructions set, it can be used for both 8-bit and 32-bit embedded products. The ARM corp. has already defined the 64-bit instruction extension and for that reason many Laptop and Server manufactures are planning to introduce ARM-based Laptop and Servers. 3) Who will use our textbook? The primary audience of our textbook on ARM (ARM Assembly Language Programming & Architecture by Mazidi & Naimi) is undergraduate engineering students in Electrical and Computer Engineering departments. It can also be used by practicing engineers who need to move away from 8- and 16-bit legacy chips such as the 8051, AVR, PIC and HCS08/12 family of microcontrollers to ARM. Designers of the x86-based systems wanting to design ARM-based products can also benefit from this textbook. Table of Contents Chapter 1: The History of ARM and Microcontrollers Chapter 2: ARM Architecture and Assembly Language Programming Chapter 3: Arithmetic and Logic Instructions and Programs Chapter 4: Branch, Call, and Looping in ARM Chapter 5: Signed Numbers and IEEE 754 Floating Point Chapter 6: ARM Memory Map, Memory Access, and Stack Chapter 7: ARM Pipeline and CPU Evolution Appendix A: ARM Cortex-M3 Instruction Description Appendix B: ARM Assembler Directives Appendix C: Macros Appendix D: Flowcharts and Pseudocode Appendix E: Passing Arguments into Functions Appendix F: ASCII Codes
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码改变世界ctw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值