Course: Computer Architectures
Related source: ARM v7-M Architecture, Directives and Memory
Source: R. Ferrero, Politecnico di Torino, ALU instructions, 26 PDF pages. Filename: 6_ALUinstructions.pdf; no lecture number or date is stated. The source PDF remains local.

Learning route

The ALU performs arithmetic, logical and bit-manipulation operations. Start with the flags, then compare CMP/TST with ordinary ALU instructions, trace addition and subtraction with carry, and finish with multiplication, division, logic, shifts and 64-bit register pairs. The reusable explanations are in ARM ALU Operations and Flags.

What the deck explains

The source defines how N, Z, C and V are produced, how CMP, CMN, TST and TEQ test values without writing a result register, and how an S suffix requests flag updates. It then introduces ADD, ADC, SUB, SBC, RSB, multiply/accumulate instructions, signed and unsigned division, logical operations, shifts and rotates. The final exercise applies these rules to a 64-bit value stored as a high/low register pair.

Core flag rules

  • N copies the result’s highest bit; in two’s complement it indicates a negative signed result.
  • Z is one when the result is zero.
  • After addition, C is one when a carry leaves the 32-bit result.
  • After subtraction, C is one when the unsigned minuend is at least the subtrahend, meaning no borrow was needed.
  • V indicates signed overflow.
  • A comparison or an ALU instruction with S updates flags; the same instruction without S normally leaves them unchanged.

Comparison examples

CMP r0, #10

computes r0 - 10 for flags but does not update r0. For r0=12, the result is 2: N=0, Z=0, C=1, V=0. For r0=12 compared with 12, Z=1. For 12 compared with 14, the signed result is -2: N=1, Z=0, C=0, V=0.

CMN performs the equivalent flag-setting addition. TST computes AND and is useful for testing selected bits; TEQ computes XOR and sets Z when two bit patterns are equal.

64-bit register pairs

A 64-bit value is represented as:

high:low = r1:r0

For addition:

ADDS r4, r0, r2
ADC  r5, r1, r3

The low-word carry flows into the high-word addition, giving r5:r4 = r1:r0 + r3:r2.

For subtraction:

SUBS r4, r0, r2
SBC  r5, r1, r3

SBC incorporates the borrow convention through + C - 1.

64-bit shifts

For a pair r1:r0, a left shift moves the old top bit of the low word into the low bit of the high word:

LSLS r2, r0, #1
ADC  r3, r1, r1

A logical right shift moves the old low bit of the high word into the top bit of the low word:

LSRS r4, r1, #1
RRX  r5, r0

An arithmetic right shift uses ASRS for the high word so the sign bit is preserved, then RRX for the low word:

ASRS r6, r1, #1
RRX  r7, r0

The carry flag is the one-bit bridge between the registers. Rotation exercises require the same careful tracking of the bit leaving one word and entering the other.

Source page map

PagesMaterial
2-6flags and their arithmetic meaning
7flag exercises
8-11comparison/test instructions and PSR access
12-17ALU datapath, addition, subtraction and reverse subtraction
18-21multiplication, accumulation and division
22-24logic, shifts and rotates
2564-bit shift exercises
26course-board link

Retrieval questions

  1. Why does ADDS update flags while ADD normally does not?
    Answer: the S suffix requests status updates.
  2. What does CMP change?
    Answer: flags only; it does not write the subtraction result to its operand register.
  3. Why does 64-bit addition use ADC for the high word?
    Answer: it includes the carry produced by the low-word addition.
  4. What is the difference between LSR and ASR?
    Answer: LSR inserts zeros; ASR copies the sign bit.