【汇编】ARMV8-arch64汇编学习笔记

1、寻址模式

【汇编】ARMV8-arch64汇编学习笔记_第1张图片
[Rn, offset]! 前变址寻址
最终访问内存的地址 = Rn+offset
操作后Rn的值 = Rn+offset

[Rn], offset 后变址寻址
最终访问内存的地址 = Rn
操作后Rn的值 = Rn+offset

[Rn, offset] 偏移寻址
最终访问内存的地址 = Rn+offset
操作后Rn的值不变

	注意:对于偏移寻址,还可以使用寄存器偏移寻址、扩展寄存器偏移寻址
	[base,Xm{,LSL #imm}]
	[base,Wm,(S|U)XTW {#imm}]

示例:
(1)、在进程切换调用的cpu_switch_to函数中,使用到了后变址寻址

ENTRY(cpu_switch_to)
	mov	x10, #THREAD_CPU_CONTEXT
	add	x8, x0, x10
	mov	x9, sp
	stp	x19, x20, [x8], #16		// store callee-saved registers
	stp	x21, x22, [x8], #16
	stp	x23, x24, [x8], #16
	stp	x25, x26, [x8], #16
	stp	x27, x28, [x8], #16
	stp	x29, x9, [x8], #16
	str	lr, [x8]
	add	x8, x1, x10
	ldp	x19, x20, [x8], #16		// restore callee-saved registers
	ldp	x21, x22, [x8], #16
	ldp	x23, x24, [x8], #16
	ldp	x25, x26, [x8], #16
	ldp	x27, x28, [x8], #16
	ldp	x29, x9, [x8], #16
	ldr	lr, [x8]
	mov	sp, x9
#ifdef CONFIG_THREAD_INFO_IN_TASK
	msr	sp_el0, x1
#else
	and	x9, x9, #~(THREAD_SIZE - 1)
	msr	sp_el0, x9
#endif
	ret
ENDPROC(cpu_switch_to)

2、load/store指令的介绍

术语:

sign-extends :符号扩展,前面补符合位和0
zero-extends :0扩展,即前面补0

S : sign-extends
B : byte
H : half-word
R :register
P : pair 双字操作

(1)、Load-Store Pair

LDP Wt1, Wt2, addr  //从addr处读取两个word到Wt1和Wt2
LDP Xt1, Xt2, addr  //从addr处读取两个double-word到Xt1和Xt2
LDPSW Xt1, Xt2, addr  //从addr处读取两个word到Xt1和Xt2, sign-extends
STP Wt1, Wt2, addr	//将Wt1和Wt2写入addr地址处
STP Xt1, Xt2, addr	//将Xt1和Xt2写入addr地址处

(2)、LDNP and STNP //非暂存指令(Non-temporal),不会加载到cache

LDNP Wt1, Wt2, [base,#imm]
LDNP Xt1, Xt2, [base,#imm]
STNP Wt1, Wt2, [base,#imm]
STNP Xt1, Xt2, [base,#imm]

(3)、Load-Store Unprivileged 在EL1中执行数据的加载和写入,权限等是按照EL0的配置来执行

LDTR Wt, [base,#simm9]
LDTR Xt, [base,#simm9]
LDTRB Wt, [base,#simm9]  加载一个字节并sign-extends扩展到Wt, 在EL1下执行的,但是按照EL0的权限来执行
LDTRSB Wt, [base,#simm9]
LDTRSB Xt, [base,#simm9]
LDTRH Wt, [base,#simm9]
LDTRSH Wt, [base,#simm9]
LDTRSH Xt, [base,#simm9]
LDTRSW Xt, [base,#simm9]
STTR Wt, [base,#simm9]
STTR Xt, [base,#simm9]
STTRB Wt, [base,#simm9]
STTRH Wt, [base,#simm9]

(4)、Load-Store Exclusive

【补充armv8的exclusive操作】

为了解决多核情况下的锁竞争问题,arm引入了exclusive操作,并添加了相应的指令。
exclusive的操作的核心,就是会将锁,用一个状态机进行维护,该状态机有2种状态,open状态和exclusive状态。要想成功的对锁进行上锁,状态必须要从exclusive状态切换到open状态,其他状态,都是失败的。
LDXR指令,将状态从open状态切换到exclusive状态,STXR指令,将状态从exclusive状态切换到open状态
【汇编】ARMV8-arch64汇编学习笔记_第2张图片

术语 : (R-register,  P-pair)
LDXR Wt, [base{,#0}]
LDXR Xt, [base{,#0}]
LDXRB Wt, [base{,#0}]
LDXRH Wt, [base{,#0}]
LDXP Wt, Wt2, [base{,#0}]
LDXP Xt, Xt2, [base{,#0}]
STXR Ws, Wt, [base{,#0}]
STXR Ws, Xt, [base{,#0}]
STXRB Ws, Wt, [base{,#0}]
STXRH Ws, Wt, [base{,#0}]
STXP Ws, Wt, Wt2, [base{,#0}]
STXP Ws, Xt, Xt2, [base{,#0}]

(5)、 Load-Acquire / Store-Release 标记物理地址为非独占访问
(Non-exclusive)

LDAR Wt, [base{,#0}]
LDAR Xt, [base{,#0}]
LDARB Wt, [base{,#0}]
LDARH Wt, [base{,#0}]
STLR Wt, [base{,#0}]
STLR Xt, [base{,#0}]
STLRB Wt, [base{,#0}]
STLRH Wt, [base{,#0}]

(Exclusive)

LDAXR Wt, [base{,#0}]
LDAXR Xt, [base{,#0}]
LDAXRB Wt, [base{,#0}]
LDAXRH Wt, [base{,#0}]
LDAXP Wt, Wt2, [base{,#0}]
LDAXP Xt, Xt2, [base{,#0}]
STLXR Ws, Wt, [base{,#0}]
STLXR Ws, Xt, [base{,#0}]
STLXRB Ws, Wt, [base{,#0}]
STLXRH Ws, Xt|Wt, [base{,#0}]
STLXP Ws, Wt, Wt2, [base{,#0}]
STLXP Ws, Xt, Xt2, [base{,#0}]

你可能感兴趣的:(ARM)