#概要
アセンブラーを作る。
avrのアセンブラー、作ってみた。
#投入したソース
asm.inc(16)
asm.dec(16)
asm.cpi(16, 10)
asm.ldi(16, 1)
asm.in(4, 16)
asm.out(4, 16)
asm.breq(-3)
asm.brne(-3)
asm.brge(-3)
asm.brlt(-3)
asm.rjmp(-3)
asm.ret()
asm.reti()
asm.nop()
#結果
9503
950A
300A
E001
B104
B904
F3E9
F7E9
F3EC
F7EC
CFFD
9508
9518
0000
#サンプルコード
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 ado12(i) {
var d;
if (i < 0)
{
d = Math.abs(i);
d = ~d + 1 >>> 0;
}
else
{
d = i >>> 0;
}
var b = d.toString(2);
return ('000000000000' + b).slice(-12);
}
function ado7(i) {
var d;
if (i < 0)
{
d = Math.abs(i);
d = ~d + 1 >>> 0;
}
else
{
d = i >>> 0;
}
var b = d.toString(2);
return ('000000000000' + b).slice(-7);
}
function reg5(i) {
var b = i.toString(2);
return ('00000' + b).slice(-5);
}
function reg5l(i) {
var d = i & 0xf;
var b = d.toString(2);
return ('0000' + b).slice(-4);
}
function reg5h(i) {
var d = i >> 4;
var b = d.toString(2);
return ('00' + b).slice(-1);
}
function reg4(i) {
var b = i.toString(2);
return ('00000' + b).slice(-4);
}
function imm6l(i) {
var d = i & 0xf;
var b = d.toString(2);
return ('0000' + b).slice(-4);
}
function imm6h(i) {
var d = i >> 4;
var b = d.toString(2);
return ('00' + b).slice(-2);
}
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 asm = {
PC: 0,
add: function(r0, r1) {
out.value += opcode("000011" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
sub: function(r0, r1) {
out.value += opcode("000110" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
and: function(r0, r1) {
out.value += opcode("001000" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
or: function(r0, r1) {
out.value += opcode("001010" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
eor: function(r0, r1) {
out.value += opcode("001001" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
mul: function(r0, r1) {
out.value += opcode("100111" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
mov: function(r0, r1) {
out.value += opcode("001011" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
lsl: function(r0, r1) {
out.value += opcode("000011" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
cp: function(r0, r1) {
out.value += opcode("000101" + reg5h(r1) + reg5(r0) + reg5l(r1));
this.PC++;
},
inc: function(r) {
out.value += opcode("1001010" + reg5(r) + "0011");
this.PC++;
},
dec: function(r) {
out.value += opcode("1001010" + reg5(r) + "1010");
this.PC++;
},
cpi: function(r, i) {
out.value += opcode("0011" + imm8h(i) + reg4(r) + imm8l(i));
this.PC++;
},
ldi: function(r, i) {
out.value += opcode("1110" + imm8h(i) + reg4(r) + imm8l(i));
this.PC++;
},
in: function(io, r) {
out.value += opcode("10110" + imm6h(io) + reg5(r) + imm6l(io));
this.PC++;
},
out: function(io, r) {
out.value += opcode("10111" + imm6h(io) + reg5(r) + imm6l(io));
this.PC++;
},
breq: function(i) {
out.value += opcode("111100" + ado7(i) + "001");
this.PC++;
},
brne: function(i) {
out.value += opcode("111101" + ado7(i) + "001");
this.PC++;
},
brge: function(i) {
out.value += opcode("111100" + ado7(i) + "100");
this.PC++;
},
brlt: function(i) {
out.value += opcode("111101" + ado7(i) + "100");
this.PC++;
},
rcall: function(i) {
out.value += opcode("1101" + ado12(i));
this.PC++;
},
rjmp: function(i) {
out.value += opcode("1100" + ado12(i));
this.PC++;
},
ret: function() {
out.value += opcode("1001010100001000");
this.PC++;
},
reti: function() {
out.value += opcode("1001010100011000");
this.PC++;
},
nop: function() {
out.value += opcode("0000000000000000");
this.PC++;
},
}
#成果物
以上。