To build a jump instruction in machine code, you choose an opcode for the jump you need, encode the destination as either a displacement from the current instruction or an absolute address, and pack both into the bit layout your instruction set defines. On a fixed-width RISC machine, an unconditional branch is often a single 32-bit word; on a variable-length CISC machine like x86-64, the same jump may be two bytes when it targets something close, or six bytes when the displacement is large. The destination is the part people get wrong: it's relative to the end of the jump instruction on most architectures, not the address where the jump starts. This guide walks through the encodings, the arithmetic, and the check you can run before executing anything.

I'll use three concrete instruction sets to keep it real: a simplified 16-bit teaching machine, the LEGv8-style AArch64 subset common in textbooks, and x86-64 jump encodings. You don't need an assembler to follow along, but you do need to be precise about byte order and program-counter values.

Decide which jump you actually need

Before touching bits, answer two questions: is the branch conditional or unconditional, and how far can the target be from the branch? Those two answers eliminate most encoding options.

An unconditional jump always transfers control. A conditional branch transfers only when a flag or register comparison says so. Machines also distinguish direct jumps, where the target address is encoded in the instruction, from indirect jumps, where the target comes from a register or memory. Function returns are usually indirect; loop back-edges are usually direct.

Range matters because encoding space is finite. A short-format jump dedicates fewer bits to the displacement, so it reaches less far but occupies less memory. Long-format jumps reach across the whole address space but are wider. If you're writing machine code by hand for a small program, short jumps almost always suffice; if you're generating code that might land anywhere, you need the long form or a relocation step.

For teaching purposes, keep a table like this in your notes:

Jump kind Encoding style Typical reach
Unconditional direct, short 8-bit relative displacement ±128 bytes
Unconditional direct, long 32-bit relative displacement ±2 GiB
Conditional branch Displacement plus condition code Depends on format
Indirect Register or memory operand Full register width

That table is conceptual; the exact sizes depend on the architecture. The point is that "a jump" isn't one instruction. Pick the variant whose range covers every target your program will actually use.

Prerequisites before you start hand-encoding

You need four things nailed down before the first byte:

1: The instruction set reference. The official manual for your target architecture specifies opcode values, field positions, alignment rules, and endianness. Don't work from memory. 2: The load address. Relative jumps are computed from the program counter, so you must know where the code will sit in memory, or at least know that displacements are position-independent. 3: The byte order. Little-endian machines store the least significant byte first; big-endian stores it last. Getting this wrong produces a jump to a nonsensical address. 4: An assembler or disassembler for cross-checking. Even if you assemble by hand, let a tool confirm your bytes. GNU as and objdump, LLVM's tools, or a simulator like QEMU and a debugger all work.

If you have those, the process is mechanical. If you don't, you'll spend hours chasing a bug that turns out to be a swapped byte.

Step-by-step: encoding a fixed-width branch on a RISC machine

This example uses a simplified AArch64-style encoding: 32-bit instructions, a 6-bit opcode, a 26-bit signed word-offset field shifted left by two. That's close to the real B instruction on AArch64, which uses opcode bits 000101 in the top six bits and a 26-bit immediate representing a signed offset in units of four bytes.

Step 1 — Write down the branch address and target address. Suppose the branch instruction sits at address 0x1000 and the target label is at 0x1040.

Step 2 — Compute the byte displacement. Subtract the branch address from the target: 0x1040 − 0x1000 = 0x40. On most architectures the displacement is measured from the address of the branch instruction itself (AArch64 does this; x86-64 measures from the end of the instruction). Check which convention applies.

Step 3 — Convert to instruction units. Divide by four: 0x40 / 4 = 0x10. That's 16 words.

Step 4 — Check the range. A 26-bit signed field holds values from −33,554,432 to 33,554,431. Sixteen is comfortably inside.

Step 5 — Mask to 26 bits and shift into position. The opcode occupies bits 31–26. The immediate occupies bits 25–0. So the final word is (0x05 << 26) | (0x10 & 0x03FFFFFF).

Step 6 — Do the arithmetic. 0x05 << 26 is 0x14000000. OR with 0x10 gives 0x14000010.

Step 7 — Serialize in the machine's byte order. Little-endian gives bytes 10 00 00 14. Big-endian gives 14 00 00 10.

Step 8 — Verify. Load the four bytes into a disassembler or simulator and confirm it decodes as a branch to 0x1040. If it targets a different address, check whether the displacement should have been measured from the end of the instruction, whether you shifted the immediate correctly, and whether the sign extension was applied.

Success check: the disassembly shows the intended target address, and stepping through the simulator lands the program counter there. Failure symptom: the branch lands four bytes off, which almost always means a unit-conversion mistake — dividing by four when you shouldn't, or forgetting that x86-64 measures from the instruction end.

Working with backward jumps and negative displacements

Backward jumps — loop back-edges, mostly — produce negative displacements, and negative numbers are where two's complement bites.

Say a loop body starts at 0x2000 and the conditional branch that closes the loop sits at 0x2020, with a target of 0x2000. The displacement is 0x2000 − 0x2020 = −0x20. Divide by four: −8. In two's complement 26-bit form, −8 is 0x3FFFFF8 before masking, and after masking with 0x03FFFFFF it's 0x3FFFFF8. OR into the opcode and serialize.

The trap here is sign extension. A displacement field only handles negative values correctly if the decoder treats the top bit as a sign bit and sign-extends to full address width before adding. Some toy instruction sets get this wrong, and a backward jump to a nearby label ends up leaping gigabytes away. If you're writing your own simulator, explicitly test with a small negative displacement that wraps the program counter back by a handful of instructions, and confirm the simulated PC lands where you expect.

A second trap is alignment. If the architecture scales the immediate by instruction size (four bytes on most fixed-width RISC cores), the low two bits of the target must be zero. Misaligned targets either fault or get silently rounded, depending on the hardware. Check alignment before you encode.

CISC variable-length jumps: x86-64 example

x86-64 does it differently, and the differences are instructive. A short jump uses opcode EB followed by a signed 8-bit displacement. A near jump uses E9 followed by a signed 32-bit displacement. Conditional jumps use one-byte opcodes in the 70–7F range for short forms and two-byte 0F 80–0F 8F for near forms.

Crucially, x86-64 displacements are measured from the end of the jump instruction, not its start. If a jmp rel8 occupies two bytes and starts at 0x4000, the displacement is added to 0x4002.

Worked example: a short unconditional jump at 0x4000 targeting 0x4008. Instruction length is two bytes, so displacement is 0x4008 − 0x4002 = 6. Bytes: EB 06. Assembling gives you a two-byte branch that reaches 128 bytes forward and 127 bytes backward.

Now the same target with a near jump: E9 plus a 32-bit displacement 06 00 00 00, five bytes total, reaching 0x4008 as well but with a much larger range. Notice how the choice is a pure size-versus-range tradeoff. If the target is within 127 bytes forward of the instruction end, the short form saves three bytes. Compilers exploit this constantly: assemblers choose the shortest encoding that reaches the target, and if the target later moves out of range, the assembler does a second pass and widens the jump.

That two-pass behavior is worth understanding if you ever hand-patch machine code. If you replace a short jump with a near jump, every subsequent address shifts by three bytes, which breaks any absolute references after it. Patch in place only when the encoding length stays the same.

Conditional branches: separating the condition from the target

A conditional branch carries two pieces of information: which condition to test, and where to go. Architectures structure this differently, and the structure affects how you encode it.

On x86, the condition is baked into the opcode. 74 means jump if zero flag set (je), 75 means jump if zero flag clear (jne), 7C means jump if sign flag differs from overflow (jl), and so on. The opcode determines the test; the operand is purely the displacement. To switch conditions, you change one byte.

On AArch64 and similar RISC designs, there's a separate conditional-branch instruction whose 19-bit immediate encodes the displacement and whose opcode encodes the condition. Some designs add a hybrid: a conditional branch with a short range and its condition, plus a follow-on unconditional branch for the rare far case. That two-instruction pattern is how RISC-V handles branches that can't fit a single conditional branch: emit the inverted condition to skip over a single unconditional jump, which then reaches the far target.

Here's the RISC-V pattern in principle. To branch to a distant label on condition C, emit b!C +8 — branch if not C, skipping the next instruction — followed by j farLabel. The skip is always 8 bytes because both instructions are 4 bytes, so encoding the near branch is trivial. You're trading one extra instruction for unbounded conditional reach.

When you build conditional branches by hand, encode the response of the test exactly as the manual specifies. Flag semantics differ: some architectures set flags only on explicit compare, others on every arithmetic result. Getting the negation logic backward gives you a loop that runs zero times or never terminates.

Indirect jumps and the limits of hand-encoding

Direct jumps carry their target in the instruction. Indirect jumps carry the target in a register or memory location, and that changes everything about when the address gets fixed.

On x86-64, FF E0 jumps to the address in RAX; FF 24 25 jumps to the address stored at a 32-bit absolute memory operand. On AArch64, BR Xn transfers to the address in register Xn. The encoding is short because the address isn't there. The real address is whatever the register held when the instruction executed.

That's the mechanism a compiler uses for switch statements and virtual dispatch: build a table of target addresses, load the right entry, branch indirectly. It also means the machine code at that site and byte-for-byte reproducible, yet the reachable destination can change at runtime.

Indirect jumps are also the reason hand-encoding a complete program gets impractical past a certain size. Once you need relocations, the loader computing addresses when the binary is mapped, you need an object file format and a linker. Hand-encoding is best at understanding what an assembler does and at writing tiny patches, not at authoring whole binaries.

Verifying a hand-encoded jump

Every hand-encoded jump should pass four checks before you trust it.

Disassemble it. A single objdump -D -b binary -m i386:x86-64 yourfile.bin or the equivalent for your architecture tells you whether the decoder agrees with your intent. A mismatched target reveals displacement or byte-order errors immediately.

Step it in a simulator. Load the bytes into QEMU, Unicorn, or a small emulator, set a breakpoint, and step. Confirm the program counter goes to the address you computed. A jump to the wrong place is usually off by exactly the instruction length, which points at a measurement-convention mistake.

Test both boundaries. A forward jump at maximum positive displacement and a backward jump at maximum negative displacement are the cases most likely to overflow or sign-extend incorrectly. If you're writing an encoder, unit-test both.

Check alignment and endianness against real output from a known-good assembler. Assemble a trivial program with the architecture's standard toolchain and compare the bytes. Agreement confirms your field layout. If you can't get agreement on a two-byte jump, the problem is almost never subtle; it's a swapped field or a wrong opcode, and the diff shows which.

Common encoding mistakes and how to spot them

The failure modes cluster into a handful of causes.

Branching to the wrong place by exactly the instruction length: you measured the displacement from the wrong end of the instruction. x86 measures from the end; many RISC architectures measure from the start. Fix the convention.

Branching with an off-by-factor-of-four error: you failed to scale the immediate by instruction size, or scaled when you shouldn't have. The architecture manual states the scaling; check it rather than guessing.

A conditional branch that behaves like its opposite: the condition code is inverted, or the flag it tests isn't the one that was set. Trace the flag the previous instruction actually modified.

Garbage in the high bits of a branch word: you forgot to mask the opcode field before OR-ing in the immediate, or masking stripped bits the opcode needed. Encode each field separately and OR at the end.

Endianness reversal: a jump that lands millions of bytes away, often faulting. Serialize and immediately disassemble to catch this before executing.

Jumps that work in a debugger but fail from disk: you assumed a load address that doesn't match reality. Relative jumps survive relocation; absolute ones don't. Prefer relative encodings when you can.

That's the practical loop: encode, disassemble, simulate, and only then run. Each check is cheap, and together they collapse the debugging space from "something is wrong with my program" to "this one field is wrong."

Building jump instructions by hand is one of the fastest ways to understand what an assembler is doing, because a jump contains exactly what you need to be precise about: an opcode, a signed displacement, a measurement convention, an alignment rule, and a byte order. Get those five right and the instruction works; get any one wrong and the machine goes somewhere you didn't send it.