paiza.ioでelixir その192

# 概要
paiza.ioでelixirやってみた。
練習問題やってみた。

# 練習問題
命令セット
|ニーモニック	|意味					|stackの状態	|op code
|:--|:--|:--|:--|
|nop		|何もしない。				|	変化無し	|000
|jp i	|無条件でiへ飛ぶ。				|変化無し	|1nn
|push 1	|stackに整数を積む。				|1個増える	|2nn
|get i	|stackに変数iの値を入れる。		|1個増える	|3nn
|set i	|変数iにstackを入れる。			|1個減る		|4nn
|out	|	stackをbyteで出力する。		|	1個減る		|500
|if i	|stackが真ならiへ飛ぶ。			|1個減る		|6nn
|if! i	|stackが偽ならiへ飛ぶ。			|1個減る		|7nn
|dup	|	stackにコピーを積む。		|	1個増える	|800
|drop	|stackを一個減らす。				|1個減る		|900
|+		|stack-1,stackの加算を求める。	|1個減る		|f00
|-		|stack-1,stackの減算を求める。	|1個減る		|f01
|*		|stack-1,stackの乗算を求める。	|1個減る		|f02
|/		|stack-1,stackの除算を求める。	|1個減る		|f03
|%		|stack-1,stackの剰余を求める。	|1個減る		|f04
|=		|stack-1,stackの等しいを求める。	|1個減る		|f05
|>		|stack-1,stackの大小を求める。	|1個減る		|f06

アセンブラを書け。

# サンプルコード

```

defmodule Main do
    def add(n) do
	    IO.puts "rom[#{n}] <= 12'hf_00;"
		n = n + 1
	end
	def sub(n) do
	    IO.puts "rom[#{n}] <= 12'hf_01;"
		n = n + 1
	end
	def mul(n) do
	    IO.puts "rom[#{n}] <= 12'hf_02;"
		n = n + 1
	end
	def div(n) do
	    IO.puts "rom[#{n}] <= 12'hf_03;"
		n = n + 1
	end
	def dup(n) do
	    IO.puts "rom[#{n}] <= 12'h8_00;"
		n = n + 1
	end
	def drop(n) do
	    IO.puts "rom[#{n}] <= 12'h9_00;"
		n = n + 1
	end
	def get(n) do
	    IO.puts "rom[#{n}] <= 12'h5_00;"
		n = n + 1
	end
	def push(s, n) do
	    {v, _} = Integer.parse(s)
	    IO.puts "rom[#{n}] <= 12'h2_09;"
		n = n + 1
	end
    def asm(str) do
        IO.puts str
        Enum.reduce(String.split(str, " "), 0, fn s, n ->
            cond do
	        s == "+" ->
		        add(n)
    	    s == "-" ->
	    	    sub(n)
	        s == "*" ->
		        mul(n)
    	    s == "/" ->
	    	    div(n)
	        s == "dup" ->
		        dup(n)
    	    s == "drop" ->
	    	    drop(n)
	        s == "." ->
		        get(n)
    	    true ->
                push(s, n)
            end
        end)
    end
end

Main.asm("9 9 - 9 9 / .")
Main.asm("9 9 / 9 9 / + .")
Main.asm("9 9 + 9 + 9 / .")
Main.asm("9 9 9 + 9 / dup + .")
Main.asm("9 9 9 + 9 / dup + - .")
Main.asm("9 dup 9 + 9 + 9 / - .")
Main.asm("9 9 9 + 9 / - .")
Main.asm("9 9 9 drop 9 / - .")
Main.asm("9 9 - 9 * 9 + .")
Main.asm("9 9 / 9 dup 9 / + .")
Main.asm("9 9 9 + 9 / + .")
Main.asm("9 dup 9 9 + + 9 / + .")
Main.asm("9 9 9 + 9 / dup + + .")
Main.asm("9 dup 9 9 + 9 / dup + - + .")
Main.asm("9 dup dup 9 + 9 + 9 / - + .")




```

# 実行結果

```
9 9 - 9 9 / .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'hf_01;
rom[3] <= 12'h2_09;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'h5_00;
9 9 / 9 9 / + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'hf_03;
rom[3] <= 12'h2_09;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'hf_00;
rom[7] <= 12'h5_00;
9 9 + 9 + 9 / .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'hf_00;
rom[3] <= 12'h2_09;
rom[4] <= 12'hf_00;
rom[5] <= 12'h2_09;
rom[6] <= 12'hf_03;
rom[7] <= 12'h5_00;
9 9 9 + 9 / dup + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'h8_00;
rom[7] <= 12'hf_00;
rom[8] <= 12'h5_00;
9 9 9 + 9 / dup + - .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'h8_00;
rom[7] <= 12'hf_00;
rom[8] <= 12'hf_01;
rom[9] <= 12'h5_00;
9 dup 9 + 9 + 9 / - .
rom[0] <= 12'h2_09;
rom[1] <= 12'h8_00;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_00;
rom[6] <= 12'h2_09;
rom[7] <= 12'hf_03;
rom[8] <= 12'hf_01;
rom[9] <= 12'h5_00;
9 9 9 + 9 / - .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'hf_01;
rom[7] <= 12'h5_00;
9 9 9 drop 9 / - .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'h9_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'hf_01;
rom[7] <= 12'h5_00;
9 9 - 9 * 9 + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'hf_01;
rom[3] <= 12'h2_09;
rom[4] <= 12'hf_02;
rom[5] <= 12'h2_09;
rom[6] <= 12'hf_00;
rom[7] <= 12'h5_00;
9 9 / 9 dup 9 / + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'hf_03;
rom[3] <= 12'h2_09;
rom[4] <= 12'h8_00;
rom[5] <= 12'h2_09;
rom[6] <= 12'hf_03;
rom[7] <= 12'hf_00;
rom[8] <= 12'h5_00;
9 9 9 + 9 / + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'hf_00;
rom[7] <= 12'h5_00;
9 dup 9 9 + + 9 / + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h8_00;
rom[2] <= 12'h2_09;
rom[3] <= 12'h2_09;
rom[4] <= 12'hf_00;
rom[5] <= 12'hf_00;
rom[6] <= 12'h2_09;
rom[7] <= 12'hf_03;
rom[8] <= 12'hf_00;
rom[9] <= 12'h5_00;
9 9 9 + 9 / dup + + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h2_09;
rom[2] <= 12'h2_09;
rom[3] <= 12'hf_00;
rom[4] <= 12'h2_09;
rom[5] <= 12'hf_03;
rom[6] <= 12'h8_00;
rom[7] <= 12'hf_00;
rom[8] <= 12'hf_00;
rom[9] <= 12'h5_00;
9 dup 9 9 + 9 / dup + - + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h8_00;
rom[2] <= 12'h2_09;
rom[3] <= 12'h2_09;
rom[4] <= 12'hf_00;
rom[5] <= 12'h2_09;
rom[6] <= 12'hf_03;
rom[7] <= 12'h8_00;
rom[8] <= 12'hf_00;
rom[9] <= 12'hf_01;
rom[10] <= 12'hf_00;
rom[11] <= 12'h5_00;
9 dup dup 9 + 9 + 9 / - + .
rom[0] <= 12'h2_09;
rom[1] <= 12'h8_00;
rom[2] <= 12'h8_00;
rom[3] <= 12'h2_09;
rom[4] <= 12'hf_00;
rom[5] <= 12'h2_09;
rom[6] <= 12'hf_00;
rom[7] <= 12'h2_09;
rom[8] <= 12'hf_03;
rom[9] <= 12'hf_01;
rom[10] <= 12'hf_00;
rom[11] <= 12'h5_00;


```

# 成果物

https://paiza.io/projects/vvi-E8vtP6N_Vn2nkFxcqw

以上。