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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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...