ilasmでstack machine その45

# 概要
ilasmでstack machineやってみた。
練習問題やってみた。


# 練習問題
ilasmをrom化せよ。
数９を四個使って、１から１５まで求めよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/a2139dcc-0487-aad8-2113-c5ba697ed70e.png)


# サンプルコード

```

var out = document.getElementById("out");
var src = document.getElementById("src"); 

function hexs(val) {
  var v = val.charCodeAt(0);
  var w = v.toString(16);
  w = "0000" + w;
  code = w.substring(w.length - 2, w.length);
  return code
}
function hex(val) {
  var v = parseInt(val);
  var w = v.toString(16);
  w = "0000" + w;
  code = w.substring(w.length - 2, w.length);
  return code
}
function run() {
  var str = src.value;
  str = str.replace(/\t/g, " ");
  str = str.replace(/  /g, " ");
  str = str.replace(/  /g, " ");
  str = str.replace(/  /g, " ");
  var codes = str.split("\n");  
  var pc = 0;  
  var len = codes.length;
  var i;
  var label = {};
  var res = "";
  var adr = 1;
  for (i = 0; i < len; i++)
  {
    if (codes[i].indexOf(":") > -1)
    {
      label[codes[i]] = adr;
    }
    else if (codes[i].indexOf("ldstr") > -1)
    {
      adr += 6;
    }
    else
    {
      adr++;
    } 
  }
  adr = 0;
  while (i > 0)
  {
    var code = codes[pc].split(" ");
    switch (code[1])
    {
    case "ldstr":
      res += "rom[" + adr + "] = 12'h201;\n";
      adr++;
      res += "rom[" + adr + "] = 12'h2" + hexs(code[2][1]) + ";\n";
      adr++;
      res += "rom[" + adr + "] = 12'h2" + hexs(code[2][2]) + ";\n";
      adr++;
      res += "rom[" + adr + "] = 12'h2" + hexs(code[2][3]) + ";\n";
      adr++;
      res += "rom[" + adr + "] = 12'h2" + hexs(code[2][4]) + ";\n";
      adr++;
      res += "rom[" + adr + "] = 12'h200;\n";
      pc++;
      adr++;
    break;
    case "ldc.i4":
      res += "rom[" + adr + "] = 12'h1" + hex(code[2]) + ";\n";
      pc++;
      adr++;
    break;
    case "stloc": 
      if (code[2] == "a")  
        res += "rom[" + adr + "] = 12'hf09;\n";
      if (code[2] == "b")  
        res += "rom[" + adr + "] = 12'hf0b;\n";
      pc++;
      adr++;
    break;
    case "ldloc":
      if (code[2] == "a")  
        res += "rom[" + adr + "] = 12'hf0a;\n";
      if (code[2] == "b")  
        res += "rom[" + adr + "] = 12'hf0c;\n";
      pc++;
      adr++;
    break; 
    case "call":
      if (code[4] == "System.Console::WriteLine(int32)")
        res += "rom[" + adr + "] = 12'h400;\n";
      if (code[4] == "System.Console::WriteLine(string)")
        res += "rom[" + adr + "] = 12'h500;\n";
      pc++;
      adr++;
    break;
    case "br": 
      res += "rom[" + adr + "] = 12'h9" + hex(label[code[2] + ":"]) + ";\n";
      pc++;
      adr++;
    break;
    case "brtrue": 
      res += "rom[" + adr + "] = 12'hb" + hex(label[code[2] + ":"]) + ";\n";
      pc++;
      adr++;
    break;
    case "brfalse": 
      res += "rom[" + adr + "] = 12'ha" + hex(label[code[2] + ":"]) + ";\n";
      pc++;
      adr++;
    break;      
    case "bgt":  
      res += "rom[" + adr + "] = 12'h7" + hex(label[code[2] + ":"]) + ";\n";
      pc++;
      adr++;
    break;
    case "dup":  
      res += "rom[" + adr + "] = 12'hf00;\n";
      pc++;
      adr++;
    break;
    case "pop":  
      res += "rom[" + adr + "] = 12'hf01;\n";
      pc++;
      adr++;
    break;
    case "add":  
      res += "rom[" + adr + "] = 12'hf02;\n";
      pc++;
      adr++;
    break;
    case "mul":  
      res += "rom[" + adr + "] = 12'hf04;\n";
      pc++;
      adr++;
    break;
    case "sub":  
      res += "rom[" + adr + "] = 12'hf03;\n";
      pc++;
      adr++;
    break;
    case "div":  
      res += "rom[" + adr + "] = 12'hf05;\n";
      pc++;
      adr++;
    break;
    case "rem":  
      res += "rom[" + adr + "] = 12'hf06;\n";
      pc++; 
      adr++;
    break;
    case "ret":  
      res += "rom[" + adr + "] = 12'h001;\n";
      i = 0;
    break;
    case undefined:
      pc++;
    break;
    default:
      alert(code[1]);
      pc++;
    break;
    }
  }
  out.value = res;
}




```

# 投入したソース

```
	ldc.i4	9
	ldc.i4	9
	sub
	ldc.i4	9
	ldc.i4	9
	div
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d2:
	ldc.i4	9
	ldc.i4	9
	div
	ldc.i4	9
	ldc.i4	9
	div
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d3:
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	add
	ldc.i4	9
	div
	call	void [mscorlib] System.Console::WriteLine(int32)
d4:
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	ldc.i4	9
	pop 
	dup
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d5:
	ldc.i4	9
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	dup
	add
	sub
	call	void [mscorlib] System.Console::WriteLine(int32)
d6:
	ldc.i4	9
	dup
	ldc.i4	9
	add
	ldc.i4	9
	add
	ldc.i4	9
	div
	sub
	call	void [mscorlib] System.Console::WriteLine(int32)
d7:
	ldc.i4	9
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	sub
	call	void [mscorlib] System.Console::WriteLine(int32)
d8:
	ldc.i4	9
	ldc.i4	9
	ldc.i4	9
	div
	sub
	ldc.i4	9
	pop
	call	void [mscorlib] System.Console::WriteLine(int32)
d9:
	ldc.i4	9
	ldc.i4	9
	sub
	ldc.i4	9
	mul
	ldc.i4	9
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d10:
	ldc.i4	9
	ldc.i4	9
	div
	ldc.i4	9
	add
	ldc.i4	9
	pop
	call	void [mscorlib] System.Console::WriteLine(int32)
d11:
	ldc.i4	9
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d12:
	ldc.i4	9
	dup
	ldc.i4	9
	ldc.i4	9
	add
	add
	ldc.i4	9
	div
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d13:
	ldc.i4	9
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	dup
	add
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d14:
	ldc.i4	9
	dup
	ldc.i4	9
	ldc.i4	9
	add
	ldc.i4	9
	div
	dup
	add
	sub
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
d15:
	ldc.i4	9
	dup
	dup
	ldc.i4	9
	add
	ldc.i4	9
	add
	ldc.i4	9
	div
	sub
	add
	call	void [mscorlib] System.Console::WriteLine(int32)
	ret

```

# 実行結果

```
rom[0] = 12'h109;
rom[1] = 12'h109;
rom[2] = 12'hf03;
rom[3] = 12'h109;
rom[4] = 12'h109;
rom[5] = 12'hf05;
rom[6] = 12'hf02;
rom[7] = 12'h400;
rom[8] = 12'h109;
rom[9] = 12'h109;
rom[10] = 12'hf05;
rom[11] = 12'h109;
rom[12] = 12'h109;
rom[13] = 12'hf05;
rom[14] = 12'hf02;
rom[15] = 12'h400;
rom[16] = 12'h109;
rom[17] = 12'h109;
rom[18] = 12'hf02;
rom[19] = 12'h109;
rom[20] = 12'hf02;
rom[21] = 12'h109;
rom[22] = 12'hf05;
rom[23] = 12'h400;
rom[24] = 12'h109;
rom[25] = 12'h109;
rom[26] = 12'hf02;
rom[27] = 12'h109;
rom[28] = 12'hf05;
rom[29] = 12'h109;
rom[30] = 12'hf01;
rom[31] = 12'hf00;
rom[32] = 12'hf02;
rom[33] = 12'h400;
rom[34] = 12'h109;
rom[35] = 12'h109;
rom[36] = 12'h109;
rom[37] = 12'hf02;
rom[38] = 12'h109;
rom[39] = 12'hf05;
rom[40] = 12'hf00;
rom[41] = 12'hf02;
rom[42] = 12'hf03;
rom[43] = 12'h400;
rom[44] = 12'h109;
rom[45] = 12'hf00;
rom[46] = 12'h109;
rom[47] = 12'hf02;
rom[48] = 12'h109;
rom[49] = 12'hf02;
rom[50] = 12'h109;
rom[51] = 12'hf05;
rom[52] = 12'hf03;
rom[53] = 12'h400;
rom[54] = 12'h109;
rom[55] = 12'h109;
rom[56] = 12'h109;
rom[57] = 12'hf02;
rom[58] = 12'h109;
rom[59] = 12'hf05;
rom[60] = 12'hf03;
rom[61] = 12'h400;
rom[62] = 12'h109;
rom[63] = 12'h109;
rom[64] = 12'h109;
rom[65] = 12'hf05;
rom[66] = 12'hf03;
rom[67] = 12'h109;
rom[68] = 12'hf01;
rom[69] = 12'h400;
rom[70] = 12'h109;
rom[71] = 12'h109;
rom[72] = 12'hf03;
rom[73] = 12'h109;
rom[74] = 12'hf04;
rom[75] = 12'h109;
rom[76] = 12'hf02;
rom[77] = 12'h400;
rom[78] = 12'h109;
rom[79] = 12'h109;
rom[80] = 12'hf05;
rom[81] = 12'h109;
rom[82] = 12'hf02;
rom[83] = 12'h109;
rom[84] = 12'hf01;
rom[85] = 12'h400;
rom[86] = 12'h109;
rom[87] = 12'h109;
rom[88] = 12'h109;
rom[89] = 12'hf02;
rom[90] = 12'h109;
rom[91] = 12'hf05;
rom[92] = 12'hf02;
rom[93] = 12'h400;
rom[94] = 12'h109;
rom[95] = 12'hf00;
rom[96] = 12'h109;
rom[97] = 12'h109;
rom[98] = 12'hf02;
rom[99] = 12'hf02;
rom[100] = 12'h109;
rom[101] = 12'hf05;
rom[102] = 12'hf02;
rom[103] = 12'h400;
rom[104] = 12'h109;
rom[105] = 12'h109;
rom[106] = 12'h109;
rom[107] = 12'hf02;
rom[108] = 12'h109;
rom[109] = 12'hf05;
rom[110] = 12'hf00;
rom[111] = 12'hf02;
rom[112] = 12'hf02;
rom[113] = 12'h400;
rom[114] = 12'h109;
rom[115] = 12'hf00;
rom[116] = 12'h109;
rom[117] = 12'h109;
rom[118] = 12'hf02;
rom[119] = 12'h109;
rom[120] = 12'hf05;
rom[121] = 12'hf00;
rom[122] = 12'hf02;
rom[123] = 12'hf03;
rom[124] = 12'hf02;
rom[125] = 12'h400;
rom[126] = 12'h109;
rom[127] = 12'hf00;
rom[128] = 12'hf00;
rom[129] = 12'h109;
rom[130] = 12'hf02;
rom[131] = 12'h109;
rom[132] = 12'hf02;
rom[133] = 12'h109;
rom[134] = 12'hf05;
rom[135] = 12'hf03;
rom[136] = 12'hf02;
rom[137] = 12'h400;
rom[138] = 12'h001;

```

# 成果物

https://embed.plnkr.co/plunk/R63ppPjuyx8Me4zy


以上。