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
Ncopies the result’s highest bit; in two’s complement it indicates a negative signed result.Zis one when the result is zero.- After addition,
Cis one when a carry leaves the 32-bit result. - After subtraction,
Cis one when the unsigned minuend is at least the subtrahend, meaning no borrow was needed. Vindicates signed overflow.- A comparison or an ALU instruction with
Supdates flags; the same instruction withoutSnormally leaves them unchanged.
Comparison examples
CMP r0, #10computes 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:r0For addition:
ADDS r4, r0, r2
ADC r5, r1, r3The 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, r3SBC 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, r1A 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, r0An 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, r0The 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
| Pages | Material |
|---|---|
| 2-6 | flags and their arithmetic meaning |
| 7 | flag exercises |
| 8-11 | comparison/test instructions and PSR access |
| 12-17 | ALU datapath, addition, subtraction and reverse subtraction |
| 18-21 | multiplication, accumulation and division |
| 22-24 | logic, shifts and rotates |
| 25 | 64-bit shift exercises |
| 26 | course-board link |
Retrieval questions
- Why does
ADDSupdate flags whileADDnormally does not?
Answer: theSsuffix requests status updates. - What does
CMPchange?
Answer: flags only; it does not write the subtraction result to its operand register. - Why does 64-bit addition use
ADCfor the high word?
Answer: it includes the carry produced by the low-word addition. - What is the difference between
LSRandASR?
Answer:LSRinserts zeros;ASRcopies the sign bit.