Welcome to the Heart of the Machine!
Hi there! Today we are going on a journey deep inside your computer. Have you ever wondered how a computer actually understands a line of code like x = 5 + 2? Computers don't speak Python or Java; they speak the language of electricity—binary (1s and 0s).
In this chapter, we are going to learn about Assembly Language. Think of it as the "bridge" between the human world and the computer’s raw electrical signals. It’s powerful, fast, and gives you total control over the processor. Don't worry if it seems a bit "robotic" at first—once you learn the patterns, it’s like solving a fun logic puzzle!
1. Machine Code vs. Assembly Language
Before we write any programs, we need to understand the two levels of "low-level" languages:
Machine Code: This is the only thing a processor actually understands. It consists entirely of binary (e.g., 01101010). It is incredibly difficult for humans to read or write without making mistakes.
Assembly Language: To make things easier for us, we use mnemonics. A mnemonic is a short, easy-to-remember abbreviation that represents a machine code instruction. For example, instead of writing a long binary string to add two numbers, we might just write ADD.
Quick Review:
• Machine Code = 1s and 0s (For computers).
• Assembly Language = Mnemonics like ADD or LDR (For humans).
• Assembler = The software that translates Assembly into Machine Code.
Did you know? Every different type of processor (like the one in your phone vs. the one in your laptop) has its own specific Instruction Set. This means Assembly code written for one might not work on the other!
2. The Format of an Instruction
Every instruction in Assembly is usually broken down into two main parts. Think of it like a simple sentence: a verb and a noun.
The Opcode (The Verb)
The Opcode (short for Operation Code) tells the processor what to do. Examples include ADD, SUB (subtract), or MOV (move).
The Operand (The Noun)
The Operand tells the processor what to act upon. This could be a fixed number, a memory address, or a register (a tiny, super-fast storage space inside the CPU).
Analogy: If you are following a recipe that says "Chop the onion," the Opcode is "Chop" and the Operand is "the onion."
Key Takeaway: An instruction = Opcode (Action) + Operand (Data/Location).
3. Addressing Modes: Finding the Data
The "Addressing Mode" tells the CPU how to interpret the Operand. Oxford AQA requires you to know three specific types. Don't worry, we'll use an analogy to make these clear!
1. Immediate Addressing:
The operand is the actual value you want to use.
Example: ADD #5 (Add the number 5 directly).
Analogy: Someone hands you \( \$5 \) in cash. You have the money right there!
2. Direct Addressing:
The operand is a memory address. The CPU must go to that address to find the value.
Example: LDR R1, 101 (Go to memory location 101 and load whatever is inside it into Register 1).
Analogy: Someone gives you a locker number. You have to go to that locker to find the money.
3. Indirect Addressing:
The operand is a register that holds the memory address of the data.
Example: If Register 2 contains the number "200", and you use indirect addressing, the CPU goes to memory location 200 to find the data.
Analogy: You are given a note that says, "The key to the locker is in the blue box." You go to the blue box to find the location of the money.
Common Mistake to Avoid: Students often confuse Direct and Immediate. Just remember: Immediate uses the number itself (usually marked with a #), while Direct uses the number as a "house address" to find a different value.
4. The OxfordAQA Assembly Instruction Set
In your exam, you will use a specific set of instructions based on the ARM processor. Here are the most important ones you need to master:
Data Transfer
• LDR (Load): Copies a value from memory into a register.
• STR (Store): Copies a value from a register into memory.
• MOV (Move): Copies a value from one place to another (like register to register).
Arithmetic and Logic
• ADD: Adds two values.
• SUB: Subtracts one value from another.
• AND, OR, NOT, XOR: Performs bitwise logical operations.
• LSL (Logical Shift Left): Moves bits to the left (multiplies by 2).
• LSR (Logical Shift Right): Moves bits to the right (divides by 2).
Program Flow (Branching)
Normally, the CPU executes instructions one by one in order. Branching allows the program to "jump" to a different part of the code.
• CMP (Compare): Compares two values (usually used before a jump).
• B (Branch): An unconditional jump to a specific label.
• BEQ / BNE: Conditional jumps (Branch if Equal / Branch if Not Equal).
• HALT: Stops the program execution.
5. Tracing and Writing Programs
When you are asked to "trace" a program, you are acting like a computer. You follow the instructions one by one and keep track of what is in the registers and memory.
Example Process:
1. MOV R1, #10 (Register 1 now holds 10).
2. MOV R2, #5 (Register 2 now holds 5).
3. ADD R1, R1, R2 (Add R1 and R2, store the result in R1. R1 is now 15).
4. STR R1, 100 (Store the value 15 into memory location 100).
Memory Aid: Most instructions follow the format: Destination, Source 1, Source 2. The first register listed is almost always where the "answer" is saved!
Final Summary: Key Points to Remember
1. Assembly is Human-Readable: We use mnemonics (short words) instead of binary.
2. The Instruction Structure: Every instruction has an Opcode (the command) and an Operand (the data or location).
3. Addressing is Crucial: Know the difference between Immediate (the value), Direct (the address), and Indirect (the register holding the address).
4. Registers are Fast: Assembly focuses on moving data between registers and main memory (RAM).
5. Program Flow: Use CMP and Branch instructions to create "if statements" and "loops" in Assembly.
Don't worry if this seems tricky at first! Assembly is a very logical language. Once you get used to the idea that you are manually moving data in and out of little boxes (registers), it all starts to click. Keep practicing with simple ADD and MOV instructions!