ARM 아키텍처에서는 데이터 처리 명령어를 사용하여 레지스터 간의 연산을 수행할 수 있다. 데이터 처리 명령어는 산술 연산, 논리 연산, 시프트 연산 등을 포함한다. 이 글에서는 ARM Data Processing 명령어에 대해 알아본다.
Instruction Format
Syntax
<Operation>{<cond>}{S} Rd, Rn, Operand2
<Operation>
구성
- Arithmetic (산술) :
ADD, ADC, SUB, SBC, RSB, RSC
- Logic (로직) :
AND, ORR, EOR, BIC
- Comparisons (비교) :
CMP, CMN, TST, TEQ
- Data Movement (데이터 이동) :
MOV, MVN
연산은 메모리상이 아닌 레지스터 상에서 동작한다.
First operand인 Rd는 항상 레지스터이고, Second operand(Operand2)는 barrel shifter를 통해 ALU로 보내진다.
Flexible second operand (Operand2)
Constant (상수)
#constant
32비트 워드 내에서 짝수의 비트만큼 8비트 값을 오른쪽으로 회전(ROR)시켜 생성할 수 있는 모든 값을 가질 수 있다.
MOV R0, #4096 ; uses 0x40 ROR 26
ADD R1, RR2, #0xFF0000 ; uses 0xFF ROR 16
MOV R0, #0xFFFFFFFF ; = MVN R0, #0
Register with optional shift
Rm {, shift}
Rm
: 데이터를 담을 레지스터shift
- ASR #n : arithmetic shift right n bits
- LSL #n : logical shift left n bits
- LSR #n : logical shift right n bits
- ROR #n : rotate right n bits
LDR R0, [R1, R2, LSL#2]
Shifts
LSL (Logical Shift Left)
2의 제곱으로 곱하기
LSR (Logical Shift Right)
2의 제곱으로 나누기
ASR (Arithmetic Shift Right)
2의 제곱으로 나누고, 부호 비트는 보존
ROR (Rotate Right)
LSB에서 MSB까지 wrap around로 비트 회전
RRX (Rotate Right Extended)
CF에서 MSB까지 랩 어라운드로 단일 비트 회전
Arithmetic Operations (산술 연산)
Opcode | OP | Operation | Description |
0100 | ADD | Rd := Rn + Op2 | Add |
0101 | ADC | Rd := Rn + Op2 + Carry | Add with Carry |
0011 | RSB | Rd := Op2 – Rn | Reverse subtract |
0111 | RSC | Rd := Op2 – Rn + Carry – 1 | Reverse subtract with Carry |
0010 | SUB | Rd := Rn – Op2 | Substract |
0110 | SBC | Rd := Rn – Op2 + Carry – 1 | Substract with Carry |
ADDS r4, r0, r2 ; adding the least significant words
ADC r5, r1, r3 ; adding the most significant words
SUBS r8, r6, #240 ; sets the flags based on the result
RSB r4, r4, #1280 ; subtracts contents of R4 from 1280
Comparisons Operations (비교 연산)
Opcode | OP | Operation | Description |
1010 | CMN | CPSR flags := Rn + Op2 | Compare Negative |
1011 | CMP | CPSR flags := Rn – Op2 | Compare |
1000 | TEQ | CPSR flags := Rn EOR Op2 | Test bitwise equaility |
1001 | TST | CPSR flags := Rn AND Op2 | Test bits |
CMP r2, r9
CMN r0, #6400
CMPGT sp, r7, LSL #2
TST r0, #0x3F8
TSTNE r1, r5, ASR r1
Logical Opeations (로직 연산)
Opcode | OP | Operation | Description |
0000 | AND | Rd := Rn AND Op2 | AND |
1100 | ORR | Rd := Rn OR Op2 | OR |
0001 | EOR | Rd := Rn XOR Op2 | Exclusive OR |
1110 | BIC | Rd := Rn AND (NOT Op2) | Bit Clear |
EORS r0,r0,r3,ROR r6
EORS r7, r11, #0x18181818
BIC r0, r1, #0xab