Register-Based Virtual Machine in JavaScript
This challenge asks you to implement a simplified, register-based virtual machine (VM) in JavaScript. Register-based VMs operate by storing data in a fixed number of registers rather than directly in memory, offering potential performance benefits in certain scenarios. Building this VM will test your understanding of data structures, control flow, and low-level programming concepts.
Problem Description
You are to create a VM that executes a sequence of instructions. The VM will have a fixed number of registers (e.g., 8) and a set of supported instructions. The instructions will be represented as strings, and the VM should interpret and execute them sequentially. The VM should maintain the state of its registers throughout execution. The goal is to create a functional VM that can execute a given program represented as an array of instructions.
Key Requirements:
- Registers: The VM must have a fixed number of registers (let's say 8, numbered 0-7).
- Instruction Set: The VM must support the following instructions:
MOV reg1, reg2: Copy the value from registerreg2to registerreg1.ADD reg1, reg2: Add the values of registerreg2to registerreg1, storing the result inreg1.SUB reg1, reg2: Subtract the value of registerreg2from registerreg1, storing the result inreg1.MUL reg1, reg2: Multiply the values of registerreg1andreg2, storing the result inreg1.DIV reg1, reg2: Divide the value of registerreg1by the value of registerreg2, storing the result inreg1. Handle division by zero gracefully (setreg1to 0).HLT: Halt the VM execution.
- Execution: The VM should execute the instructions in the order they appear in the program.
- Error Handling: The VM should handle invalid instructions or invalid register numbers gracefully (e.g., by throwing an error or halting execution).
- Initial State: All registers should be initialized to 0.
Expected Behavior:
The VM should take an array of instructions as input and execute them. After execution, the state of the registers should reflect the results of the operations performed. The VM should halt when it encounters the HLT instruction or encounters an error.
Edge Cases to Consider:
- Invalid instruction names.
- Register numbers outside the valid range (0-7).
- Division by zero.
- Empty instruction array.
- Instructions with incorrect number of arguments.
Examples
Example 1:
Input: ["MOV 0, 1", "ADD 0, 2", "HLT"]
Output: { 0: 3, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0 }
Explanation: Register 0 is initialized to 0. Register 1 is initialized to 0. The value from register 1 (0) is copied to register 0. The value 2 is added to register 0, resulting in 2. The VM halts.
Example 2:
Input: ["MOV 0, 5", "MUL 0, 3", "HLT"]
Output: { 0: 15, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0 }
Explanation: Register 0 is initialized to 0. Register 5 is initialized to 0. The value from register 5 (0) is copied to register 0. The value 3 is multiplied by register 0 (0), resulting in 0. The VM halts.
Example 3:
Input: ["MOV 0, 10", "DIV 0, 2", "HLT"]
Output: { 0: 5, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0 }
Explanation: Register 0 is initialized to 0. Register 10 is initialized to 0. The value from register 10 (0) is copied to register 0. The value of register 0 (10) is divided by 2, resulting in 5. The VM halts.
Constraints
- The number of registers is fixed at 8 (indexed 0-7).
- Instructions are strings in the format "INSTRUCTION reg1, reg2" (e.g., "ADD 0, 1").
- Register numbers are integers between 0 and 7 (inclusive).
- The program (array of instructions) can contain up to 100 instructions.
- Performance is not a primary concern for this challenge; focus on correctness and clarity.
Notes
- Consider using a regular expression to parse the instructions.
- Think about how to represent the VM's state (registers) in your code.
- Start with a small set of instructions and gradually add more.
- Test your VM thoroughly with various inputs, including edge cases.
- Error handling is important for a robust VM. Consider throwing exceptions or returning error codes.
- The register numbers in the instructions refer to the indices of the registers array.