What is Assembler?
Software that converts a program written in assembly language into machine instruction bit patterns. It turns labels into addresses and expands pseudo-instructions into real instructions.
Japanese name: アセンブラ
Software that converts a program written in assembly language into machine instruction bit patterns. It turns labels into addresses and expands pseudo-instructions into real instructions.
Japanese name: アセンブラ
An assembler reads assembly source line by line. For each instruction it looks up the mnemonic, checks the operands, and packs the opcode, register numbers and immediate values into the fixed fields of the instruction format, for example a 32-bit RISC-V word. Directives such as .text, .data, .word and .globl tell it which section to emit into, which data to lay down and which symbols to export. Because assembly maps almost one-to-one onto machine instructions, every instruction set architecture (ISA) has its own assembly language.
Labels let programmers write branch targets and data names instead of numeric addresses. A label may be used before it is defined, so a classic assembler makes two passes: the first walks the source assigning an address to every label and building a symbol table, and the second encodes each instruction using those addresses. Distances to labels in the same section are filled in directly. References to symbols in other files, or to addresses that depend on final placement, cannot be resolved yet, so the assembler leaves a placeholder and records a relocation entry for the linker in the output object file.
Assemblers accept convenient pseudo-instructions that do not exist in hardware. In RISC-V, nop becomes addi x0, x0, 0, mv rd, rs becomes addi rd, rs, 0, j label becomes jal x0, label, and ret becomes jalr x0, 0(ra). li loads any constant: small values need one addi, while a full 32-bit value becomes lui for the upper 20 bits plus addi for the lower 12, with the upper part adjusted by one when the lower part is negative because addi sign-extends its immediate. Assemblers may also rewrite a conditional branch whose target lies beyond its reach of about ±4 KiB into an inverted branch skipping over a jal.
A compiler translates a high-level language, where one statement can become many instructions and the mapping involves real decisions about registers and optimization. An assembler makes almost no such choices; its job is encoding, label resolution and pseudo-instruction expansion. Its output is an object file, not a runnable program: the linker still has to combine object files and libraries and fix final addresses. A disassembler performs the reverse translation from bit patterns back to mnemonics, but it cannot recover comments or, without a symbol table, the original label names.
Answer: A. addi x0, x0, 0
RISC-V has no dedicated do-nothing instruction; nop is encoded as "addi x0, x0, 0". The write to x0 is discarded, so no register or memory changes. Likewise "mv rd, rs" is "addi rd, rs, 0", "j label" is "jal x0, label" and "ret" is "jalr x0, 0(ra)". Using x0 to keep the number of instruction kinds small is part of the RISC-V design philosophy.
A compiler translates a high-level language such as C into assembly or machine code, making decisions about register use and optimization. An assembler translates assembly language almost one-to-one into machine code, mainly encoding instructions and turning labels into addresses.
It is a convenient mnemonic that the hardware does not implement directly; the assembler replaces it with one or more real instructions. In RISC-V, for example, nop is assembled as addi x0, x0, 0 and li may become lui followed by addi.
Usually not. It produces an object file with machine code, a symbol table and relocation entries, and the linker turns one or more object files into an executable.
Assembler appears in 4 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.