Summary
This course introduces ARM assembly language programming, focusing on low-level interactions with hardware. It covers the ARM processor architecture, registers, memory, data types, and basic instructions like data movement, arithmetic, logical operations, and shifts. The course uses an emulator for practical examples, demonstrating how to write, compile, and run simple assembly programs, and explains concepts like system calls, stack memory, and program counter.
Key Insights
Subtraction results can be ambiguous (large positive vs. negative); flags resolve this.
When subtracting two numbers, the resulting bit pattern might represent either a large positive number or a negative number. The 'N' flag in the CPSR clarifies whether the result is negative.
Logical shifts move bits left (LSL) or right (LSR), filling vacated bits with zeros.
Logical Shift Left (LSL) moves bits left, filling the rightmost bits with zeros. Logical Shift Right (LSR) moves bits right, filling the leftmost bits with zeros. Shifting by 'n' positions multiplies/divides by 2^n.
LSL by N is equivalent to multiplying by 2^N; LSR by N is equivalent to dividing by 2^N.
A left logical shift by N positions effectively multiplies the number by 2 raised to the power of N. A right logical shift by N positions effectively divides the number by 2 raised to the power of N. This provides an efficient way to perform multiplication and division by powers of two.
Sections
Introduction to ARM Assembly
Assembly language is a low-level programming language close to machine code.
Assembly language is a low-level programming language for computers or programmable devices that is closest to machine language and is often specific to a particular computer architecture. ARM is an increasingly popular assembly language, with over 200 billion devices containing an ARM chip, making it valuable to understand for efficient hardware interaction.
Course objective: Fundamental understanding of ARM processors and assembly programming.
The course aims to provide a fundamental understanding of the ARM processor and assembly programming in general, enabling students to write basic ARM assembly programs using available instructions.
ARM processors are widely used in embedded devices, phones, and laptops.
ARM is a programming language used in various applications, particularly in embedded devices, popular smartphones (Android and some iPhones), and increasingly in laptops like new MacBooks. Understanding ARM processors and programming them directly is important for efficient hardware and software interaction.
Course covers ARM basics, arithmetic, logical operators, shifts, branches, loops, and hardware interactions.
The course will cover basic ARM instructions, arithmetic and logical operations, shift operators, branching, loops, hardware interactions, and troubleshooting in a Linux-based system.
The course utilizes the CPU Emulator for easy, accessible learning.
The course primarily uses an online emulator called CPU Emulator, allowing easy follow-along without special equipment or prerequisites, though final videos may require a Linux-based system.
General assembly principles learned apply to other languages like x86.
The principles taught in this series apply to any assembly language, with the syntax specifically targeted towards ARM processors, enabling adaptation to other architectures like x86.
ARM Processor Architecture and Development Environment
Registers are fast, limited-size memory areas close to the CPU for quick access.
Registers are memory areas very close to the CPU, allowing for quick access and writing. They offer fast general storage but have a limited capacity. In this 32-bit processor, each register can hold 32 bits of data, often referred to as a 'word'.
Data sizes: bit, byte (8 bits), half-word (16 bits on 32-bit), word (32 bits on 32-bit).
Data sizes include a single bit, a byte (8 bits), a half-word (16 bits on a 32-bit processor, 32 bits on a 64-bit processor), and a word (32 bits on a 32-bit processor, 64 bits on a 64-bit processor). Registers typically hold data equivalent to a 'word' size.
R0-R6 are general-purpose registers; R7 is used for system calls.
Registers R0 to R6 are general-purpose, available for any storage needs. Register R7 has a special function related to system calls, used to communicate requests to the operating system by storing numerical codes that map to specific actions.
Stack memory is slower but larger than registers, used for complex data.
Stack memory, typically stored in RAM, is slower to access than registers but can hold significantly more data. It's used for representing more complex data sets, like lists of numbers, where each memory location can store a value.
SP, LR, and PC are special-purpose registers: Stack Pointer, Link Register, Program Counter.
Special purpose registers include SP (Stack Pointer, indicating the next available memory address on the stack), LR (Link Register, storing the return address for functions), and PC (Program Counter, tracking the next instruction to execute).
CPSR stores program status information like flags for negative, zero, carry, overflow.
The CPSR (Current Program Status Register) stores information about the program's current state, including flags that indicate the result of operations (e.g., negative, zero, carry, overflow). These flags are crucial for conditional logic.
ARM processors are bi-endian, supporting both Little Endian and Big Endian modes.
ARM processors are bi-endian, meaning they can operate in either Little Endian (most significant bit on the left) or Big Endian (most significant bit on the right) modes, depending on the configuration or operation.
Basic ARM Assembly Program Structure
'_start' label marks the program's entry point, exported globally.
The '_start' label signifies the beginning of program execution. '.global _start' makes this label visible externally, allowing the system to locate and run the program from this point.
MOV instruction moves data from source to destination (register or immediate value).
The MOV instruction is used to move data. It takes a destination (e.g., a register like R0) and a source (e.g., an immediate value preceded by '#' or another register). Example: MOV R0, #30 moves the decimal value 30 into register R0.
Hexadecimal values can be represented using '0x' prefix.
Hexadecimal values can be specified by prefixing them with '0x', for example, '0x0A' represents the hexadecimal value A. The course primarily uses decimal for ease of understanding unless hex is specifically required.
SWI instruction triggers a software interrupt to call the operating system.
SWI (Software Interrupt) is used to request services from the operating system. A specific number (e.g., 1 for exit) is placed in register R7, and SWI then transfers control to the OS to perform the requested action.
The emulator's SWI implementation may differ from actual ARM processors.
The SWI instruction's behavior in the CPU Emulator might not perfectly match that of an actual ARM processor, but it demonstrates the intended usage for requesting OS services like program termination.
Program counter (PC) tracks the address of the next instruction to be executed.
The Program Counter (PC) register holds the memory address of the next instruction that the processor will execute. Stepping through instructions shows how the PC updates to point to the subsequent instruction.
Addressing Modes in ARM Assembly
Immediate addressing: Moving a constant value directly into a register.
Immediate addressing involves loading a constant, immediate value directly into a register. Example: MOV R0, #5.
Register direct addressing: Moving data between registers.
Register direct addressing involves moving data directly from one register to another. Example: MOV R1, R0.
Data section declares initialized data like lists for use in programs.
A 'data' section allows declaration of initialized data. Data is given a label (like a variable name), a type (e.g., '.word' for 32-bit numbers), and the values themselves.
LDR loads data from memory (stack) into registers.
The LDR (Load Register) instruction is used to load data from memory into a register. It can be used with direct addressing (loading the address of a label) or register indirect addressing.
Direct addressing initializes a register with the memory address of a label.
Direct addressing with LDR (e.g., LDR R0, =list) loads the base memory address of the specified label ('list') into the target register (R0).
Register indirect addressing accesses data at the memory address stored in a register.
Register indirect addressing uses square brackets around a register (e.g., LDR R1, [R0]) to access the data located at the memory address currently held by the register R0 and load it into R1.
Register indirect with offset accesses memory address + offset value.
Register indirect addressing with an offset (e.g., LDR R2, [R0, #4]) calculates a memory address by adding an immediate offset to the base address in R0, then loads the data from that calculated address into R2.
Pre-increment addressing accesses memory, then increments the address register.
Pre-increment addressing (e.g., LDR R1, [R0, #4]!) first calculates the memory address by adding the offset to the register's value, loads the data from that address, and then updates the register with the new address.
Post-increment addressing accesses memory at the current address, then increments the address register.
Post-increment addressing (e.g., LDR R1, [R0], #4) first loads data from the memory address currently in the register (R0), and then updates the register by adding the specified offset to it.
Arithmetic Operations in ARM Assembly
ADD, SUB, and MUL instructions perform addition, subtraction, and multiplication.
Basic arithmetic operations are supported by ADD (destination = src1 + src2), SUB (destination = src1 - src2), and MUL (destination = src1 * src2). Division is not included due to complexity.
Arithmetic operations can optionally set flags in the CPSR register.
Arithmetic instructions can be suffixed with 'S' (e.g., SUBS, ADDS, MULS) to automatically set flags in the CPSR register based on the operation's result (negative, zero, carry, overflow).
CPSR flags (N, Z, C, V) indicate result properties: Negative, Zero, Carry, Overflow.
The CPSR register contains flags: N (Negative), Z (Zero), C (Carry), and V (Overflow). These flags are essential for conditional branching and understanding the outcome of arithmetic operations.
Subtraction results can be ambiguous (large positive vs. negative); flags resolve this.
When subtracting two numbers, the resulting bit pattern might represent either a large positive number or a negative number. The 'N' flag in the CPSR clarifies whether the result is negative.
ADC performs addition with carry, handling results that exceed register capacity.
The ADC (Add with Carry) instruction adds three values: the first source register, the second source register, and the value of the Carry flag (C) from the CPSR. This is used for multi-word arithmetic.
SBC performs subtraction with borrow.
The SBC (Subtract with Borrow) instruction subtracts the second source and the inverted Carry flag (borrow) from the first source. This is used for multi-word subtraction.
Logical Operators in ARM Assembly
AND, ORR, EOR perform bitwise AND, OR, and Exclusive OR operations.
Logical operators include AND (result bit is 1 if both input bits are 1), ORR (result bit is 1 if either input bit is 1), and EOR (result bit is 1 if input bits differ). They take a destination and two sources.
MVN performs a bitwise NOT (negation) on the source, storing the result.
The MVN (Move Not) instruction computes the bitwise negation of the source operand and stores the result in the destination register. It effectively flips all the bits.
Bitwise AND can be used to clear specific bits in a register.
By ANDing a register with a mask where the desired bits are 1 and the bits to be cleared are 0, specific bits in the register can be selectively cleared to 0.
Logical Shifts and Rotations
Logical shifts move bits left (LSL) or right (LSR), filling vacated bits with zeros.
Logical Shift Left (LSL) moves bits left, filling the rightmost bits with zeros. Logical Shift Right (LSR) moves bits right, filling the leftmost bits with zeros. Shifting by 'n' positions multiplies/divides by 2^n.
LSL by N is equivalent to multiplying by 2^N; LSR by N is equivalent to dividing by 2^N.
A left logical shift by N positions effectively multiplies the number by 2 raised to the power of N. A right logical shift by N positions effectively divides the number by 2 raised to the power of N. This provides an efficient way to perform multiplication and division by powers of two.
Rotations (ROR) move bits where the discarded bit loops back to the other end.
Rotate Right (ROR) shifts bits to the right, but the bit shifted off the right end is inserted into the leftmost position. ARM does not have a direct Rotate Left instruction; it can be simulated using ROR.
Shift operations can be combined with data movement instructions.
Shift operations can be appended to data movement instructions (like MOV). For example, 'MOV R1, R0, LSL #1' moves the value in R0 to R1 and then shifts it left by one position in a single instruction.
Rotations are used in more abstract applications like hashing, crypto, and graphics.
Rotations are less common than shifts and are typically used in specialized applications such as hashing algorithms, cryptography, and graphics processing. They are often included for historical reasons.
Conditionals and Branching in ARM
CMP instruction compares two values by subtracting them and setting CPSR flags.
The CMP (Compare) instruction subtracts the second operand from the first (e.g., CMP R0, R1 performs R0 - R1) without changing the registers, but updates the CPSR flags based on the result.
Branch instructions (e.g., BGT) alter program flow based on CPSR flags.
Branch instructions, like BGT (Branch if Greater Than), conditionally change the program's execution path by jumping to a specified label if the condition met by the CPSR flags (set by CMP or other instructions) is true.
Conditional execution allows replicating 'if-else' logic from high-level languages.
By combining comparison instructions with conditional branch instructions, programmers can implement control flow structures similar to 'if-else' statements found in higher-level programming languages.
Labels define specific locations in the code that branch instructions can jump to.
Labels are symbolic names given to specific instructions or code segments. Branch instructions use these labels to determine the target address for altering the program flow.
Ask a Question
*Uses 1 Wisdom coin from your coin balance





![Learn C Programming and OOP with Dr. Chuck [feat. classic book by Kernighan and Ritchie]](https://i.ytimg.com/vi/PaPN51Mm5qQ/hqdefault.jpg)





