Compute AArch64 Branch Targets by Sign-Extending the Immediate Field

Last updated: November 29, 2026

Quick Overview

Given a 32-bit AArch64 instruction word and its program counter, decode an unconditional B or BL instruction and return the absolute branch target. You must implement the sign extension of the 26-bit immediate yourself.

Arm
Coding & Algorithms
Software Engineer
Arm
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a 32-bit AArch64 instruction word and its program counter, decode an unconditional B or BL instruction and return the absolute branch target. You must implement the sign extension of the 26-bit immediate yourself.

A warm-up in the style of Arm phone screens for toolchain, debugger and simulator teams, where decoding instruction fields is daily work in disassemblers and Fixed Virtual Platforms. The interviewer checks clean bit manipulation, correct handling of negative offsets and awareness that branch offsets count words, not bytes.

What the Interviewer Expects
  • Extract the opcode and imm26 field with shifts and masks
  • Implement sign extension without relying on language-specific casts
  • Scale the offset by 4 because instructions are word aligned
  • Wrap the result to 64 bits
  • Reject instructions that are not B or BL
Key Topics to Cover
bit manipulation
sign extension
two's complement
instruction decoding
AArch64
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you extend this to B.cond, CBZ and TBZ, which have 19-bit and 14-bit immediates?
  • How would you encode a branch, and what do you do when the target is out of the plus or minus 128 MB range?
  • How does a linker handle a call that is out of range for BL?
  • How would you test the decoder exhaustively?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: pc (a 64-bit address) and insn (a 32-bit word). B has opcode bits[31:26] = 000101, BL has 100101. The low 26 bits hold a signed word offset. Output: pc + sign_extend(imm26) * 4, wrapp...

High-Level Design

Two small pieces: a general sign_extend(value, bits) helper that is reusable for every immediate width in the ISA, and a decoder that checks the opcode, pulls out the field, sign extends, scales, an...


Submit Your Answer
Markdown supported

Related Questions