How to resolve the algorithm Subleq step by step in the Janet programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Subleq step by step in the Janet programming language

Table of Contents

Problem Statement

Subleq is an example of a One-Instruction Set Computer (OISC). It is named after its only instruction, which is SUbtract and Branch if Less than or EQual to zero.
Your task is to create an interpreter which emulates a SUBLEQ machine. The machine's memory consists of an array of signed integers.   These integers may be interpreted in three ways: Any reasonable word size that accommodates all three of the above uses is fine. The program should load the initial contents of the emulated machine's memory, set the instruction pointer to the first address (which is defined to be address 0), and begin emulating the machine, which works as follows: Your solution may initialize the emulated machine's memory in any convenient manner, but if you accept it as input, it should be a separate input stream from the one fed to the emulated machine once it is running. And if fed as text input, it should be in the form of raw subleq "machine code" - whitespace-separated decimal numbers, with no symbolic names or other assembly-level extensions, to be loaded into memory starting at address   0   (zero). For purposes of this task, show the output of your solution when fed the below   "Hello, world!"   program. As written, this example assumes ASCII or a superset of it, such as any of the Latin-N character sets or Unicode;   you may translate the numbers representing characters (starting with 72=ASCII 'H') into another character set if your implementation runs in a non-ASCII-compatible environment. If 0 is not an appropriate terminator in your character set, the program logic will need some adjustment as well. The above "machine code" corresponds to something like this in a hypothetical assembler language for a signed 8-bit version of the machine:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Subleq step by step in the Janet programming language

Source code in the janet programming language

(defn main [& args]
  (let [filename  (get args 1)
        fh        (file/open filename)
        program   (file/read fh :all)
        memory    (eval-string (string "@[" program "]"))
        size      (length memory)]

    (var pc 0)

    (while (<= 0 pc size)
      (let [a (get memory pc)
            b (get memory (inc pc))
            c (get memory (+ pc 2))]
      (set pc (+ pc 3))
      (cond
        (< a 0) (put memory b (first (file/read stdin 1)))
        (< b 0) (file/write stdout (buffer/push-byte @"" (get memory a)))
        true    
          (do
            (put memory b (- (get memory b) (get memory a)))
            (if (<= (get memory b) 0) 
               (set pc c))))))))


  

You may also check:How to resolve the algorithm Benford's law step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Binary strings step by step in the Ecstasy programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Undefined values step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Nim programming language