Assembly language and addressing modes
Talking to the processor
- The CPU runs machine code — raw bit patterns.
- Assembly language is a readable version of it, one instruction at a time.
- We also meet the addressing modes that say where to find an operand.
Machine code and assembly
- Machine code is bit patterns, specific to one architecture — what the CPU actually runs.
- Assembly language is a readable form with one instruction per machine instruction, written with mnemonics like
LDD,ADD,JMP. - An assembler translates assembly into machine code.
Assembly language is:
Assembly uses mnemonics and maps one-to-one to machine code; an assembler translates it.
The two-pass assembler
- A two-pass assembler reads the source twice:
- Pass 1 builds a symbol table — recording the address of each label (like
LOOP:); no code yet. - Pass 2 generates the code — and when an instruction refers to a label (
JMP LOOP), it looks the address up in the symbol table. - Two passes are needed to handle forward references (a jump to a label defined later).
What does pass 1 of a two-pass assembler do?
Pass 1 records where each label is (the symbol table); pass 2 then generates code, using the table to resolve label references.
Why does the assembler need two passes?
A jump may target a label that appears later in the source; pass 1 finds all labels first so pass 2 can resolve them.
The instruction set
- A small generic set groups into:
- data movement (
LDD,LDM,STO,MOV), arithmetic (ADD,SUB,INC,DEC), - logic/bit (
AND,OR,XOR,LSL,LSR), compare & branch (CMP,JMP,JPE,JPN), I/O (IN,OUT), andEND. - To trace a program, make a table of the PC, ACC, index register, variables and flags, and update it after each instruction until
END.
Addressing modes
| Mode | Where the operand is | Example |
|---|---|---|
| immediate | the value is in the instruction | LDM #10 |
| direct | the instruction holds an address; use the value there | LDD 200 |
| indirect | the address holds another address, which holds the data | LDI 200 |
| indexed | effective address = address + index register (arrays) | LDX 100 |
| relative | the address is relative to the PC | — |
In immediate addressing (e.g. LDM #10), the operand is:
Immediate addressing uses the literal value in the instruction (here, 10).
Indexed addressing is most useful for:
The effective address is base address + index register, so increasing the index walks through an array.
Match each addressing mode to its meaning.
Immediate = value; direct = address of the value; indirect = address of an address.
You've got it
- machine code = bit patterns; assembly = mnemonics, 1:1, translated by an assembler
- a two-pass assembler builds a symbol table (pass 1), then code (pass 2) — to handle forward references
- immediate (value in instruction), direct (address), indirect (address of an address), indexed (address + index register, for arrays)