Computer Science and IT‹›⌂ 0:00 studied

Module 3 · 13 min read · Free chapter

Machine instructions and addressing modes

In short

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.

What are you preparing for? We show the practice and previous year questions of your exam.

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 X=(A+B)×(C+D)X = (A + B) \times (C + D), where A, B, C, D and X are variables in memory.

  • Three-address instructions name two sources and a destination, so ADD T1, A, B means T1←A+BT_1 \leftarrow A + B.
  • Two-address instructions reuse the first operand as the destination, so ADD R1, B means R1←R1+BR_1 \leftarrow R_1 + B.
  • One-address instructions use an implied register called the accumulator (AC), so ADD B means AC←AC+B\mathit{AC} \leftarrow \mathit{AC} + B.
  • Zero-address instructions work on a stack, so ADD pops 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 A B+C D+×A\ B + C\ D + \times, 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 2k2^k operations, so N operations need ⌈log⁡2N⌉\lceil \log_2 N \rceil bits. Likewise a field that names one of R registers needs ⌈log⁡2R⌉\lceil \log_2 R \rceil bits, and an address field for a memory of 2m2^m 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 log⁡264=6\log_2 64 = 6 bits and each register field needs log⁡232=5\log_2 32 = 5 bits. That leaves 32−6−5−5=1632 - 6 - 5 - 5 = 16 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 log⁡265536=16\log_2 65536 = 16 bits and the register field needs 4 bits. So only 24−16−4=424 - 16 - 4 = 4 bits are left for the opcode, which allows 24=162^4 = 16 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 (16−12)×26=256(16 - 12) \times 2^6 = 256 one-address instructions fit.

The same reasoning continues downwards. If only 200 one-address instructions are defined, then 256−200=56256 - 200 = 56 patterns are left, and each can be extended by another 6 bits to give up to 56×64=358456 \times 64 = 3584 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 M[x]M[x] 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): EA=A\mathit{EA} = A, so the operand is M[A]M[A] and one memory access is needed.
  • Indirect: EA=M[A]\mathit{EA} = M[A], 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: EA=(R)\mathit{EA} = (R), 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 EA=A+(XR)\mathit{EA} = A + (\mathit{XR}), where the address field A is the start of an array and the index register XR holds the position. In base-register mode EA=(BR)+displacement\mathit{EA} = (\mathit{BR}) + \mathit{displacement}, 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 EA=(PC)+offset\mathit{EA} = (\mathit{PC}) + \mathit{offset}. 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 EA=(R)\mathit{EA} = (R) and then adds the operand size to R, while auto-decrement first subtracts it from R and then uses EA=(R)\mathit{EA} = (R). 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 M[600]=900M[600] = 900, M[900]=120M[900] = 120, M[500]=750M[500] = 750, M[650]=410M[650] = 410, M[902]=88M[902] = 88 and M[499]=66M[499] = 66. 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 M[600]=900M[600] = 900 120
Register R1 none, the operand is in R1 500
Register indirect R1 500 750
Indexed 600+50=650600 + 50 = 650 410
PC-relative 302+600=902302 + 600 = 902 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 M[A]M[A], 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 2004−24=19802004 - 24 = 1980. To jump forward to 2100 instead, the offset would have to be 2100−2004=962100 - 2004 = 96.

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:

  1. Write and compare programs for one expression using three-, two-, one- and zero-address instructions.
  2. Define each addressing mode with its effective address, an example and its number of memory references.
  3. Compare RISC and CISC under several heads and link the comparison to pipelining.
  4. Design an instruction format from given counts of operations, registers and memory words.

Practice questions (our own, not previous-year questions).

  1. Write programs for Y=A−BC+D×EY = \dfrac{A - B}{C + D \times E} using three-, two-, one- and zero-address instructions, and compare their lengths. (10 marks)
  2. Compare RISC and CISC architectures under at least six heads, and explain why RISC suits pipelining. (10 marks)
  3. 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 ⌈log⁡2N⌉\lceil \log_2 N \rceil opcode bits, and R registers need ⌈log⁡2R⌉\lceil \log_2 R \rceil 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.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. The register that holds the address of the next instruction to be fetched is the:

  2. An instruction ADD B that means AC←AC+B\mathit{AC} \leftarrow \mathit{AC} + B is a:

  3. For X=(A+B)×(C+D)X = (A + B) \times (C + D) on a stack machine, the number of instructions including PUSH and POP is:

  4. A 32-bit instruction has a 6-bit opcode and two 5-bit register fields. A signed immediate in the remaining bits ranges from:

  5. A 16-bit instruction has a 4-bit opcode and two 6-bit address fields. If 12 two-address instructions exist, the maximum number of one-address instructions is:

  6. A processor has 40 distinct operations. The minimum opcode width is:

  7. Which is a feature of RISC architecture?

  8. CISC processors most often use which kind of control unit?

  9. In which mode is the operand part of the instruction itself?

  10. The address field holds 600, M[600]=900M[600] = 900 and M[900]=120M[900] = 120. In indirect mode the operand is:

  11. A 4-byte branch at address 2000 has an offset of −24 in PC-relative mode. The target address is:

  12. The address field holds 600 and the index register holds 50. The effective address in indexed mode is:

  13. Which mode needs two memory references to obtain the operand, after the instruction is fetched?

  14. Which addressing mode makes code position independent?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Which addressing mode needs two memory references for the operand?

Indirect mode, because the CPU first reads the operand's address from memory and then reads the operand.

How is the effective address found in PC-relative mode?

Add the offset to the PC, which already holds the address of the next instruction.

What is an expanding opcode?

It reuses unused opcode patterns of a format with more operands, extending them into freed address bits to make more instructions with fewer operands.

What are the main differences between RISC and CISC?

RISC has fixed-length simple instructions, load-store access and many registers, while CISC has variable-length complex instructions with memory operands.

More in this module: Computer organisation and architecture

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Organization and Architecture (IIT Madras, 106106166): instruction set architecture and addressing modes