アセンブラーを作る。 その4

# 概要
アセンブラーを作る。
AYUMUというcpuのアセンブラー、作ってみた。

# 参考にしたページ

https://qiita.com/Prokuma/items/0b2ce47378c745ee263e


# 投入したソースコード

```
ayumu.xor(r0, r0)
ayumu.addi(r0, 201)
ayumu.intf(T0, r0)
ayumu.xor(r8, r8)
ayumu.xor(r12, r12)
ayumu.xor(r13, r13)
ayumu.addi(r12, 0)
ayumu.addi(r13, 15)
ayumu.intp(T0, 0)
ayumu.wt(Out0, r1)
ayumu.xor(r12, r12)
ayumu.xor(r12, r13)
ayumu.addi(r12, 0)
ayumu.addi(r13, 9)
ayumu.jmp(0)
ayumu.st(r8, r8)
ayumu.addi(r8, 1)
ayumu.st(r12, r8)
ayumu.addi(r8, 1)
ayumu.st(r13, r8)
ayumu.addi(r1, 1)
ayumu.wt(Out0, r1)
ayumu.ld(r13, r8)
ayumu.subi(r8, 1)
ayumu.ld(r12, r8)
ayumu.subi(r8, 1)
ayumu.ld(r8, r8)
ayumu.jmpp()

```

# 実行結果

```
5800
80C9
6200
5800
58CC
58DD
8C00
8D0F
5200
4601
58CC
58CD
8C00
8D09
1000
4300
8001
43C0
8001
43D0
8101
4601
47D0
9001
47C0
9001
4700
1020

```

# サンプルコード

```

var src = document.getElementById('src');
var out = document.getElementById('out');
var r0 = 0;
var r1 = 1;
var T0 = 0;
var r8 = 0;
var r12 = 12;
var r13 = 13;
var Out0 = 0;
function opcode4(a) {
	var res;
  switch (a) 
  {
	case "0000":
		res = "0";
	break;
	case "0001":
		res = "1";
	break;
	case "0010":
		res = "2";
	break;
	case "0011":
		res = "3";
	break;
	case "0100":
		res = "4";
	break;
	case "0101":
		res = "5";
	break;
	case "0110":
		res = "6";
	break;
	case "0111":
		res = "7";
	break;
	case "1000":
		res = "8";
	break;
	case "1001":
		res = "9";
	break;
	case "1010":
		res = "A";
	break;
	case "1011":
		res = "B";
	break;
	case "1100":
		res = "C";
	break;
	case "1101":
		res = "D";
	break;
	case "1110":
		res = "E";
	break;
	case "1111":
		res = "F";
	break;
	}
  return res;
};
function opcode(s) {
  var res;
  res = opcode4(s.substring(0, 4)) 
    + opcode4(s.substring(4, 8))
    + opcode4(s.substring(8, 12))
    + opcode4(s.substring(12, 16));  
  return res + "\n";
}
function reg4(i) {
	var b = i.toString(2);
  return ('00000' + b).slice(-4);
}
function imm8l(i) {
	var d = i & 0xf;
	var b = d.toString(2);
  return ('0000' + b).slice(-4);
}
function imm8h(i) {
	var d = i >> 4;
	var b = d.toString(2);
  return ('0000' + b).slice(-4);
}
var ayumu = {
 	PC: 0,
  xor: function(r0, r1) {
		out.value += opcode("01011000" + reg4(r0) + reg4(r1));
		this.PC++;
	},
	addi: function(r0, i) {
		out.value += opcode("1000" + reg4(r0) + imm8h(i) + imm8l(i));
		this.PC++;
	},
  subi: function(r0, i) {
		out.value += opcode("1001" + reg4(r0) + imm8h(i) + imm8l(i));
		this.PC++;
	},
	rd: function(r0, r1) {
		out.value += opcode("01000010" + reg4(r0) + reg4(r1));
		this.PC++;
	},
	wt: function(r0, r1) {
		out.value += opcode("01000110" + reg4(r0) + reg4(r1));
		this.PC++;
	},
	intp: function(r0, i) {
		out.value += opcode("01010010" + reg4(r0) + imm8l(i));
		this.PC++;
	},
	intf: function(r0, i) {
		out.value += opcode("01100010" + reg4(r0) + imm8l(i));
		this.PC++;
	},
  jmp: function(i) {
		out.value += opcode("000100000000" + reg4(i));
		this.PC++;
  },
	st: function(r0, r1) {
		out.value += opcode("01000011" + reg4(r0) + reg4(r1));
		this.PC++;
	},
	ld: function(r0, r1) {
		out.value += opcode("01000111" + reg4(r0) + reg4(r1));
		this.PC++;
	},
  jmpp: function() {
		out.value += opcode("0001000000100000");
		this.PC++;
  },
	nop: function() {
		out.value += opcode("0000000000000000");
		this.PC++;
  },
}
function run() {
  var str = src.value;
  var codes = str.split("\n");  
  var len = codes.length - 1;
  while (ayumu.PC < len)
  {
    eval(codes[ayumu.PC]);
  }
  alert("ok")
}




```

# 成果物

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


以上。