Comparison Instructions

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/33

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

34 Terms

1
New cards

Comparison Instructions

Instructions that do not store a result but instead update condition flags in the Program Status Register.

2
New cards

Purpose of Comparison Instructions

To test register values and control program flow using condition codes and conditional branches.

3
New cards

CMP (Compare)

Subtracts an operand from a register and updates condition flags without storing the result.

4
New cards

Typical Use of CMP

Checking whether a register equals a specific value, such as at the start or end of a loop.

5
New cards

CMP Operation

Performs: Rn − operand2, updates flags only.

6
New cards

CMN (Compare Negative)

Adds an operand to a register and updates condition flags without storing the result.

7
New cards

CMN as the Inverse of CMP

CMN performs addition instead of subtraction; the assembler may replace CMP with CMN automatically.

8
New cards

Assembler Optimization Example (CMP to CMN)

CMP r0, #-20 is converted by the assembler to CMN r0, #0x14.

9
New cards

To simplify instruction encoding and improve efficiency for certain immediate values.

Why does CMN Exists?

10
New cards

TST (Test)

Performs a logical AND between a register and an operand and updates condition flags.

11
New cards

Updates N, Z, and C flags, but does not affect the V flag

Flags Affected by TST

12
New cards

Typical Use of TST

Checking whether specific bits in a register are set or clear.

13
New cards

TEQ (Test Equivalence)

Performs a logical XOR between a register and an operand and updates condition flags.

14
New cards

Updates N, Z, and C flags, but does not affect the V flag.

Flags Affected by TEQ

15
New cards

Typical Use of TEQ

Checking whether two values are equal by testing if their XOR result is zero.

16
New cards

TEQ Equality Test Rule

If the XOR result is zero, the Z flag is set, indicating equality.

17
New cards

instruction{<cond>} <Rn>, <operand2>

General Syntax of Comparison Instructions

18
New cards

operand2 (Comparison Instructions)

Can be a register with an optional shift or an immediate value.

19
New cards

Conditional Execution with Comparison Instructions

Comparison instructions are commonly followed by conditional branches like BEQ, BNE, etc.

20
New cards

Program Status Register (PSR)

Holds condition flags and other processor state information.

21
New cards

MRS (Move PSR to Register)

Copies status register contents into a general-purpose register.

22
New cards

MSR (Move Register to PSR)

Writes values from a general-purpose register into the status register.

23
New cards

ARM7TDMI Status Registers

Includes a CPSR (Current Program Status Register) and multiple SPSRs (Saved Program Status Registers).

24
New cards

Reading ARM7TDMI Status Registers

MRS r0, CPSR reads CPSR,
MRS r1, SPSR reads SPSR.

25
New cards

ARM7TDMI SPSR Restriction

Accessing an SPSR in User mode is invalid and results in UNPREDICTABLE behavior.

26
New cards

Register Restriction for MRS

Register r15 (PC) cannot be used as the destination for MRS.

27
New cards

Cortex-M4 Status Register Structure

Has a single status register accessible through APSR, IPSR, EPSR, or PSR views.

28
New cards

APSR (Application PSR)

The only Cortex-M4 status view that contains condition flags.

29
New cards

Reading Flags on Cortex-M4

MRS r3, APSR reads flag values into r3.

30
New cards

Writing Flags on Cortex-M4

MSR APSR, r2 updates only the flags.

31
New cards

Writing Full Status on Cortex-M4

MSR PSR, r7 writes all status information.

32
New cards

Practical Use of Flags in Programs

Flags are usually consumed immediately by conditional instructions rather than read explicitly

33
New cards

Why Direct Flag Reading Is Rare

Conditional branches make control flow simpler and more efficient than manually inspecting flags.

34
New cards

Typical Control Flow Pattern

Comparison instruction → conditional branch → loop or decision logic.