What is Immediate operand?

A constant embedded directly in an instruction. In RISC-V addi it is a 12-bit signed integer that is sign-extended before use.

Field: ISA & Assembly

Japanese name: 即値

Why constants are embedded in instructions

Small constants such as 0, 1, 4 and −1 are used extremely often in programs, for example when advancing an array index or moving the stack position. Putting the constant in the instruction avoids the effort of reading it from memory and reduces the number of instructions. In an ISA with fixed-length instructions, however, the number of bits available for the constant is limited.

An example in RISC-V

In the RISC-V addi instruction, the immediate is a 12-bit signed integer representing −2048 to 2047. Of the 32 bits of the instruction, 7 are used for the opcode, 5 each for two register numbers, and 3 to distinguish the function, leaving 12 bits. A larger constant is built with two instructions. For 0x12345678, lui places 0x12345 in the upper 20 bits and addi adds 0x678. One point needs care. addi sign-extends its immediate, so when the lower 12 bits are 0x800 or more, they are added as a negative number. In that case the value placed in the upper part is made larger by one. The assembler performs this adjustment automatically.

Other ISAs

x86 instructions are variable in length, so an 8-, 16- or 32-bit immediate can simply follow the instruction. The 64-bit Arm instruction set has instructions that insert a value 16 bits at a time, and it represents immediates for logical operations as a repeating bit pattern. In any ISA, a large constant that does not fit in an instruction may be read from a table of constants in memory.

Try it: questions about Immediate operand

In RISC-V, what is the usual way to put a 32-bit constant that does not fit in a 12-bit immediate (e.g. 0x12345678) into a register?

  1. AUse a single addi with the whole 32-bit immediate
  2. BSet the upper 20 bits with lui, then add the lower 12 bits with addi
  3. CConstants cannot be used, so the value must always be read from input
Show answer and explanation

Answer: B. Set the upper 20 bits with lui, then add the lower 12 bits with addi

lui (load upper immediate) places a 20-bit immediate in bits 31-12 of a register and clears the low 12 bits. Following it with addi to add the low 12 bits produces any 32-bit constant. However, the addi immediate is sign-extended, so when the top bit of the low 12 bits is 1, the lui value must be made one larger. Writing "li a0, 0x12345678" in assembly makes the assembler expand it into these two instructions as needed. In fixed-length ISAs, building large constants in pieces is the norm.

FAQ

What is sign extension?

When a value with few bits is widened to a value with more bits, the most significant bit is copied into the higher positions. This is done so that the value of a negative number does not change.

Does the programmer have to do anything when the immediate has too few bits?

Normally not. If the code is written to load a constant, the assembler or compiler automatically turns it into the required combination of instructions.

Questions that mention Immediate operand (2)

Immediate operand appears in 3 practice questions across 1 topic, including ISA & Assembly.

  1. Which statement about instruction length in the RISC-V base ISA and in x86 is correct?
  2. In RISC-V instructions, each register field (rd, rs1, rs2) is 5 bits wide. Why?

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary