5 kyu

Simple assembler interpreter

2,781 of 8,312ShinuToki

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 Trained29721
Total Skips8435
Total Code Submissions72449
Total Times Completed8312
Python Completions2781
JavaScript Completions1740
PHP Completions234
Java Completions1015
Lua Completions105
CoffeeScript Completions16
C++ Completions815
Haskell Completions149
Julia Completions39
Kotlin Completions180
Rust Completions485
C# Completions515
Clojure Completions42
Go Completions240
Scala Completions91
Crystal Completions13
COBOL Completions5
OCaml Completions14
C Completions143
Ruby Completions46
Total Stars1122
% of votes with a positive feedback rating94% of 1461
Total "Very Satisfied" Votes1303
Total "Somewhat Satisfied" Votes134
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