ARM Assembly 6: Shift 和 Rotate

基础概念

LSL(Logical Shift Left)

  • 功能: 将寄存器中的位向左移动,右边用零填充。左移相当于对二进制数进行乘以2的幂的操作。

  • 语法: LSL{S} Rd, Rn, #shamt

    • Rd: 结果存储的目标寄存器。

    • Rn: 要进行位移的源寄存器。

    • #shamt: 位移的位数(立即数)。

    • S: 可选,若存在则更新条件标志。

MOV R0, #0b00000001   ; R0 = 1
LSL R1, R0, #2        ; R1 = R0 << 2,结果为0b00000100,即4

LSR(Logical Shift Right)

  • 功能: 将寄存器中的位向右移动,左边用零填充。右移相当于对二进制数进行除以2的幂的操作。

  • 语法: LSR{S} Rd, Rn, #shamt

    • Rd: 结果存储的目标寄存器。

    • Rn: 要进行位移的源寄存器。

    • #shamt: 位移的位数(立即数)。

    • S: 可选,若存在则更新条件标志。

lsl/r会改变数字的sign,因为它会向端侧添加0

MOV R0, #0b00001000   ; R0 = 8
LSR R1, R0, #2        ; R1 = R0 >> 2,结果为0b00000010,即2

ASR(Arithmetic Shift Right)

  • 功能: 将寄存器中的位向右移动,同时保持符号位(最高有效位)的值。左侧的空位用符号位填充。这种操作通常用于有符号整数的除法。

  • 语法: ASR{S} Rd, Rn, #shamt

    • Rd: 结果存储的目标寄存器。

    • Rn: 要进行位移的源寄存器。

    • #shamt: 位移的位数(立即数)。

    • S: 可选,若存在则更新条件标志。

MOV R0, #-8           ; R0 = 0b11111000 (假设为8位表示)
ASR R1, R0, #2        ; R1 = R0 >> 2,结果为0b11111110,即-2

ASL(Arithmetic Shift Left)

  • 功能: 在ARM汇编中,ASL通常与LSL(Logical Shift Left)等效,因为左移操作不影响符号位。左移操作将寄存器中的位向左移动,右边用零填充。

  • 语法: LSL{S} Rd, Rn, #shamt(通常用LSL代替ASL)

知识点

  • 符号位保持: ASR在右移时保持符号位不变,这对于处理有符号整数非常重要。

  • 立即数: 位移的位数通常是一个立即数,表示要移动的位数。

  • 条件标志: 使用S后缀(如ASRS)可以更新条件标志,这对于条件执行非常有用。

  • 性能: 位移操作通常比乘法和除法更高效,因此在需要快速计算时非常有用。

使用场景: encryption/decryption

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值