2016年2月23日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Java (実行環境)

コンピュータシステムの理論と実装 (Noam Nisan (著)、Shimon Schocken (著)、斎藤 康毅(翻訳)、オライリージャパン)の12章(オペレーティングシステム)、12.5(プロジェクト)を取り組んでみる。

12.5(プロジェクト)

コード(Emacs)

Array.jack

class Array {
  field Array that;

    /** Constructs a new Array of the given size. */
    function Array new(int size) {
      return Memory.alloc(size);
    }

    /** De-allocates the array and frees its space. */
    method void dispose() {
      do Memory.deAlloc(this);
      return;
    }
}

Math.jack

class Math {
  static Array twoToThe;
  
  function int bit(int x, int j) {
    return x & twoToThe[j];
  }
  function int pow(int a, int x) {
    if (x = 0) {
      return 1;
    }
    return Math.multiply(a, Math.pow(a, x - 1));
  }
  function void init() {
    var int i;
    
    let twoToThe = Array.new(16);
    let i = 0;
    let twoToThe[i] = 1;
    let i = i + 1;
    while (i < 16) {
      let twoToThe[i] = twoToThe[i-1] + twoToThe[i-1];
      let i = i + 1;
    }
    return;
  }
  function int abs(int x) {
    if (x < 0) {
      let x = - x;
    }
    return x;
  }
  function int multiply(int x, int y) {
    var int sum;
    var int shiftedX;
    var int j;

    let sum = 0;
    let shiftedX = x;

    let j = 0;
    while (j < 16) {
      if (y & twoToThe[j]) {
        let sum = sum + shiftedX;
      }
      let shiftedX = shiftedX + shiftedX;
      let j = j + 1;
    }
    return sum;
  }
  function int divide(int x, int y) {
    var int q;

    if (x < 0) {
      return -Math.divide(-x, y);
    }
    if (y < 0) {
      return -Math.divide(x, -y);
    }
    if (y > x) {
      return 0;
    }
    let q = Math.divide(x, y + y);
    if ((x - Math.multiply(Math.multiply(2, q), y)) < y) {
      return Math.multiply(2, q);
    } else {
      return Math.multiply(2, q) + 1;
    }
  }
  function int sqrt(int x) {
    var int y;
    var int j;
    var int n;
    var int m;

    let y = 0;
    let j = Math.divide(16, 2) - 1;
    while ((j > 0) | (j = 0)) {
      let n = Math.pow(2, j);
      let m = Math.pow(y + n, 2);
      if (((m < x) | (m = x) ) & (m > 0)) {
        let y = y + n;
      }
      let j = j - 1;
    }
    return y;
  }
  function int max(int a, int b) {
    if (a < b) {
      return b;
    }
    return a;
  }
  function int min(int a, int b) {
    if (a < b) {
      return a;
    }
    return b;
  }
}

0 コメント:

コメントを投稿