LoginSignup
0
0

More than 1 year has passed since last update.

インタープリタを作る その17

Last updated at Posted at 2021-06-09

概要

インタープリタを作ってみた。
avrインタープリター書いてみた。
条件分岐を動かす。ループしてみる。

方針

sregの内部表現に合わせる。

sreg フラグ
sreg[0] c
sreg[1] z
sreg[2] n
sreg[3] v
sreg[4] s
sreg[5] h
sreg[6] t
sreg[7] i

cpi,breq,brneを実装する。

cpiの実装

zフラグのみ

    cpi: function(_Rd, K) {
        K = hen(K);
        var Rd = this.dataspace[_Rd];
        var R = [false, false, false, false, false, false, false, false];
        var SREG = this.dataspace[this.sreg];
        R[0] = !!(Rd[0] ^ K[0]);
        R[1] = !!(Rd[1] ^ K[1]);
        R[2] = !!(Rd[2] ^ K[2]);
        R[3] = !!(Rd[3] ^ K[3]);
        R[4] = !!(Rd[4] ^ K[4]);
        R[5] = !!(Rd[5] ^ K[5]);
        R[6] = !!(Rd[6] ^ K[6]);
        R[7] = !!(Rd[7] ^ K[7]);
        SREG[1] = !R[7] && !R[6] && !R[5] && !R[4] && !R[3] && !R[2] && !R[1] && !R[0];
        this.PC++;
        this.dataspace[this.sreg] = SREG;
    },

breqの実装

    breq: function(k) {
        if (this.dataspace[this.sreg][1] === true) 
        {
            this.PC = this.PC + k;
        }
        this.PC++;
    },

投入したソース

レジスタ16が、9になるまでループする。
レジスタ31の値が、0から9まで変化する。

avr.ldi(16, 0x00)
avr.ldi(31, 0x30)
avr.inc(16)
avr.inc(31)
avr.cpi(16, 0x09)
avr.brne(-4)

実行結果

0123456789

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0