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.
Japanese name: 即値
A constant embedded directly in an instruction. In RISC-V addi it is a 12-bit signed integer that is sign-extended before use.
Japanese name: 即値
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.
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.
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.
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.
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.
Normally not. If the code is written to load a constant, the assembler or compiler automatically turns it into the required combination of instructions.
Immediate operand appears in 3 practice questions across 1 topic, including ISA & Assembly.
As an Amazon Associate I earn from qualifying purchases.
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.