Module 3 · 13 min read · Free chapter
Machine instructions and addressing modes
An instruction has an opcode and operand fields, and N operations need ⌈log₂ N⌉ opcode bits. Immediate mode carries the operand, direct uses EA = A, indirect uses EA = M[A], indexed adds an index register, and PC-relative adds an offset to the address of the next instruction.
Start from zero: the delivery note
Think of a dabba delivery man in Mumbai who gets a small note each morning. One note says "Deliver to flat 302", so he goes straight there. Another says "Ask the watchman of building C for the flat number", so he makes an extra trip before the real one. A third says "The dabba is already in your bag", and then no trip is needed at all.
A machine instruction is exactly such a note for the CPU. It names an action, such as add or move, and it tells the CPU where to find the things to act on. The part that names the action is the opcode, and the parts that point to the data are the operands.
The different ways of pointing to data are called addressing modes. The first note is like direct addressing, the second like indirect addressing, and the third like immediate addressing. This chapter shows how instructions are laid out in bits and how each mode finds its operand.
Why it matters. Every program written in C or Java finally runs as such instructions. Exams ask how many bits an instruction needs, how many memory trips a mode costs and where the operand finally is, and all three answers come from this simple picture.
Instruction formats: three, two, one and zero addresses
An instruction is a fixed group of bits split into fields. A typical format has an opcode field followed by one or more address fields, each naming a register, a memory location or a constant. The CPU keeps the address of the next instruction in the program counter (PC), fetches the instruction, decodes its opcode and then executes it.
Instructions differ in how many addresses they carry. Take the job , where A, B, C, D and X are variables in memory.
- Three-address instructions name two sources and a destination, so
ADD T1, A, Bmeans . - Two-address instructions reuse the first operand as the destination, so
ADD R1, Bmeans . - One-address instructions use an implied register called the accumulator (AC), so
ADD Bmeans . - Zero-address instructions work on a stack, so
ADDpops the top two values and pushes their sum.
3-address 2-address 1-address 0-address
ADD T1, A, B MOV R1, A LOAD A PUSH A
ADD T2, C, D ADD R1, B ADD B PUSH B
MUL X, T1, T2 MOV R2, C STORE T ADD
ADD R2, D LOAD C PUSH C
MUL R1, R2 ADD D PUSH D
MOV X, R1 MUL T ADD
STORE X MUL
POP X
The four programs need 3, 6, 7 and 8 instructions. So fewer addresses per instruction give shorter instructions but longer programs, and the designer must balance the two. The zero-address program is simply the postfix form , which is why stack machines suit expression evaluation.
Why it matters. Instruction length decides how much memory a program needs and how fast it can be fetched. GATE asks you to count instructions or memory accesses for such a program, so practise writing each version once.
Opcode and operand bits: numericals
Every field must be wide enough for the choices it encodes. With k opcode bits we can name operations, so N operations need bits. Likewise a field that names one of R registers needs bits, and an address field for a memory of words needs m bits.
Worked example 1. A processor has 32-bit instructions, 64 operations and 32 general registers, with the format opcode, Rdest, Rsrc, immediate. The opcode needs bits and each register field needs bits. That leaves bits for the immediate, so a signed constant can range from −32,768 to +32,767.
Worked example 2. A memory has 64 K words, and each instruction is one 24-bit word holding an opcode, one register field for 16 registers and one memory address. The address needs bits and the register field needs 4 bits. So only bits are left for the opcode, which allows operations.
Expanding opcodes. Instructions with fewer operands have spare bits, and a clever format uses them to make the opcode longer. Suppose a 16-bit instruction has a 4-bit opcode and two 6-bit address fields, and 12 of the 16 opcode patterns are used by two-address instructions. The remaining 4 patterns can each be extended by the 6 bits of the first address field, so up to one-address instructions fit.
The same reasoning continues downwards. If only 200 one-address instructions are defined, then patterns are left, and each can be extended by another 6 bits to give up to zero-address instructions.
Why it matters. These numericals appear in GATE in some form every few years. They test one habit: write the total bits, subtract each field, and treat every unused pattern as room for more instructions.
RISC and CISC
Two design ideas shape instruction sets. A CISC (complex instruction set computer) offers many powerful instructions, some of which read memory, add and write memory in one go. A RISC (reduced instruction set computer) keeps only simple instructions and lets the compiler combine them.
| Feature | RISC | CISC |
|---|---|---|
| Instruction length | Fixed, often 32 bits | Variable |
| Instructions and modes | Few and simple | Many, some complex |
| Memory access | Only load and store touch memory | Arithmetic can use memory operands |
| Registers | Many general-purpose registers | Fewer registers |
| Cycles per instruction | Mostly one, pipeline friendly | Several, varying by instruction |
| Control unit | Usually hardwired | Often microprogrammed |
| Examples | ARM, MIPS, RISC-V | x86 |
RISC moves work from the hardware to the compiler. Because every instruction has the same length and simple decoding, the processor can overlap many instructions, as explained in instruction pipelining and hazards. CISC programs are shorter and need less memory, which mattered a great deal when memory was costly.
Modern chips blur the line. A typical x86 processor translates its complex instructions inside the chip into simple RISC-like micro-operations and then overlaps those.
Why it matters. This choice explains why phones mostly use RISC processors that save power, while desktop software built over decades keeps x86 alive. Exams ask for the features in a table like the one above, so learn each row as a pair.
Basic addressing modes and memory references
An addressing mode is the rule that turns the address field of an instruction into the actual operand. The memory address finally used is called the effective address (EA). Let A be the value in the address field, R a register named in the instruction and the memory word at address x.
- Immediate: the operand is the constant A itself, as in
ADD R1, #5, so no memory access is needed to get it. - Direct (absolute): , so the operand is and one memory access is needed.
- Indirect: , so the CPU first reads the address from memory and then reads the operand, which needs two memory accesses.
- Register: the operand is in register R, so no memory access is needed and the field needs only a few bits.
- Register indirect: , so the register holds a pointer and one memory access fetches the operand.
These counts are for getting the operand only, after the instruction itself has been fetched. A question that asks for the total memory references must also add the fetch of the instruction words.
Each mode has a natural use. Immediate mode suits constants, register mode suits values in heavy use, and register indirect mode is how C pointers and stack operations work. Indirect mode lets one short address field reach a large memory, but every operand costs an extra trip.
Why it matters. Register modes are the fastest because they never leave the CPU, which is why compilers keep loop counters in registers. Exams ask which mode needs the fewest memory references, or which mode a C statement such as x = *p uses.
Displacement modes and effective-address numericals
Many modes add a displacement to a register. In indexed mode , where the address field A is the start of an array and the index register XR holds the position. In base-register mode , where the base register holds the start of a program or segment and the small displacement picks a word inside it.
In PC-relative mode . By the time the operand is formed, the PC already holds the address of the next instruction, so the offset is counted from there. Branches use this mode, and it keeps code position independent because the same offset works wherever the program is loaded.
Auto-increment mode uses and then adds the operand size to R, while auto-decrement first subtracts it from R and then uses . Both step through arrays and implement stack push and pop.
Worked example. A two-word instruction is stored at addresses 300 and 301, and its address field (word 301) holds 600. Register R1 holds 500, the index register XR holds 50, and memory holds , , , , and . After the fetch the PC holds 302, and the table gives the effective address and operand for each mode.
| Mode | Effective address | Operand |
|---|---|---|
| Immediate | none, the operand is word 301 | 600 |
| Direct | 600 | 900 |
| Indirect | 120 | |
| Register R1 | none, the operand is in R1 | 500 |
| Register indirect R1 | 500 | 750 |
| Indexed | 410 | |
| PC-relative | 88 | |
| Auto-decrement R1 | R1 becomes 499, and that is the address used | 66 |
The trick in such questions is to stop at the right level. Direct mode stops at , indirect mode goes one step further, and relative mode starts from the updated PC, not from the address of the instruction.
Worked example. A 4-byte branch instruction sits at byte address 2000 and its signed offset field holds −24. Since the PC holds 2004 during execution, the target is . To jump forward to 2100 instead, the offset would have to be .
Why it matters. Arrays, records, relocatable programs and branches each need a different way of forming addresses, and these modes cover them with short fields. GATE effective-address questions are pure bookkeeping, so a neat table like the one above avoids nearly every mistake.
Descriptive lens: how written papers ask this
Written papers for engineering services, IT officer posts and state Assistant Programmer exams ask about this topic in four ways:
- Write and compare programs for one expression using three-, two-, one- and zero-address instructions.
- Define each addressing mode with its effective address, an example and its number of memory references.
- Compare RISC and CISC under several heads and link the comparison to pipelining.
- Design an instruction format from given counts of operations, registers and memory words.
Practice questions (our own, not previous-year questions).
- Write programs for using three-, two-, one- and zero-address instructions, and compare their lengths. (10 marks)
- Compare RISC and CISC architectures under at least six heads, and explain why RISC suits pipelining. (10 marks)
- Define the effective address and explain eight addressing modes with one example each, stating the memory references of each. (15 marks)
Answer plan for question 2.
- Begin by defining RISC and CISC in one sentence each, and state that RISC moves work to the compiler.
- Compare instruction length, which is fixed in RISC and variable in CISC.
- Compare the number of instructions and addressing modes, which are few in RISC and many in CISC.
- Explain the load-store rule of RISC against the memory operands allowed in CISC.
- Contrast many registers, hardwired control and one-cycle instructions in RISC with fewer registers and microprogrammed control in CISC.
- Explain that fixed length and simple decoding let a RISC pipeline fetch and decode one instruction every cycle.
- Conclude that modern processors combine both ideas by translating CISC instructions into RISC-like micro-operations.
Quick revision
- An instruction has an opcode field and operand fields, and the PC holds the address of the next instruction.
- Three-, two-, one- and zero-address instructions trade shorter instructions against longer programs.
- N operations need opcode bits, and R registers need bits per register field.
- In an expanding opcode, each unused pattern multiplies by 2 to the power of the freed bits.
- RISC has fixed-length simple instructions, load-store access and many registers, while CISC has variable-length complex instructions and memory operands.
- Immediate and register modes need no memory reference for the operand, direct needs one and indirect needs two.
- Indexed is A + (XR), base is (BR) + displacement, and relative is (PC) + offset with the PC already advanced.
- Auto-decrement changes the register before use, and auto-increment changes it after use.