Back
Close
  • 218

Learning Opportunities

This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.

Statement

 Goal

You must create an emulator that takes a set of instructions and interprets them.
An emulator is a software program that mimics hardware, in this case a CPU. In this puzzle, you'll have to emulate an Arithmetic and Logic Unit (ALU) and registers. This CPU uses three 8-bit registers numbered 0,1, and 2, initialized to zero.

Example:
Instruction - Opcode - description; comment
1015 - 1knn - LD k, nn ; Load 0x15 into register 0x0

Opcode table
0000 – EXIT - Stop Execution and print result
1knn - LD k, nn - Load value nn into register k
20xy - ADD x, y - Add the value of register y to register x and store the value in register x, if the result is greater than 8 bits set register 2 to 1 else 0. Only the lowest 8-bits are stored in register x.
30xy - SUB x, y - Subtract the value of register y from register x and store the value in register x, if register x < register y set register 2 to 1 else 0.
40xy – OR x,y – Bitwise OR on value of register x and register y, store value in register x
50xy – AND x,y – Bitwise AND on value of register x and register y, store value in register x
60xy – XOR x, y - Bitwise XOR on value of register x and register y, store value in register x
7knn – SE k, nn – Skip next instruction if value of register k equals nn
8knn – SNE k, nn – Skip next instruction if value of register k is not equal to nn
90xy – SE x,y – Skip next instruction if value of register x equals value of register y
A0xy – SNE x,y - Skip next instruction if value of register x is not equal to the value of register y

Example set of instructions:
1005110520010000

opcode - instruction - comment
1005 - LD 0, 05 - Load 0x05 into register 0x0
1105 - LD 1, 05 - Load 0x05 into register 0x1
2001 - ADD 0,1 - Add register 0x0 to register 0x1, store in register 0x0
0000 - EXIT - stop and print results

Example execution:
# register values = 0, 0, 0
opcode 1005
# register values = 5, 0, 0
opcode 1105
# register values = 5, 5, 0
opcode 2001
# register values = 10, 5, 0
opcode 0000
# exit and print register values
PRINT 10 5 0
Input
You will receive a string of hexadecimal characters that represent the instructions to be interpreted. Each character represents 4 bits and an opcode is made up of 4 characters, or 16 bits. An opcode is a single operation that must be performed.

Line 1: A string of hex characters, program, without spaces
Output
Print the values of registers 0,1 and 2, in decimal notation and separated by spaces.
Constraints
program length <= 100 and is divisible by 4
Example
Input
1005110520010000
Output
10 5 0

A higher resolution is required to access the IDE