5 kyu

Simple assembler interpreter

2,735 of 8,191ShinuToki

Description:

This is the first part of this kata series. Second part is here.

We want to create a simple interpreter of assembler which will support the following instructions:

  • mov x y - copies y (either a constant value or the content of a register) into register x
  • inc x - increases the content of the register x by one
  • dec x - decreases the content of the register x by one
  • jnz x y - jumps to an instruction y steps away (positive means forward, negative means backward, y can be a register or a constant), but only if x (a constant or a register) is not zero

Register names are alphabetical (letters only). Constants are always integers (positive or negative).

Note: the jnz instruction moves relative to itself. For example, an offset of -1 would continue at the previous instruction, while an offset of 2 would skip over the next instruction.

The function will take an input list with the sequence of the program instructions and will execute them. The program ends when there are no more instructions to execute, then it returns a dictionary (a table in COBOL) with the contents of the registers.

Also, every inc/dec/jnz on a register will always be preceeded by a mov on the register first, so you don't need to worry about uninitialized registers.

Example

["mov a 5"; "inc a"; "dec a"; "dec a"; "jnz a -1"; "inc a"]

visualized:

mov a 5
inc a
dec a
dec a
jnz a -1
inc a

The above code will:

  • set register a to 5,
  • increase its value by 1,
  • decrease its value by 2,
  • then decrease its value until it is zero (jnz a -1 jumps to the previous instruction if a is not zero)
  • and then increase its value by 1, leaving register a at 1

So, the function should return:

Map("a" -> 1)
{'a': 1}
{a=1}
new Dictionary<string, int> { { "a" , 1 } };
fromList [("a", 1)]
Dict{String,Number}("a"=>1)
{"a": 1}
{:a 1}
map[string]int{"a": 1}
{"a" => 1}
      [["a", 1]]
  (int[]){['a'] = 1}

This kata is based on the Advent of Code 2016 - day 12

Interpreters
Algorithms

More By Author:

Check out these other kata created by ShinuToki

Stats:

CreatedApr 3, 2017
PublishedApr 3, 2017
Warriors Trained29416
Total Skips8376
Total Code Submissions71903
Total Times Completed8191
Python Completions2735
JavaScript Completions1722
PHP Completions232
Java Completions1008
Lua Completions104
CoffeeScript Completions15
C++ Completions793
Haskell Completions147
Julia Completions38
Kotlin Completions178
Rust Completions474
C# Completions504
Clojure Completions41
Go Completions233
Scala Completions85
Crystal Completions12
COBOL Completions4
OCaml Completions12
C Completions138
Ruby Completions43
Total Stars1116
% of votes with a positive feedback rating94% of 1452
Total "Very Satisfied" Votes1295
Total "Somewhat Satisfied" Votes133
Total "Not Satisfied" Votes24
Total Rank Assessments24
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • ShinuToki Avatar
  • jcsahnwaldt Avatar
  • anter69 Avatar
  • voximity Avatar
  • luochen1990 Avatar
  • DillonBroadus Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Firefly2002 Avatar
  • mmalkavian Avatar
  • Voile Avatar
  • replomancer Avatar
  • optimalstrategy Avatar
  • CrazyGuy108 Avatar
  • FArekkusu Avatar
  • scottmyran Avatar
  • Naiten Avatar
  • CHlM3RA Avatar
  • metalim Avatar
  • monadius Avatar
  • prisioner Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • akvptp Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • dmercertaylor Avatar
  • OmNomRarg Avatar
  • Krzem5 Avatar
  • stuenofotso@gmail.com Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • KayleighWasTaken Avatar
Ad