Interpret a Tiny Register Assembly Language With Labels and Jumps

Last updated: November 29, 2026

Quick Overview

Write an interpreter for a small assembly language used in a test harness: instructions set, add, mul, jnz, and print operate on named registers or integer literals, jnz jumps to a label or relative offset when its operand is non-zero, and # starts a comment.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Write an interpreter for a small assembly language used in a test harness: instructions set, add, mul, jnz, and print operate on named registers or integer literals, jnz jumps to a label or relative offset when its operand is non-zero, and # starts a comment.

This kind of question shows up in Google onsites to see how a candidate structures a small interpreter: a parsing pass, a label table, and an execution loop. The interviewer evaluates clean separation of phases, operand resolution, and safety features like a step limit against infinite loops.

What the Interviewer Expects
  • Separates parsing (comments, labels, operands) from execution
  • Resolves labels to instruction indices in a first pass
  • Implements operand lookup that accepts registers or literals
  • Handles jnz with labels and relative offsets, being careful with the incremented program counter
  • Adds a step limit and clear errors for unknown instructions
Key Topics to Cover
Interpreters
Parsing
Program Counter
Symbol Tables
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
  • Add call and ret with a call stack.
  • Detect at parse time jumps to undefined labels.
  • How would you speed this up for hot loops, for example by compiling to Python closures?
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
  • One instruction per line; blank lines and # comments are ignored; a line ending in : defines a label.
  • set r x, add r x, mul r x, jnz x target, print x; x is a register name or an i...
High-Level Design

Two phases. The parse pass strips comments, records each label as the index of the next real instruction, and stores instructions as (op, args) tuples. The run loop fetches prog[pc], increments `p...


Submit Your Answer
Markdown supported